blob: 333c5094b97dd7001b36e66efca2c5362d8b916e [file] [log] [blame]
James Smart43140ca2017-03-04 09:30:34 -08001
dea31012005-04-17 16:05:31 -05002/*******************************************************************
3 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04004 * Fibre Channel Host Bus Adapters. *
James Smartd080abe2017-02-12 13:52:39 -08005 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
6 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07007 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08009 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050011 * *
12 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040013 * modify it under the terms of version 2 of the GNU General *
14 * Public License as published by the Free Software Foundation. *
15 * This program is distributed in the hope that it will be useful. *
16 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
17 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
18 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
19 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
20 * TO BE LEGALLY INVALID. See the GNU General Public License for *
21 * more details, a copy of which can be found in the file COPYING *
22 * included with this package. *
dea31012005-04-17 16:05:31 -050023 *******************************************************************/
24
dea31012005-04-17 16:05:31 -050025#include <linux/blkdev.h>
26#include <linux/pci.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010030#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050031
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040032#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_cmnd.h>
34#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040036#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040037#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040038#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050039
James Smart895427b2017-02-12 13:52:30 -080040#include <linux/nvme-fc-driver.h>
41
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_hw.h"
44#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040045#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040046#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080049#include "lpfc_scsi.h"
50#include "lpfc_nvme.h"
James Smartf358dd02017-02-12 13:52:34 -080051#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050052#include "lpfc_crtn.h"
53#include "lpfc_logmsg.h"
54#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050055#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040056#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070057#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050058
59/* There are only four IOCB completion types. */
60typedef enum _lpfc_iocb_type {
61 LPFC_UNKNOWN_IOCB,
62 LPFC_UNSOL_IOCB,
63 LPFC_SOL_IOCB,
64 LPFC_ABORT_IOCB
65} lpfc_iocb_type;
66
James Smart4f774512009-05-22 14:52:35 -040067
68/* Provide function prototypes local to this module. */
69static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70 uint32_t);
71static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040072 uint8_t *, uint32_t *);
73static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040075static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76 struct hbq_dmabuf *);
James Smart895427b2017-02-12 13:52:30 -080077static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
James Smart05580562011-05-24 11:40:48 -040078 struct lpfc_cqe *);
James Smart895427b2017-02-12 13:52:30 -080079static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040080 int);
James Smartba20c852012-08-03 12:36:52 -040081static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
82 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040083static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
84static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart895427b2017-02-12 13:52:30 -080085static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
86 struct lpfc_sli_ring *pring,
87 struct lpfc_iocbq *cmdiocb);
James Smart05580562011-05-24 11:40:48 -040088
James Smart4f774512009-05-22 14:52:35 -040089static IOCB_t *
90lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
91{
92 return &iocbq->iocb;
93}
94
95/**
96 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
97 * @q: The Work Queue to operate on.
98 * @wqe: The work Queue Entry to put on the Work queue.
99 *
100 * This routine will copy the contents of @wqe to the next available entry on
101 * the @q. This function will then ring the Work Queue Doorbell to signal the
102 * HBA to start processing the Work Queue Entry. This function returns 0 if
103 * successful. If no entries are available on @q then this function will return
104 * -ENOMEM.
105 * The caller is expected to hold the hbalock when calling this routine.
106 **/
107static uint32_t
108lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
109{
James Smart2e90f4b2011-12-13 13:22:37 -0500110 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400111 struct lpfc_register doorbell;
112 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400113 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400114
James Smart2e90f4b2011-12-13 13:22:37 -0500115 /* sanity check on queue memory */
116 if (unlikely(!q))
117 return -ENOMEM;
118 temp_wqe = q->qe[q->host_index].wqe;
119
James Smart4f774512009-05-22 14:52:35 -0400120 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400121 idx = ((q->host_index + 1) % q->entry_count);
122 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400123 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400124 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400125 }
126 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400127 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500128 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400129 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500130 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
131 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400132 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart6b3b3bd2016-12-19 15:07:30 -0800133 /* ensure WQE bcopy flushed before doorbell write */
134 wmb();
James Smart4f774512009-05-22 14:52:35 -0400135
136 /* Update the host index before invoking device */
137 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400138
139 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400140
141 /* Ring Doorbell */
142 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500143 if (q->db_format == LPFC_DB_LIST_FORMAT) {
144 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
145 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
146 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
147 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
148 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
149 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
150 } else {
151 return -EINVAL;
152 }
153 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400154
155 return 0;
156}
157
158/**
159 * lpfc_sli4_wq_release - Updates internal hba index for WQ
160 * @q: The Work Queue to operate on.
161 * @index: The index to advance the hba index to.
162 *
163 * This routine will update the HBA index of a queue to reflect consumption of
164 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
165 * an entry the host calls this function to update the queue's internal
166 * pointers. This routine returns the number of entries that were consumed by
167 * the HBA.
168 **/
169static uint32_t
170lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
171{
172 uint32_t released = 0;
173
James Smart2e90f4b2011-12-13 13:22:37 -0500174 /* sanity check on queue memory */
175 if (unlikely(!q))
176 return 0;
177
James Smart4f774512009-05-22 14:52:35 -0400178 if (q->hba_index == index)
179 return 0;
180 do {
181 q->hba_index = ((q->hba_index + 1) % q->entry_count);
182 released++;
183 } while (q->hba_index != index);
184 return released;
185}
186
187/**
188 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
189 * @q: The Mailbox Queue to operate on.
190 * @wqe: The Mailbox Queue Entry to put on the Work queue.
191 *
192 * This routine will copy the contents of @mqe to the next available entry on
193 * the @q. This function will then ring the Work Queue Doorbell to signal the
194 * HBA to start processing the Work Queue Entry. This function returns 0 if
195 * successful. If no entries are available on @q then this function will return
196 * -ENOMEM.
197 * The caller is expected to hold the hbalock when calling this routine.
198 **/
199static uint32_t
200lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
201{
James Smart2e90f4b2011-12-13 13:22:37 -0500202 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400203 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400204
James Smart2e90f4b2011-12-13 13:22:37 -0500205 /* sanity check on queue memory */
206 if (unlikely(!q))
207 return -ENOMEM;
208 temp_mqe = q->qe[q->host_index].mqe;
209
James Smart4f774512009-05-22 14:52:35 -0400210 /* If the host has not yet processed the next entry then we are done */
211 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
212 return -ENOMEM;
213 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
214 /* Save off the mailbox pointer for completion */
215 q->phba->mbox = (MAILBOX_t *)temp_mqe;
216
217 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400218 q->host_index = ((q->host_index + 1) % q->entry_count);
219
220 /* Ring Doorbell */
221 doorbell.word0 = 0;
222 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
223 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
224 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400225 return 0;
226}
227
228/**
229 * lpfc_sli4_mq_release - Updates internal hba index for MQ
230 * @q: The Mailbox Queue to operate on.
231 *
232 * This routine will update the HBA index of a queue to reflect consumption of
233 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
234 * an entry the host calls this function to update the queue's internal
235 * pointers. This routine returns the number of entries that were consumed by
236 * the HBA.
237 **/
238static uint32_t
239lpfc_sli4_mq_release(struct lpfc_queue *q)
240{
James Smart2e90f4b2011-12-13 13:22:37 -0500241 /* sanity check on queue memory */
242 if (unlikely(!q))
243 return 0;
244
James Smart4f774512009-05-22 14:52:35 -0400245 /* Clear the mailbox pointer for completion */
246 q->phba->mbox = NULL;
247 q->hba_index = ((q->hba_index + 1) % q->entry_count);
248 return 1;
249}
250
251/**
252 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
253 * @q: The Event Queue to get the first valid EQE from
254 *
255 * This routine will get the first valid Event Queue Entry from @q, update
256 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
257 * the Queue (no more work to do), or the Queue is full of EQEs that have been
258 * processed, but not popped back to the HBA then this routine will return NULL.
259 **/
260static struct lpfc_eqe *
261lpfc_sli4_eq_get(struct lpfc_queue *q)
262{
James Smart2e90f4b2011-12-13 13:22:37 -0500263 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400264 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500265
266 /* sanity check on queue memory */
267 if (unlikely(!q))
268 return NULL;
269 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400270
271 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400272 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400273 return NULL;
274 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400275 idx = ((q->hba_index + 1) % q->entry_count);
276 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400277 return NULL;
278
James Smart027140e2012-08-03 12:35:44 -0400279 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400280
281 /*
282 * insert barrier for instruction interlock : data from the hardware
283 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800284 * upon. Speculative instructions were allowing a bcopy at the start
285 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
286 * after our return, to copy data before the valid bit check above
287 * was done. As such, some of the copied data was stale. The barrier
288 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400289 */
290 mb();
James Smart4f774512009-05-22 14:52:35 -0400291 return eqe;
292}
293
294/**
James Smartba20c852012-08-03 12:36:52 -0400295 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
296 * @q: The Event Queue to disable interrupts
297 *
298 **/
299static inline void
300lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
301{
302 struct lpfc_register doorbell;
303
304 doorbell.word0 = 0;
305 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
306 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
307 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
308 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
309 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
310 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
311}
312
313/**
James Smart4f774512009-05-22 14:52:35 -0400314 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
315 * @q: The Event Queue that the host has completed processing for.
316 * @arm: Indicates whether the host wants to arms this CQ.
317 *
318 * This routine will mark all Event Queue Entries on @q, from the last
319 * known completed entry to the last entry that was processed, as completed
320 * by clearing the valid bit for each completion queue entry. Then it will
321 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
322 * The internal host index in the @q will be updated by this routine to indicate
323 * that the host has finished processing the entries. The @arm parameter
324 * indicates that the queue should be rearmed when ringing the doorbell.
325 *
326 * This function will return the number of EQEs that were popped.
327 **/
328uint32_t
329lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
330{
331 uint32_t released = 0;
332 struct lpfc_eqe *temp_eqe;
333 struct lpfc_register doorbell;
334
James Smart2e90f4b2011-12-13 13:22:37 -0500335 /* sanity check on queue memory */
336 if (unlikely(!q))
337 return 0;
338
James Smart4f774512009-05-22 14:52:35 -0400339 /* while there are valid entries */
340 while (q->hba_index != q->host_index) {
341 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400342 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400343 released++;
344 q->host_index = ((q->host_index + 1) % q->entry_count);
345 }
346 if (unlikely(released == 0 && !arm))
347 return 0;
348
349 /* ring doorbell for number popped */
350 doorbell.word0 = 0;
351 if (arm) {
352 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
353 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
354 }
355 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
356 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500357 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
358 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
359 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400360 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500361 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
362 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
363 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400364 return released;
365}
366
367/**
368 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
369 * @q: The Completion Queue to get the first valid CQE from
370 *
371 * This routine will get the first valid Completion Queue Entry from @q, update
372 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
373 * the Queue (no more work to do), or the Queue is full of CQEs that have been
374 * processed, but not popped back to the HBA then this routine will return NULL.
375 **/
376static struct lpfc_cqe *
377lpfc_sli4_cq_get(struct lpfc_queue *q)
378{
379 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400380 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400381
James Smart2e90f4b2011-12-13 13:22:37 -0500382 /* sanity check on queue memory */
383 if (unlikely(!q))
384 return NULL;
385
James Smart4f774512009-05-22 14:52:35 -0400386 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400387 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400388 return NULL;
389 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400390 idx = ((q->hba_index + 1) % q->entry_count);
391 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400392 return NULL;
393
394 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400395 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400396
397 /*
398 * insert barrier for instruction interlock : data from the hardware
399 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800400 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
401 * instructions allowing action on content before valid bit checked,
402 * add barrier here as well. May not be needed as "content" is a
403 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400404 */
405 mb();
James Smart4f774512009-05-22 14:52:35 -0400406 return cqe;
407}
408
409/**
410 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
411 * @q: The Completion Queue that the host has completed processing for.
412 * @arm: Indicates whether the host wants to arms this CQ.
413 *
414 * This routine will mark all Completion queue entries on @q, from the last
415 * known completed entry to the last entry that was processed, as completed
416 * by clearing the valid bit for each completion queue entry. Then it will
417 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
418 * The internal host index in the @q will be updated by this routine to indicate
419 * that the host has finished processing the entries. The @arm parameter
420 * indicates that the queue should be rearmed when ringing the doorbell.
421 *
422 * This function will return the number of CQEs that were released.
423 **/
424uint32_t
425lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
426{
427 uint32_t released = 0;
428 struct lpfc_cqe *temp_qe;
429 struct lpfc_register doorbell;
430
James Smart2e90f4b2011-12-13 13:22:37 -0500431 /* sanity check on queue memory */
432 if (unlikely(!q))
433 return 0;
James Smart4f774512009-05-22 14:52:35 -0400434 /* while there are valid entries */
435 while (q->hba_index != q->host_index) {
436 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400437 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400438 released++;
439 q->host_index = ((q->host_index + 1) % q->entry_count);
440 }
441 if (unlikely(released == 0 && !arm))
442 return 0;
443
444 /* ring doorbell for number popped */
445 doorbell.word0 = 0;
446 if (arm)
447 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
448 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
449 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500450 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
451 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
452 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400453 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
454 return released;
455}
456
457/**
458 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
459 * @q: The Header Receive Queue to operate on.
460 * @wqe: The Receive Queue Entry to put on the Receive queue.
461 *
462 * This routine will copy the contents of @wqe to the next available entry on
463 * the @q. This function will then ring the Receive Queue Doorbell to signal the
464 * HBA to start processing the Receive Queue Entry. This function returns the
465 * index that the rqe was copied to if successful. If no entries are available
466 * on @q then this function will return -ENOMEM.
467 * The caller is expected to hold the hbalock when calling this routine.
468 **/
James Smart895427b2017-02-12 13:52:30 -0800469int
James Smart4f774512009-05-22 14:52:35 -0400470lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
471 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
472{
James Smart2e90f4b2011-12-13 13:22:37 -0500473 struct lpfc_rqe *temp_hrqe;
474 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400475 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500476 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400477
James Smart2e90f4b2011-12-13 13:22:37 -0500478 /* sanity check on queue memory */
479 if (unlikely(!hq) || unlikely(!dq))
480 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500481 put_index = hq->host_index;
James Smart2e90f4b2011-12-13 13:22:37 -0500482 temp_hrqe = hq->qe[hq->host_index].rqe;
483 temp_drqe = dq->qe[dq->host_index].rqe;
484
James Smart4f774512009-05-22 14:52:35 -0400485 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
486 return -EINVAL;
487 if (hq->host_index != dq->host_index)
488 return -EINVAL;
489 /* If the host has not yet processed the next entry then we are done */
490 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
491 return -EBUSY;
492 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
493 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
494
495 /* Update the host index to point to the next slot */
496 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
497 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
498
499 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400500 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400501 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500502 if (hq->db_format == LPFC_DB_RING_FORMAT) {
503 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
504 hq->entry_repost);
505 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
506 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
507 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
508 hq->entry_repost);
509 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
510 hq->host_index);
511 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
512 } else {
513 return -EINVAL;
514 }
James Smart547077a2017-05-15 15:20:40 -0700515 hq->RQ_buf_posted += hq->entry_repost;
James Smart962bc512013-01-03 15:44:00 -0500516 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400517 }
518 return put_index;
519}
520
521/**
522 * lpfc_sli4_rq_release - Updates internal hba index for RQ
523 * @q: The Header Receive Queue to operate on.
524 *
525 * This routine will update the HBA index of a queue to reflect consumption of
526 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
527 * consumed an entry the host calls this function to update the queue's
528 * internal pointers. This routine returns the number of entries that were
529 * consumed by the HBA.
530 **/
531static uint32_t
532lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
533{
James Smart2e90f4b2011-12-13 13:22:37 -0500534 /* sanity check on queue memory */
535 if (unlikely(!hq) || unlikely(!dq))
536 return 0;
537
James Smart4f774512009-05-22 14:52:35 -0400538 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
539 return 0;
540 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
541 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
542 return 1;
543}
544
James Smarte59058c2008-08-24 21:49:00 -0400545/**
James Smart3621a712009-04-06 18:47:14 -0400546 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400547 * @phba: Pointer to HBA context object.
548 * @pring: Pointer to driver SLI ring object.
549 *
550 * This function returns pointer to next command iocb entry
551 * in the command ring. The caller must hold hbalock to prevent
552 * other threads consume the next command iocb.
553 * SLI-2/SLI-3 provide different sized iocbs.
554 **/
James Smarted957682007-06-17 19:56:37 -0500555static inline IOCB_t *
556lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
557{
James Smart7e56aa22012-08-03 12:35:34 -0400558 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
559 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500560}
561
James Smarte59058c2008-08-24 21:49:00 -0400562/**
James Smart3621a712009-04-06 18:47:14 -0400563 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400564 * @phba: Pointer to HBA context object.
565 * @pring: Pointer to driver SLI ring object.
566 *
567 * This function returns pointer to next response iocb entry
568 * in the response ring. The caller must hold hbalock to make sure
569 * that no other thread consume the next response iocb.
570 * SLI-2/SLI-3 provide different sized iocbs.
571 **/
James Smarted957682007-06-17 19:56:37 -0500572static inline IOCB_t *
573lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
574{
James Smart7e56aa22012-08-03 12:35:34 -0400575 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
576 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500577}
578
James Smarte59058c2008-08-24 21:49:00 -0400579/**
James Smart3621a712009-04-06 18:47:14 -0400580 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400581 * @phba: Pointer to HBA context object.
582 *
583 * This function is called with hbalock held. This function
584 * allocates a new driver iocb object from the iocb pool. If the
585 * allocation is successful, it returns pointer to the newly
586 * allocated iocb object else it returns NULL.
587 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400588struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500589__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400590{
591 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
592 struct lpfc_iocbq * iocbq = NULL;
593
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100594 lockdep_assert_held(&phba->hbalock);
595
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400596 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400597 if (iocbq)
598 phba->iocb_cnt++;
599 if (phba->iocb_cnt > phba->iocb_max)
600 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400601 return iocbq;
602}
603
James Smarte59058c2008-08-24 21:49:00 -0400604/**
James Smartda0436e2009-05-22 14:51:39 -0400605 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
606 * @phba: Pointer to HBA context object.
607 * @xritag: XRI value.
608 *
609 * This function clears the sglq pointer from the array of acive
610 * sglq's. The xritag that is passed in is used to index into the
611 * array. Before the xritag can be used it needs to be adjusted
612 * by subtracting the xribase.
613 *
614 * Returns sglq ponter = success, NULL = Failure.
615 **/
James Smart895427b2017-02-12 13:52:30 -0800616struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400617__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
618{
James Smartda0436e2009-05-22 14:51:39 -0400619 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400620
621 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
622 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400623 return sglq;
624}
625
626/**
627 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
628 * @phba: Pointer to HBA context object.
629 * @xritag: XRI value.
630 *
631 * This function returns the sglq pointer from the array of acive
632 * sglq's. The xritag that is passed in is used to index into the
633 * array. Before the xritag can be used it needs to be adjusted
634 * by subtracting the xribase.
635 *
636 * Returns sglq ponter = success, NULL = Failure.
637 **/
James Smart0f65ff62010-02-26 14:14:23 -0500638struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400639__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
640{
James Smartda0436e2009-05-22 14:51:39 -0400641 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400642
643 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400644 return sglq;
645}
646
647/**
James Smart1151e3e2011-02-16 12:39:35 -0500648 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500649 * @phba: Pointer to HBA context object.
650 * @xritag: xri used in this exchange.
651 * @rrq: The RRQ to be cleared.
652 *
James Smart19ca7602010-11-20 23:11:55 -0500653 **/
James Smart1151e3e2011-02-16 12:39:35 -0500654void
655lpfc_clr_rrq_active(struct lpfc_hba *phba,
656 uint16_t xritag,
657 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500658{
James Smart1151e3e2011-02-16 12:39:35 -0500659 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500660
James Smart1151e3e2011-02-16 12:39:35 -0500661 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
662 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500663
664 /* The target DID could have been swapped (cable swap)
665 * we should use the ndlp from the findnode if it is
666 * available.
667 */
James Smart1151e3e2011-02-16 12:39:35 -0500668 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500669 ndlp = rrq->ndlp;
670
James Smart1151e3e2011-02-16 12:39:35 -0500671 if (!ndlp)
672 goto out;
673
James Smartcff261f2013-12-17 20:29:47 -0500674 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500675 rrq->send_rrq = 0;
676 rrq->xritag = 0;
677 rrq->rrq_stop_time = 0;
678 }
James Smart1151e3e2011-02-16 12:39:35 -0500679out:
James Smart19ca7602010-11-20 23:11:55 -0500680 mempool_free(rrq, phba->rrq_pool);
681}
682
683/**
684 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
685 * @phba: Pointer to HBA context object.
686 *
687 * This function is called with hbalock held. This function
688 * Checks if stop_time (ratov from setting rrq active) has
689 * been reached, if it has and the send_rrq flag is set then
690 * it will call lpfc_send_rrq. If the send_rrq flag is not set
691 * then it will just call the routine to clear the rrq and
692 * free the rrq resource.
693 * The timer is set to the next rrq that is going to expire before
694 * leaving the routine.
695 *
696 **/
697void
698lpfc_handle_rrq_active(struct lpfc_hba *phba)
699{
700 struct lpfc_node_rrq *rrq;
701 struct lpfc_node_rrq *nextrrq;
702 unsigned long next_time;
703 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500704 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500705
706 spin_lock_irqsave(&phba->hbalock, iflags);
707 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400708 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500709 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500710 &phba->active_rrq_list, list) {
711 if (time_after(jiffies, rrq->rrq_stop_time))
712 list_move(&rrq->list, &send_rrq);
713 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500714 next_time = rrq->rrq_stop_time;
715 }
716 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500717 if ((!list_empty(&phba->active_rrq_list)) &&
718 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500719 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500720 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
721 list_del(&rrq->list);
722 if (!rrq->send_rrq)
723 /* this call will free the rrq */
724 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
725 else if (lpfc_send_rrq(phba, rrq)) {
726 /* if we send the rrq then the completion handler
727 * will clear the bit in the xribitmap.
728 */
729 lpfc_clr_rrq_active(phba, rrq->xritag,
730 rrq);
731 }
732 }
James Smart19ca7602010-11-20 23:11:55 -0500733}
734
735/**
736 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
737 * @vport: Pointer to vport context object.
738 * @xri: The xri used in the exchange.
739 * @did: The targets DID for this exchange.
740 *
741 * returns NULL = rrq not found in the phba->active_rrq_list.
742 * rrq = rrq for this xri and target.
743 **/
744struct lpfc_node_rrq *
745lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
746{
747 struct lpfc_hba *phba = vport->phba;
748 struct lpfc_node_rrq *rrq;
749 struct lpfc_node_rrq *nextrrq;
750 unsigned long iflags;
751
752 if (phba->sli_rev != LPFC_SLI_REV4)
753 return NULL;
754 spin_lock_irqsave(&phba->hbalock, iflags);
755 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
756 if (rrq->vport == vport && rrq->xritag == xri &&
757 rrq->nlp_DID == did){
758 list_del(&rrq->list);
759 spin_unlock_irqrestore(&phba->hbalock, iflags);
760 return rrq;
761 }
762 }
763 spin_unlock_irqrestore(&phba->hbalock, iflags);
764 return NULL;
765}
766
767/**
768 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
769 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500770 * @ndlp: Pointer to the lpfc_node_list structure.
771 * If ndlp is NULL Remove all active RRQs for this vport from the
772 * phba->active_rrq_list and clear the rrq.
773 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500774 **/
775void
James Smart1151e3e2011-02-16 12:39:35 -0500776lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500777
778{
779 struct lpfc_hba *phba = vport->phba;
780 struct lpfc_node_rrq *rrq;
781 struct lpfc_node_rrq *nextrrq;
782 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500783 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500784
785 if (phba->sli_rev != LPFC_SLI_REV4)
786 return;
James Smart1151e3e2011-02-16 12:39:35 -0500787 if (!ndlp) {
788 lpfc_sli4_vport_delete_els_xri_aborted(vport);
789 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500790 }
James Smart1151e3e2011-02-16 12:39:35 -0500791 spin_lock_irqsave(&phba->hbalock, iflags);
792 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
793 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
794 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500795 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500796
797 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
798 list_del(&rrq->list);
799 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
800 }
James Smart19ca7602010-11-20 23:11:55 -0500801}
802
803/**
James Smart1151e3e2011-02-16 12:39:35 -0500804 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500805 * @phba: Pointer to HBA context object.
806 * @ndlp: Targets nodelist pointer for this exchange.
807 * @xritag the xri in the bitmap to test.
808 *
809 * This function is called with hbalock held. This function
810 * returns 0 = rrq not active for this xri
811 * 1 = rrq is valid for this xri.
812 **/
James Smart1151e3e2011-02-16 12:39:35 -0500813int
814lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500815 uint16_t xritag)
816{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100817 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500818 if (!ndlp)
819 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500820 if (!ndlp->active_rrqs_xri_bitmap)
821 return 0;
822 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500823 return 1;
824 else
825 return 0;
826}
827
828/**
829 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
830 * @phba: Pointer to HBA context object.
831 * @ndlp: nodelist pointer for this target.
832 * @xritag: xri used in this exchange.
833 * @rxid: Remote Exchange ID.
834 * @send_rrq: Flag used to determine if we should send rrq els cmd.
835 *
836 * This function takes the hbalock.
837 * The active bit is always set in the active rrq xri_bitmap even
838 * if there is no slot avaiable for the other rrq information.
839 *
840 * returns 0 rrq actived for this xri
841 * < 0 No memory or invalid ndlp.
842 **/
843int
844lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500845 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500846{
James Smart19ca7602010-11-20 23:11:55 -0500847 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500848 struct lpfc_node_rrq *rrq;
849 int empty;
850
851 if (!ndlp)
852 return -EINVAL;
853
854 if (!phba->cfg_enable_rrq)
855 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500856
857 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500858 if (phba->pport->load_flag & FC_UNLOADING) {
859 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
860 goto out;
861 }
862
863 /*
864 * set the active bit even if there is no mem available.
865 */
866 if (NLP_CHK_FREE_REQ(ndlp))
867 goto out;
868
869 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
870 goto out;
871
James Smartcff261f2013-12-17 20:29:47 -0500872 if (!ndlp->active_rrqs_xri_bitmap)
873 goto out;
874
875 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500876 goto out;
877
James Smart19ca7602010-11-20 23:11:55 -0500878 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500879 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
880 if (!rrq) {
881 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
882 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
883 " DID:0x%x Send:%d\n",
884 xritag, rxid, ndlp->nlp_DID, send_rrq);
885 return -EINVAL;
886 }
James Smarte5771b42013-03-01 16:37:14 -0500887 if (phba->cfg_enable_rrq == 1)
888 rrq->send_rrq = send_rrq;
889 else
890 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500891 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400892 rrq->rrq_stop_time = jiffies +
893 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500894 rrq->ndlp = ndlp;
895 rrq->nlp_DID = ndlp->nlp_DID;
896 rrq->vport = ndlp->vport;
897 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500898 spin_lock_irqsave(&phba->hbalock, iflags);
899 empty = list_empty(&phba->active_rrq_list);
900 list_add_tail(&rrq->list, &phba->active_rrq_list);
901 phba->hba_flag |= HBA_RRQ_ACTIVE;
902 if (empty)
903 lpfc_worker_wake_up(phba);
904 spin_unlock_irqrestore(&phba->hbalock, iflags);
905 return 0;
906out:
907 spin_unlock_irqrestore(&phba->hbalock, iflags);
908 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
909 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
910 " DID:0x%x Send:%d\n",
911 xritag, rxid, ndlp->nlp_DID, send_rrq);
912 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500913}
914
915/**
James Smart895427b2017-02-12 13:52:30 -0800916 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -0400917 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500918 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400919 *
James Smartdafe8ce2014-09-03 12:56:40 -0400920 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400921 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400922 * list is not empty then it is successful, it returns pointer to the newly
923 * allocated sglq object else it returns NULL.
924 **/
925static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -0800926__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400927{
James Smart895427b2017-02-12 13:52:30 -0800928 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -0400929 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500930 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500931 struct lpfc_scsi_buf *lpfc_cmd;
932 struct lpfc_nodelist *ndlp;
933 int found = 0;
934
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100935 lockdep_assert_held(&phba->hbalock);
936
James Smart19ca7602010-11-20 23:11:55 -0500937 if (piocbq->iocb_flag & LPFC_IO_FCP) {
938 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
939 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500940 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400941 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500942 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400943 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
944 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
945 ndlp = NULL;
946 else
947 ndlp = piocbq->context_un.ndlp;
948 } else {
James Smart19ca7602010-11-20 23:11:55 -0500949 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400950 }
James Smart19ca7602010-11-20 23:11:55 -0500951
James Smart895427b2017-02-12 13:52:30 -0800952 spin_lock(&phba->sli4_hba.sgl_list_lock);
953 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500954 start_sglq = sglq;
955 while (!found) {
956 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -0800957 break;
James Smart895427b2017-02-12 13:52:30 -0800958 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
959 test_bit(sglq->sli4_lxritag,
960 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500961 /* This xri has an rrq outstanding for this DID.
962 * put it back in the list and get another xri.
963 */
James Smart895427b2017-02-12 13:52:30 -0800964 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -0500965 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -0800966 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -0500967 struct lpfc_sglq, list);
968 if (sglq == start_sglq) {
969 sglq = NULL;
970 break;
971 } else
972 continue;
973 }
974 sglq->ndlp = ndlp;
975 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400976 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500977 sglq->state = SGL_ALLOCATED;
978 }
James Smart895427b2017-02-12 13:52:30 -0800979 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400980 return sglq;
981}
982
983/**
James Smartf358dd02017-02-12 13:52:34 -0800984 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
985 * @phba: Pointer to HBA context object.
986 * @piocb: Pointer to the iocbq.
987 *
988 * This function is called with the sgl_list lock held. This function
989 * gets a new driver sglq object from the sglq list. If the
990 * list is not empty then it is successful, it returns pointer to the newly
991 * allocated sglq object else it returns NULL.
992 **/
993struct lpfc_sglq *
994__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
995{
996 struct list_head *lpfc_nvmet_sgl_list;
997 struct lpfc_sglq *sglq = NULL;
998
999 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1000
1001 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1002
1003 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1004 if (!sglq)
1005 return NULL;
1006 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1007 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001008 return sglq;
1009}
1010
James Smarte59058c2008-08-24 21:49:00 -04001011/**
James Smart3621a712009-04-06 18:47:14 -04001012 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001013 * @phba: Pointer to HBA context object.
1014 *
1015 * This function is called with no lock held. This function
1016 * allocates a new driver iocb object from the iocb pool. If the
1017 * allocation is successful, it returns pointer to the newly
1018 * allocated iocb object else it returns NULL.
1019 **/
James Smart2e0fef82007-06-17 19:56:36 -05001020struct lpfc_iocbq *
1021lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001022{
James Smart2e0fef82007-06-17 19:56:36 -05001023 struct lpfc_iocbq * iocbq = NULL;
1024 unsigned long iflags;
1025
1026 spin_lock_irqsave(&phba->hbalock, iflags);
1027 iocbq = __lpfc_sli_get_iocbq(phba);
1028 spin_unlock_irqrestore(&phba->hbalock, iflags);
1029 return iocbq;
1030}
1031
James Smarte59058c2008-08-24 21:49:00 -04001032/**
James Smart4f774512009-05-22 14:52:35 -04001033 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1034 * @phba: Pointer to HBA context object.
1035 * @iocbq: Pointer to driver iocb object.
1036 *
1037 * This function is called with hbalock held to release driver
1038 * iocb object to the iocb pool. The iotag in the iocb object
1039 * does not change for each use of the iocb object. This function
1040 * clears all other fields of the iocb object when it is freed.
1041 * The sqlq structure that holds the xritag and phys and virtual
1042 * mappings for the scatter gather list is retrieved from the
1043 * active array of sglq. The get of the sglq pointer also clears
1044 * the entry in the array. If the status of the IO indiactes that
1045 * this IO was aborted then the sglq entry it put on the
1046 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1047 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001048 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001049 **/
1050static void
1051__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1052{
1053 struct lpfc_sglq *sglq;
1054 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001055 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001056 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001057
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001058 lockdep_assert_held(&phba->hbalock);
1059
James Smart4f774512009-05-22 14:52:35 -04001060 if (iocbq->sli4_xritag == NO_XRI)
1061 sglq = NULL;
1062 else
James Smart6d368e52011-05-24 11:44:12 -04001063 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1064
James Smart0e9bb8d2013-03-01 16:35:12 -05001065
James Smart4f774512009-05-22 14:52:35 -04001066 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001067 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1068 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1069 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001070 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001071 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001072 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001073 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1074 spin_unlock_irqrestore(
1075 &phba->sli4_hba.sgl_list_lock, iflag);
1076 goto out;
1077 }
1078
James Smart895427b2017-02-12 13:52:30 -08001079 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001080 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1081 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001082 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1083 iflag);
James Smart341af102010-01-26 23:07:37 -05001084 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001085 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001086 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001087 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001088 } else {
James Smart895427b2017-02-12 13:52:30 -08001089 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1090 iflag);
James Smart4f774512009-05-22 14:52:35 -04001091 sglq->state = SGL_FREED;
1092 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001093 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001094 &phba->sli4_hba.lpfc_els_sgl_list);
1095 spin_unlock_irqrestore(
1096 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001097
1098 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001099 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001100 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001101 }
James Smart4f774512009-05-22 14:52:35 -04001102 }
1103
James Smartf358dd02017-02-12 13:52:34 -08001104out:
James Smart4f774512009-05-22 14:52:35 -04001105 /*
1106 * Clean all volatile data fields, preserve iotag and node struct.
1107 */
1108 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001109 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001110 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001111 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1112 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001113 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1114}
1115
James Smart2a9bf3d2010-06-07 15:24:45 -04001116
James Smart4f774512009-05-22 14:52:35 -04001117/**
James Smart3772a992009-05-22 14:50:54 -04001118 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1119 * @phba: Pointer to HBA context object.
1120 * @iocbq: Pointer to driver iocb object.
1121 *
1122 * This function is called with hbalock held to release driver
1123 * iocb object to the iocb pool. The iotag in the iocb object
1124 * does not change for each use of the iocb object. This function
1125 * clears all other fields of the iocb object when it is freed.
1126 **/
1127static void
1128__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1129{
1130 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1131
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001132 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001133
1134 /*
James Smart3772a992009-05-22 14:50:54 -04001135 * Clean all volatile data fields, preserve iotag and node struct.
1136 */
1137 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1138 iocbq->sli4_xritag = NO_XRI;
1139 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1140}
1141
1142/**
James Smart3621a712009-04-06 18:47:14 -04001143 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001144 * @phba: Pointer to HBA context object.
1145 * @iocbq: Pointer to driver iocb object.
1146 *
1147 * This function is called with hbalock held to release driver
1148 * iocb object to the iocb pool. The iotag in the iocb object
1149 * does not change for each use of the iocb object. This function
1150 * clears all other fields of the iocb object when it is freed.
1151 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001152static void
James Smart2e0fef82007-06-17 19:56:36 -05001153__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1154{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001155 lockdep_assert_held(&phba->hbalock);
1156
James Smart3772a992009-05-22 14:50:54 -04001157 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001158 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001159}
1160
James Smarte59058c2008-08-24 21:49:00 -04001161/**
James Smart3621a712009-04-06 18:47:14 -04001162 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001163 * @phba: Pointer to HBA context object.
1164 * @iocbq: Pointer to driver iocb object.
1165 *
1166 * This function is called with no lock held to release the iocb to
1167 * iocb pool.
1168 **/
James Smart2e0fef82007-06-17 19:56:36 -05001169void
1170lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1171{
1172 unsigned long iflags;
1173
1174 /*
1175 * Clean all volatile data fields, preserve iotag and node struct.
1176 */
1177 spin_lock_irqsave(&phba->hbalock, iflags);
1178 __lpfc_sli_release_iocbq(phba, iocbq);
1179 spin_unlock_irqrestore(&phba->hbalock, iflags);
1180}
1181
James Smarte59058c2008-08-24 21:49:00 -04001182/**
James Smarta257bf92009-04-06 18:48:10 -04001183 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1184 * @phba: Pointer to HBA context object.
1185 * @iocblist: List of IOCBs.
1186 * @ulpstatus: ULP status in IOCB command field.
1187 * @ulpWord4: ULP word-4 in IOCB command field.
1188 *
1189 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1190 * on the list by invoking the complete callback function associated with the
1191 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1192 * fields.
1193 **/
1194void
1195lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1196 uint32_t ulpstatus, uint32_t ulpWord4)
1197{
1198 struct lpfc_iocbq *piocb;
1199
1200 while (!list_empty(iocblist)) {
1201 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001202 if (!piocb->iocb_cmpl)
1203 lpfc_sli_release_iocbq(phba, piocb);
1204 else {
1205 piocb->iocb.ulpStatus = ulpstatus;
1206 piocb->iocb.un.ulpWord[4] = ulpWord4;
1207 (piocb->iocb_cmpl) (phba, piocb, piocb);
1208 }
1209 }
1210 return;
1211}
1212
1213/**
James Smart3621a712009-04-06 18:47:14 -04001214 * lpfc_sli_iocb_cmd_type - Get the iocb type
1215 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001216 *
1217 * This function is called by ring event handler function to get the iocb type.
1218 * This function translates the iocb command to an iocb command type used to
1219 * decide the final disposition of each completed IOCB.
1220 * The function returns
1221 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1222 * LPFC_SOL_IOCB if it is a solicited iocb completion
1223 * LPFC_ABORT_IOCB if it is an abort iocb
1224 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1225 *
1226 * The caller is not required to hold any lock.
1227 **/
dea31012005-04-17 16:05:31 -05001228static lpfc_iocb_type
1229lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1230{
1231 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1232
1233 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1234 return 0;
1235
1236 switch (iocb_cmnd) {
1237 case CMD_XMIT_SEQUENCE_CR:
1238 case CMD_XMIT_SEQUENCE_CX:
1239 case CMD_XMIT_BCAST_CN:
1240 case CMD_XMIT_BCAST_CX:
1241 case CMD_ELS_REQUEST_CR:
1242 case CMD_ELS_REQUEST_CX:
1243 case CMD_CREATE_XRI_CR:
1244 case CMD_CREATE_XRI_CX:
1245 case CMD_GET_RPI_CN:
1246 case CMD_XMIT_ELS_RSP_CX:
1247 case CMD_GET_RPI_CR:
1248 case CMD_FCP_IWRITE_CR:
1249 case CMD_FCP_IWRITE_CX:
1250 case CMD_FCP_IREAD_CR:
1251 case CMD_FCP_IREAD_CX:
1252 case CMD_FCP_ICMND_CR:
1253 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001254 case CMD_FCP_TSEND_CX:
1255 case CMD_FCP_TRSP_CX:
1256 case CMD_FCP_TRECEIVE_CX:
1257 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001258 case CMD_ADAPTER_MSG:
1259 case CMD_ADAPTER_DUMP:
1260 case CMD_XMIT_SEQUENCE64_CR:
1261 case CMD_XMIT_SEQUENCE64_CX:
1262 case CMD_XMIT_BCAST64_CN:
1263 case CMD_XMIT_BCAST64_CX:
1264 case CMD_ELS_REQUEST64_CR:
1265 case CMD_ELS_REQUEST64_CX:
1266 case CMD_FCP_IWRITE64_CR:
1267 case CMD_FCP_IWRITE64_CX:
1268 case CMD_FCP_IREAD64_CR:
1269 case CMD_FCP_IREAD64_CX:
1270 case CMD_FCP_ICMND64_CR:
1271 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001272 case CMD_FCP_TSEND64_CX:
1273 case CMD_FCP_TRSP64_CX:
1274 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001275 case CMD_GEN_REQUEST64_CR:
1276 case CMD_GEN_REQUEST64_CX:
1277 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001278 case DSSCMD_IWRITE64_CR:
1279 case DSSCMD_IWRITE64_CX:
1280 case DSSCMD_IREAD64_CR:
1281 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001282 type = LPFC_SOL_IOCB;
1283 break;
1284 case CMD_ABORT_XRI_CN:
1285 case CMD_ABORT_XRI_CX:
1286 case CMD_CLOSE_XRI_CN:
1287 case CMD_CLOSE_XRI_CX:
1288 case CMD_XRI_ABORTED_CX:
1289 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001290 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001291 type = LPFC_ABORT_IOCB;
1292 break;
1293 case CMD_RCV_SEQUENCE_CX:
1294 case CMD_RCV_ELS_REQ_CX:
1295 case CMD_RCV_SEQUENCE64_CX:
1296 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001297 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001298 case CMD_IOCB_RCV_SEQ64_CX:
1299 case CMD_IOCB_RCV_ELS64_CX:
1300 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001301 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001302 type = LPFC_UNSOL_IOCB;
1303 break;
James Smart3163f722008-02-08 18:50:25 -05001304 case CMD_IOCB_XMIT_MSEQ64_CR:
1305 case CMD_IOCB_XMIT_MSEQ64_CX:
1306 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1307 case CMD_IOCB_RCV_ELS_LIST64_CX:
1308 case CMD_IOCB_CLOSE_EXTENDED_CN:
1309 case CMD_IOCB_ABORT_EXTENDED_CN:
1310 case CMD_IOCB_RET_HBQE64_CN:
1311 case CMD_IOCB_FCP_IBIDIR64_CR:
1312 case CMD_IOCB_FCP_IBIDIR64_CX:
1313 case CMD_IOCB_FCP_ITASKMGT64_CX:
1314 case CMD_IOCB_LOGENTRY_CN:
1315 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1316 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001317 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001318 type = LPFC_UNKNOWN_IOCB;
1319 break;
dea31012005-04-17 16:05:31 -05001320 default:
1321 type = LPFC_UNKNOWN_IOCB;
1322 break;
1323 }
1324
1325 return type;
1326}
1327
James Smarte59058c2008-08-24 21:49:00 -04001328/**
James Smart3621a712009-04-06 18:47:14 -04001329 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001330 * @phba: Pointer to HBA context object.
1331 *
1332 * This function is called from SLI initialization code
1333 * to configure every ring of the HBA's SLI interface. The
1334 * caller is not required to hold any lock. This function issues
1335 * a config_ring mailbox command for each ring.
1336 * This function returns zero if successful else returns a negative
1337 * error code.
1338 **/
dea31012005-04-17 16:05:31 -05001339static int
James Smarted957682007-06-17 19:56:37 -05001340lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001341{
1342 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001343 LPFC_MBOXQ_t *pmb;
1344 MAILBOX_t *pmbox;
1345 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001346
James Smarted957682007-06-17 19:56:37 -05001347 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1348 if (!pmb)
1349 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001350 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001351 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001352 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001353 lpfc_config_ring(phba, i, pmb);
1354 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1355 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001357 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001358 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1359 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001360 rc, pmbox->mbxCommand,
1361 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001362 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001363 ret = -ENXIO;
1364 break;
dea31012005-04-17 16:05:31 -05001365 }
1366 }
James Smarted957682007-06-17 19:56:37 -05001367 mempool_free(pmb, phba->mbox_mem_pool);
1368 return ret;
dea31012005-04-17 16:05:31 -05001369}
1370
James Smarte59058c2008-08-24 21:49:00 -04001371/**
James Smart3621a712009-04-06 18:47:14 -04001372 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001373 * @phba: Pointer to HBA context object.
1374 * @pring: Pointer to driver SLI ring object.
1375 * @piocb: Pointer to the driver iocb object.
1376 *
1377 * This function is called with hbalock held. The function adds the
1378 * new iocb to txcmplq of the given ring. This function always returns
1379 * 0. If this function is called for ELS ring, this function checks if
1380 * there is a vport associated with the ELS command. This function also
1381 * starts els_tmofunc timer if this is an ELS command.
1382 **/
dea31012005-04-17 16:05:31 -05001383static int
James Smart2e0fef82007-06-17 19:56:36 -05001384lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1385 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001386{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001387 lockdep_assert_held(&phba->hbalock);
1388
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001389 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001390
dea31012005-04-17 16:05:31 -05001391 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001392 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001393
James Smart92d7f7b2007-06-17 19:56:38 -05001394 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1395 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001396 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1397 BUG_ON(!piocb->vport);
1398 if (!(piocb->vport->load_flag & FC_UNLOADING))
1399 mod_timer(&piocb->vport->els_tmofunc,
1400 jiffies +
1401 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1402 }
dea31012005-04-17 16:05:31 -05001403
James Smart2e0fef82007-06-17 19:56:36 -05001404 return 0;
dea31012005-04-17 16:05:31 -05001405}
1406
James Smarte59058c2008-08-24 21:49:00 -04001407/**
James Smart3621a712009-04-06 18:47:14 -04001408 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001409 * @phba: Pointer to HBA context object.
1410 * @pring: Pointer to driver SLI ring object.
1411 *
1412 * This function is called with hbalock held to get next
1413 * iocb in txq of the given ring. If there is any iocb in
1414 * the txq, the function returns first iocb in the list after
1415 * removing the iocb from the list, else it returns NULL.
1416 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001417struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001418lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001419{
dea31012005-04-17 16:05:31 -05001420 struct lpfc_iocbq *cmd_iocb;
1421
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001422 lockdep_assert_held(&phba->hbalock);
1423
James Smart858c9f62007-06-17 19:56:39 -05001424 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001425 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001426}
1427
James Smarte59058c2008-08-24 21:49:00 -04001428/**
James Smart3621a712009-04-06 18:47:14 -04001429 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001430 * @phba: Pointer to HBA context object.
1431 * @pring: Pointer to driver SLI ring object.
1432 *
1433 * This function is called with hbalock held and the caller must post the
1434 * iocb without releasing the lock. If the caller releases the lock,
1435 * iocb slot returned by the function is not guaranteed to be available.
1436 * The function returns pointer to the next available iocb slot if there
1437 * is available slot in the ring, else it returns NULL.
1438 * If the get index of the ring is ahead of the put index, the function
1439 * will post an error attention event to the worker thread to take the
1440 * HBA to offline state.
1441 **/
dea31012005-04-17 16:05:31 -05001442static IOCB_t *
1443lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1444{
James Smart34b02dc2008-08-24 21:49:55 -04001445 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001446 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001447
1448 lockdep_assert_held(&phba->hbalock);
1449
James Smart7e56aa22012-08-03 12:35:34 -04001450 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1451 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1452 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001453
James Smart7e56aa22012-08-03 12:35:34 -04001454 if (unlikely(pring->sli.sli3.local_getidx ==
1455 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001456
James Smart7e56aa22012-08-03 12:35:34 -04001457 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001458
James Smart7e56aa22012-08-03 12:35:34 -04001459 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001460 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001461 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001462 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001463 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001464 pring->sli.sli3.local_getidx,
1465 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001466
James Smart2e0fef82007-06-17 19:56:36 -05001467 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001468 /*
1469 * All error attention handlers are posted to
1470 * worker thread
1471 */
1472 phba->work_ha |= HA_ERATT;
1473 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001474
James Smart5e9d9b82008-06-14 22:52:53 -04001475 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001476
1477 return NULL;
1478 }
1479
James Smart7e56aa22012-08-03 12:35:34 -04001480 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001481 return NULL;
1482 }
1483
James Smarted957682007-06-17 19:56:37 -05001484 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001485}
1486
James Smarte59058c2008-08-24 21:49:00 -04001487/**
James Smart3621a712009-04-06 18:47:14 -04001488 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001489 * @phba: Pointer to HBA context object.
1490 * @iocbq: Pointer to driver iocb object.
1491 *
1492 * This function gets an iotag for the iocb. If there is no unused iotag and
1493 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1494 * array and assigns a new iotag.
1495 * The function returns the allocated iotag if successful, else returns zero.
1496 * Zero is not a valid iotag.
1497 * The caller is not required to hold any lock.
1498 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001499uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001500lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001501{
James Smart2e0fef82007-06-17 19:56:36 -05001502 struct lpfc_iocbq **new_arr;
1503 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001504 size_t new_len;
1505 struct lpfc_sli *psli = &phba->sli;
1506 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001507
James Smart2e0fef82007-06-17 19:56:36 -05001508 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001509 iotag = psli->last_iotag;
1510 if(++iotag < psli->iocbq_lookup_len) {
1511 psli->last_iotag = iotag;
1512 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001513 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001514 iocbq->iotag = iotag;
1515 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001516 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001517 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1518 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001519 spin_unlock_irq(&phba->hbalock);
1520 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001521 GFP_KERNEL);
1522 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001523 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001524 old_arr = psli->iocbq_lookup;
1525 if (new_len <= psli->iocbq_lookup_len) {
1526 /* highly unprobable case */
1527 kfree(new_arr);
1528 iotag = psli->last_iotag;
1529 if(++iotag < psli->iocbq_lookup_len) {
1530 psli->last_iotag = iotag;
1531 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001532 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001533 iocbq->iotag = iotag;
1534 return iotag;
1535 }
James Smart2e0fef82007-06-17 19:56:36 -05001536 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001537 return 0;
1538 }
1539 if (psli->iocbq_lookup)
1540 memcpy(new_arr, old_arr,
1541 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001542 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001543 psli->iocbq_lookup = new_arr;
1544 psli->iocbq_lookup_len = new_len;
1545 psli->last_iotag = iotag;
1546 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001547 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001548 iocbq->iotag = iotag;
1549 kfree(old_arr);
1550 return iotag;
1551 }
James Smart8f6d98d2006-08-01 07:34:00 -04001552 } else
James Smart2e0fef82007-06-17 19:56:36 -05001553 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001554
James Smartbc739052010-08-04 16:11:18 -04001555 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001556 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1557 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001558
James Bottomley604a3e32005-10-29 10:28:33 -05001559 return 0;
dea31012005-04-17 16:05:31 -05001560}
1561
James Smarte59058c2008-08-24 21:49:00 -04001562/**
James Smart3621a712009-04-06 18:47:14 -04001563 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001564 * @phba: Pointer to HBA context object.
1565 * @pring: Pointer to driver SLI ring object.
1566 * @iocb: Pointer to iocb slot in the ring.
1567 * @nextiocb: Pointer to driver iocb object which need to be
1568 * posted to firmware.
1569 *
1570 * This function is called with hbalock held to post a new iocb to
1571 * the firmware. This function copies the new iocb to ring iocb slot and
1572 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1573 * a completion call back for this iocb else the function will free the
1574 * iocb object.
1575 **/
dea31012005-04-17 16:05:31 -05001576static void
1577lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1578 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1579{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001580 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001581 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001582 * Set up an iotag
dea31012005-04-17 16:05:31 -05001583 */
James Bottomley604a3e32005-10-29 10:28:33 -05001584 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001585
James Smarte2a0a9d2008-12-04 22:40:02 -05001586
James Smarta58cbd52007-08-02 11:09:43 -04001587 if (pring->ringno == LPFC_ELS_RING) {
1588 lpfc_debugfs_slow_ring_trc(phba,
1589 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1590 *(((uint32_t *) &nextiocb->iocb) + 4),
1591 *(((uint32_t *) &nextiocb->iocb) + 6),
1592 *(((uint32_t *) &nextiocb->iocb) + 7));
1593 }
1594
dea31012005-04-17 16:05:31 -05001595 /*
1596 * Issue iocb command to adapter
1597 */
James Smart92d7f7b2007-06-17 19:56:38 -05001598 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001599 wmb();
1600 pring->stats.iocb_cmd++;
1601
1602 /*
1603 * If there is no completion routine to call, we can release the
1604 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1605 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1606 */
1607 if (nextiocb->iocb_cmpl)
1608 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001609 else
James Smart2e0fef82007-06-17 19:56:36 -05001610 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001611
1612 /*
1613 * Let the HBA know what IOCB slot will be the next one the
1614 * driver will put a command into.
1615 */
James Smart7e56aa22012-08-03 12:35:34 -04001616 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1617 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001618}
1619
James Smarte59058c2008-08-24 21:49:00 -04001620/**
James Smart3621a712009-04-06 18:47:14 -04001621 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001622 * @phba: Pointer to HBA context object.
1623 * @pring: Pointer to driver SLI ring object.
1624 *
1625 * The caller is not required to hold any lock for calling this function.
1626 * This function updates the chip attention bits for the ring to inform firmware
1627 * that there are pending work to be done for this ring and requests an
1628 * interrupt when there is space available in the ring. This function is
1629 * called when the driver is unable to post more iocbs to the ring due
1630 * to unavailability of space in the ring.
1631 **/
dea31012005-04-17 16:05:31 -05001632static void
James Smart2e0fef82007-06-17 19:56:36 -05001633lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001634{
1635 int ringno = pring->ringno;
1636
1637 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1638
1639 wmb();
1640
1641 /*
1642 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1643 * The HBA will tell us when an IOCB entry is available.
1644 */
1645 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1646 readl(phba->CAregaddr); /* flush */
1647
1648 pring->stats.iocb_cmd_full++;
1649}
1650
James Smarte59058c2008-08-24 21:49:00 -04001651/**
James Smart3621a712009-04-06 18:47:14 -04001652 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001653 * @phba: Pointer to HBA context object.
1654 * @pring: Pointer to driver SLI ring object.
1655 *
1656 * This function updates the chip attention register bit for the
1657 * given ring to inform HBA that there is more work to be done
1658 * in this ring. The caller is not required to hold any lock.
1659 **/
dea31012005-04-17 16:05:31 -05001660static void
James Smart2e0fef82007-06-17 19:56:36 -05001661lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001662{
1663 int ringno = pring->ringno;
1664
1665 /*
1666 * Tell the HBA that there is work to do in this ring.
1667 */
James Smart34b02dc2008-08-24 21:49:55 -04001668 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1669 wmb();
1670 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1671 readl(phba->CAregaddr); /* flush */
1672 }
dea31012005-04-17 16:05:31 -05001673}
1674
James Smarte59058c2008-08-24 21:49:00 -04001675/**
James Smart3621a712009-04-06 18:47:14 -04001676 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001677 * @phba: Pointer to HBA context object.
1678 * @pring: Pointer to driver SLI ring object.
1679 *
1680 * This function is called with hbalock held to post pending iocbs
1681 * in the txq to the firmware. This function is called when driver
1682 * detects space available in the ring.
1683 **/
dea31012005-04-17 16:05:31 -05001684static void
James Smart2e0fef82007-06-17 19:56:36 -05001685lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001686{
1687 IOCB_t *iocb;
1688 struct lpfc_iocbq *nextiocb;
1689
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001690 lockdep_assert_held(&phba->hbalock);
1691
dea31012005-04-17 16:05:31 -05001692 /*
1693 * Check to see if:
1694 * (a) there is anything on the txq to send
1695 * (b) link is up
1696 * (c) link attention events can be processed (fcp ring only)
1697 * (d) IOCB processing is not blocked by the outstanding mbox command.
1698 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001699
1700 if (lpfc_is_link_up(phba) &&
1701 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001702 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001703 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001704
1705 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1706 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1707 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1708
1709 if (iocb)
1710 lpfc_sli_update_ring(phba, pring);
1711 else
1712 lpfc_sli_update_full_ring(phba, pring);
1713 }
1714
1715 return;
1716}
1717
James Smarte59058c2008-08-24 21:49:00 -04001718/**
James Smart3621a712009-04-06 18:47:14 -04001719 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001720 * @phba: Pointer to HBA context object.
1721 * @hbqno: HBQ number.
1722 *
1723 * This function is called with hbalock held to get the next
1724 * available slot for the given HBQ. If there is free slot
1725 * available for the HBQ it will return pointer to the next available
1726 * HBQ entry else it will return NULL.
1727 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001728static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001729lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1730{
1731 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1732
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001733 lockdep_assert_held(&phba->hbalock);
1734
James Smarted957682007-06-17 19:56:37 -05001735 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1736 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1737 hbqp->next_hbqPutIdx = 0;
1738
1739 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001740 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001741 uint32_t getidx = le32_to_cpu(raw_index);
1742
1743 hbqp->local_hbqGetIdx = getidx;
1744
1745 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1746 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001747 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001748 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001749 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001750 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001751 hbqp->entry_count);
1752
1753 phba->link_state = LPFC_HBA_ERROR;
1754 return NULL;
1755 }
1756
1757 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1758 return NULL;
1759 }
1760
James Smart51ef4c22007-08-02 11:10:31 -04001761 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1762 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001763}
1764
James Smarte59058c2008-08-24 21:49:00 -04001765/**
James Smart3621a712009-04-06 18:47:14 -04001766 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001767 * @phba: Pointer to HBA context object.
1768 *
1769 * This function is called with no lock held to free all the
1770 * hbq buffers while uninitializing the SLI interface. It also
1771 * frees the HBQ buffers returned by the firmware but not yet
1772 * processed by the upper layers.
1773 **/
James Smarted957682007-06-17 19:56:37 -05001774void
1775lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1776{
James Smart92d7f7b2007-06-17 19:56:38 -05001777 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1778 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001779 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001780 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001781
James Smart51ef4c22007-08-02 11:10:31 -04001782 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001783 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001784 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001785 for (i = 0; i < hbq_count; ++i) {
1786 list_for_each_entry_safe(dmabuf, next_dmabuf,
1787 &phba->hbqs[i].hbq_buffer_list, list) {
1788 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1789 list_del(&hbq_buf->dbuf.list);
1790 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1791 }
James Smarta8adb832007-10-27 13:37:53 -04001792 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001793 }
James Smart3163f722008-02-08 18:50:25 -05001794
1795 /* Mark the HBQs not in use */
1796 phba->hbq_in_use = 0;
1797 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001798}
1799
James Smarte59058c2008-08-24 21:49:00 -04001800/**
James Smart3621a712009-04-06 18:47:14 -04001801 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001802 * @phba: Pointer to HBA context object.
1803 * @hbqno: HBQ number.
1804 * @hbq_buf: Pointer to HBQ buffer.
1805 *
1806 * This function is called with the hbalock held to post a
1807 * hbq buffer to the firmware. If the function finds an empty
1808 * slot in the HBQ, it will post the buffer. The function will return
1809 * pointer to the hbq entry if it successfully post the buffer
1810 * else it will return NULL.
1811 **/
James Smart3772a992009-05-22 14:50:54 -04001812static int
James Smarted957682007-06-17 19:56:37 -05001813lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001814 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001815{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001816 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001817 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1818}
1819
1820/**
1821 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1822 * @phba: Pointer to HBA context object.
1823 * @hbqno: HBQ number.
1824 * @hbq_buf: Pointer to HBQ buffer.
1825 *
1826 * This function is called with the hbalock held to post a hbq buffer to the
1827 * firmware. If the function finds an empty slot in the HBQ, it will post the
1828 * buffer and place it on the hbq_buffer_list. The function will return zero if
1829 * it successfully post the buffer else it will return an error.
1830 **/
1831static int
1832lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1833 struct hbq_dmabuf *hbq_buf)
1834{
James Smarted957682007-06-17 19:56:37 -05001835 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001836 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001837
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001838 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001839 /* Get next HBQ entry slot to use */
1840 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1841 if (hbqe) {
1842 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1843
James Smart92d7f7b2007-06-17 19:56:38 -05001844 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1845 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08001846 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05001847 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001848 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1849 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1850 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001851 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1852 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001853 /* flush */
James Smarted957682007-06-17 19:56:37 -05001854 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001855 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001856 return 0;
1857 } else
1858 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001859}
1860
James Smart4f774512009-05-22 14:52:35 -04001861/**
1862 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1863 * @phba: Pointer to HBA context object.
1864 * @hbqno: HBQ number.
1865 * @hbq_buf: Pointer to HBQ buffer.
1866 *
1867 * This function is called with the hbalock held to post an RQE to the SLI4
1868 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1869 * the hbq_buffer_list and return zero, otherwise it will return an error.
1870 **/
1871static int
1872lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1873 struct hbq_dmabuf *hbq_buf)
1874{
1875 int rc;
1876 struct lpfc_rqe hrqe;
1877 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08001878 struct lpfc_queue *hrq;
1879 struct lpfc_queue *drq;
1880
1881 if (hbqno != LPFC_ELS_HBQ)
1882 return 1;
1883 hrq = phba->sli4_hba.hdr_rq;
1884 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04001885
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001886 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001887 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1888 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1889 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1890 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08001891 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04001892 if (rc < 0)
1893 return rc;
James Smart895427b2017-02-12 13:52:30 -08001894 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04001895 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1896 return 0;
1897}
1898
James Smarte59058c2008-08-24 21:49:00 -04001899/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001900static struct lpfc_hbq_init lpfc_els_hbq = {
1901 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001902 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001903 .mask_count = 0,
1904 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001905 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001906 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001907 .init_count = 40,
1908 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001909};
James Smarted957682007-06-17 19:56:37 -05001910
James Smarte59058c2008-08-24 21:49:00 -04001911/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001912struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001913 &lpfc_els_hbq,
1914};
1915
James Smarte59058c2008-08-24 21:49:00 -04001916/**
James Smart3621a712009-04-06 18:47:14 -04001917 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001918 * @phba: Pointer to HBA context object.
1919 * @hbqno: HBQ number.
1920 * @count: Number of HBQ buffers to be posted.
1921 *
James Smartd7c255b2008-08-24 21:50:00 -04001922 * This function is called with no lock held to post more hbq buffers to the
1923 * given HBQ. The function returns the number of HBQ buffers successfully
1924 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001925 **/
James Smart311464e2007-08-02 11:10:37 -04001926static int
James Smart92d7f7b2007-06-17 19:56:38 -05001927lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1928{
James Smartd7c255b2008-08-24 21:50:00 -04001929 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001930 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001931 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001932 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001933 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001934 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001935
James Smartd7c255b2008-08-24 21:50:00 -04001936 if ((phba->hbqs[hbqno].buffer_count + count) >
1937 lpfc_hbq_defs[hbqno]->entry_count)
1938 count = lpfc_hbq_defs[hbqno]->entry_count -
1939 phba->hbqs[hbqno].buffer_count;
1940 if (!count)
1941 return 0;
1942 /* Allocate HBQ entries */
1943 for (i = 0; i < count; i++) {
1944 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1945 if (!hbq_buffer)
1946 break;
1947 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1948 }
James Smart3163f722008-02-08 18:50:25 -05001949 /* Check whether HBQ is still in use */
1950 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001951 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001952 goto err;
1953 while (!list_empty(&hbq_buf_list)) {
1954 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1955 dbuf.list);
1956 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1957 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001958 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001959 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001960 posted++;
1961 } else
James Smart51ef4c22007-08-02 11:10:31 -04001962 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001963 }
James Smart3163f722008-02-08 18:50:25 -05001964 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001965 return posted;
1966err:
1967 spin_unlock_irqrestore(&phba->hbalock, flags);
1968 while (!list_empty(&hbq_buf_list)) {
1969 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1970 dbuf.list);
1971 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1972 }
James Smart92d7f7b2007-06-17 19:56:38 -05001973 return 0;
James Smarted957682007-06-17 19:56:37 -05001974}
1975
James Smarte59058c2008-08-24 21:49:00 -04001976/**
James Smart3621a712009-04-06 18:47:14 -04001977 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001978 * @phba: Pointer to HBA context object.
1979 * @qno: HBQ number.
1980 *
1981 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001982 * is called with no lock held. The function returns the number of HBQ entries
1983 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001984 **/
James Smarted957682007-06-17 19:56:37 -05001985int
James Smart92d7f7b2007-06-17 19:56:38 -05001986lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001987{
James Smartdef9c7a2009-12-21 17:02:28 -05001988 if (phba->sli_rev == LPFC_SLI_REV4)
1989 return 0;
1990 else
1991 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1992 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001993}
1994
James Smarte59058c2008-08-24 21:49:00 -04001995/**
James Smart3621a712009-04-06 18:47:14 -04001996 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001997 * @phba: Pointer to HBA context object.
1998 * @qno: HBQ queue number.
1999 *
2000 * This function is called from SLI initialization code path with
2001 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002002 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002003 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002004static int
James Smart92d7f7b2007-06-17 19:56:38 -05002005lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002006{
James Smartdef9c7a2009-12-21 17:02:28 -05002007 if (phba->sli_rev == LPFC_SLI_REV4)
2008 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002009 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002010 else
2011 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2012 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002013}
2014
James Smarte59058c2008-08-24 21:49:00 -04002015/**
James Smart3772a992009-05-22 14:50:54 -04002016 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2017 * @phba: Pointer to HBA context object.
2018 * @hbqno: HBQ number.
2019 *
2020 * This function removes the first hbq buffer on an hbq list and returns a
2021 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2022 **/
2023static struct hbq_dmabuf *
2024lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2025{
2026 struct lpfc_dmabuf *d_buf;
2027
2028 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2029 if (!d_buf)
2030 return NULL;
2031 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2032}
2033
2034/**
James Smart2d7dbc42017-02-12 13:52:35 -08002035 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2036 * @phba: Pointer to HBA context object.
2037 * @hbqno: HBQ number.
2038 *
2039 * This function removes the first RQ buffer on an RQ buffer list and returns a
2040 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2041 **/
2042static struct rqb_dmabuf *
2043lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2044{
2045 struct lpfc_dmabuf *h_buf;
2046 struct lpfc_rqb *rqbp;
2047
2048 rqbp = hrq->rqbp;
2049 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2050 struct lpfc_dmabuf, list);
2051 if (!h_buf)
2052 return NULL;
2053 rqbp->buffer_count--;
2054 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2055}
2056
2057/**
James Smart3621a712009-04-06 18:47:14 -04002058 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002059 * @phba: Pointer to HBA context object.
2060 * @tag: Tag of the hbq buffer.
2061 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002062 * This function searches for the hbq buffer associated with the given tag in
2063 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2064 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002065 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002066static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002067lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2068{
James Smart92d7f7b2007-06-17 19:56:38 -05002069 struct lpfc_dmabuf *d_buf;
2070 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002071 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002072
James Smart51ef4c22007-08-02 11:10:31 -04002073 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002074 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002075 return NULL;
2076
James Smart3772a992009-05-22 14:50:54 -04002077 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002078 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002079 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002080 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002081 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002082 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002083 }
2084 }
James Smart3772a992009-05-22 14:50:54 -04002085 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002086 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002087 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002088 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002089 return NULL;
James Smarted957682007-06-17 19:56:37 -05002090}
2091
James Smarte59058c2008-08-24 21:49:00 -04002092/**
James Smart3621a712009-04-06 18:47:14 -04002093 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002094 * @phba: Pointer to HBA context object.
2095 * @hbq_buffer: Pointer to HBQ buffer.
2096 *
2097 * This function is called with hbalock. This function gives back
2098 * the hbq buffer to firmware. If the HBQ does not have space to
2099 * post the buffer, it will free the buffer.
2100 **/
James Smarted957682007-06-17 19:56:37 -05002101void
James Smart51ef4c22007-08-02 11:10:31 -04002102lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002103{
2104 uint32_t hbqno;
2105
James Smart51ef4c22007-08-02 11:10:31 -04002106 if (hbq_buffer) {
2107 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002108 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002109 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002110 }
2111}
2112
James Smarte59058c2008-08-24 21:49:00 -04002113/**
James Smart3621a712009-04-06 18:47:14 -04002114 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002115 * @mbxCommand: mailbox command code.
2116 *
2117 * This function is called by the mailbox event handler function to verify
2118 * that the completed mailbox command is a legitimate mailbox command. If the
2119 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2120 * and the mailbox event handler will take the HBA offline.
2121 **/
dea31012005-04-17 16:05:31 -05002122static int
2123lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2124{
2125 uint8_t ret;
2126
2127 switch (mbxCommand) {
2128 case MBX_LOAD_SM:
2129 case MBX_READ_NV:
2130 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002131 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002132 case MBX_RUN_BIU_DIAG:
2133 case MBX_INIT_LINK:
2134 case MBX_DOWN_LINK:
2135 case MBX_CONFIG_LINK:
2136 case MBX_CONFIG_RING:
2137 case MBX_RESET_RING:
2138 case MBX_READ_CONFIG:
2139 case MBX_READ_RCONFIG:
2140 case MBX_READ_SPARM:
2141 case MBX_READ_STATUS:
2142 case MBX_READ_RPI:
2143 case MBX_READ_XRI:
2144 case MBX_READ_REV:
2145 case MBX_READ_LNK_STAT:
2146 case MBX_REG_LOGIN:
2147 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002148 case MBX_CLEAR_LA:
2149 case MBX_DUMP_MEMORY:
2150 case MBX_DUMP_CONTEXT:
2151 case MBX_RUN_DIAGS:
2152 case MBX_RESTART:
2153 case MBX_UPDATE_CFG:
2154 case MBX_DOWN_LOAD:
2155 case MBX_DEL_LD_ENTRY:
2156 case MBX_RUN_PROGRAM:
2157 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002158 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002159 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002160 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002161 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002162 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002163 case MBX_LOAD_AREA:
2164 case MBX_RUN_BIU_DIAG64:
2165 case MBX_CONFIG_PORT:
2166 case MBX_READ_SPARM64:
2167 case MBX_READ_RPI64:
2168 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002169 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002170 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002171 case MBX_SET_DEBUG:
2172 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002173 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002174 case MBX_REG_VPI:
2175 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002176 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002177 case MBX_PORT_CAPABILITIES:
2178 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002179 case MBX_SLI4_CONFIG:
2180 case MBX_SLI4_REQ_FTRS:
2181 case MBX_REG_FCFI:
2182 case MBX_UNREG_FCFI:
2183 case MBX_REG_VFI:
2184 case MBX_UNREG_VFI:
2185 case MBX_INIT_VPI:
2186 case MBX_INIT_VFI:
2187 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002188 case MBX_READ_EVENT_LOG_STATUS:
2189 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002190 case MBX_SECURITY_MGMT:
2191 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002192 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002193 ret = mbxCommand;
2194 break;
2195 default:
2196 ret = MBX_SHUTDOWN;
2197 break;
2198 }
James Smart2e0fef82007-06-17 19:56:36 -05002199 return ret;
dea31012005-04-17 16:05:31 -05002200}
James Smarte59058c2008-08-24 21:49:00 -04002201
2202/**
James Smart3621a712009-04-06 18:47:14 -04002203 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002204 * @phba: Pointer to HBA context object.
2205 * @pmboxq: Pointer to mailbox command.
2206 *
2207 * This is completion handler function for mailbox commands issued from
2208 * lpfc_sli_issue_mbox_wait function. This function is called by the
2209 * mailbox event handler function with no lock held. This function
2210 * will wake up thread waiting on the wait queue pointed by context1
2211 * of the mailbox.
2212 **/
James Smart04c68492009-05-22 14:52:52 -04002213void
James Smart2e0fef82007-06-17 19:56:36 -05002214lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002215{
2216 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002217 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002218
2219 /*
2220 * If pdone_q is empty, the driver thread gave up waiting and
2221 * continued running.
2222 */
James Smart7054a602007-04-25 09:52:34 -04002223 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002224 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002225 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2226 if (pdone_q)
2227 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002228 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002229 return;
2230}
2231
James Smarte59058c2008-08-24 21:49:00 -04002232
2233/**
James Smart3621a712009-04-06 18:47:14 -04002234 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002235 * @phba: Pointer to HBA context object.
2236 * @pmb: Pointer to mailbox object.
2237 *
2238 * This function is the default mailbox completion handler. It
2239 * frees the memory resources associated with the completed mailbox
2240 * command. If the completed command is a REG_LOGIN mailbox command,
2241 * this function will issue a UREG_LOGIN to re-claim the RPI.
2242 **/
dea31012005-04-17 16:05:31 -05002243void
James Smart2e0fef82007-06-17 19:56:36 -05002244lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002245{
James Smartd439d282010-09-29 11:18:45 -04002246 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002247 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002248 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002249 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002250 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002251 int rc;
2252
dea31012005-04-17 16:05:31 -05002253 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002254
dea31012005-04-17 16:05:31 -05002255 if (mp) {
2256 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2257 kfree(mp);
2258 }
James Smart7054a602007-04-25 09:52:34 -04002259
2260 /*
2261 * If a REG_LOGIN succeeded after node is destroyed or node
2262 * is in re-discovery driver need to cleanup the RPI.
2263 */
James Smart2e0fef82007-06-17 19:56:36 -05002264 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002265 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2266 !pmb->u.mb.mbxStatus) {
2267 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002268 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002269 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002270 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002271 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002272 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2273 if (rc != MBX_NOT_FINISHED)
2274 return;
2275 }
2276
James Smart695a8142010-01-26 23:08:03 -05002277 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2278 !(phba->pport->load_flag & FC_UNLOADING) &&
2279 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002280 shost = lpfc_shost_from_vport(vport);
2281 spin_lock_irq(shost->host_lock);
2282 vport->vpi_state |= LPFC_VPI_REGISTERED;
2283 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2284 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002285 }
2286
James Smartd439d282010-09-29 11:18:45 -04002287 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2288 ndlp = (struct lpfc_nodelist *)pmb->context2;
2289 lpfc_nlp_put(ndlp);
2290 pmb->context2 = NULL;
2291 }
2292
James Smartdcf2a4e2010-09-29 11:18:53 -04002293 /* Check security permission status on INIT_LINK mailbox command */
2294 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2295 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2296 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2297 "2860 SLI authentication is required "
2298 "for INIT_LINK but has not done yet\n");
2299
James Smart04c68492009-05-22 14:52:52 -04002300 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2301 lpfc_sli4_mbox_cmd_free(phba, pmb);
2302 else
2303 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002304}
James Smartbe6bb942015-04-07 15:07:22 -04002305 /**
2306 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2307 * @phba: Pointer to HBA context object.
2308 * @pmb: Pointer to mailbox object.
2309 *
2310 * This function is the unreg rpi mailbox completion handler. It
2311 * frees the memory resources associated with the completed mailbox
2312 * command. An additional refrenece is put on the ndlp to prevent
2313 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2314 * the unreg mailbox command completes, this routine puts the
2315 * reference back.
2316 *
2317 **/
2318void
2319lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2320{
2321 struct lpfc_vport *vport = pmb->vport;
2322 struct lpfc_nodelist *ndlp;
2323
2324 ndlp = pmb->context1;
2325 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2326 if (phba->sli_rev == LPFC_SLI_REV4 &&
2327 (bf_get(lpfc_sli_intf_if_type,
2328 &phba->sli4_hba.sli_intf) ==
2329 LPFC_SLI_INTF_IF_TYPE_2)) {
2330 if (ndlp) {
2331 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2332 "0010 UNREG_LOGIN vpi:%x "
2333 "rpi:%x DID:%x map:%x %p\n",
2334 vport->vpi, ndlp->nlp_rpi,
2335 ndlp->nlp_DID,
2336 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002337 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002338 lpfc_nlp_put(ndlp);
2339 }
2340 }
2341 }
2342
2343 mempool_free(pmb, phba->mbox_mem_pool);
2344}
dea31012005-04-17 16:05:31 -05002345
James Smarte59058c2008-08-24 21:49:00 -04002346/**
James Smart3621a712009-04-06 18:47:14 -04002347 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002348 * @phba: Pointer to HBA context object.
2349 *
2350 * This function is called with no lock held. This function processes all
2351 * the completed mailbox commands and gives it to upper layers. The interrupt
2352 * service routine processes mailbox completion interrupt and adds completed
2353 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2354 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2355 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2356 * function returns the mailbox commands to the upper layer by calling the
2357 * completion handler function of each mailbox.
2358 **/
dea31012005-04-17 16:05:31 -05002359int
James Smart2e0fef82007-06-17 19:56:36 -05002360lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002361{
James Smart92d7f7b2007-06-17 19:56:38 -05002362 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002363 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002364 int rc;
2365 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002366
2367 phba->sli.slistat.mbox_event++;
2368
James Smart92d7f7b2007-06-17 19:56:38 -05002369 /* Get all completed mailboxe buffers into the cmplq */
2370 spin_lock_irq(&phba->hbalock);
2371 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2372 spin_unlock_irq(&phba->hbalock);
2373
dea31012005-04-17 16:05:31 -05002374 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002375 do {
2376 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2377 if (pmb == NULL)
2378 break;
2379
James Smart04c68492009-05-22 14:52:52 -04002380 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002381
James Smart858c9f62007-06-17 19:56:39 -05002382 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2383 if (pmb->vport) {
2384 lpfc_debugfs_disc_trc(pmb->vport,
2385 LPFC_DISC_TRC_MBOX_VPORT,
2386 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2387 (uint32_t)pmbox->mbxCommand,
2388 pmbox->un.varWords[0],
2389 pmbox->un.varWords[1]);
2390 }
2391 else {
2392 lpfc_debugfs_disc_trc(phba->pport,
2393 LPFC_DISC_TRC_MBOX,
2394 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2395 (uint32_t)pmbox->mbxCommand,
2396 pmbox->un.varWords[0],
2397 pmbox->un.varWords[1]);
2398 }
2399 }
2400
dea31012005-04-17 16:05:31 -05002401 /*
2402 * It is a fatal error if unknown mbox command completion.
2403 */
2404 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2405 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002406 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002407 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002408 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002409 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002410 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002411 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002412 lpfc_sli_config_mbox_subsys_get(phba,
2413 pmb),
2414 lpfc_sli_config_mbox_opcode_get(phba,
2415 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002416 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002417 phba->work_hs = HS_FFER3;
2418 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002419 continue;
dea31012005-04-17 16:05:31 -05002420 }
2421
dea31012005-04-17 16:05:31 -05002422 if (pmbox->mbxStatus) {
2423 phba->sli.slistat.mbox_stat_err++;
2424 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2425 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002426 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002427 LOG_MBOX | LOG_SLI,
2428 "(%d):0305 Mbox cmd cmpl "
2429 "error - RETRYing Data: x%x "
2430 "(x%x/x%x) x%x x%x x%x\n",
2431 pmb->vport ? pmb->vport->vpi : 0,
2432 pmbox->mbxCommand,
2433 lpfc_sli_config_mbox_subsys_get(phba,
2434 pmb),
2435 lpfc_sli_config_mbox_opcode_get(phba,
2436 pmb),
2437 pmbox->mbxStatus,
2438 pmbox->un.varWords[0],
2439 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002440 pmbox->mbxStatus = 0;
2441 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002442 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002443 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002444 continue;
dea31012005-04-17 16:05:31 -05002445 }
2446 }
2447
2448 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002449 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002450 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002451 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2452 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002453 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002454 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002455 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2456 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002457 pmb->mbox_cmpl,
2458 *((uint32_t *) pmbox),
2459 pmbox->un.varWords[0],
2460 pmbox->un.varWords[1],
2461 pmbox->un.varWords[2],
2462 pmbox->un.varWords[3],
2463 pmbox->un.varWords[4],
2464 pmbox->un.varWords[5],
2465 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002466 pmbox->un.varWords[7],
2467 pmbox->un.varWords[8],
2468 pmbox->un.varWords[9],
2469 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002470
James Smart92d7f7b2007-06-17 19:56:38 -05002471 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002472 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002473 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002474 return 0;
dea31012005-04-17 16:05:31 -05002475}
James Smart92d7f7b2007-06-17 19:56:38 -05002476
James Smarte59058c2008-08-24 21:49:00 -04002477/**
James Smart3621a712009-04-06 18:47:14 -04002478 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002479 * @phba: Pointer to HBA context object.
2480 * @pring: Pointer to driver SLI ring object.
2481 * @tag: buffer tag.
2482 *
2483 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2484 * is set in the tag the buffer is posted for a particular exchange,
2485 * the function will return the buffer without replacing the buffer.
2486 * If the buffer is for unsolicited ELS or CT traffic, this function
2487 * returns the buffer and also posts another buffer to the firmware.
2488 **/
James Smart76bb24e2007-10-27 13:38:00 -04002489static struct lpfc_dmabuf *
2490lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002491 struct lpfc_sli_ring *pring,
2492 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002493{
James Smart9f1e1b52008-12-04 22:39:40 -05002494 struct hbq_dmabuf *hbq_entry;
2495
James Smart76bb24e2007-10-27 13:38:00 -04002496 if (tag & QUE_BUFTAG_BIT)
2497 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002498 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2499 if (!hbq_entry)
2500 return NULL;
2501 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002502}
James Smart57127f12007-10-27 13:37:05 -04002503
James Smart3772a992009-05-22 14:50:54 -04002504/**
2505 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2506 * @phba: Pointer to HBA context object.
2507 * @pring: Pointer to driver SLI ring object.
2508 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2509 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2510 * @fch_type: the type for the first frame of the sequence.
2511 *
2512 * This function is called with no lock held. This function uses the r_ctl and
2513 * type of the received sequence to find the correct callback function to call
2514 * to process the sequence.
2515 **/
2516static int
2517lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2518 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2519 uint32_t fch_type)
2520{
2521 int i;
2522
James Smartf358dd02017-02-12 13:52:34 -08002523 switch (fch_type) {
2524 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002525 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002526 return 1;
2527 default:
2528 break;
2529 }
2530
James Smart3772a992009-05-22 14:50:54 -04002531 /* unSolicited Responses */
2532 if (pring->prt[0].profile) {
2533 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2534 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2535 saveq);
2536 return 1;
2537 }
2538 /* We must search, based on rctl / type
2539 for the right routine */
2540 for (i = 0; i < pring->num_mask; i++) {
2541 if ((pring->prt[i].rctl == fch_r_ctl) &&
2542 (pring->prt[i].type == fch_type)) {
2543 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2544 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2545 (phba, pring, saveq);
2546 return 1;
2547 }
2548 }
2549 return 0;
2550}
James Smarte59058c2008-08-24 21:49:00 -04002551
2552/**
James Smart3621a712009-04-06 18:47:14 -04002553 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002554 * @phba: Pointer to HBA context object.
2555 * @pring: Pointer to driver SLI ring object.
2556 * @saveq: Pointer to the unsolicited iocb.
2557 *
2558 * This function is called with no lock held by the ring event handler
2559 * when there is an unsolicited iocb posted to the response ring by the
2560 * firmware. This function gets the buffer associated with the iocbs
2561 * and calls the event handler for the ring. This function handles both
2562 * qring buffers and hbq buffers.
2563 * When the function returns 1 the caller can free the iocb object otherwise
2564 * upper layer functions will free the iocb objects.
2565 **/
dea31012005-04-17 16:05:31 -05002566static int
2567lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2568 struct lpfc_iocbq *saveq)
2569{
2570 IOCB_t * irsp;
2571 WORD5 * w5p;
2572 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002573 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002574 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002575
dea31012005-04-17 16:05:31 -05002576 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002577
2578 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2579 if (pring->lpfc_sli_rcv_async_status)
2580 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2581 else
2582 lpfc_printf_log(phba,
2583 KERN_WARNING,
2584 LOG_SLI,
2585 "0316 Ring %d handler: unexpected "
2586 "ASYNC_STATUS iocb received evt_code "
2587 "0x%x\n",
2588 pring->ringno,
2589 irsp->un.asyncstat.evt_code);
2590 return 1;
2591 }
2592
James Smart3163f722008-02-08 18:50:25 -05002593 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2594 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2595 if (irsp->ulpBdeCount > 0) {
2596 dmzbuf = lpfc_sli_get_buff(phba, pring,
2597 irsp->un.ulpWord[3]);
2598 lpfc_in_buf_free(phba, dmzbuf);
2599 }
2600
2601 if (irsp->ulpBdeCount > 1) {
2602 dmzbuf = lpfc_sli_get_buff(phba, pring,
2603 irsp->unsli3.sli3Words[3]);
2604 lpfc_in_buf_free(phba, dmzbuf);
2605 }
2606
2607 if (irsp->ulpBdeCount > 2) {
2608 dmzbuf = lpfc_sli_get_buff(phba, pring,
2609 irsp->unsli3.sli3Words[7]);
2610 lpfc_in_buf_free(phba, dmzbuf);
2611 }
2612
2613 return 1;
2614 }
2615
James Smart92d7f7b2007-06-17 19:56:38 -05002616 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002617 if (irsp->ulpBdeCount != 0) {
2618 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002619 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002620 if (!saveq->context2)
2621 lpfc_printf_log(phba,
2622 KERN_ERR,
2623 LOG_SLI,
2624 "0341 Ring %d Cannot find buffer for "
2625 "an unsolicited iocb. tag 0x%x\n",
2626 pring->ringno,
2627 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002628 }
2629 if (irsp->ulpBdeCount == 2) {
2630 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002631 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002632 if (!saveq->context3)
2633 lpfc_printf_log(phba,
2634 KERN_ERR,
2635 LOG_SLI,
2636 "0342 Ring %d Cannot find buffer for an"
2637 " unsolicited iocb. tag 0x%x\n",
2638 pring->ringno,
2639 irsp->unsli3.sli3Words[7]);
2640 }
2641 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002642 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002643 if (irsp->ulpBdeCount != 0) {
2644 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2645 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002646 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002647 lpfc_printf_log(phba,
2648 KERN_ERR,
2649 LOG_SLI,
2650 "0343 Ring %d Cannot find "
2651 "buffer for an unsolicited iocb"
2652 ". tag 0x%x\n", pring->ringno,
2653 irsp->un.ulpWord[3]);
2654 }
2655 if (irsp->ulpBdeCount == 2) {
2656 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2657 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002658 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002659 lpfc_printf_log(phba,
2660 KERN_ERR,
2661 LOG_SLI,
2662 "0344 Ring %d Cannot find "
2663 "buffer for an unsolicited "
2664 "iocb. tag 0x%x\n",
2665 pring->ringno,
2666 irsp->unsli3.sli3Words[7]);
2667 }
2668 }
James Smart92d7f7b2007-06-17 19:56:38 -05002669 }
James Smart9c2face2008-01-11 01:53:18 -05002670 if (irsp->ulpBdeCount != 0 &&
2671 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2672 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2673 int found = 0;
2674
2675 /* search continue save q for same XRI */
2676 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002677 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2678 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002679 list_add_tail(&saveq->list, &iocbq->list);
2680 found = 1;
2681 break;
2682 }
2683 }
2684 if (!found)
2685 list_add_tail(&saveq->clist,
2686 &pring->iocb_continue_saveq);
2687 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2688 list_del_init(&iocbq->clist);
2689 saveq = iocbq;
2690 irsp = &(saveq->iocb);
2691 } else
2692 return 0;
2693 }
2694 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2695 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2696 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002697 Rctl = FC_RCTL_ELS_REQ;
2698 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002699 } else {
2700 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2701 Rctl = w5p->hcsw.Rctl;
2702 Type = w5p->hcsw.Type;
2703
2704 /* Firmware Workaround */
2705 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2706 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2707 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002708 Rctl = FC_RCTL_ELS_REQ;
2709 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002710 w5p->hcsw.Rctl = Rctl;
2711 w5p->hcsw.Type = Type;
2712 }
2713 }
James Smart92d7f7b2007-06-17 19:56:38 -05002714
James Smart3772a992009-05-22 14:50:54 -04002715 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002716 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002717 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002718 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002719 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002720
James Smart92d7f7b2007-06-17 19:56:38 -05002721 return 1;
dea31012005-04-17 16:05:31 -05002722}
2723
James Smarte59058c2008-08-24 21:49:00 -04002724/**
James Smart3621a712009-04-06 18:47:14 -04002725 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002726 * @phba: Pointer to HBA context object.
2727 * @pring: Pointer to driver SLI ring object.
2728 * @prspiocb: Pointer to response iocb object.
2729 *
2730 * This function looks up the iocb_lookup table to get the command iocb
2731 * corresponding to the given response iocb using the iotag of the
2732 * response iocb. This function is called with the hbalock held.
2733 * This function returns the command iocb object if it finds the command
2734 * iocb else returns NULL.
2735 **/
dea31012005-04-17 16:05:31 -05002736static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002737lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2738 struct lpfc_sli_ring *pring,
2739 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002740{
dea31012005-04-17 16:05:31 -05002741 struct lpfc_iocbq *cmd_iocb = NULL;
2742 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002743 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002744
James Bottomley604a3e32005-10-29 10:28:33 -05002745 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002746
James Bottomley604a3e32005-10-29 10:28:33 -05002747 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2748 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002749 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002750 /* remove from txcmpl queue list */
2751 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002752 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002753 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002754 }
dea31012005-04-17 16:05:31 -05002755 }
2756
dea31012005-04-17 16:05:31 -05002757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002758 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002759 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002760 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002761 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002762 return NULL;
2763}
2764
James Smarte59058c2008-08-24 21:49:00 -04002765/**
James Smart3772a992009-05-22 14:50:54 -04002766 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2767 * @phba: Pointer to HBA context object.
2768 * @pring: Pointer to driver SLI ring object.
2769 * @iotag: IOCB tag.
2770 *
2771 * This function looks up the iocb_lookup table to get the command iocb
2772 * corresponding to the given iotag. This function is called with the
2773 * hbalock held.
2774 * This function returns the command iocb object if it finds the command
2775 * iocb else returns NULL.
2776 **/
2777static struct lpfc_iocbq *
2778lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2779 struct lpfc_sli_ring *pring, uint16_t iotag)
2780{
James Smart895427b2017-02-12 13:52:30 -08002781 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002782
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002783 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002784 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2785 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002786 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2787 /* remove from txcmpl queue list */
2788 list_del_init(&cmd_iocb->list);
2789 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002790 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002791 }
James Smart3772a992009-05-22 14:50:54 -04002792 }
James Smart89533e92016-10-13 15:06:15 -07002793
James Smart3772a992009-05-22 14:50:54 -04002794 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002795 "0372 iotag x%x lookup error: max iotag (x%x) "
2796 "iocb_flag x%x\n",
2797 iotag, phba->sli.last_iotag,
2798 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002799 return NULL;
2800}
2801
2802/**
James Smart3621a712009-04-06 18:47:14 -04002803 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002804 * @phba: Pointer to HBA context object.
2805 * @pring: Pointer to driver SLI ring object.
2806 * @saveq: Pointer to the response iocb to be processed.
2807 *
2808 * This function is called by the ring event handler for non-fcp
2809 * rings when there is a new response iocb in the response ring.
2810 * The caller is not required to hold any locks. This function
2811 * gets the command iocb associated with the response iocb and
2812 * calls the completion handler for the command iocb. If there
2813 * is no completion handler, the function will free the resources
2814 * associated with command iocb. If the response iocb is for
2815 * an already aborted command iocb, the status of the completion
2816 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2817 * This function always returns 1.
2818 **/
dea31012005-04-17 16:05:31 -05002819static int
James Smart2e0fef82007-06-17 19:56:36 -05002820lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002821 struct lpfc_iocbq *saveq)
2822{
James Smart2e0fef82007-06-17 19:56:36 -05002823 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002824 int rc = 1;
2825 unsigned long iflag;
2826
2827 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002828 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002829 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002830 spin_unlock_irqrestore(&phba->hbalock, iflag);
2831
dea31012005-04-17 16:05:31 -05002832 if (cmdiocbp) {
2833 if (cmdiocbp->iocb_cmpl) {
2834 /*
James Smartea2151b2008-09-07 11:52:10 -04002835 * If an ELS command failed send an event to mgmt
2836 * application.
2837 */
2838 if (saveq->iocb.ulpStatus &&
2839 (pring->ringno == LPFC_ELS_RING) &&
2840 (cmdiocbp->iocb.ulpCommand ==
2841 CMD_ELS_REQUEST64_CR))
2842 lpfc_send_els_failure_event(phba,
2843 cmdiocbp, saveq);
2844
2845 /*
dea31012005-04-17 16:05:31 -05002846 * Post all ELS completions to the worker thread.
2847 * All other are passed to the completion callback.
2848 */
2849 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002850 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2851 (cmdiocbp->iocb_flag &
2852 LPFC_DRIVER_ABORTED)) {
2853 spin_lock_irqsave(&phba->hbalock,
2854 iflag);
James Smart07951072007-04-25 09:51:38 -04002855 cmdiocbp->iocb_flag &=
2856 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002857 spin_unlock_irqrestore(&phba->hbalock,
2858 iflag);
James Smart07951072007-04-25 09:51:38 -04002859 saveq->iocb.ulpStatus =
2860 IOSTAT_LOCAL_REJECT;
2861 saveq->iocb.un.ulpWord[4] =
2862 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002863
2864 /* Firmware could still be in progress
2865 * of DMAing payload, so don't free data
2866 * buffer till after a hbeat.
2867 */
James Smart341af102010-01-26 23:07:37 -05002868 spin_lock_irqsave(&phba->hbalock,
2869 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002870 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002871 spin_unlock_irqrestore(&phba->hbalock,
2872 iflag);
2873 }
James Smart0f65ff62010-02-26 14:14:23 -05002874 if (phba->sli_rev == LPFC_SLI_REV4) {
2875 if (saveq->iocb_flag &
2876 LPFC_EXCHANGE_BUSY) {
2877 /* Set cmdiocb flag for the
2878 * exchange busy so sgl (xri)
2879 * will not be released until
2880 * the abort xri is received
2881 * from hba.
2882 */
2883 spin_lock_irqsave(
2884 &phba->hbalock, iflag);
2885 cmdiocbp->iocb_flag |=
2886 LPFC_EXCHANGE_BUSY;
2887 spin_unlock_irqrestore(
2888 &phba->hbalock, iflag);
2889 }
2890 if (cmdiocbp->iocb_flag &
2891 LPFC_DRIVER_ABORTED) {
2892 /*
2893 * Clear LPFC_DRIVER_ABORTED
2894 * bit in case it was driver
2895 * initiated abort.
2896 */
2897 spin_lock_irqsave(
2898 &phba->hbalock, iflag);
2899 cmdiocbp->iocb_flag &=
2900 ~LPFC_DRIVER_ABORTED;
2901 spin_unlock_irqrestore(
2902 &phba->hbalock, iflag);
2903 cmdiocbp->iocb.ulpStatus =
2904 IOSTAT_LOCAL_REJECT;
2905 cmdiocbp->iocb.un.ulpWord[4] =
2906 IOERR_ABORT_REQUESTED;
2907 /*
2908 * For SLI4, irsiocb contains
2909 * NO_XRI in sli_xritag, it
2910 * shall not affect releasing
2911 * sgl (xri) process.
2912 */
2913 saveq->iocb.ulpStatus =
2914 IOSTAT_LOCAL_REJECT;
2915 saveq->iocb.un.ulpWord[4] =
2916 IOERR_SLI_ABORTED;
2917 spin_lock_irqsave(
2918 &phba->hbalock, iflag);
2919 saveq->iocb_flag |=
2920 LPFC_DELAY_MEM_FREE;
2921 spin_unlock_irqrestore(
2922 &phba->hbalock, iflag);
2923 }
James Smart07951072007-04-25 09:51:38 -04002924 }
dea31012005-04-17 16:05:31 -05002925 }
James Smart2e0fef82007-06-17 19:56:36 -05002926 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002927 } else
2928 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002929 } else {
2930 /*
2931 * Unknown initiating command based on the response iotag.
2932 * This could be the case on the ELS ring because of
2933 * lpfc_els_abort().
2934 */
2935 if (pring->ringno != LPFC_ELS_RING) {
2936 /*
2937 * Ring <ringno> handler: unexpected completion IoTag
2938 * <IoTag>
2939 */
James Smarta257bf92009-04-06 18:48:10 -04002940 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002941 "0322 Ring %d handler: "
2942 "unexpected completion IoTag x%x "
2943 "Data: x%x x%x x%x x%x\n",
2944 pring->ringno,
2945 saveq->iocb.ulpIoTag,
2946 saveq->iocb.ulpStatus,
2947 saveq->iocb.un.ulpWord[4],
2948 saveq->iocb.ulpCommand,
2949 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002950 }
2951 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002952
dea31012005-04-17 16:05:31 -05002953 return rc;
2954}
2955
James Smarte59058c2008-08-24 21:49:00 -04002956/**
James Smart3621a712009-04-06 18:47:14 -04002957 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002958 * @phba: Pointer to HBA context object.
2959 * @pring: Pointer to driver SLI ring object.
2960 *
2961 * This function is called from the iocb ring event handlers when
2962 * put pointer is ahead of the get pointer for a ring. This function signal
2963 * an error attention condition to the worker thread and the worker
2964 * thread will transition the HBA to offline state.
2965 **/
James Smart2e0fef82007-06-17 19:56:36 -05002966static void
2967lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002968{
James Smart34b02dc2008-08-24 21:49:55 -04002969 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002970 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002971 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002972 * rsp ring <portRspMax>
2973 */
2974 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002975 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002976 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002977 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002978 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002979
James Smart2e0fef82007-06-17 19:56:36 -05002980 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002981
2982 /*
2983 * All error attention handlers are posted to
2984 * worker thread
2985 */
2986 phba->work_ha |= HA_ERATT;
2987 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002988
James Smart5e9d9b82008-06-14 22:52:53 -04002989 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002990
2991 return;
2992}
2993
James Smarte59058c2008-08-24 21:49:00 -04002994/**
James Smart3621a712009-04-06 18:47:14 -04002995 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002996 * @ptr: Pointer to address of HBA context object.
2997 *
2998 * This function is invoked by the Error Attention polling timer when the
2999 * timer times out. It will check the SLI Error Attention register for
3000 * possible attention events. If so, it will post an Error Attention event
3001 * and wake up worker thread to process it. Otherwise, it will set up the
3002 * Error Attention polling timer for the next poll.
3003 **/
3004void lpfc_poll_eratt(unsigned long ptr)
3005{
3006 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003007 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003008 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003009
3010 phba = (struct lpfc_hba *)ptr;
3011
James Smartaa6fbb72012-08-03 12:36:03 -04003012 /* Here we will also keep track of interrupts per sec of the hba */
3013 sli_intr = phba->sli.slistat.sli_intr;
3014
3015 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3016 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3017 sli_intr);
3018 else
3019 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3020
James Smart65791f12016-07-06 12:35:56 -07003021 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3022 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003023 phba->sli.slistat.sli_ips = cnt;
3024
3025 phba->sli.slistat.sli_prev_intr = sli_intr;
3026
James Smart93996272008-08-24 21:50:30 -04003027 /* Check chip HA register for error event */
3028 eratt = lpfc_sli_check_eratt(phba);
3029
3030 if (eratt)
3031 /* Tell the worker thread there is work to do */
3032 lpfc_worker_wake_up(phba);
3033 else
3034 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003035 mod_timer(&phba->eratt_poll,
3036 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003037 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003038 return;
3039}
3040
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003041
James Smarte59058c2008-08-24 21:49:00 -04003042/**
James Smart3621a712009-04-06 18:47:14 -04003043 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003044 * @phba: Pointer to HBA context object.
3045 * @pring: Pointer to driver SLI ring object.
3046 * @mask: Host attention register mask for this ring.
3047 *
3048 * This function is called from the interrupt context when there is a ring
3049 * event for the fcp ring. The caller does not hold any lock.
3050 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003051 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003052 * LE bit set. The function will call the completion handler of the command iocb
3053 * if the response iocb indicates a completion for a command iocb or it is
3054 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3055 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003056 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003057 * to check it explicitly.
3058 */
3059int
James Smart2e0fef82007-06-17 19:56:36 -05003060lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3061 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003062{
James Smart34b02dc2008-08-24 21:49:55 -04003063 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003064 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003065 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003066 struct lpfc_iocbq *cmdiocbq = NULL;
3067 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003068 uint32_t status;
3069 uint32_t portRspPut, portRspMax;
3070 int rc = 1;
3071 lpfc_iocb_type type;
3072 unsigned long iflag;
3073 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003074
James Smart2e0fef82007-06-17 19:56:36 -05003075 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003076 pring->stats.iocb_event++;
3077
dea31012005-04-17 16:05:31 -05003078 /*
3079 * The next available response entry should never exceed the maximum
3080 * entries. If it does, treat it as an adapter hardware error.
3081 */
James Smart7e56aa22012-08-03 12:35:34 -04003082 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003083 portRspPut = le32_to_cpu(pgp->rspPutInx);
3084 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003085 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003086 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003087 return 1;
3088 }
James Smart45ed1192009-10-02 15:17:02 -04003089 if (phba->fcp_ring_in_use) {
3090 spin_unlock_irqrestore(&phba->hbalock, iflag);
3091 return 1;
3092 } else
3093 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003094
3095 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003096 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003097 /*
3098 * Fetch an entry off the ring and copy it into a local data
3099 * structure. The copy involves a byte-swap since the
3100 * network byte order and pci byte orders are different.
3101 */
James Smarted957682007-06-17 19:56:37 -05003102 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003103 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003104
James Smart7e56aa22012-08-03 12:35:34 -04003105 if (++pring->sli.sli3.rspidx >= portRspMax)
3106 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003107
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003108 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3109 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003110 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003111 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003112 irsp = &rspiocbq.iocb;
3113
dea31012005-04-17 16:05:31 -05003114 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3115 pring->stats.iocb_rsp++;
3116 rsp_cmpl++;
3117
3118 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003119 /*
3120 * If resource errors reported from HBA, reduce
3121 * queuedepths of the SCSI device.
3122 */
3123 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003124 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3125 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003126 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003127 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003128 spin_lock_irqsave(&phba->hbalock, iflag);
3129 }
3130
dea31012005-04-17 16:05:31 -05003131 /* Rsp ring <ringno> error: IOCB */
3132 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003133 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003134 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003135 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003136 irsp->un.ulpWord[0],
3137 irsp->un.ulpWord[1],
3138 irsp->un.ulpWord[2],
3139 irsp->un.ulpWord[3],
3140 irsp->un.ulpWord[4],
3141 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003142 *(uint32_t *)&irsp->un1,
3143 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003144 }
3145
3146 switch (type) {
3147 case LPFC_ABORT_IOCB:
3148 case LPFC_SOL_IOCB:
3149 /*
3150 * Idle exchange closed via ABTS from port. No iocb
3151 * resources need to be recovered.
3152 */
3153 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003154 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003155 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003156 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003157 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003158 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003159 break;
3160 }
3161
James Bottomley604a3e32005-10-29 10:28:33 -05003162 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3163 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003164 if (unlikely(!cmdiocbq))
3165 break;
3166 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3167 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3168 if (cmdiocbq->iocb_cmpl) {
3169 spin_unlock_irqrestore(&phba->hbalock, iflag);
3170 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3171 &rspiocbq);
3172 spin_lock_irqsave(&phba->hbalock, iflag);
3173 }
dea31012005-04-17 16:05:31 -05003174 break;
James Smarta4bc3372006-12-02 13:34:16 -05003175 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003176 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003177 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003178 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003179 break;
dea31012005-04-17 16:05:31 -05003180 default:
3181 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3182 char adaptermsg[LPFC_MAX_ADPTMSG];
3183 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3184 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3185 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003186 dev_warn(&((phba->pcidev)->dev),
3187 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003188 phba->brd_no, adaptermsg);
3189 } else {
3190 /* Unknown IOCB command */
3191 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003192 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003193 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003194 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003195 irsp->ulpStatus,
3196 irsp->ulpIoTag,
3197 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003198 }
3199 break;
3200 }
3201
3202 /*
3203 * The response IOCB has been processed. Update the ring
3204 * pointer in SLIM. If the port response put pointer has not
3205 * been updated, sync the pgp->rspPutInx and fetch the new port
3206 * response put pointer.
3207 */
James Smart7e56aa22012-08-03 12:35:34 -04003208 writel(pring->sli.sli3.rspidx,
3209 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003210
James Smart7e56aa22012-08-03 12:35:34 -04003211 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003212 portRspPut = le32_to_cpu(pgp->rspPutInx);
3213 }
3214
3215 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3216 pring->stats.iocb_rsp_full++;
3217 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3218 writel(status, phba->CAregaddr);
3219 readl(phba->CAregaddr);
3220 }
3221 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3222 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3223 pring->stats.iocb_cmd_empty++;
3224
3225 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003226 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003227 lpfc_sli_resume_iocb(phba, pring);
3228
3229 if ((pring->lpfc_sli_cmd_available))
3230 (pring->lpfc_sli_cmd_available) (phba, pring);
3231
3232 }
3233
James Smart45ed1192009-10-02 15:17:02 -04003234 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003235 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003236 return rc;
3237}
3238
James Smarte59058c2008-08-24 21:49:00 -04003239/**
James Smart3772a992009-05-22 14:50:54 -04003240 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3241 * @phba: Pointer to HBA context object.
3242 * @pring: Pointer to driver SLI ring object.
3243 * @rspiocbp: Pointer to driver response IOCB object.
3244 *
3245 * This function is called from the worker thread when there is a slow-path
3246 * response IOCB to process. This function chains all the response iocbs until
3247 * seeing the iocb with the LE bit set. The function will call
3248 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3249 * completion of a command iocb. The function will call the
3250 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3251 * The function frees the resources or calls the completion handler if this
3252 * iocb is an abort completion. The function returns NULL when the response
3253 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3254 * this function shall chain the iocb on to the iocb_continueq and return the
3255 * response iocb passed in.
3256 **/
3257static struct lpfc_iocbq *
3258lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3259 struct lpfc_iocbq *rspiocbp)
3260{
3261 struct lpfc_iocbq *saveq;
3262 struct lpfc_iocbq *cmdiocbp;
3263 struct lpfc_iocbq *next_iocb;
3264 IOCB_t *irsp = NULL;
3265 uint32_t free_saveq;
3266 uint8_t iocb_cmd_type;
3267 lpfc_iocb_type type;
3268 unsigned long iflag;
3269 int rc;
3270
3271 spin_lock_irqsave(&phba->hbalock, iflag);
3272 /* First add the response iocb to the countinueq list */
3273 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3274 pring->iocb_continueq_cnt++;
3275
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003276 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003277 irsp = &rspiocbp->iocb;
3278 if (irsp->ulpLe) {
3279 /*
3280 * By default, the driver expects to free all resources
3281 * associated with this iocb completion.
3282 */
3283 free_saveq = 1;
3284 saveq = list_get_first(&pring->iocb_continueq,
3285 struct lpfc_iocbq, list);
3286 irsp = &(saveq->iocb);
3287 list_del_init(&pring->iocb_continueq);
3288 pring->iocb_continueq_cnt = 0;
3289
3290 pring->stats.iocb_rsp++;
3291
3292 /*
3293 * If resource errors reported from HBA, reduce
3294 * queuedepths of the SCSI device.
3295 */
3296 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003297 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3298 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003299 spin_unlock_irqrestore(&phba->hbalock, iflag);
3300 phba->lpfc_rampdown_queue_depth(phba);
3301 spin_lock_irqsave(&phba->hbalock, iflag);
3302 }
3303
3304 if (irsp->ulpStatus) {
3305 /* Rsp ring <ringno> error: IOCB */
3306 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3307 "0328 Rsp Ring %d error: "
3308 "IOCB Data: "
3309 "x%x x%x x%x x%x "
3310 "x%x x%x x%x x%x "
3311 "x%x x%x x%x x%x "
3312 "x%x x%x x%x x%x\n",
3313 pring->ringno,
3314 irsp->un.ulpWord[0],
3315 irsp->un.ulpWord[1],
3316 irsp->un.ulpWord[2],
3317 irsp->un.ulpWord[3],
3318 irsp->un.ulpWord[4],
3319 irsp->un.ulpWord[5],
3320 *(((uint32_t *) irsp) + 6),
3321 *(((uint32_t *) irsp) + 7),
3322 *(((uint32_t *) irsp) + 8),
3323 *(((uint32_t *) irsp) + 9),
3324 *(((uint32_t *) irsp) + 10),
3325 *(((uint32_t *) irsp) + 11),
3326 *(((uint32_t *) irsp) + 12),
3327 *(((uint32_t *) irsp) + 13),
3328 *(((uint32_t *) irsp) + 14),
3329 *(((uint32_t *) irsp) + 15));
3330 }
3331
3332 /*
3333 * Fetch the IOCB command type and call the correct completion
3334 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3335 * get freed back to the lpfc_iocb_list by the discovery
3336 * kernel thread.
3337 */
3338 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3339 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3340 switch (type) {
3341 case LPFC_SOL_IOCB:
3342 spin_unlock_irqrestore(&phba->hbalock, iflag);
3343 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3344 spin_lock_irqsave(&phba->hbalock, iflag);
3345 break;
3346
3347 case LPFC_UNSOL_IOCB:
3348 spin_unlock_irqrestore(&phba->hbalock, iflag);
3349 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3350 spin_lock_irqsave(&phba->hbalock, iflag);
3351 if (!rc)
3352 free_saveq = 0;
3353 break;
3354
3355 case LPFC_ABORT_IOCB:
3356 cmdiocbp = NULL;
3357 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3358 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3359 saveq);
3360 if (cmdiocbp) {
3361 /* Call the specified completion routine */
3362 if (cmdiocbp->iocb_cmpl) {
3363 spin_unlock_irqrestore(&phba->hbalock,
3364 iflag);
3365 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3366 saveq);
3367 spin_lock_irqsave(&phba->hbalock,
3368 iflag);
3369 } else
3370 __lpfc_sli_release_iocbq(phba,
3371 cmdiocbp);
3372 }
3373 break;
3374
3375 case LPFC_UNKNOWN_IOCB:
3376 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3377 char adaptermsg[LPFC_MAX_ADPTMSG];
3378 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3379 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3380 MAX_MSG_DATA);
3381 dev_warn(&((phba->pcidev)->dev),
3382 "lpfc%d: %s\n",
3383 phba->brd_no, adaptermsg);
3384 } else {
3385 /* Unknown IOCB command */
3386 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3387 "0335 Unknown IOCB "
3388 "command Data: x%x "
3389 "x%x x%x x%x\n",
3390 irsp->ulpCommand,
3391 irsp->ulpStatus,
3392 irsp->ulpIoTag,
3393 irsp->ulpContext);
3394 }
3395 break;
3396 }
3397
3398 if (free_saveq) {
3399 list_for_each_entry_safe(rspiocbp, next_iocb,
3400 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003401 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003402 __lpfc_sli_release_iocbq(phba, rspiocbp);
3403 }
3404 __lpfc_sli_release_iocbq(phba, saveq);
3405 }
3406 rspiocbp = NULL;
3407 }
3408 spin_unlock_irqrestore(&phba->hbalock, iflag);
3409 return rspiocbp;
3410}
3411
3412/**
3413 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003414 * @phba: Pointer to HBA context object.
3415 * @pring: Pointer to driver SLI ring object.
3416 * @mask: Host attention register mask for this ring.
3417 *
James Smart3772a992009-05-22 14:50:54 -04003418 * This routine wraps the actual slow_ring event process routine from the
3419 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003420 **/
James Smart3772a992009-05-22 14:50:54 -04003421void
James Smart2e0fef82007-06-17 19:56:36 -05003422lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3423 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003424{
James Smart3772a992009-05-22 14:50:54 -04003425 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3426}
3427
3428/**
3429 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3430 * @phba: Pointer to HBA context object.
3431 * @pring: Pointer to driver SLI ring object.
3432 * @mask: Host attention register mask for this ring.
3433 *
3434 * This function is called from the worker thread when there is a ring event
3435 * for non-fcp rings. The caller does not hold any lock. The function will
3436 * remove each response iocb in the response ring and calls the handle
3437 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3438 **/
3439static void
3440lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3441 struct lpfc_sli_ring *pring, uint32_t mask)
3442{
James Smart34b02dc2008-08-24 21:49:55 -04003443 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003444 IOCB_t *entry;
3445 IOCB_t *irsp = NULL;
3446 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003447 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003448 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003449 uint32_t status;
dea31012005-04-17 16:05:31 -05003450
James Smart34b02dc2008-08-24 21:49:55 -04003451 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003452 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003453 pring->stats.iocb_event++;
3454
dea31012005-04-17 16:05:31 -05003455 /*
3456 * The next available response entry should never exceed the maximum
3457 * entries. If it does, treat it as an adapter hardware error.
3458 */
James Smart7e56aa22012-08-03 12:35:34 -04003459 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003460 portRspPut = le32_to_cpu(pgp->rspPutInx);
3461 if (portRspPut >= portRspMax) {
3462 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003463 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003464 * rsp ring <portRspMax>
3465 */
James Smarted957682007-06-17 19:56:37 -05003466 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003467 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003468 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003469 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003470
James Smart2e0fef82007-06-17 19:56:36 -05003471 phba->link_state = LPFC_HBA_ERROR;
3472 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003473
3474 phba->work_hs = HS_FFER3;
3475 lpfc_handle_eratt(phba);
3476
James Smart3772a992009-05-22 14:50:54 -04003477 return;
dea31012005-04-17 16:05:31 -05003478 }
3479
3480 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003481 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003482 /*
3483 * Build a completion list and call the appropriate handler.
3484 * The process is to get the next available response iocb, get
3485 * a free iocb from the list, copy the response data into the
3486 * free iocb, insert to the continuation list, and update the
3487 * next response index to slim. This process makes response
3488 * iocb's in the ring available to DMA as fast as possible but
3489 * pays a penalty for a copy operation. Since the iocb is
3490 * only 32 bytes, this penalty is considered small relative to
3491 * the PCI reads for register values and a slim write. When
3492 * the ulpLe field is set, the entire Command has been
3493 * received.
3494 */
James Smarted957682007-06-17 19:56:37 -05003495 entry = lpfc_resp_iocb(phba, pring);
3496
James Smart858c9f62007-06-17 19:56:39 -05003497 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003498 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003499 if (rspiocbp == NULL) {
3500 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003501 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003502 break;
3503 }
3504
James Smarted957682007-06-17 19:56:37 -05003505 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3506 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003507 irsp = &rspiocbp->iocb;
3508
James Smart7e56aa22012-08-03 12:35:34 -04003509 if (++pring->sli.sli3.rspidx >= portRspMax)
3510 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003511
James Smarta58cbd52007-08-02 11:09:43 -04003512 if (pring->ringno == LPFC_ELS_RING) {
3513 lpfc_debugfs_slow_ring_trc(phba,
3514 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3515 *(((uint32_t *) irsp) + 4),
3516 *(((uint32_t *) irsp) + 6),
3517 *(((uint32_t *) irsp) + 7));
3518 }
3519
James Smart7e56aa22012-08-03 12:35:34 -04003520 writel(pring->sli.sli3.rspidx,
3521 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003522
James Smart3772a992009-05-22 14:50:54 -04003523 spin_unlock_irqrestore(&phba->hbalock, iflag);
3524 /* Handle the response IOCB */
3525 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3526 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003527
3528 /*
3529 * If the port response put pointer has not been updated, sync
3530 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3531 * response put pointer.
3532 */
James Smart7e56aa22012-08-03 12:35:34 -04003533 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003534 portRspPut = le32_to_cpu(pgp->rspPutInx);
3535 }
James Smart7e56aa22012-08-03 12:35:34 -04003536 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003537
James Smart92d7f7b2007-06-17 19:56:38 -05003538 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003539 /* At least one response entry has been freed */
3540 pring->stats.iocb_rsp_full++;
3541 /* SET RxRE_RSP in Chip Att register */
3542 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3543 writel(status, phba->CAregaddr);
3544 readl(phba->CAregaddr); /* flush */
3545 }
3546 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3547 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3548 pring->stats.iocb_cmd_empty++;
3549
3550 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003551 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003552 lpfc_sli_resume_iocb(phba, pring);
3553
3554 if ((pring->lpfc_sli_cmd_available))
3555 (pring->lpfc_sli_cmd_available) (phba, pring);
3556
3557 }
3558
James Smart2e0fef82007-06-17 19:56:36 -05003559 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003560 return;
dea31012005-04-17 16:05:31 -05003561}
3562
James Smarte59058c2008-08-24 21:49:00 -04003563/**
James Smart4f774512009-05-22 14:52:35 -04003564 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3565 * @phba: Pointer to HBA context object.
3566 * @pring: Pointer to driver SLI ring object.
3567 * @mask: Host attention register mask for this ring.
3568 *
3569 * This function is called from the worker thread when there is a pending
3570 * ELS response iocb on the driver internal slow-path response iocb worker
3571 * queue. The caller does not hold any lock. The function will remove each
3572 * response iocb from the response worker queue and calls the handle
3573 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3574 **/
3575static void
3576lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3577 struct lpfc_sli_ring *pring, uint32_t mask)
3578{
3579 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003580 struct hbq_dmabuf *dmabuf;
3581 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003582 unsigned long iflag;
3583
James Smart45ed1192009-10-02 15:17:02 -04003584 spin_lock_irqsave(&phba->hbalock, iflag);
3585 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3586 spin_unlock_irqrestore(&phba->hbalock, iflag);
3587 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003588 /* Get the response iocb from the head of work queue */
3589 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003590 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003591 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003592 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003593
3594 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3595 case CQE_CODE_COMPL_WQE:
3596 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3597 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003598 /* Translate ELS WCQE to response IOCBQ */
3599 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3600 irspiocbq);
3601 if (irspiocbq)
3602 lpfc_sli_sp_handle_rspiocb(phba, pring,
3603 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003604 break;
3605 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003606 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003607 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3608 cq_event);
3609 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3610 break;
3611 default:
3612 break;
3613 }
James Smart4f774512009-05-22 14:52:35 -04003614 }
3615}
3616
3617/**
James Smart3621a712009-04-06 18:47:14 -04003618 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003619 * @phba: Pointer to HBA context object.
3620 * @pring: Pointer to driver SLI ring object.
3621 *
3622 * This function aborts all iocbs in the given ring and frees all the iocb
3623 * objects in txq. This function issues an abort iocb for all the iocb commands
3624 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3625 * the return of this function. The caller is not required to hold any locks.
3626 **/
James Smart2e0fef82007-06-17 19:56:36 -05003627void
dea31012005-04-17 16:05:31 -05003628lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3629{
James Smart2534ba72007-04-25 09:52:20 -04003630 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003631 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003632
James Smart92d7f7b2007-06-17 19:56:38 -05003633 if (pring->ringno == LPFC_ELS_RING) {
3634 lpfc_fabric_abort_hba(phba);
3635 }
3636
dea31012005-04-17 16:05:31 -05003637 /* Error everything on txq and txcmplq
3638 * First do the txq.
3639 */
James Smartdb55fba2014-04-04 13:52:02 -04003640 if (phba->sli_rev >= LPFC_SLI_REV4) {
3641 spin_lock_irq(&pring->ring_lock);
3642 list_splice_init(&pring->txq, &completions);
3643 pring->txq_cnt = 0;
3644 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003645
James Smartdb55fba2014-04-04 13:52:02 -04003646 spin_lock_irq(&phba->hbalock);
3647 /* Next issue ABTS for everything on the txcmplq */
3648 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3649 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3650 spin_unlock_irq(&phba->hbalock);
3651 } else {
3652 spin_lock_irq(&phba->hbalock);
3653 list_splice_init(&pring->txq, &completions);
3654 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003655
James Smartdb55fba2014-04-04 13:52:02 -04003656 /* Next issue ABTS for everything on the txcmplq */
3657 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3658 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3659 spin_unlock_irq(&phba->hbalock);
3660 }
James Smart2534ba72007-04-25 09:52:20 -04003661
James Smarta257bf92009-04-06 18:48:10 -04003662 /* Cancel all the IOCBs from the completions list */
3663 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3664 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003665}
3666
James Smarte59058c2008-08-24 21:49:00 -04003667/**
James Smart895427b2017-02-12 13:52:30 -08003668 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3669 * @phba: Pointer to HBA context object.
3670 * @pring: Pointer to driver SLI ring object.
3671 *
3672 * This function aborts all iocbs in the given ring and frees all the iocb
3673 * objects in txq. This function issues an abort iocb for all the iocb commands
3674 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3675 * the return of this function. The caller is not required to hold any locks.
3676 **/
3677void
3678lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3679{
3680 LIST_HEAD(completions);
3681 struct lpfc_iocbq *iocb, *next_iocb;
3682
3683 if (pring->ringno == LPFC_ELS_RING)
3684 lpfc_fabric_abort_hba(phba);
3685
3686 spin_lock_irq(&phba->hbalock);
3687 /* Next issue ABTS for everything on the txcmplq */
3688 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3689 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3690 spin_unlock_irq(&phba->hbalock);
3691}
3692
3693
3694/**
James Smartdb55fba2014-04-04 13:52:02 -04003695 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3696 * @phba: Pointer to HBA context object.
3697 * @pring: Pointer to driver SLI ring object.
3698 *
3699 * This function aborts all iocbs in FCP rings and frees all the iocb
3700 * objects in txq. This function issues an abort iocb for all the iocb commands
3701 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3702 * the return of this function. The caller is not required to hold any locks.
3703 **/
3704void
3705lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3706{
3707 struct lpfc_sli *psli = &phba->sli;
3708 struct lpfc_sli_ring *pring;
3709 uint32_t i;
3710
3711 /* Look on all the FCP Rings for the iotag */
3712 if (phba->sli_rev >= LPFC_SLI_REV4) {
3713 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003714 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003715 lpfc_sli_abort_iocb_ring(phba, pring);
3716 }
3717 } else {
James Smart895427b2017-02-12 13:52:30 -08003718 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003719 lpfc_sli_abort_iocb_ring(phba, pring);
3720 }
3721}
3722
James Smart895427b2017-02-12 13:52:30 -08003723/**
3724 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3725 * @phba: Pointer to HBA context object.
3726 *
3727 * This function aborts all wqes in NVME rings. This function issues an
3728 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3729 * the txcmplq is not guaranteed to complete before the return of this
3730 * function. The caller is not required to hold any locks.
3731 **/
3732void
3733lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3734{
3735 struct lpfc_sli_ring *pring;
3736 uint32_t i;
3737
3738 if (phba->sli_rev < LPFC_SLI_REV4)
3739 return;
3740
3741 /* Abort all IO on each NVME ring. */
3742 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3743 pring = phba->sli4_hba.nvme_wq[i]->pring;
3744 lpfc_sli_abort_wqe_ring(phba, pring);
3745 }
3746}
3747
James Smartdb55fba2014-04-04 13:52:02 -04003748
3749/**
James Smart3621a712009-04-06 18:47:14 -04003750 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003751 * @phba: Pointer to HBA context object.
3752 *
3753 * This function flushes all iocbs in the fcp ring and frees all the iocb
3754 * objects in txq and txcmplq. This function will not issue abort iocbs
3755 * for all the iocb commands in txcmplq, they will just be returned with
3756 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3757 * slot has been permanently disabled.
3758 **/
3759void
3760lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3761{
3762 LIST_HEAD(txq);
3763 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003764 struct lpfc_sli *psli = &phba->sli;
3765 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003766 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003767
3768 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003769 /* Indicate the I/O queues are flushed */
3770 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003771 spin_unlock_irq(&phba->hbalock);
3772
James Smartdb55fba2014-04-04 13:52:02 -04003773 /* Look on all the FCP Rings for the iotag */
3774 if (phba->sli_rev >= LPFC_SLI_REV4) {
3775 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003776 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003777
James Smartdb55fba2014-04-04 13:52:02 -04003778 spin_lock_irq(&pring->ring_lock);
3779 /* Retrieve everything on txq */
3780 list_splice_init(&pring->txq, &txq);
3781 /* Retrieve everything on the txcmplq */
3782 list_splice_init(&pring->txcmplq, &txcmplq);
3783 pring->txq_cnt = 0;
3784 pring->txcmplq_cnt = 0;
3785 spin_unlock_irq(&pring->ring_lock);
3786
3787 /* Flush the txq */
3788 lpfc_sli_cancel_iocbs(phba, &txq,
3789 IOSTAT_LOCAL_REJECT,
3790 IOERR_SLI_DOWN);
3791 /* Flush the txcmpq */
3792 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3793 IOSTAT_LOCAL_REJECT,
3794 IOERR_SLI_DOWN);
3795 }
3796 } else {
James Smart895427b2017-02-12 13:52:30 -08003797 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003798
3799 spin_lock_irq(&phba->hbalock);
3800 /* Retrieve everything on txq */
3801 list_splice_init(&pring->txq, &txq);
3802 /* Retrieve everything on the txcmplq */
3803 list_splice_init(&pring->txcmplq, &txcmplq);
3804 pring->txq_cnt = 0;
3805 pring->txcmplq_cnt = 0;
3806 spin_unlock_irq(&phba->hbalock);
3807
3808 /* Flush the txq */
3809 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3810 IOERR_SLI_DOWN);
3811 /* Flush the txcmpq */
3812 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3813 IOERR_SLI_DOWN);
3814 }
James Smarta8e497d2008-08-24 21:50:11 -04003815}
3816
3817/**
James Smart895427b2017-02-12 13:52:30 -08003818 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3819 * @phba: Pointer to HBA context object.
3820 *
3821 * This function flushes all wqes in the nvme rings and frees all resources
3822 * in the txcmplq. This function does not issue abort wqes for the IO
3823 * commands in txcmplq, they will just be returned with
3824 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3825 * slot has been permanently disabled.
3826 **/
3827void
3828lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3829{
3830 LIST_HEAD(txcmplq);
3831 struct lpfc_sli_ring *pring;
3832 uint32_t i;
3833
3834 if (phba->sli_rev < LPFC_SLI_REV4)
3835 return;
3836
3837 /* Hint to other driver operations that a flush is in progress. */
3838 spin_lock_irq(&phba->hbalock);
3839 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3840 spin_unlock_irq(&phba->hbalock);
3841
3842 /* Cycle through all NVME rings and complete each IO with
3843 * a local driver reason code. This is a flush so no
3844 * abort exchange to FW.
3845 */
3846 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3847 pring = phba->sli4_hba.nvme_wq[i]->pring;
3848
3849 /* Retrieve everything on the txcmplq */
3850 spin_lock_irq(&pring->ring_lock);
3851 list_splice_init(&pring->txcmplq, &txcmplq);
3852 pring->txcmplq_cnt = 0;
3853 spin_unlock_irq(&pring->ring_lock);
3854
3855 /* Flush the txcmpq &&&PAE */
3856 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3857 IOSTAT_LOCAL_REJECT,
3858 IOERR_SLI_DOWN);
3859 }
3860}
3861
3862/**
James Smart3772a992009-05-22 14:50:54 -04003863 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003864 * @phba: Pointer to HBA context object.
3865 * @mask: Bit mask to be checked.
3866 *
3867 * This function reads the host status register and compares
3868 * with the provided bit mask to check if HBA completed
3869 * the restart. This function will wait in a loop for the
3870 * HBA to complete restart. If the HBA does not restart within
3871 * 15 iterations, the function will reset the HBA again. The
3872 * function returns 1 when HBA fail to restart otherwise returns
3873 * zero.
3874 **/
James Smart3772a992009-05-22 14:50:54 -04003875static int
3876lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003877{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003878 uint32_t status;
3879 int i = 0;
3880 int retval = 0;
dea31012005-04-17 16:05:31 -05003881
Jamie Wellnitz41415862006-02-28 19:25:27 -05003882 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003883 if (lpfc_readl(phba->HSregaddr, &status))
3884 return 1;
dea31012005-04-17 16:05:31 -05003885
Jamie Wellnitz41415862006-02-28 19:25:27 -05003886 /*
3887 * Check status register every 100ms for 5 retries, then every
3888 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3889 * every 2.5 sec for 4.
3890 * Break our of the loop if errors occurred during init.
3891 */
3892 while (((status & mask) != mask) &&
3893 !(status & HS_FFERM) &&
3894 i++ < 20) {
dea31012005-04-17 16:05:31 -05003895
Jamie Wellnitz41415862006-02-28 19:25:27 -05003896 if (i <= 5)
3897 msleep(10);
3898 else if (i <= 10)
3899 msleep(500);
3900 else
3901 msleep(2500);
dea31012005-04-17 16:05:31 -05003902
Jamie Wellnitz41415862006-02-28 19:25:27 -05003903 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003904 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003905 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003906 lpfc_sli_brdrestart(phba);
3907 }
3908 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003909 if (lpfc_readl(phba->HSregaddr, &status)) {
3910 retval = 1;
3911 break;
3912 }
dea31012005-04-17 16:05:31 -05003913 }
dea31012005-04-17 16:05:31 -05003914
Jamie Wellnitz41415862006-02-28 19:25:27 -05003915 /* Check to see if any errors occurred during init */
3916 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3918 "2751 Adapter failed to restart, "
3919 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3920 status,
3921 readl(phba->MBslimaddr + 0xa8),
3922 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003923 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003924 retval = 1;
3925 }
dea31012005-04-17 16:05:31 -05003926
Jamie Wellnitz41415862006-02-28 19:25:27 -05003927 return retval;
dea31012005-04-17 16:05:31 -05003928}
3929
James Smartda0436e2009-05-22 14:51:39 -04003930/**
3931 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3932 * @phba: Pointer to HBA context object.
3933 * @mask: Bit mask to be checked.
3934 *
3935 * This function checks the host status register to check if HBA is
3936 * ready. This function will wait in a loop for the HBA to be ready
3937 * If the HBA is not ready , the function will will reset the HBA PCI
3938 * function again. The function returns 1 when HBA fail to be ready
3939 * otherwise returns zero.
3940 **/
3941static int
3942lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3943{
3944 uint32_t status;
3945 int retval = 0;
3946
3947 /* Read the HBA Host Status Register */
3948 status = lpfc_sli4_post_status_check(phba);
3949
3950 if (status) {
3951 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3952 lpfc_sli_brdrestart(phba);
3953 status = lpfc_sli4_post_status_check(phba);
3954 }
3955
3956 /* Check to see if any errors occurred during init */
3957 if (status) {
3958 phba->link_state = LPFC_HBA_ERROR;
3959 retval = 1;
3960 } else
3961 phba->sli4_hba.intr_enable = 0;
3962
3963 return retval;
3964}
3965
3966/**
3967 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3968 * @phba: Pointer to HBA context object.
3969 * @mask: Bit mask to be checked.
3970 *
3971 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3972 * from the API jump table function pointer from the lpfc_hba struct.
3973 **/
3974int
3975lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3976{
3977 return phba->lpfc_sli_brdready(phba, mask);
3978}
3979
James Smart92908312006-03-07 15:04:13 -05003980#define BARRIER_TEST_PATTERN (0xdeadbeef)
3981
James Smarte59058c2008-08-24 21:49:00 -04003982/**
James Smart3621a712009-04-06 18:47:14 -04003983 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003984 * @phba: Pointer to HBA context object.
3985 *
James Smart1b511972011-12-13 13:23:09 -05003986 * This function is called before resetting an HBA. This function is called
3987 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003988 **/
James Smart2e0fef82007-06-17 19:56:36 -05003989void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003990{
James Smart65a29c12006-07-06 15:50:50 -04003991 uint32_t __iomem *resp_buf;
3992 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003993 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003994 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003995 int i;
3996 uint8_t hdrtype;
3997
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003998 lockdep_assert_held(&phba->hbalock);
3999
James Smart92908312006-03-07 15:04:13 -05004000 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4001 if (hdrtype != 0x80 ||
4002 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4003 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4004 return;
4005
4006 /*
4007 * Tell the other part of the chip to suspend temporarily all
4008 * its DMA activity.
4009 */
James Smart65a29c12006-07-06 15:50:50 -04004010 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004011
4012 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004013 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4014 return;
James Smart92908312006-03-07 15:04:13 -05004015 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4016 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004017 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004018
James Smart9940b972011-03-11 16:06:12 -05004019 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4020 return;
4021 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004022 /* Clear Chip error bit */
4023 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004024 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004025 }
4026
4027 mbox = 0;
4028 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4029 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4030
4031 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004032 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004033 writel(mbox, mbox_buf);
4034
James Smart9940b972011-03-11 16:06:12 -05004035 for (i = 0; i < 50; i++) {
4036 if (lpfc_readl((resp_buf + 1), &resp_data))
4037 return;
4038 if (resp_data != ~(BARRIER_TEST_PATTERN))
4039 mdelay(1);
4040 else
4041 break;
4042 }
4043 resp_data = 0;
4044 if (lpfc_readl((resp_buf + 1), &resp_data))
4045 return;
4046 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004047 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004048 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004049 goto restore_hc;
4050 else
4051 goto clear_errat;
4052 }
4053
4054 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004055 resp_data = 0;
4056 for (i = 0; i < 500; i++) {
4057 if (lpfc_readl(resp_buf, &resp_data))
4058 return;
4059 if (resp_data != mbox)
4060 mdelay(1);
4061 else
4062 break;
4063 }
James Smart92908312006-03-07 15:04:13 -05004064
4065clear_errat:
4066
James Smart9940b972011-03-11 16:06:12 -05004067 while (++i < 500) {
4068 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4069 return;
4070 if (!(ha_copy & HA_ERATT))
4071 mdelay(1);
4072 else
4073 break;
4074 }
James Smart92908312006-03-07 15:04:13 -05004075
4076 if (readl(phba->HAregaddr) & HA_ERATT) {
4077 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004078 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004079 }
4080
4081restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004082 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004083 writel(hc_copy, phba->HCregaddr);
4084 readl(phba->HCregaddr); /* flush */
4085}
4086
James Smarte59058c2008-08-24 21:49:00 -04004087/**
James Smart3621a712009-04-06 18:47:14 -04004088 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004089 * @phba: Pointer to HBA context object.
4090 *
4091 * This function issues a kill_board mailbox command and waits for
4092 * the error attention interrupt. This function is called for stopping
4093 * the firmware processing. The caller is not required to hold any
4094 * locks. This function calls lpfc_hba_down_post function to free
4095 * any pending commands after the kill. The function will return 1 when it
4096 * fails to kill the board else will return 0.
4097 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004098int
James Smart2e0fef82007-06-17 19:56:36 -05004099lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004100{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004101 struct lpfc_sli *psli;
4102 LPFC_MBOXQ_t *pmb;
4103 uint32_t status;
4104 uint32_t ha_copy;
4105 int retval;
4106 int i = 0;
4107
4108 psli = &phba->sli;
4109
4110 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004111 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004112 "0329 Kill HBA Data: x%x x%x\n",
4113 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004114
James Smart98c9ea52007-10-27 13:37:33 -04004115 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4116 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004117 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004118
4119 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004120 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004121 if (lpfc_readl(phba->HCregaddr, &status)) {
4122 spin_unlock_irq(&phba->hbalock);
4123 mempool_free(pmb, phba->mbox_mem_pool);
4124 return 1;
4125 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004126 status &= ~HC_ERINT_ENA;
4127 writel(status, phba->HCregaddr);
4128 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004129 phba->link_flag |= LS_IGNORE_ERATT;
4130 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004131
4132 lpfc_kill_board(phba, pmb);
4133 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4134 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4135
4136 if (retval != MBX_SUCCESS) {
4137 if (retval != MBX_BUSY)
4138 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004139 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4140 "2752 KILL_BOARD command failed retval %d\n",
4141 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004142 spin_lock_irq(&phba->hbalock);
4143 phba->link_flag &= ~LS_IGNORE_ERATT;
4144 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004145 return 1;
4146 }
4147
James Smartf4b4c682009-05-22 14:53:12 -04004148 spin_lock_irq(&phba->hbalock);
4149 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4150 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004151
Jamie Wellnitz41415862006-02-28 19:25:27 -05004152 mempool_free(pmb, phba->mbox_mem_pool);
4153
4154 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4155 * attention every 100ms for 3 seconds. If we don't get ERATT after
4156 * 3 seconds we still set HBA_ERROR state because the status of the
4157 * board is now undefined.
4158 */
James Smart9940b972011-03-11 16:06:12 -05004159 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4160 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004161 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4162 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004163 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4164 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004165 }
4166
4167 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004168 if (ha_copy & HA_ERATT) {
4169 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004170 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004171 }
James Smart2e0fef82007-06-17 19:56:36 -05004172 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004173 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004174 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004175 phba->link_flag &= ~LS_IGNORE_ERATT;
4176 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004177
Jamie Wellnitz41415862006-02-28 19:25:27 -05004178 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004179 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004180
James Smart2e0fef82007-06-17 19:56:36 -05004181 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004182}
4183
James Smarte59058c2008-08-24 21:49:00 -04004184/**
James Smart3772a992009-05-22 14:50:54 -04004185 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004186 * @phba: Pointer to HBA context object.
4187 *
4188 * This function resets the HBA by writing HC_INITFF to the control
4189 * register. After the HBA resets, this function resets all the iocb ring
4190 * indices. This function disables PCI layer parity checking during
4191 * the reset.
4192 * This function returns 0 always.
4193 * The caller is not required to hold any locks.
4194 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004195int
James Smart2e0fef82007-06-17 19:56:36 -05004196lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004197{
4198 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004199 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004200 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004201 int i;
dea31012005-04-17 16:05:31 -05004202
Jamie Wellnitz41415862006-02-28 19:25:27 -05004203 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004204
Jamie Wellnitz41415862006-02-28 19:25:27 -05004205 /* Reset HBA */
4206 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004207 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004208 (phba->pport) ? phba->pport->port_state : 0,
4209 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004210
4211 /* perform board reset */
4212 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004213 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004214 if (phba->pport) {
4215 phba->pport->fc_myDID = 0;
4216 phba->pport->fc_prevDID = 0;
4217 }
dea31012005-04-17 16:05:31 -05004218
Jamie Wellnitz41415862006-02-28 19:25:27 -05004219 /* Turn off parity checking and serr during the physical reset */
4220 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4221 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4222 (cfg_value &
4223 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4224
James Smart3772a992009-05-22 14:50:54 -04004225 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4226
Jamie Wellnitz41415862006-02-28 19:25:27 -05004227 /* Now toggle INITFF bit in the Host Control Register */
4228 writel(HC_INITFF, phba->HCregaddr);
4229 mdelay(1);
4230 readl(phba->HCregaddr); /* flush */
4231 writel(0, phba->HCregaddr);
4232 readl(phba->HCregaddr); /* flush */
4233
4234 /* Restore PCI cmd register */
4235 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004236
4237 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004238 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004239 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004240 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004241 pring->sli.sli3.rspidx = 0;
4242 pring->sli.sli3.next_cmdidx = 0;
4243 pring->sli.sli3.local_getidx = 0;
4244 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004245 pring->missbufcnt = 0;
4246 }
dea31012005-04-17 16:05:31 -05004247
James Smart2e0fef82007-06-17 19:56:36 -05004248 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004249 return 0;
4250}
4251
James Smarte59058c2008-08-24 21:49:00 -04004252/**
James Smartda0436e2009-05-22 14:51:39 -04004253 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4254 * @phba: Pointer to HBA context object.
4255 *
4256 * This function resets a SLI4 HBA. This function disables PCI layer parity
4257 * checking during resets the device. The caller is not required to hold
4258 * any locks.
4259 *
4260 * This function returns 0 always.
4261 **/
4262int
4263lpfc_sli4_brdreset(struct lpfc_hba *phba)
4264{
4265 struct lpfc_sli *psli = &phba->sli;
4266 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004267 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004268
4269 /* Reset HBA */
4270 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004271 "0295 Reset HBA Data: x%x x%x x%x\n",
4272 phba->pport->port_state, psli->sli_flag,
4273 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004274
4275 /* perform board reset */
4276 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004277 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004278 phba->pport->fc_myDID = 0;
4279 phba->pport->fc_prevDID = 0;
4280
James Smartda0436e2009-05-22 14:51:39 -04004281 spin_lock_irq(&phba->hbalock);
4282 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4283 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004284 spin_unlock_irq(&phba->hbalock);
4285
James Smart02936352014-04-04 13:52:12 -04004286 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4287 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4288 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4289 return rc;
4290 }
4291
James Smartda0436e2009-05-22 14:51:39 -04004292 /* Now physically reset the device */
4293 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4294 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004295
4296 /* Turn off parity checking and serr during the physical reset */
4297 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4298 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4299 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4300
James Smart88318812012-09-29 11:29:29 -04004301 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004302 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004303 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004304
James Smartbe858b62010-12-15 17:57:20 -05004305 /* Restore PCI cmd register */
4306 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4307
James Smart27b01b82012-05-09 21:19:44 -04004308 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004309}
4310
4311/**
4312 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004313 * @phba: Pointer to HBA context object.
4314 *
4315 * This function is called in the SLI initialization code path to
4316 * restart the HBA. The caller is not required to hold any lock.
4317 * This function writes MBX_RESTART mailbox command to the SLIM and
4318 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4319 * function to free any pending commands. The function enables
4320 * POST only during the first initialization. The function returns zero.
4321 * The function does not guarantee completion of MBX_RESTART mailbox
4322 * command before the return of this function.
4323 **/
James Smartda0436e2009-05-22 14:51:39 -04004324static int
4325lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004326{
4327 MAILBOX_t *mb;
4328 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004329 volatile uint32_t word0;
4330 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004331 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004332
James Smart2e0fef82007-06-17 19:56:36 -05004333 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004334
James Smart0d878412009-10-02 15:16:56 -04004335 /* Take PCIe device Advanced Error Reporting (AER) state */
4336 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4337
Jamie Wellnitz41415862006-02-28 19:25:27 -05004338 psli = &phba->sli;
4339
4340 /* Restart HBA */
4341 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004342 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004343 (phba->pport) ? phba->pport->port_state : 0,
4344 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004345
4346 word0 = 0;
4347 mb = (MAILBOX_t *) &word0;
4348 mb->mbxCommand = MBX_RESTART;
4349 mb->mbxHc = 1;
4350
James Smart92908312006-03-07 15:04:13 -05004351 lpfc_reset_barrier(phba);
4352
Jamie Wellnitz41415862006-02-28 19:25:27 -05004353 to_slim = phba->MBslimaddr;
4354 writel(*(uint32_t *) mb, to_slim);
4355 readl(to_slim); /* flush */
4356
4357 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004358 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004359 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004360 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004361 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004362 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004363 writel(*(uint32_t *) mb, to_slim);
4364 readl(to_slim); /* flush */
4365
4366 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004367 if (phba->pport)
4368 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004369 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004370 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004371 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004372
James Smart64ba8812006-08-02 15:24:34 -04004373 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4374 psli->stats_start = get_seconds();
4375
James Smarteaf15d52008-12-04 22:39:29 -05004376 /* Give the INITFF and Post time to settle. */
4377 mdelay(100);
dea31012005-04-17 16:05:31 -05004378
James Smart0d878412009-10-02 15:16:56 -04004379 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4380 if (hba_aer_enabled)
4381 pci_disable_pcie_error_reporting(phba->pcidev);
4382
Jamie Wellnitz41415862006-02-28 19:25:27 -05004383 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004384
4385 return 0;
4386}
4387
James Smarte59058c2008-08-24 21:49:00 -04004388/**
James Smartda0436e2009-05-22 14:51:39 -04004389 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4390 * @phba: Pointer to HBA context object.
4391 *
4392 * This function is called in the SLI initialization code path to restart
4393 * a SLI4 HBA. The caller is not required to hold any lock.
4394 * At the end of the function, it calls lpfc_hba_down_post function to
4395 * free any pending commands.
4396 **/
4397static int
4398lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4399{
4400 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004401 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004402 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004403
4404 /* Restart HBA */
4405 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4406 "0296 Restart HBA Data: x%x x%x\n",
4407 phba->pport->port_state, psli->sli_flag);
4408
James Smart75baf692010-06-08 18:31:21 -04004409 /* Take PCIe device Advanced Error Reporting (AER) state */
4410 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4411
James Smart27b01b82012-05-09 21:19:44 -04004412 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004413
4414 spin_lock_irq(&phba->hbalock);
4415 phba->pport->stopped = 0;
4416 phba->link_state = LPFC_INIT_START;
4417 phba->hba_flag = 0;
4418 spin_unlock_irq(&phba->hbalock);
4419
4420 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4421 psli->stats_start = get_seconds();
4422
James Smart75baf692010-06-08 18:31:21 -04004423 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4424 if (hba_aer_enabled)
4425 pci_disable_pcie_error_reporting(phba->pcidev);
4426
James Smartda0436e2009-05-22 14:51:39 -04004427 lpfc_hba_down_post(phba);
4428
James Smart27b01b82012-05-09 21:19:44 -04004429 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004430}
4431
4432/**
4433 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4434 * @phba: Pointer to HBA context object.
4435 *
4436 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4437 * API jump table function pointer from the lpfc_hba struct.
4438**/
4439int
4440lpfc_sli_brdrestart(struct lpfc_hba *phba)
4441{
4442 return phba->lpfc_sli_brdrestart(phba);
4443}
4444
4445/**
James Smart3621a712009-04-06 18:47:14 -04004446 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004447 * @phba: Pointer to HBA context object.
4448 *
4449 * This function is called after a HBA restart to wait for successful
4450 * restart of the HBA. Successful restart of the HBA is indicated by
4451 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4452 * iteration, the function will restart the HBA again. The function returns
4453 * zero if HBA successfully restarted else returns negative error code.
4454 **/
James Smart4492b732017-04-27 15:08:26 -07004455int
dea31012005-04-17 16:05:31 -05004456lpfc_sli_chipset_init(struct lpfc_hba *phba)
4457{
4458 uint32_t status, i = 0;
4459
4460 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004461 if (lpfc_readl(phba->HSregaddr, &status))
4462 return -EIO;
dea31012005-04-17 16:05:31 -05004463
4464 /* Check status register to see what current state is */
4465 i = 0;
4466 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4467
James Smartdcf2a4e2010-09-29 11:18:53 -04004468 /* Check every 10ms for 10 retries, then every 100ms for 90
4469 * retries, then every 1 sec for 50 retires for a total of
4470 * ~60 seconds before reset the board again and check every
4471 * 1 sec for 50 retries. The up to 60 seconds before the
4472 * board ready is required by the Falcon FIPS zeroization
4473 * complete, and any reset the board in between shall cause
4474 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004475 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004476 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004477 /* Adapter failed to init, timeout, status reg
4478 <status> */
James Smarted957682007-06-17 19:56:37 -05004479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004480 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004481 "timeout, status reg x%x, "
4482 "FW Data: A8 x%x AC x%x\n", status,
4483 readl(phba->MBslimaddr + 0xa8),
4484 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004485 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004486 return -ETIMEDOUT;
4487 }
4488
4489 /* Check to see if any errors occurred during init */
4490 if (status & HS_FFERM) {
4491 /* ERROR: During chipset initialization */
4492 /* Adapter failed to init, chipset, status reg
4493 <status> */
James Smarted957682007-06-17 19:56:37 -05004494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004495 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004496 "chipset, status reg x%x, "
4497 "FW Data: A8 x%x AC x%x\n", status,
4498 readl(phba->MBslimaddr + 0xa8),
4499 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004500 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004501 return -EIO;
4502 }
4503
James Smartdcf2a4e2010-09-29 11:18:53 -04004504 if (i <= 10)
dea31012005-04-17 16:05:31 -05004505 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004506 else if (i <= 100)
4507 msleep(100);
4508 else
4509 msleep(1000);
dea31012005-04-17 16:05:31 -05004510
James Smartdcf2a4e2010-09-29 11:18:53 -04004511 if (i == 150) {
4512 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004513 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004514 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004515 }
4516 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004517 if (lpfc_readl(phba->HSregaddr, &status))
4518 return -EIO;
dea31012005-04-17 16:05:31 -05004519 }
4520
4521 /* Check to see if any errors occurred during init */
4522 if (status & HS_FFERM) {
4523 /* ERROR: During chipset initialization */
4524 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004525 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004526 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004527 "status reg x%x, "
4528 "FW Data: A8 x%x AC x%x\n", status,
4529 readl(phba->MBslimaddr + 0xa8),
4530 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004531 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004532 return -EIO;
4533 }
4534
4535 /* Clear all interrupt enable conditions */
4536 writel(0, phba->HCregaddr);
4537 readl(phba->HCregaddr); /* flush */
4538
4539 /* setup host attn register */
4540 writel(0xffffffff, phba->HAregaddr);
4541 readl(phba->HAregaddr); /* flush */
4542 return 0;
4543}
4544
James Smarte59058c2008-08-24 21:49:00 -04004545/**
James Smart3621a712009-04-06 18:47:14 -04004546 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004547 *
4548 * This function calculates and returns the number of HBQs required to be
4549 * configured.
4550 **/
James Smart78b2d852007-08-02 11:10:21 -04004551int
James Smarted957682007-06-17 19:56:37 -05004552lpfc_sli_hbq_count(void)
4553{
James Smart92d7f7b2007-06-17 19:56:38 -05004554 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004555}
4556
James Smarte59058c2008-08-24 21:49:00 -04004557/**
James Smart3621a712009-04-06 18:47:14 -04004558 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004559 *
4560 * This function adds the number of hbq entries in every HBQ to get
4561 * the total number of hbq entries required for the HBA and returns
4562 * the total count.
4563 **/
James Smarted957682007-06-17 19:56:37 -05004564static int
4565lpfc_sli_hbq_entry_count(void)
4566{
4567 int hbq_count = lpfc_sli_hbq_count();
4568 int count = 0;
4569 int i;
4570
4571 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004572 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004573 return count;
4574}
4575
James Smarte59058c2008-08-24 21:49:00 -04004576/**
James Smart3621a712009-04-06 18:47:14 -04004577 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004578 *
4579 * This function calculates amount of memory required for all hbq entries
4580 * to be configured and returns the total memory required.
4581 **/
dea31012005-04-17 16:05:31 -05004582int
James Smarted957682007-06-17 19:56:37 -05004583lpfc_sli_hbq_size(void)
4584{
4585 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4586}
4587
James Smarte59058c2008-08-24 21:49:00 -04004588/**
James Smart3621a712009-04-06 18:47:14 -04004589 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004590 * @phba: Pointer to HBA context object.
4591 *
4592 * This function is called during the SLI initialization to configure
4593 * all the HBQs and post buffers to the HBQ. The caller is not
4594 * required to hold any locks. This function will return zero if successful
4595 * else it will return negative error code.
4596 **/
James Smarted957682007-06-17 19:56:37 -05004597static int
4598lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4599{
4600 int hbq_count = lpfc_sli_hbq_count();
4601 LPFC_MBOXQ_t *pmb;
4602 MAILBOX_t *pmbox;
4603 uint32_t hbqno;
4604 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004605
James Smart92d7f7b2007-06-17 19:56:38 -05004606 /* Get a Mailbox buffer to setup mailbox
4607 * commands for HBA initialization
4608 */
James Smarted957682007-06-17 19:56:37 -05004609 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4610
4611 if (!pmb)
4612 return -ENOMEM;
4613
James Smart04c68492009-05-22 14:52:52 -04004614 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004615
4616 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4617 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004618 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004619
4620 hbq_entry_index = 0;
4621 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4622 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4623 phba->hbqs[hbqno].hbqPutIdx = 0;
4624 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4625 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004626 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004627 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4628 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004629 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4630
4631 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4632 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4633 mbxStatus <status>, ring <num> */
4634
4635 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004636 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004637 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004638 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004639 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004640 pmbox->mbxStatus, hbqno);
4641
4642 phba->link_state = LPFC_HBA_ERROR;
4643 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004644 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004645 }
4646 }
4647 phba->hbq_count = hbq_count;
4648
James Smarted957682007-06-17 19:56:37 -05004649 mempool_free(pmb, phba->mbox_mem_pool);
4650
James Smart92d7f7b2007-06-17 19:56:38 -05004651 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004652 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4653 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004654 return 0;
4655}
4656
James Smarte59058c2008-08-24 21:49:00 -04004657/**
James Smart4f774512009-05-22 14:52:35 -04004658 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4659 * @phba: Pointer to HBA context object.
4660 *
4661 * This function is called during the SLI initialization to configure
4662 * all the HBQs and post buffers to the HBQ. The caller is not
4663 * required to hold any locks. This function will return zero if successful
4664 * else it will return negative error code.
4665 **/
4666static int
4667lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4668{
4669 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004670 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4671 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004672 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004673 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004674 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004675 return 0;
4676}
4677
4678/**
James Smart3621a712009-04-06 18:47:14 -04004679 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004680 * @phba: Pointer to HBA context object.
4681 * @sli_mode: sli mode - 2/3
4682 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004683 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004684 * to issue config_port mailbox command. This function restarts the
4685 * HBA firmware and issues a config_port mailbox command to configure
4686 * the SLI interface in the sli mode specified by sli_mode
4687 * variable. The caller is not required to hold any locks.
4688 * The function returns 0 if successful, else returns negative error
4689 * code.
4690 **/
James Smart93996272008-08-24 21:50:30 -04004691int
4692lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004693{
4694 LPFC_MBOXQ_t *pmb;
4695 uint32_t resetcount = 0, rc = 0, done = 0;
4696
4697 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4698 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004699 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004700 return -ENOMEM;
4701 }
4702
James Smarted957682007-06-17 19:56:37 -05004703 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004704 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004705 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004706 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004707 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004708 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004709 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004710 rc = lpfc_sli_chipset_init(phba);
4711 if (rc)
4712 break;
4713
James Smart2e0fef82007-06-17 19:56:36 -05004714 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004715 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004716 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004717 resetcount++;
4718
James Smarted957682007-06-17 19:56:37 -05004719 /* Call pre CONFIG_PORT mailbox command initialization. A
4720 * value of 0 means the call was successful. Any other
4721 * nonzero value is a failure, but if ERESTART is returned,
4722 * the driver may reset the HBA and try again.
4723 */
dea31012005-04-17 16:05:31 -05004724 rc = lpfc_config_port_prep(phba);
4725 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004726 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004727 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004728 } else if (rc)
dea31012005-04-17 16:05:31 -05004729 break;
James Smart6d368e52011-05-24 11:44:12 -04004730
James Smart2e0fef82007-06-17 19:56:36 -05004731 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004732 lpfc_config_port(phba, pmb);
4733 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004734 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4735 LPFC_SLI3_HBQ_ENABLED |
4736 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004737 LPFC_SLI3_BG_ENABLED |
4738 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004739 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004741 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004742 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004743 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004744 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004745 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004746 spin_unlock_irq(&phba->hbalock);
4747 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004748 } else {
4749 /* Allow asynchronous mailbox command to go through */
4750 spin_lock_irq(&phba->hbalock);
4751 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4752 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004753 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004754
4755 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4756 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4757 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4758 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004759 }
dea31012005-04-17 16:05:31 -05004760 }
James Smarted957682007-06-17 19:56:37 -05004761 if (!done) {
4762 rc = -EINVAL;
4763 goto do_prep_failed;
4764 }
James Smart04c68492009-05-22 14:52:52 -04004765 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4766 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004767 rc = -ENXIO;
4768 goto do_prep_failed;
4769 }
James Smart04c68492009-05-22 14:52:52 -04004770 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004771 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004772 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4773 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4774 phba->max_vpi : phba->max_vports;
4775
James Smart34b02dc2008-08-24 21:49:55 -04004776 } else
4777 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004778 phba->fips_level = 0;
4779 phba->fips_spec_rev = 0;
4780 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004781 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004782 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4783 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4784 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4785 "2850 Security Crypto Active. FIPS x%d "
4786 "(Spec Rev: x%d)",
4787 phba->fips_level, phba->fips_spec_rev);
4788 }
4789 if (pmb->u.mb.un.varCfgPort.sec_err) {
4790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4791 "2856 Config Port Security Crypto "
4792 "Error: x%x ",
4793 pmb->u.mb.un.varCfgPort.sec_err);
4794 }
James Smart04c68492009-05-22 14:52:52 -04004795 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004796 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004797 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004798 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004799
4800 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4801 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004802
4803 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004804 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004805 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4806 else
4807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4808 "0443 Adapter did not grant "
4809 "BlockGuard\n");
4810 }
James Smart34b02dc2008-08-24 21:49:55 -04004811 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004812 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004813 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004814 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004815 }
James Smart92d7f7b2007-06-17 19:56:38 -05004816do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004817 mempool_free(pmb, phba->mbox_mem_pool);
4818 return rc;
4819}
4820
James Smarte59058c2008-08-24 21:49:00 -04004821
4822/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08004823 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04004824 * @phba: Pointer to HBA context object.
4825 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004826 * This function is the main SLI initialization function. This function
4827 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04004828 * error attention handler code. Caller is not required to hold any
4829 * locks. This function issues config_port mailbox command to configure
4830 * the SLI, setup iocb rings and HBQ rings. In the end the function
4831 * calls the config_port_post function to issue init_link mailbox
4832 * command and to start the discovery. The function will return zero
4833 * if successful, else it will return negative error code.
4834 **/
James Smarted957682007-06-17 19:56:37 -05004835int
4836lpfc_sli_hba_setup(struct lpfc_hba *phba)
4837{
4838 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004839 int mode = 3, i;
4840 int longs;
James Smarted957682007-06-17 19:56:37 -05004841
James Smart12247e82016-07-06 12:36:09 -07004842 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004843 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004844 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004845 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004846 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004847 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004848 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004849 break;
4850 }
James Smarted957682007-06-17 19:56:37 -05004851 mode = 2;
4852 break;
4853 case 0:
4854 case 3:
4855 break;
4856 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004858 "1819 Unrecognized sli_mode parameter: %d.\n",
4859 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004860
4861 break;
4862 }
James Smartb5c53952016-03-31 14:12:30 -07004863 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004864
James Smart93996272008-08-24 21:50:30 -04004865 rc = lpfc_sli_config_port(phba, mode);
4866
James Smart12247e82016-07-06 12:36:09 -07004867 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004868 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004869 "1820 Unable to select SLI-3. "
4870 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004871 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004872 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004873 else if (rc && mode == 2)
4874 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004875 if (rc)
dea31012005-04-17 16:05:31 -05004876 goto lpfc_sli_hba_setup_error;
4877
James Smart0d878412009-10-02 15:16:56 -04004878 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4879 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4880 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4881 if (!rc) {
4882 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4883 "2709 This device supports "
4884 "Advanced Error Reporting (AER)\n");
4885 spin_lock_irq(&phba->hbalock);
4886 phba->hba_flag |= HBA_AER_ENABLED;
4887 spin_unlock_irq(&phba->hbalock);
4888 } else {
4889 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4890 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004891 "Advanced Error Reporting (AER): %d\n",
4892 rc);
James Smart0d878412009-10-02 15:16:56 -04004893 phba->cfg_aer_support = 0;
4894 }
4895 }
4896
James Smarted957682007-06-17 19:56:37 -05004897 if (phba->sli_rev == 3) {
4898 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4899 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004900 } else {
4901 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4902 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004903 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004904 }
4905
4906 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004907 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4908 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004909 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004910
4911 if (rc)
4912 goto lpfc_sli_hba_setup_error;
4913
James Smart6d368e52011-05-24 11:44:12 -04004914 /* Initialize VPIs. */
4915 if (phba->sli_rev == LPFC_SLI_REV3) {
4916 /*
4917 * The VPI bitmask and physical ID array are allocated
4918 * and initialized once only - at driver load. A port
4919 * reset doesn't need to reinitialize this memory.
4920 */
4921 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4922 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4923 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4924 GFP_KERNEL);
4925 if (!phba->vpi_bmask) {
4926 rc = -ENOMEM;
4927 goto lpfc_sli_hba_setup_error;
4928 }
4929
4930 phba->vpi_ids = kzalloc(
4931 (phba->max_vpi+1) * sizeof(uint16_t),
4932 GFP_KERNEL);
4933 if (!phba->vpi_ids) {
4934 kfree(phba->vpi_bmask);
4935 rc = -ENOMEM;
4936 goto lpfc_sli_hba_setup_error;
4937 }
4938 for (i = 0; i < phba->max_vpi; i++)
4939 phba->vpi_ids[i] = i;
4940 }
4941 }
4942
James Smart93996272008-08-24 21:50:30 -04004943 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004944 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4945 rc = lpfc_sli_hbq_setup(phba);
4946 if (rc)
4947 goto lpfc_sli_hba_setup_error;
4948 }
James Smart04c68492009-05-22 14:52:52 -04004949 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004950 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004951 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004952
4953 rc = lpfc_config_port_post(phba);
4954 if (rc)
4955 goto lpfc_sli_hba_setup_error;
4956
James Smarted957682007-06-17 19:56:37 -05004957 return rc;
4958
James Smart92d7f7b2007-06-17 19:56:38 -05004959lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004960 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004961 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004962 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004963 return rc;
4964}
4965
James Smartda0436e2009-05-22 14:51:39 -04004966/**
4967 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4968 * @phba: Pointer to HBA context object.
4969 * @mboxq: mailbox pointer.
4970 * This function issue a dump mailbox command to read config region
4971 * 23 and parse the records in the region and populate driver
4972 * data structure.
4973 **/
4974static int
James Smartff78d8f2011-12-13 13:21:35 -05004975lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004976{
James Smartff78d8f2011-12-13 13:21:35 -05004977 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004978 struct lpfc_dmabuf *mp;
4979 struct lpfc_mqe *mqe;
4980 uint32_t data_length;
4981 int rc;
4982
4983 /* Program the default value of vlan_id and fc_map */
4984 phba->valid_vlan = 0;
4985 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4986 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4987 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4988
James Smartff78d8f2011-12-13 13:21:35 -05004989 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4990 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004991 return -ENOMEM;
4992
James Smartff78d8f2011-12-13 13:21:35 -05004993 mqe = &mboxq->u.mqe;
4994 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4995 rc = -ENOMEM;
4996 goto out_free_mboxq;
4997 }
4998
James Smartda0436e2009-05-22 14:51:39 -04004999 mp = (struct lpfc_dmabuf *) mboxq->context1;
5000 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5001
5002 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5003 "(%d):2571 Mailbox cmd x%x Status x%x "
5004 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5005 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5006 "CQ: x%x x%x x%x x%x\n",
5007 mboxq->vport ? mboxq->vport->vpi : 0,
5008 bf_get(lpfc_mqe_command, mqe),
5009 bf_get(lpfc_mqe_status, mqe),
5010 mqe->un.mb_words[0], mqe->un.mb_words[1],
5011 mqe->un.mb_words[2], mqe->un.mb_words[3],
5012 mqe->un.mb_words[4], mqe->un.mb_words[5],
5013 mqe->un.mb_words[6], mqe->un.mb_words[7],
5014 mqe->un.mb_words[8], mqe->un.mb_words[9],
5015 mqe->un.mb_words[10], mqe->un.mb_words[11],
5016 mqe->un.mb_words[12], mqe->un.mb_words[13],
5017 mqe->un.mb_words[14], mqe->un.mb_words[15],
5018 mqe->un.mb_words[16], mqe->un.mb_words[50],
5019 mboxq->mcqe.word0,
5020 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5021 mboxq->mcqe.trailer);
5022
5023 if (rc) {
5024 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5025 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005026 rc = -EIO;
5027 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005028 }
5029 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005030 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005031 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5032 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005033 rc = -EIO;
5034 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005035 }
James Smartda0436e2009-05-22 14:51:39 -04005036
5037 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5038 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5039 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005040 rc = 0;
5041
5042out_free_mboxq:
5043 mempool_free(mboxq, phba->mbox_mem_pool);
5044 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005045}
5046
5047/**
5048 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5049 * @phba: pointer to lpfc hba data structure.
5050 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5051 * @vpd: pointer to the memory to hold resulting port vpd data.
5052 * @vpd_size: On input, the number of bytes allocated to @vpd.
5053 * On output, the number of data bytes in @vpd.
5054 *
5055 * This routine executes a READ_REV SLI4 mailbox command. In
5056 * addition, this routine gets the port vpd data.
5057 *
5058 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005059 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005060 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005061 **/
5062static int
5063lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5064 uint8_t *vpd, uint32_t *vpd_size)
5065{
5066 int rc = 0;
5067 uint32_t dma_size;
5068 struct lpfc_dmabuf *dmabuf;
5069 struct lpfc_mqe *mqe;
5070
5071 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5072 if (!dmabuf)
5073 return -ENOMEM;
5074
5075 /*
5076 * Get a DMA buffer for the vpd data resulting from the READ_REV
5077 * mailbox command.
5078 */
5079 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005080 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5081 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005082 if (!dmabuf->virt) {
5083 kfree(dmabuf);
5084 return -ENOMEM;
5085 }
James Smartda0436e2009-05-22 14:51:39 -04005086
5087 /*
5088 * The SLI4 implementation of READ_REV conflicts at word1,
5089 * bits 31:16 and SLI4 adds vpd functionality not present
5090 * in SLI3. This code corrects the conflicts.
5091 */
5092 lpfc_read_rev(phba, mboxq);
5093 mqe = &mboxq->u.mqe;
5094 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5095 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5096 mqe->un.read_rev.word1 &= 0x0000FFFF;
5097 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5098 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5099
5100 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5101 if (rc) {
5102 dma_free_coherent(&phba->pcidev->dev, dma_size,
5103 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005104 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005105 return -EIO;
5106 }
5107
James Smartda0436e2009-05-22 14:51:39 -04005108 /*
5109 * The available vpd length cannot be bigger than the
5110 * DMA buffer passed to the port. Catch the less than
5111 * case and update the caller's size.
5112 */
5113 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5114 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5115
James Smartd7c47992010-06-08 18:31:54 -04005116 memcpy(vpd, dmabuf->virt, *vpd_size);
5117
James Smartda0436e2009-05-22 14:51:39 -04005118 dma_free_coherent(&phba->pcidev->dev, dma_size,
5119 dmabuf->virt, dmabuf->phys);
5120 kfree(dmabuf);
5121 return 0;
5122}
5123
5124/**
James Smartcd1c8302011-10-10 21:33:25 -04005125 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5126 * @phba: pointer to lpfc hba data structure.
5127 *
5128 * This routine retrieves SLI4 device physical port name this PCI function
5129 * is attached to.
5130 *
5131 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005132 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005133 * otherwise - failed to retrieve physical port name
5134 **/
5135static int
5136lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5137{
5138 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005139 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5140 struct lpfc_controller_attribute *cntl_attr;
5141 struct lpfc_mbx_get_port_name *get_port_name;
5142 void *virtaddr = NULL;
5143 uint32_t alloclen, reqlen;
5144 uint32_t shdr_status, shdr_add_status;
5145 union lpfc_sli4_cfg_shdr *shdr;
5146 char cport_name = 0;
5147 int rc;
5148
5149 /* We assume nothing at this point */
5150 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5151 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5152
5153 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5154 if (!mboxq)
5155 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005156 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005157 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5158 lpfc_sli4_read_config(phba);
5159 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5160 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005161
5162 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5163 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5164 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5165 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5166 LPFC_SLI4_MBX_NEMBED);
5167 if (alloclen < reqlen) {
5168 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5169 "3084 Allocated DMA memory size (%d) is "
5170 "less than the requested DMA memory size "
5171 "(%d)\n", alloclen, reqlen);
5172 rc = -ENOMEM;
5173 goto out_free_mboxq;
5174 }
5175 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5176 virtaddr = mboxq->sge_array->addr[0];
5177 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5178 shdr = &mbx_cntl_attr->cfg_shdr;
5179 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5180 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5181 if (shdr_status || shdr_add_status || rc) {
5182 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5183 "3085 Mailbox x%x (x%x/x%x) failed, "
5184 "rc:x%x, status:x%x, add_status:x%x\n",
5185 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5186 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5187 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5188 rc, shdr_status, shdr_add_status);
5189 rc = -ENXIO;
5190 goto out_free_mboxq;
5191 }
5192 cntl_attr = &mbx_cntl_attr->cntl_attr;
5193 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5194 phba->sli4_hba.lnk_info.lnk_tp =
5195 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5196 phba->sli4_hba.lnk_info.lnk_no =
5197 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5198 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5199 "3086 lnk_type:%d, lnk_numb:%d\n",
5200 phba->sli4_hba.lnk_info.lnk_tp,
5201 phba->sli4_hba.lnk_info.lnk_no);
5202
5203retrieve_ppname:
5204 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5205 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5206 sizeof(struct lpfc_mbx_get_port_name) -
5207 sizeof(struct lpfc_sli4_cfg_mhdr),
5208 LPFC_SLI4_MBX_EMBED);
5209 get_port_name = &mboxq->u.mqe.un.get_port_name;
5210 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5211 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5212 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5213 phba->sli4_hba.lnk_info.lnk_tp);
5214 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5215 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5216 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5217 if (shdr_status || shdr_add_status || rc) {
5218 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5219 "3087 Mailbox x%x (x%x/x%x) failed: "
5220 "rc:x%x, status:x%x, add_status:x%x\n",
5221 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5222 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5223 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5224 rc, shdr_status, shdr_add_status);
5225 rc = -ENXIO;
5226 goto out_free_mboxq;
5227 }
5228 switch (phba->sli4_hba.lnk_info.lnk_no) {
5229 case LPFC_LINK_NUMBER_0:
5230 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5231 &get_port_name->u.response);
5232 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5233 break;
5234 case LPFC_LINK_NUMBER_1:
5235 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5236 &get_port_name->u.response);
5237 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5238 break;
5239 case LPFC_LINK_NUMBER_2:
5240 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5241 &get_port_name->u.response);
5242 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5243 break;
5244 case LPFC_LINK_NUMBER_3:
5245 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5246 &get_port_name->u.response);
5247 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5248 break;
5249 default:
5250 break;
5251 }
5252
5253 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5254 phba->Port[0] = cport_name;
5255 phba->Port[1] = '\0';
5256 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5257 "3091 SLI get port name: %s\n", phba->Port);
5258 }
5259
5260out_free_mboxq:
5261 if (rc != MBX_TIMEOUT) {
5262 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5263 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5264 else
5265 mempool_free(mboxq, phba->mbox_mem_pool);
5266 }
5267 return rc;
5268}
5269
5270/**
James Smartda0436e2009-05-22 14:51:39 -04005271 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5272 * @phba: pointer to lpfc hba data structure.
5273 *
5274 * This routine is called to explicitly arm the SLI4 device's completion and
5275 * event queues
5276 **/
5277static void
5278lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5279{
James Smart895427b2017-02-12 13:52:30 -08005280 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04005281
5282 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5283 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005284 if (phba->sli4_hba.nvmels_cq)
5285 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5286 LPFC_QUEUE_REARM);
5287
5288 if (phba->sli4_hba.fcp_cq)
5289 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5290 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5291 LPFC_QUEUE_REARM);
5292
5293 if (phba->sli4_hba.nvme_cq)
5294 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5295 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5296 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005297
James Smartf38fa0b2014-04-04 13:52:21 -04005298 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005299 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5300
James Smart895427b2017-02-12 13:52:30 -08005301 if (phba->sli4_hba.hba_eq)
5302 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5303 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5304 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005305
James Smart2d7dbc42017-02-12 13:52:35 -08005306 if (phba->nvmet_support) {
5307 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5308 lpfc_sli4_cq_release(
5309 phba->sli4_hba.nvmet_cqset[qidx],
5310 LPFC_QUEUE_REARM);
5311 }
James Smart2e90f4b2011-12-13 13:22:37 -05005312 }
James Smart1ba981f2014-02-20 09:56:45 -05005313
5314 if (phba->cfg_fof)
5315 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005316}
5317
5318/**
James Smart6d368e52011-05-24 11:44:12 -04005319 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5320 * @phba: Pointer to HBA context object.
5321 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005322 * @extnt_count: buffer to hold port available extent count.
5323 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005324 *
James Smartb76f2dc2011-07-22 18:37:42 -04005325 * This function calls the port and retrievs the number of available
5326 * extents and their size for a particular extent type.
5327 *
5328 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005329 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005330int
James Smart6d368e52011-05-24 11:44:12 -04005331lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5332 uint16_t *extnt_count, uint16_t *extnt_size)
5333{
5334 int rc = 0;
5335 uint32_t length;
5336 uint32_t mbox_tmo;
5337 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5338 LPFC_MBOXQ_t *mbox;
5339
5340 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5341 if (!mbox)
5342 return -ENOMEM;
5343
5344 /* Find out how many extents are available for this resource type */
5345 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5346 sizeof(struct lpfc_sli4_cfg_mhdr));
5347 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5348 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5349 length, LPFC_SLI4_MBX_EMBED);
5350
5351 /* Send an extents count of 0 - the GET doesn't use it. */
5352 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5353 LPFC_SLI4_MBX_EMBED);
5354 if (unlikely(rc)) {
5355 rc = -EIO;
5356 goto err_exit;
5357 }
5358
5359 if (!phba->sli4_hba.intr_enable)
5360 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5361 else {
James Smarta183a152011-10-10 21:32:43 -04005362 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005363 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5364 }
5365 if (unlikely(rc)) {
5366 rc = -EIO;
5367 goto err_exit;
5368 }
5369
5370 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5371 if (bf_get(lpfc_mbox_hdr_status,
5372 &rsrc_info->header.cfg_shdr.response)) {
5373 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5374 "2930 Failed to get resource extents "
5375 "Status 0x%x Add'l Status 0x%x\n",
5376 bf_get(lpfc_mbox_hdr_status,
5377 &rsrc_info->header.cfg_shdr.response),
5378 bf_get(lpfc_mbox_hdr_add_status,
5379 &rsrc_info->header.cfg_shdr.response));
5380 rc = -EIO;
5381 goto err_exit;
5382 }
5383
5384 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5385 &rsrc_info->u.rsp);
5386 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5387 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005388
5389 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5390 "3162 Retrieved extents type-%d from port: count:%d, "
5391 "size:%d\n", type, *extnt_count, *extnt_size);
5392
5393err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005394 mempool_free(mbox, phba->mbox_mem_pool);
5395 return rc;
5396}
5397
5398/**
5399 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5400 * @phba: Pointer to HBA context object.
5401 * @type: The extent type to check.
5402 *
5403 * This function reads the current available extents from the port and checks
5404 * if the extent count or extent size has changed since the last access.
5405 * Callers use this routine post port reset to understand if there is a
5406 * extent reprovisioning requirement.
5407 *
5408 * Returns:
5409 * -Error: error indicates problem.
5410 * 1: Extent count or size has changed.
5411 * 0: No changes.
5412 **/
5413static int
5414lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5415{
5416 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5417 uint16_t size_diff, rsrc_ext_size;
5418 int rc = 0;
5419 struct lpfc_rsrc_blks *rsrc_entry;
5420 struct list_head *rsrc_blk_list = NULL;
5421
5422 size_diff = 0;
5423 curr_ext_cnt = 0;
5424 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5425 &rsrc_ext_cnt,
5426 &rsrc_ext_size);
5427 if (unlikely(rc))
5428 return -EIO;
5429
5430 switch (type) {
5431 case LPFC_RSC_TYPE_FCOE_RPI:
5432 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5433 break;
5434 case LPFC_RSC_TYPE_FCOE_VPI:
5435 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5436 break;
5437 case LPFC_RSC_TYPE_FCOE_XRI:
5438 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5439 break;
5440 case LPFC_RSC_TYPE_FCOE_VFI:
5441 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5442 break;
5443 default:
5444 break;
5445 }
5446
5447 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5448 curr_ext_cnt++;
5449 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5450 size_diff++;
5451 }
5452
5453 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5454 rc = 1;
5455
5456 return rc;
5457}
5458
5459/**
5460 * lpfc_sli4_cfg_post_extnts -
5461 * @phba: Pointer to HBA context object.
5462 * @extnt_cnt - number of available extents.
5463 * @type - the extent type (rpi, xri, vfi, vpi).
5464 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5465 * @mbox - pointer to the caller's allocated mailbox structure.
5466 *
5467 * This function executes the extents allocation request. It also
5468 * takes care of the amount of memory needed to allocate or get the
5469 * allocated extents. It is the caller's responsibility to evaluate
5470 * the response.
5471 *
5472 * Returns:
5473 * -Error: Error value describes the condition found.
5474 * 0: if successful
5475 **/
5476static int
James Smart8a9d2e82012-05-09 21:16:12 -04005477lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005478 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5479{
5480 int rc = 0;
5481 uint32_t req_len;
5482 uint32_t emb_len;
5483 uint32_t alloc_len, mbox_tmo;
5484
5485 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005486 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005487
5488 /*
5489 * Calculate the size of an embedded mailbox. The uint32_t
5490 * accounts for extents-specific word.
5491 */
5492 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5493 sizeof(uint32_t);
5494
5495 /*
5496 * Presume the allocation and response will fit into an embedded
5497 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5498 */
5499 *emb = LPFC_SLI4_MBX_EMBED;
5500 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005501 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005502 sizeof(union lpfc_sli4_cfg_shdr) +
5503 sizeof(uint32_t);
5504 *emb = LPFC_SLI4_MBX_NEMBED;
5505 }
5506
5507 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5508 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5509 req_len, *emb);
5510 if (alloc_len < req_len) {
5511 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005512 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005513 "less than the requested DMA memory "
5514 "size (x%x)\n", alloc_len, req_len);
5515 return -ENOMEM;
5516 }
James Smart8a9d2e82012-05-09 21:16:12 -04005517 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005518 if (unlikely(rc))
5519 return -EIO;
5520
5521 if (!phba->sli4_hba.intr_enable)
5522 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5523 else {
James Smarta183a152011-10-10 21:32:43 -04005524 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005525 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5526 }
5527
5528 if (unlikely(rc))
5529 rc = -EIO;
5530 return rc;
5531}
5532
5533/**
5534 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5535 * @phba: Pointer to HBA context object.
5536 * @type: The resource extent type to allocate.
5537 *
5538 * This function allocates the number of elements for the specified
5539 * resource type.
5540 **/
5541static int
5542lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5543{
5544 bool emb = false;
5545 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5546 uint16_t rsrc_id, rsrc_start, j, k;
5547 uint16_t *ids;
5548 int i, rc;
5549 unsigned long longs;
5550 unsigned long *bmask;
5551 struct lpfc_rsrc_blks *rsrc_blks;
5552 LPFC_MBOXQ_t *mbox;
5553 uint32_t length;
5554 struct lpfc_id_range *id_array = NULL;
5555 void *virtaddr = NULL;
5556 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5557 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5558 struct list_head *ext_blk_list;
5559
5560 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5561 &rsrc_cnt,
5562 &rsrc_size);
5563 if (unlikely(rc))
5564 return -EIO;
5565
5566 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5567 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5568 "3009 No available Resource Extents "
5569 "for resource type 0x%x: Count: 0x%x, "
5570 "Size 0x%x\n", type, rsrc_cnt,
5571 rsrc_size);
5572 return -ENOMEM;
5573 }
5574
James Smart8a9d2e82012-05-09 21:16:12 -04005575 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5576 "2903 Post resource extents type-0x%x: "
5577 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005578
5579 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5580 if (!mbox)
5581 return -ENOMEM;
5582
James Smart8a9d2e82012-05-09 21:16:12 -04005583 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005584 if (unlikely(rc)) {
5585 rc = -EIO;
5586 goto err_exit;
5587 }
5588
5589 /*
5590 * Figure out where the response is located. Then get local pointers
5591 * to the response data. The port does not guarantee to respond to
5592 * all extents counts request so update the local variable with the
5593 * allocated count from the port.
5594 */
5595 if (emb == LPFC_SLI4_MBX_EMBED) {
5596 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5597 id_array = &rsrc_ext->u.rsp.id[0];
5598 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5599 } else {
5600 virtaddr = mbox->sge_array->addr[0];
5601 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5602 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5603 id_array = &n_rsrc->id;
5604 }
5605
5606 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5607 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5608
5609 /*
5610 * Based on the resource size and count, correct the base and max
5611 * resource values.
5612 */
5613 length = sizeof(struct lpfc_rsrc_blks);
5614 switch (type) {
5615 case LPFC_RSC_TYPE_FCOE_RPI:
5616 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5617 sizeof(unsigned long),
5618 GFP_KERNEL);
5619 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5620 rc = -ENOMEM;
5621 goto err_exit;
5622 }
5623 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5624 sizeof(uint16_t),
5625 GFP_KERNEL);
5626 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5627 kfree(phba->sli4_hba.rpi_bmask);
5628 rc = -ENOMEM;
5629 goto err_exit;
5630 }
5631
5632 /*
5633 * The next_rpi was initialized with the maximum available
5634 * count but the port may allocate a smaller number. Catch
5635 * that case and update the next_rpi.
5636 */
5637 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5638
5639 /* Initialize local ptrs for common extent processing later. */
5640 bmask = phba->sli4_hba.rpi_bmask;
5641 ids = phba->sli4_hba.rpi_ids;
5642 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5643 break;
5644 case LPFC_RSC_TYPE_FCOE_VPI:
5645 phba->vpi_bmask = kzalloc(longs *
5646 sizeof(unsigned long),
5647 GFP_KERNEL);
5648 if (unlikely(!phba->vpi_bmask)) {
5649 rc = -ENOMEM;
5650 goto err_exit;
5651 }
5652 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5653 sizeof(uint16_t),
5654 GFP_KERNEL);
5655 if (unlikely(!phba->vpi_ids)) {
5656 kfree(phba->vpi_bmask);
5657 rc = -ENOMEM;
5658 goto err_exit;
5659 }
5660
5661 /* Initialize local ptrs for common extent processing later. */
5662 bmask = phba->vpi_bmask;
5663 ids = phba->vpi_ids;
5664 ext_blk_list = &phba->lpfc_vpi_blk_list;
5665 break;
5666 case LPFC_RSC_TYPE_FCOE_XRI:
5667 phba->sli4_hba.xri_bmask = kzalloc(longs *
5668 sizeof(unsigned long),
5669 GFP_KERNEL);
5670 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5671 rc = -ENOMEM;
5672 goto err_exit;
5673 }
James Smart8a9d2e82012-05-09 21:16:12 -04005674 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005675 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5676 sizeof(uint16_t),
5677 GFP_KERNEL);
5678 if (unlikely(!phba->sli4_hba.xri_ids)) {
5679 kfree(phba->sli4_hba.xri_bmask);
5680 rc = -ENOMEM;
5681 goto err_exit;
5682 }
5683
5684 /* Initialize local ptrs for common extent processing later. */
5685 bmask = phba->sli4_hba.xri_bmask;
5686 ids = phba->sli4_hba.xri_ids;
5687 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5688 break;
5689 case LPFC_RSC_TYPE_FCOE_VFI:
5690 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5691 sizeof(unsigned long),
5692 GFP_KERNEL);
5693 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5694 rc = -ENOMEM;
5695 goto err_exit;
5696 }
5697 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5698 sizeof(uint16_t),
5699 GFP_KERNEL);
5700 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5701 kfree(phba->sli4_hba.vfi_bmask);
5702 rc = -ENOMEM;
5703 goto err_exit;
5704 }
5705
5706 /* Initialize local ptrs for common extent processing later. */
5707 bmask = phba->sli4_hba.vfi_bmask;
5708 ids = phba->sli4_hba.vfi_ids;
5709 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5710 break;
5711 default:
5712 /* Unsupported Opcode. Fail call. */
5713 id_array = NULL;
5714 bmask = NULL;
5715 ids = NULL;
5716 ext_blk_list = NULL;
5717 goto err_exit;
5718 }
5719
5720 /*
5721 * Complete initializing the extent configuration with the
5722 * allocated ids assigned to this function. The bitmask serves
5723 * as an index into the array and manages the available ids. The
5724 * array just stores the ids communicated to the port via the wqes.
5725 */
5726 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5727 if ((i % 2) == 0)
5728 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5729 &id_array[k]);
5730 else
5731 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5732 &id_array[k]);
5733
5734 rsrc_blks = kzalloc(length, GFP_KERNEL);
5735 if (unlikely(!rsrc_blks)) {
5736 rc = -ENOMEM;
5737 kfree(bmask);
5738 kfree(ids);
5739 goto err_exit;
5740 }
5741 rsrc_blks->rsrc_start = rsrc_id;
5742 rsrc_blks->rsrc_size = rsrc_size;
5743 list_add_tail(&rsrc_blks->list, ext_blk_list);
5744 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005745 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005746 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005747 lpfc_sli4_get_iocb_cnt(phba);
5748 phba->sli4_hba.nvme_xri_start =
5749 phba->sli4_hba.scsi_xri_start +
5750 phba->sli4_hba.scsi_xri_max;
5751 }
James Smart6d368e52011-05-24 11:44:12 -04005752
5753 while (rsrc_id < (rsrc_start + rsrc_size)) {
5754 ids[j] = rsrc_id;
5755 rsrc_id++;
5756 j++;
5757 }
5758 /* Entire word processed. Get next word.*/
5759 if ((i % 2) == 1)
5760 k++;
5761 }
5762 err_exit:
5763 lpfc_sli4_mbox_cmd_free(phba, mbox);
5764 return rc;
5765}
5766
James Smart895427b2017-02-12 13:52:30 -08005767
5768
James Smart6d368e52011-05-24 11:44:12 -04005769/**
5770 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5771 * @phba: Pointer to HBA context object.
5772 * @type: the extent's type.
5773 *
5774 * This function deallocates all extents of a particular resource type.
5775 * SLI4 does not allow for deallocating a particular extent range. It
5776 * is the caller's responsibility to release all kernel memory resources.
5777 **/
5778static int
5779lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5780{
5781 int rc;
5782 uint32_t length, mbox_tmo = 0;
5783 LPFC_MBOXQ_t *mbox;
5784 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5785 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5786
5787 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5788 if (!mbox)
5789 return -ENOMEM;
5790
5791 /*
5792 * This function sends an embedded mailbox because it only sends the
5793 * the resource type. All extents of this type are released by the
5794 * port.
5795 */
5796 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5797 sizeof(struct lpfc_sli4_cfg_mhdr));
5798 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5799 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5800 length, LPFC_SLI4_MBX_EMBED);
5801
5802 /* Send an extents count of 0 - the dealloc doesn't use it. */
5803 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5804 LPFC_SLI4_MBX_EMBED);
5805 if (unlikely(rc)) {
5806 rc = -EIO;
5807 goto out_free_mbox;
5808 }
5809 if (!phba->sli4_hba.intr_enable)
5810 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5811 else {
James Smarta183a152011-10-10 21:32:43 -04005812 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005813 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5814 }
5815 if (unlikely(rc)) {
5816 rc = -EIO;
5817 goto out_free_mbox;
5818 }
5819
5820 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5821 if (bf_get(lpfc_mbox_hdr_status,
5822 &dealloc_rsrc->header.cfg_shdr.response)) {
5823 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5824 "2919 Failed to release resource extents "
5825 "for type %d - Status 0x%x Add'l Status 0x%x. "
5826 "Resource memory not released.\n",
5827 type,
5828 bf_get(lpfc_mbox_hdr_status,
5829 &dealloc_rsrc->header.cfg_shdr.response),
5830 bf_get(lpfc_mbox_hdr_add_status,
5831 &dealloc_rsrc->header.cfg_shdr.response));
5832 rc = -EIO;
5833 goto out_free_mbox;
5834 }
5835
5836 /* Release kernel memory resources for the specific type. */
5837 switch (type) {
5838 case LPFC_RSC_TYPE_FCOE_VPI:
5839 kfree(phba->vpi_bmask);
5840 kfree(phba->vpi_ids);
5841 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5842 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5843 &phba->lpfc_vpi_blk_list, list) {
5844 list_del_init(&rsrc_blk->list);
5845 kfree(rsrc_blk);
5846 }
James Smart16a3a202013-04-17 20:14:38 -04005847 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005848 break;
5849 case LPFC_RSC_TYPE_FCOE_XRI:
5850 kfree(phba->sli4_hba.xri_bmask);
5851 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005852 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5853 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5854 list_del_init(&rsrc_blk->list);
5855 kfree(rsrc_blk);
5856 }
5857 break;
5858 case LPFC_RSC_TYPE_FCOE_VFI:
5859 kfree(phba->sli4_hba.vfi_bmask);
5860 kfree(phba->sli4_hba.vfi_ids);
5861 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5862 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5863 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5864 list_del_init(&rsrc_blk->list);
5865 kfree(rsrc_blk);
5866 }
5867 break;
5868 case LPFC_RSC_TYPE_FCOE_RPI:
5869 /* RPI bitmask and physical id array are cleaned up earlier. */
5870 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5871 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5872 list_del_init(&rsrc_blk->list);
5873 kfree(rsrc_blk);
5874 }
5875 break;
5876 default:
5877 break;
5878 }
5879
5880 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5881
5882 out_free_mbox:
5883 mempool_free(mbox, phba->mbox_mem_pool);
5884 return rc;
5885}
5886
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005887static void
James Smart7bdedb32016-07-06 12:36:00 -07005888lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5889 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005890{
James Smart65791f12016-07-06 12:35:56 -07005891 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005892
James Smart65791f12016-07-06 12:35:56 -07005893 len = sizeof(struct lpfc_mbx_set_feature) -
5894 sizeof(struct lpfc_sli4_cfg_mhdr);
5895 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5896 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5897 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005898
James Smart7bdedb32016-07-06 12:36:00 -07005899 switch (feature) {
5900 case LPFC_SET_UE_RECOVERY:
5901 bf_set(lpfc_mbx_set_feature_UER,
5902 &mbox->u.mqe.un.set_feature, 1);
5903 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5904 mbox->u.mqe.un.set_feature.param_len = 8;
5905 break;
5906 case LPFC_SET_MDS_DIAGS:
5907 bf_set(lpfc_mbx_set_feature_mds,
5908 &mbox->u.mqe.un.set_feature, 1);
5909 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5910 &mbox->u.mqe.un.set_feature, 0);
5911 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5912 mbox->u.mqe.un.set_feature.param_len = 8;
5913 break;
James Smart65791f12016-07-06 12:35:56 -07005914 }
James Smart7bdedb32016-07-06 12:36:00 -07005915
5916 return;
James Smart65791f12016-07-06 12:35:56 -07005917}
5918
James Smart6d368e52011-05-24 11:44:12 -04005919/**
5920 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5921 * @phba: Pointer to HBA context object.
5922 *
5923 * This function allocates all SLI4 resource identifiers.
5924 **/
5925int
5926lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5927{
5928 int i, rc, error = 0;
5929 uint16_t count, base;
5930 unsigned long longs;
5931
James Smartff78d8f2011-12-13 13:21:35 -05005932 if (!phba->sli4_hba.rpi_hdrs_in_use)
5933 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005934 if (phba->sli4_hba.extents_in_use) {
5935 /*
5936 * The port supports resource extents. The XRI, VPI, VFI, RPI
5937 * resource extent count must be read and allocated before
5938 * provisioning the resource id arrays.
5939 */
5940 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5941 LPFC_IDX_RSRC_RDY) {
5942 /*
5943 * Extent-based resources are set - the driver could
5944 * be in a port reset. Figure out if any corrective
5945 * actions need to be taken.
5946 */
5947 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5948 LPFC_RSC_TYPE_FCOE_VFI);
5949 if (rc != 0)
5950 error++;
5951 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5952 LPFC_RSC_TYPE_FCOE_VPI);
5953 if (rc != 0)
5954 error++;
5955 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5956 LPFC_RSC_TYPE_FCOE_XRI);
5957 if (rc != 0)
5958 error++;
5959 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5960 LPFC_RSC_TYPE_FCOE_RPI);
5961 if (rc != 0)
5962 error++;
5963
5964 /*
5965 * It's possible that the number of resources
5966 * provided to this port instance changed between
5967 * resets. Detect this condition and reallocate
5968 * resources. Otherwise, there is no action.
5969 */
5970 if (error) {
5971 lpfc_printf_log(phba, KERN_INFO,
5972 LOG_MBOX | LOG_INIT,
5973 "2931 Detected extent resource "
5974 "change. Reallocating all "
5975 "extents.\n");
5976 rc = lpfc_sli4_dealloc_extent(phba,
5977 LPFC_RSC_TYPE_FCOE_VFI);
5978 rc = lpfc_sli4_dealloc_extent(phba,
5979 LPFC_RSC_TYPE_FCOE_VPI);
5980 rc = lpfc_sli4_dealloc_extent(phba,
5981 LPFC_RSC_TYPE_FCOE_XRI);
5982 rc = lpfc_sli4_dealloc_extent(phba,
5983 LPFC_RSC_TYPE_FCOE_RPI);
5984 } else
5985 return 0;
5986 }
5987
5988 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5989 if (unlikely(rc))
5990 goto err_exit;
5991
5992 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5993 if (unlikely(rc))
5994 goto err_exit;
5995
5996 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5997 if (unlikely(rc))
5998 goto err_exit;
5999
6000 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6001 if (unlikely(rc))
6002 goto err_exit;
6003 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6004 LPFC_IDX_RSRC_RDY);
6005 return rc;
6006 } else {
6007 /*
6008 * The port does not support resource extents. The XRI, VPI,
6009 * VFI, RPI resource ids were determined from READ_CONFIG.
6010 * Just allocate the bitmasks and provision the resource id
6011 * arrays. If a port reset is active, the resources don't
6012 * need any action - just exit.
6013 */
6014 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006015 LPFC_IDX_RSRC_RDY) {
6016 lpfc_sli4_dealloc_resource_identifiers(phba);
6017 lpfc_sli4_remove_rpis(phba);
6018 }
James Smart6d368e52011-05-24 11:44:12 -04006019 /* RPIs. */
6020 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006021 if (count <= 0) {
6022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6023 "3279 Invalid provisioning of "
6024 "rpi:%d\n", count);
6025 rc = -EINVAL;
6026 goto err_exit;
6027 }
James Smart6d368e52011-05-24 11:44:12 -04006028 base = phba->sli4_hba.max_cfg_param.rpi_base;
6029 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6030 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6031 sizeof(unsigned long),
6032 GFP_KERNEL);
6033 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6034 rc = -ENOMEM;
6035 goto err_exit;
6036 }
6037 phba->sli4_hba.rpi_ids = kzalloc(count *
6038 sizeof(uint16_t),
6039 GFP_KERNEL);
6040 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6041 rc = -ENOMEM;
6042 goto free_rpi_bmask;
6043 }
6044
6045 for (i = 0; i < count; i++)
6046 phba->sli4_hba.rpi_ids[i] = base + i;
6047
6048 /* VPIs. */
6049 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006050 if (count <= 0) {
6051 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6052 "3280 Invalid provisioning of "
6053 "vpi:%d\n", count);
6054 rc = -EINVAL;
6055 goto free_rpi_ids;
6056 }
James Smart6d368e52011-05-24 11:44:12 -04006057 base = phba->sli4_hba.max_cfg_param.vpi_base;
6058 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6059 phba->vpi_bmask = kzalloc(longs *
6060 sizeof(unsigned long),
6061 GFP_KERNEL);
6062 if (unlikely(!phba->vpi_bmask)) {
6063 rc = -ENOMEM;
6064 goto free_rpi_ids;
6065 }
6066 phba->vpi_ids = kzalloc(count *
6067 sizeof(uint16_t),
6068 GFP_KERNEL);
6069 if (unlikely(!phba->vpi_ids)) {
6070 rc = -ENOMEM;
6071 goto free_vpi_bmask;
6072 }
6073
6074 for (i = 0; i < count; i++)
6075 phba->vpi_ids[i] = base + i;
6076
6077 /* XRIs. */
6078 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006079 if (count <= 0) {
6080 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6081 "3281 Invalid provisioning of "
6082 "xri:%d\n", count);
6083 rc = -EINVAL;
6084 goto free_vpi_ids;
6085 }
James Smart6d368e52011-05-24 11:44:12 -04006086 base = phba->sli4_hba.max_cfg_param.xri_base;
6087 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6088 phba->sli4_hba.xri_bmask = kzalloc(longs *
6089 sizeof(unsigned long),
6090 GFP_KERNEL);
6091 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6092 rc = -ENOMEM;
6093 goto free_vpi_ids;
6094 }
James Smart41899be2012-03-01 22:34:19 -05006095 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006096 phba->sli4_hba.xri_ids = kzalloc(count *
6097 sizeof(uint16_t),
6098 GFP_KERNEL);
6099 if (unlikely(!phba->sli4_hba.xri_ids)) {
6100 rc = -ENOMEM;
6101 goto free_xri_bmask;
6102 }
6103
6104 for (i = 0; i < count; i++)
6105 phba->sli4_hba.xri_ids[i] = base + i;
6106
6107 /* VFIs. */
6108 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006109 if (count <= 0) {
6110 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6111 "3282 Invalid provisioning of "
6112 "vfi:%d\n", count);
6113 rc = -EINVAL;
6114 goto free_xri_ids;
6115 }
James Smart6d368e52011-05-24 11:44:12 -04006116 base = phba->sli4_hba.max_cfg_param.vfi_base;
6117 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6118 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6119 sizeof(unsigned long),
6120 GFP_KERNEL);
6121 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6122 rc = -ENOMEM;
6123 goto free_xri_ids;
6124 }
6125 phba->sli4_hba.vfi_ids = kzalloc(count *
6126 sizeof(uint16_t),
6127 GFP_KERNEL);
6128 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6129 rc = -ENOMEM;
6130 goto free_vfi_bmask;
6131 }
6132
6133 for (i = 0; i < count; i++)
6134 phba->sli4_hba.vfi_ids[i] = base + i;
6135
6136 /*
6137 * Mark all resources ready. An HBA reset doesn't need
6138 * to reset the initialization.
6139 */
6140 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6141 LPFC_IDX_RSRC_RDY);
6142 return 0;
6143 }
6144
6145 free_vfi_bmask:
6146 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006147 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006148 free_xri_ids:
6149 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006150 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006151 free_xri_bmask:
6152 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006153 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006154 free_vpi_ids:
6155 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006156 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006157 free_vpi_bmask:
6158 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006159 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006160 free_rpi_ids:
6161 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006162 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006163 free_rpi_bmask:
6164 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006165 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006166 err_exit:
6167 return rc;
6168}
6169
6170/**
6171 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6172 * @phba: Pointer to HBA context object.
6173 *
6174 * This function allocates the number of elements for the specified
6175 * resource type.
6176 **/
6177int
6178lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6179{
6180 if (phba->sli4_hba.extents_in_use) {
6181 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6182 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6183 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6184 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6185 } else {
6186 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006187 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006188 kfree(phba->vpi_ids);
6189 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6190 kfree(phba->sli4_hba.xri_bmask);
6191 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006192 kfree(phba->sli4_hba.vfi_bmask);
6193 kfree(phba->sli4_hba.vfi_ids);
6194 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6195 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6196 }
6197
6198 return 0;
6199}
6200
6201/**
James Smartb76f2dc2011-07-22 18:37:42 -04006202 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6203 * @phba: Pointer to HBA context object.
6204 * @type: The resource extent type.
6205 * @extnt_count: buffer to hold port extent count response
6206 * @extnt_size: buffer to hold port extent size response.
6207 *
6208 * This function calls the port to read the host allocated extents
6209 * for a particular type.
6210 **/
6211int
6212lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6213 uint16_t *extnt_cnt, uint16_t *extnt_size)
6214{
6215 bool emb;
6216 int rc = 0;
6217 uint16_t curr_blks = 0;
6218 uint32_t req_len, emb_len;
6219 uint32_t alloc_len, mbox_tmo;
6220 struct list_head *blk_list_head;
6221 struct lpfc_rsrc_blks *rsrc_blk;
6222 LPFC_MBOXQ_t *mbox;
6223 void *virtaddr = NULL;
6224 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6225 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6226 union lpfc_sli4_cfg_shdr *shdr;
6227
6228 switch (type) {
6229 case LPFC_RSC_TYPE_FCOE_VPI:
6230 blk_list_head = &phba->lpfc_vpi_blk_list;
6231 break;
6232 case LPFC_RSC_TYPE_FCOE_XRI:
6233 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6234 break;
6235 case LPFC_RSC_TYPE_FCOE_VFI:
6236 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6237 break;
6238 case LPFC_RSC_TYPE_FCOE_RPI:
6239 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6240 break;
6241 default:
6242 return -EIO;
6243 }
6244
6245 /* Count the number of extents currently allocatd for this type. */
6246 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6247 if (curr_blks == 0) {
6248 /*
6249 * The GET_ALLOCATED mailbox does not return the size,
6250 * just the count. The size should be just the size
6251 * stored in the current allocated block and all sizes
6252 * for an extent type are the same so set the return
6253 * value now.
6254 */
6255 *extnt_size = rsrc_blk->rsrc_size;
6256 }
6257 curr_blks++;
6258 }
6259
James Smartb76f2dc2011-07-22 18:37:42 -04006260 /*
6261 * Calculate the size of an embedded mailbox. The uint32_t
6262 * accounts for extents-specific word.
6263 */
6264 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6265 sizeof(uint32_t);
6266
6267 /*
6268 * Presume the allocation and response will fit into an embedded
6269 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6270 */
6271 emb = LPFC_SLI4_MBX_EMBED;
6272 req_len = emb_len;
6273 if (req_len > emb_len) {
6274 req_len = curr_blks * sizeof(uint16_t) +
6275 sizeof(union lpfc_sli4_cfg_shdr) +
6276 sizeof(uint32_t);
6277 emb = LPFC_SLI4_MBX_NEMBED;
6278 }
6279
6280 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6281 if (!mbox)
6282 return -ENOMEM;
6283 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6284
6285 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6286 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6287 req_len, emb);
6288 if (alloc_len < req_len) {
6289 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6290 "2983 Allocated DMA memory size (x%x) is "
6291 "less than the requested DMA memory "
6292 "size (x%x)\n", alloc_len, req_len);
6293 rc = -ENOMEM;
6294 goto err_exit;
6295 }
6296 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6297 if (unlikely(rc)) {
6298 rc = -EIO;
6299 goto err_exit;
6300 }
6301
6302 if (!phba->sli4_hba.intr_enable)
6303 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6304 else {
James Smarta183a152011-10-10 21:32:43 -04006305 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006306 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6307 }
6308
6309 if (unlikely(rc)) {
6310 rc = -EIO;
6311 goto err_exit;
6312 }
6313
6314 /*
6315 * Figure out where the response is located. Then get local pointers
6316 * to the response data. The port does not guarantee to respond to
6317 * all extents counts request so update the local variable with the
6318 * allocated count from the port.
6319 */
6320 if (emb == LPFC_SLI4_MBX_EMBED) {
6321 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6322 shdr = &rsrc_ext->header.cfg_shdr;
6323 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6324 } else {
6325 virtaddr = mbox->sge_array->addr[0];
6326 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6327 shdr = &n_rsrc->cfg_shdr;
6328 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6329 }
6330
6331 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6332 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6333 "2984 Failed to read allocated resources "
6334 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6335 type,
6336 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6337 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6338 rc = -EIO;
6339 goto err_exit;
6340 }
6341 err_exit:
6342 lpfc_sli4_mbox_cmd_free(phba, mbox);
6343 return rc;
6344}
6345
6346/**
James Smart0ef69962017-04-21 16:04:50 -07006347 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006348 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006349 * @pring: Pointer to driver SLI ring object.
6350 * @sgl_list: linked link of sgl buffers to post
6351 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006352 *
James Smart895427b2017-02-12 13:52:30 -08006353 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006354 * repost them to the port by using SGL block post. This is needed after a
6355 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006356 * of buffer sgls which contains contiguous xris and uses the non-embedded
6357 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006358 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6359 * mailbox command for posting.
6360 *
6361 * Returns: 0 = success, non-zero failure.
6362 **/
6363static int
James Smart895427b2017-02-12 13:52:30 -08006364lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6365 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006366{
6367 struct lpfc_sglq *sglq_entry = NULL;
6368 struct lpfc_sglq *sglq_entry_next = NULL;
6369 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006370 int status, total_cnt;
6371 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006372 int last_xritag = NO_XRI;
6373 LIST_HEAD(prep_sgl_list);
6374 LIST_HEAD(blck_sgl_list);
6375 LIST_HEAD(allc_sgl_list);
6376 LIST_HEAD(post_sgl_list);
6377 LIST_HEAD(free_sgl_list);
6378
James Smart38c20672013-03-01 16:37:44 -05006379 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006380 spin_lock(&phba->sli4_hba.sgl_list_lock);
6381 list_splice_init(sgl_list, &allc_sgl_list);
6382 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006383 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006384
James Smart895427b2017-02-12 13:52:30 -08006385 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006386 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6387 &allc_sgl_list, list) {
6388 list_del_init(&sglq_entry->list);
6389 block_cnt++;
6390 if ((last_xritag != NO_XRI) &&
6391 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6392 /* a hole in xri block, form a sgl posting block */
6393 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6394 post_cnt = block_cnt - 1;
6395 /* prepare list for next posting block */
6396 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6397 block_cnt = 1;
6398 } else {
6399 /* prepare list for next posting block */
6400 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6401 /* enough sgls for non-embed sgl mbox command */
6402 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6403 list_splice_init(&prep_sgl_list,
6404 &blck_sgl_list);
6405 post_cnt = block_cnt;
6406 block_cnt = 0;
6407 }
6408 }
6409 num_posted++;
6410
6411 /* keep track of last sgl's xritag */
6412 last_xritag = sglq_entry->sli4_xritag;
6413
James Smart895427b2017-02-12 13:52:30 -08006414 /* end of repost sgl list condition for buffers */
6415 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006416 if (post_cnt == 0) {
6417 list_splice_init(&prep_sgl_list,
6418 &blck_sgl_list);
6419 post_cnt = block_cnt;
6420 } else if (block_cnt == 1) {
6421 status = lpfc_sli4_post_sgl(phba,
6422 sglq_entry->phys, 0,
6423 sglq_entry->sli4_xritag);
6424 if (!status) {
6425 /* successful, put sgl to posted list */
6426 list_add_tail(&sglq_entry->list,
6427 &post_sgl_list);
6428 } else {
6429 /* Failure, put sgl to free list */
6430 lpfc_printf_log(phba, KERN_WARNING,
6431 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006432 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006433 "sgl, xritag:x%x\n",
6434 sglq_entry->sli4_xritag);
6435 list_add_tail(&sglq_entry->list,
6436 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006437 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006438 }
6439 }
6440 }
6441
6442 /* continue until a nembed page worth of sgls */
6443 if (post_cnt == 0)
6444 continue;
6445
James Smart895427b2017-02-12 13:52:30 -08006446 /* post the buffer list sgls as a block */
6447 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6448 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006449
6450 if (!status) {
6451 /* success, put sgl list to posted sgl list */
6452 list_splice_init(&blck_sgl_list, &post_sgl_list);
6453 } else {
6454 /* Failure, put sgl list to free sgl list */
6455 sglq_entry_first = list_first_entry(&blck_sgl_list,
6456 struct lpfc_sglq,
6457 list);
6458 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006459 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006460 "xritag:x%x-x%x\n",
6461 sglq_entry_first->sli4_xritag,
6462 (sglq_entry_first->sli4_xritag +
6463 post_cnt - 1));
6464 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006465 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006466 }
6467
6468 /* don't reset xirtag due to hole in xri block */
6469 if (block_cnt == 0)
6470 last_xritag = NO_XRI;
6471
James Smart895427b2017-02-12 13:52:30 -08006472 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006473 post_cnt = 0;
6474 }
6475
James Smart895427b2017-02-12 13:52:30 -08006476 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006477 lpfc_free_sgl_list(phba, &free_sgl_list);
6478
James Smart895427b2017-02-12 13:52:30 -08006479 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006480 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006481 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006482 spin_lock(&phba->sli4_hba.sgl_list_lock);
6483 list_splice_init(&post_sgl_list, sgl_list);
6484 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006485 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006486 } else {
6487 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006488 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006489 return -EIO;
6490 }
James Smart895427b2017-02-12 13:52:30 -08006491
6492 /* return the number of XRIs actually posted */
6493 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006494}
6495
James Smart61bda8f2016-10-13 15:06:05 -07006496void
6497lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6498{
6499 uint32_t len;
6500
6501 len = sizeof(struct lpfc_mbx_set_host_data) -
6502 sizeof(struct lpfc_sli4_cfg_mhdr);
6503 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6504 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6505 LPFC_SLI4_MBX_EMBED);
6506
6507 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006508 mbox->u.mqe.un.set_host_data.param_len =
6509 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006510 snprintf(mbox->u.mqe.un.set_host_data.data,
6511 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6512 "Linux %s v"LPFC_DRIVER_VERSION,
6513 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6514}
6515
James Smart8a9d2e82012-05-09 21:16:12 -04006516/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006517 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006518 * @phba: Pointer to HBA context object.
6519 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006520 * This function is the main SLI4 device initialization PCI function. This
6521 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006522 * HBA error attention handler code. Caller is not required to hold any
6523 * locks.
6524 **/
6525int
6526lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6527{
James Smart2d7dbc42017-02-12 13:52:35 -08006528 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04006529 LPFC_MBOXQ_t *mboxq;
6530 struct lpfc_mqe *mqe;
6531 uint8_t *vpd;
6532 uint32_t vpd_size;
6533 uint32_t ftr_rsp = 0;
6534 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6535 struct lpfc_vport *vport = phba->pport;
6536 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006537 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006538
6539 /* Perform a PCI function reset to start from clean */
6540 rc = lpfc_pci_function_reset(phba);
6541 if (unlikely(rc))
6542 return -ENODEV;
6543
6544 /* Check the HBA Host Status Register for readyness */
6545 rc = lpfc_sli4_post_status_check(phba);
6546 if (unlikely(rc))
6547 return -ENODEV;
6548 else {
6549 spin_lock_irq(&phba->hbalock);
6550 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6551 spin_unlock_irq(&phba->hbalock);
6552 }
6553
6554 /*
6555 * Allocate a single mailbox container for initializing the
6556 * port.
6557 */
6558 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6559 if (!mboxq)
6560 return -ENOMEM;
6561
James Smartda0436e2009-05-22 14:51:39 -04006562 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006563 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006564 vpd = kzalloc(vpd_size, GFP_KERNEL);
6565 if (!vpd) {
6566 rc = -ENOMEM;
6567 goto out_free_mbox;
6568 }
6569
6570 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006571 if (unlikely(rc)) {
6572 kfree(vpd);
6573 goto out_free_mbox;
6574 }
James Smart572709e2013-07-15 18:32:43 -04006575
James Smartda0436e2009-05-22 14:51:39 -04006576 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006577 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006578 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006579 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006580 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6581 } else {
James Smart76a95d72010-11-20 23:11:48 -05006582 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006583 }
James Smart45ed1192009-10-02 15:17:02 -04006584
6585 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6586 LPFC_DCBX_CEE_MODE)
6587 phba->hba_flag |= HBA_FIP_SUPPORT;
6588 else
6589 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6590
James Smart4f2e66c2012-05-09 21:17:07 -04006591 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6592
James Smartc31098c2011-04-16 11:03:33 -04006593 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006594 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6595 "0376 READ_REV Error. SLI Level %d "
6596 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006597 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006598 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006599 kfree(vpd);
6600 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006601 }
James Smartcd1c8302011-10-10 21:33:25 -04006602
6603 /*
James Smartff78d8f2011-12-13 13:21:35 -05006604 * Continue initialization with default values even if driver failed
6605 * to read FCoE param config regions, only read parameters if the
6606 * board is FCoE
6607 */
6608 if (phba->hba_flag & HBA_FCOE_MODE &&
6609 lpfc_sli4_read_fcoe_params(phba))
6610 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6611 "2570 Failed to read FCoE parameters\n");
6612
6613 /*
James Smartcd1c8302011-10-10 21:33:25 -04006614 * Retrieve sli4 device physical port name, failure of doing it
6615 * is considered as non-fatal.
6616 */
6617 rc = lpfc_sli4_retrieve_pport_name(phba);
6618 if (!rc)
6619 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6620 "3080 Successful retrieving SLI4 device "
6621 "physical port name: %s.\n", phba->Port);
6622
James Smartda0436e2009-05-22 14:51:39 -04006623 /*
6624 * Evaluate the read rev and vpd data. Populate the driver
6625 * state with the results. If this routine fails, the failure
6626 * is not fatal as the driver will use generic values.
6627 */
6628 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6629 if (unlikely(!rc)) {
6630 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6631 "0377 Error %d parsing vpd. "
6632 "Using defaults.\n", rc);
6633 rc = 0;
6634 }
James Smart76a95d72010-11-20 23:11:48 -05006635 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006636
James Smartf1126682009-06-10 17:22:44 -04006637 /* Save information as VPD data */
6638 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6639 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6640 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6641 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6642 &mqe->un.read_rev);
6643 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6644 &mqe->un.read_rev);
6645 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6646 &mqe->un.read_rev);
6647 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6648 &mqe->un.read_rev);
6649 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6650 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6651 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6652 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6653 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6654 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6655 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6656 "(%d):0380 READ_REV Status x%x "
6657 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6658 mboxq->vport ? mboxq->vport->vpi : 0,
6659 bf_get(lpfc_mqe_status, mqe),
6660 phba->vpd.rev.opFwName,
6661 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6662 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006663
James Smart572709e2013-07-15 18:32:43 -04006664 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6665 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6666 if (phba->pport->cfg_lun_queue_depth > rc) {
6667 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6668 "3362 LUN queue depth changed from %d to %d\n",
6669 phba->pport->cfg_lun_queue_depth, rc);
6670 phba->pport->cfg_lun_queue_depth = rc;
6671 }
6672
James Smart65791f12016-07-06 12:35:56 -07006673 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006674 LPFC_SLI_INTF_IF_TYPE_0) {
6675 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6676 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6677 if (rc == MBX_SUCCESS) {
6678 phba->hba_flag |= HBA_RECOVERABLE_UE;
6679 /* Set 1Sec interval to detect UE */
6680 phba->eratt_poll_interval = 1;
6681 phba->sli4_hba.ue_to_sr = bf_get(
6682 lpfc_mbx_set_feature_UESR,
6683 &mboxq->u.mqe.un.set_feature);
6684 phba->sli4_hba.ue_to_rp = bf_get(
6685 lpfc_mbx_set_feature_UERP,
6686 &mboxq->u.mqe.un.set_feature);
6687 }
6688 }
6689
6690 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6691 /* Enable MDS Diagnostics only if the SLI Port supports it */
6692 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6693 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6694 if (rc != MBX_SUCCESS)
6695 phba->mds_diags_support = 0;
6696 }
James Smart572709e2013-07-15 18:32:43 -04006697
James Smartda0436e2009-05-22 14:51:39 -04006698 /*
6699 * Discover the port's supported feature set and match it against the
6700 * hosts requests.
6701 */
6702 lpfc_request_features(phba, mboxq);
6703 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6704 if (unlikely(rc)) {
6705 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006706 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006707 }
6708
6709 /*
6710 * The port must support FCP initiator mode as this is the
6711 * only mode running in the host.
6712 */
6713 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6714 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6715 "0378 No support for fcpi mode.\n");
6716 ftr_rsp++;
6717 }
James Smartfedd3b72011-02-16 12:39:24 -05006718 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6719 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6720 else
6721 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006722 /*
6723 * If the port cannot support the host's requested features
6724 * then turn off the global config parameters to disable the
6725 * feature in the driver. This is not a fatal error.
6726 */
James Smartbf086112011-08-21 21:48:13 -04006727 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6728 if (phba->cfg_enable_bg) {
6729 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6730 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6731 else
6732 ftr_rsp++;
6733 }
James Smartda0436e2009-05-22 14:51:39 -04006734
6735 if (phba->max_vpi && phba->cfg_enable_npiv &&
6736 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6737 ftr_rsp++;
6738
6739 if (ftr_rsp) {
6740 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6741 "0379 Feature Mismatch Data: x%08x %08x "
6742 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6743 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6744 phba->cfg_enable_npiv, phba->max_vpi);
6745 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6746 phba->cfg_enable_bg = 0;
6747 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6748 phba->cfg_enable_npiv = 0;
6749 }
6750
6751 /* These SLI3 features are assumed in SLI4 */
6752 spin_lock_irq(&phba->hbalock);
6753 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6754 spin_unlock_irq(&phba->hbalock);
6755
James Smart6d368e52011-05-24 11:44:12 -04006756 /*
6757 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6758 * calls depends on these resources to complete port setup.
6759 */
6760 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6761 if (rc) {
6762 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6763 "2920 Failed to alloc Resource IDs "
6764 "rc = x%x\n", rc);
6765 goto out_free_mbox;
6766 }
6767
James Smart61bda8f2016-10-13 15:06:05 -07006768 lpfc_set_host_data(phba, mboxq);
6769
6770 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6771 if (rc) {
6772 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6773 "2134 Failed to set host os driver version %x",
6774 rc);
6775 }
6776
James Smartda0436e2009-05-22 14:51:39 -04006777 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006778 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6779 if (rc) {
6780 phba->link_state = LPFC_HBA_ERROR;
6781 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006782 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006783 }
6784
James Smartda0436e2009-05-22 14:51:39 -04006785 mboxq->vport = vport;
6786 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6787 mp = (struct lpfc_dmabuf *) mboxq->context1;
6788 if (rc == MBX_SUCCESS) {
6789 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6790 rc = 0;
6791 }
6792
6793 /*
6794 * This memory was allocated by the lpfc_read_sparam routine. Release
6795 * it to the mbuf pool.
6796 */
6797 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6798 kfree(mp);
6799 mboxq->context1 = NULL;
6800 if (unlikely(rc)) {
6801 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6802 "0382 READ_SPARAM command failed "
6803 "status %d, mbxStatus x%x\n",
6804 rc, bf_get(lpfc_mqe_status, mqe));
6805 phba->link_state = LPFC_HBA_ERROR;
6806 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006807 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006808 }
6809
James Smart05580562011-05-24 11:40:48 -04006810 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006811
6812 /* Update the fc_host data structures with new wwn. */
6813 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6814 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6815
James Smart895427b2017-02-12 13:52:30 -08006816 /* Create all the SLI4 queues */
6817 rc = lpfc_sli4_queue_create(phba);
6818 if (rc) {
6819 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6820 "3089 Failed to allocate queues\n");
6821 rc = -ENODEV;
6822 goto out_free_mbox;
6823 }
6824 /* Set up all the queues to the device */
6825 rc = lpfc_sli4_queue_setup(phba);
6826 if (unlikely(rc)) {
6827 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6828 "0381 Error %d during queue setup.\n ", rc);
6829 goto out_stop_timers;
6830 }
6831 /* Initialize the driver internal SLI layer lists. */
6832 lpfc_sli4_setup(phba);
6833 lpfc_sli4_queue_init(phba);
6834
6835 /* update host els xri-sgl sizes and mappings */
6836 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006837 if (unlikely(rc)) {
6838 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6839 "1400 Failed to update xri-sgl size and "
6840 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08006841 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006842 }
6843
James Smart8a9d2e82012-05-09 21:16:12 -04006844 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08006845 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6846 phba->sli4_hba.els_xri_cnt);
6847 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04006848 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6849 "0582 Error %d during els sgl post "
6850 "operation\n", rc);
6851 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006852 goto out_destroy_queue;
6853 }
6854 phba->sli4_hba.els_xri_cnt = rc;
6855
James Smartf358dd02017-02-12 13:52:34 -08006856 if (phba->nvmet_support) {
6857 /* update host nvmet xri-sgl sizes and mappings */
6858 rc = lpfc_sli4_nvmet_sgl_update(phba);
6859 if (unlikely(rc)) {
6860 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6861 "6308 Failed to update nvmet-sgl size "
6862 "and mapping: %d\n", rc);
6863 goto out_destroy_queue;
6864 }
6865
6866 /* register the nvmet sgl pool to the port */
6867 rc = lpfc_sli4_repost_sgl_list(
6868 phba,
6869 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6870 phba->sli4_hba.nvmet_xri_cnt);
6871 if (unlikely(rc < 0)) {
6872 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6873 "3117 Error %d during nvmet "
6874 "sgl post\n", rc);
6875 rc = -ENODEV;
6876 goto out_destroy_queue;
6877 }
6878 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smartd613b6a2017-02-12 13:52:37 -08006879 lpfc_nvmet_create_targetport(phba);
James Smartf358dd02017-02-12 13:52:34 -08006880 } else {
James Smart895427b2017-02-12 13:52:30 -08006881 /* update host scsi xri-sgl sizes and mappings */
6882 rc = lpfc_sli4_scsi_sgl_update(phba);
6883 if (unlikely(rc)) {
6884 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6885 "6309 Failed to update scsi-sgl size "
6886 "and mapping: %d\n", rc);
6887 goto out_destroy_queue;
6888 }
6889
6890 /* update host nvme xri-sgl sizes and mappings */
6891 rc = lpfc_sli4_nvme_sgl_update(phba);
6892 if (unlikely(rc)) {
6893 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6894 "6082 Failed to update nvme-sgl size "
6895 "and mapping: %d\n", rc);
6896 goto out_destroy_queue;
6897 }
James Smart8a9d2e82012-05-09 21:16:12 -04006898 }
6899
James Smart2d7dbc42017-02-12 13:52:35 -08006900 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
6901
6902 /* Post initial buffers to all RQs created */
6903 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6904 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6905 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6906 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6907 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
6908 rqbp->entry_count = 256;
6909 rqbp->buffer_count = 0;
6910
6911 /* Divide by 4 and round down to multiple of 16 */
6912 rc = (phba->cfg_nvmet_mrq_post >> 2) & 0xfff8;
6913 phba->sli4_hba.nvmet_mrq_hdr[i]->entry_repost = rc;
6914 phba->sli4_hba.nvmet_mrq_data[i]->entry_repost = rc;
6915
6916 lpfc_post_rq_buffer(
6917 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6918 phba->sli4_hba.nvmet_mrq_data[i],
6919 phba->cfg_nvmet_mrq_post);
6920 }
6921 }
6922
James Smart895427b2017-02-12 13:52:30 -08006923 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6924 /* register the allocated scsi sgl pool to the port */
6925 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6926 if (unlikely(rc)) {
6927 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6928 "0383 Error %d during scsi sgl post "
6929 "operation\n", rc);
6930 /* Some Scsi buffers were moved to abort scsi list */
6931 /* A pci function reset will repost them */
6932 rc = -ENODEV;
6933 goto out_destroy_queue;
6934 }
James Smartda0436e2009-05-22 14:51:39 -04006935 }
6936
James Smart01649562017-02-12 13:52:32 -08006937 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
6938 (phba->nvmet_support == 0)) {
6939
6940 /* register the allocated nvme sgl pool to the port */
6941 rc = lpfc_repost_nvme_sgl_list(phba);
6942 if (unlikely(rc)) {
6943 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6944 "6116 Error %d during nvme sgl post "
6945 "operation\n", rc);
6946 /* Some NVME buffers were moved to abort nvme list */
6947 /* A pci function reset will repost them */
6948 rc = -ENODEV;
6949 goto out_destroy_queue;
6950 }
James Smartda0436e2009-05-22 14:51:39 -04006951 }
6952
6953 /* Post the rpi header region to the device. */
6954 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6955 if (unlikely(rc)) {
6956 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6957 "0393 Error %d during rpi post operation\n",
6958 rc);
6959 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006960 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006961 }
James Smart97f2ecf2012-03-01 22:35:23 -05006962 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006963
James Smart895427b2017-02-12 13:52:30 -08006964 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08006965 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08006966 /*
6967 * The FC Port needs to register FCFI (index 0)
6968 */
6969 lpfc_reg_fcfi(phba, mboxq);
6970 mboxq->vport = phba->pport;
6971 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6972 if (rc != MBX_SUCCESS)
6973 goto out_unset_queue;
6974 rc = 0;
6975 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6976 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08006977 } else {
6978 /* We are a NVME Target mode with MRQ > 1 */
6979
6980 /* First register the FCFI */
6981 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
6982 mboxq->vport = phba->pport;
6983 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6984 if (rc != MBX_SUCCESS)
6985 goto out_unset_queue;
6986 rc = 0;
6987 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
6988 &mboxq->u.mqe.un.reg_fcfi_mrq);
6989
6990 /* Next register the MRQs */
6991 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
6992 mboxq->vport = phba->pport;
6993 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6994 if (rc != MBX_SUCCESS)
6995 goto out_unset_queue;
6996 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08006997 }
6998 /* Check if the port is configured to be disabled */
6999 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007000 }
7001
7002 /* Arm the CQs and then EQs on device */
7003 lpfc_sli4_arm_cqeq_intr(phba);
7004
7005 /* Indicate device interrupt mode */
7006 phba->sli4_hba.intr_enable = 1;
7007
7008 /* Allow asynchronous mailbox command to go through */
7009 spin_lock_irq(&phba->hbalock);
7010 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7011 spin_unlock_irq(&phba->hbalock);
7012
7013 /* Post receive buffers to the device */
7014 lpfc_sli4_rb_setup(phba);
7015
James Smartfc2b9892010-02-26 14:15:29 -05007016 /* Reset HBA FCF states after HBA reset */
7017 phba->fcf.fcf_flag = 0;
7018 phba->fcf.current_rec.flag = 0;
7019
James Smartda0436e2009-05-22 14:51:39 -04007020 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007021 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007022 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007023
7024 /* Start heart beat timer */
7025 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007026 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007027 phba->hb_outstanding = 0;
7028 phba->last_completion_time = jiffies;
7029
7030 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007031 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007032 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007033
James Smart75baf692010-06-08 18:31:21 -04007034 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7035 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7036 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7037 if (!rc) {
7038 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7039 "2829 This device supports "
7040 "Advanced Error Reporting (AER)\n");
7041 spin_lock_irq(&phba->hbalock);
7042 phba->hba_flag |= HBA_AER_ENABLED;
7043 spin_unlock_irq(&phba->hbalock);
7044 } else {
7045 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7046 "2830 This device does not support "
7047 "Advanced Error Reporting (AER)\n");
7048 phba->cfg_aer_support = 0;
7049 }
James Smart0a96e972011-07-22 18:37:28 -04007050 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007051 }
7052
James Smartda0436e2009-05-22 14:51:39 -04007053 /*
7054 * The port is ready, set the host's link state to LINK_DOWN
7055 * in preparation for link interrupts.
7056 */
James Smartda0436e2009-05-22 14:51:39 -04007057 spin_lock_irq(&phba->hbalock);
7058 phba->link_state = LPFC_LINK_DOWN;
7059 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007060 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7061 (phba->hba_flag & LINK_DISABLED)) {
7062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7063 "3103 Adapter Link is disabled.\n");
7064 lpfc_down_link(phba, mboxq);
7065 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7066 if (rc != MBX_SUCCESS) {
7067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7068 "3104 Adapter failed to issue "
7069 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7070 goto out_unset_queue;
7071 }
7072 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007073 /* don't perform init_link on SLI4 FC port loopback test */
7074 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7075 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7076 if (rc)
7077 goto out_unset_queue;
7078 }
James Smart5350d872011-10-10 21:33:49 -04007079 }
7080 mempool_free(mboxq, phba->mbox_mem_pool);
7081 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007082out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007083 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007084 lpfc_sli4_queue_unset(phba);
7085out_destroy_queue:
7086 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007087out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007088 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007089out_free_mbox:
7090 mempool_free(mboxq, phba->mbox_mem_pool);
7091 return rc;
7092}
James Smarte59058c2008-08-24 21:49:00 -04007093
7094/**
James Smart3621a712009-04-06 18:47:14 -04007095 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007096 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007097 *
James Smarte59058c2008-08-24 21:49:00 -04007098 * This is the callback function for mailbox timer. The mailbox
7099 * timer is armed when a new mailbox command is issued and the timer
7100 * is deleted when the mailbox complete. The function is called by
7101 * the kernel timer code when a mailbox does not complete within
7102 * expected time. This function wakes up the worker thread to
7103 * process the mailbox timeout and returns. All the processing is
7104 * done by the worker thread function lpfc_mbox_timeout_handler.
7105 **/
dea31012005-04-17 16:05:31 -05007106void
7107lpfc_mbox_timeout(unsigned long ptr)
7108{
James Smart92d7f7b2007-06-17 19:56:38 -05007109 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05007110 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007111 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007112
James Smart2e0fef82007-06-17 19:56:36 -05007113 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007114 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007115 if (!tmo_posted)
7116 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7117 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7118
James Smart5e9d9b82008-06-14 22:52:53 -04007119 if (!tmo_posted)
7120 lpfc_worker_wake_up(phba);
7121 return;
dea31012005-04-17 16:05:31 -05007122}
7123
James Smarte8d3c3b2013-10-10 12:21:30 -04007124/**
7125 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7126 * are pending
7127 * @phba: Pointer to HBA context object.
7128 *
7129 * This function checks if any mailbox completions are present on the mailbox
7130 * completion queue.
7131 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007132static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007133lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7134{
7135
7136 uint32_t idx;
7137 struct lpfc_queue *mcq;
7138 struct lpfc_mcqe *mcqe;
7139 bool pending_completions = false;
7140
7141 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7142 return false;
7143
7144 /* Check for completions on mailbox completion queue */
7145
7146 mcq = phba->sli4_hba.mbx_cq;
7147 idx = mcq->hba_index;
7148 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7149 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7150 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7151 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7152 pending_completions = true;
7153 break;
7154 }
7155 idx = (idx + 1) % mcq->entry_count;
7156 if (mcq->hba_index == idx)
7157 break;
7158 }
7159 return pending_completions;
7160
7161}
7162
7163/**
7164 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7165 * that were missed.
7166 * @phba: Pointer to HBA context object.
7167 *
7168 * For sli4, it is possible to miss an interrupt. As such mbox completions
7169 * maybe missed causing erroneous mailbox timeouts to occur. This function
7170 * checks to see if mbox completions are on the mailbox completion queue
7171 * and will process all the completions associated with the eq for the
7172 * mailbox completion queue.
7173 **/
7174bool
7175lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7176{
7177
7178 uint32_t eqidx;
7179 struct lpfc_queue *fpeq = NULL;
7180 struct lpfc_eqe *eqe;
7181 bool mbox_pending;
7182
7183 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7184 return false;
7185
7186 /* Find the eq associated with the mcq */
7187
7188 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007189 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007190 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7191 phba->sli4_hba.mbx_cq->assoc_qid) {
7192 fpeq = phba->sli4_hba.hba_eq[eqidx];
7193 break;
7194 }
7195 if (!fpeq)
7196 return false;
7197
7198 /* Turn off interrupts from this EQ */
7199
7200 lpfc_sli4_eq_clr_intr(fpeq);
7201
7202 /* Check to see if a mbox completion is pending */
7203
7204 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7205
7206 /*
7207 * If a mbox completion is pending, process all the events on EQ
7208 * associated with the mbox completion queue (this could include
7209 * mailbox commands, async events, els commands, receive queue data
7210 * and fcp commands)
7211 */
7212
7213 if (mbox_pending)
7214 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7215 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7216 fpeq->EQ_processed++;
7217 }
7218
7219 /* Always clear and re-arm the EQ */
7220
7221 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7222
7223 return mbox_pending;
7224
7225}
James Smarte59058c2008-08-24 21:49:00 -04007226
7227/**
James Smart3621a712009-04-06 18:47:14 -04007228 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007229 * @phba: Pointer to HBA context object.
7230 *
7231 * This function is called from worker thread when a mailbox command times out.
7232 * The caller is not required to hold any locks. This function will reset the
7233 * HBA and recover all the pending commands.
7234 **/
dea31012005-04-17 16:05:31 -05007235void
7236lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7237{
James Smart2e0fef82007-06-17 19:56:36 -05007238 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007239 MAILBOX_t *mb = NULL;
7240
James Smart1dcb58e2007-04-25 09:51:30 -04007241 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007242
James Smarte8d3c3b2013-10-10 12:21:30 -04007243 /* If the mailbox completed, process the completion and return */
7244 if (lpfc_sli4_process_missed_mbox_completions(phba))
7245 return;
7246
James Smarteb016562014-09-03 12:58:06 -04007247 if (pmbox != NULL)
7248 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007249 /* Check the pmbox pointer first. There is a race condition
7250 * between the mbox timeout handler getting executed in the
7251 * worklist and the mailbox actually completing. When this
7252 * race condition occurs, the mbox_active will be NULL.
7253 */
7254 spin_lock_irq(&phba->hbalock);
7255 if (pmbox == NULL) {
7256 lpfc_printf_log(phba, KERN_WARNING,
7257 LOG_MBOX | LOG_SLI,
7258 "0353 Active Mailbox cleared - mailbox timeout "
7259 "exiting\n");
7260 spin_unlock_irq(&phba->hbalock);
7261 return;
7262 }
7263
dea31012005-04-17 16:05:31 -05007264 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007265 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007266 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007267 mb->mbxCommand,
7268 phba->pport->port_state,
7269 phba->sli.sli_flag,
7270 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007271 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007272
James Smart1dcb58e2007-04-25 09:51:30 -04007273 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7274 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007275 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007276 */
James Smart2e0fef82007-06-17 19:56:36 -05007277 spin_lock_irq(&phba->pport->work_port_lock);
7278 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7279 spin_unlock_irq(&phba->pport->work_port_lock);
7280 spin_lock_irq(&phba->hbalock);
7281 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007282 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007283 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007284
James Smartdb55fba2014-04-04 13:52:02 -04007285 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007286
7287 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007288 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007289
7290 /* Reset the HBA device */
7291 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007292}
7293
James Smarte59058c2008-08-24 21:49:00 -04007294/**
James Smart3772a992009-05-22 14:50:54 -04007295 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007296 * @phba: Pointer to HBA context object.
7297 * @pmbox: Pointer to mailbox object.
7298 * @flag: Flag indicating how the mailbox need to be processed.
7299 *
7300 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007301 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7302 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007303 * The mailbox command can be submitted in polling mode, in which case
7304 * this function will wait in a polling loop for the completion of the
7305 * mailbox.
7306 * If the mailbox is submitted in no_wait mode (not polling) the
7307 * function will submit the command and returns immediately without waiting
7308 * for the mailbox completion. The no_wait is supported only when HBA
7309 * is in SLI2/SLI3 mode - interrupts are enabled.
7310 * The SLI interface allows only one mailbox pending at a time. If the
7311 * mailbox is issued in polling mode and there is already a mailbox
7312 * pending, then the function will return an error. If the mailbox is issued
7313 * in NO_WAIT mode and there is a mailbox pending already, the function
7314 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7315 * The sli layer owns the mailbox object until the completion of mailbox
7316 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7317 * return codes the caller owns the mailbox command after the return of
7318 * the function.
7319 **/
James Smart3772a992009-05-22 14:50:54 -04007320static int
7321lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7322 uint32_t flag)
dea31012005-04-17 16:05:31 -05007323{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007324 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007325 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007326 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007327 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007328 int i;
James Smart09372822008-01-11 01:52:54 -05007329 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007330 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007331 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007332 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007333 int processing_queue = 0;
7334
7335 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7336 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007337 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007338 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007339 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7340 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7341 return MBX_SUCCESS;
7342 }
James Smart58da1ff2008-04-07 10:15:56 -04007343 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007344 pmbox = lpfc_mbox_get(phba);
7345 if (!pmbox) {
7346 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7347 return MBX_SUCCESS;
7348 }
7349 }
dea31012005-04-17 16:05:31 -05007350
James Smarted957682007-06-17 19:56:37 -05007351 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007352 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007353 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007354 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007355 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007356 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007357 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007358 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007359 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007360 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007361 }
7362 }
7363
Linas Vepstas8d63f372007-02-14 14:28:36 -06007364 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007365 if (unlikely(pci_channel_offline(phba->pcidev))) {
7366 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7367 goto out_not_finished;
7368 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007369
James Smarta257bf92009-04-06 18:48:10 -04007370 /* If HBA has a deferred error attention, fail the iocb. */
7371 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7372 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7373 goto out_not_finished;
7374 }
7375
dea31012005-04-17 16:05:31 -05007376 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007377
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007378 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007379 status = MBX_SUCCESS;
7380
James Smart2e0fef82007-06-17 19:56:36 -05007381 if (phba->link_state == LPFC_HBA_ERROR) {
7382 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007383
7384 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007385 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7386 "(%d):0311 Mailbox command x%x cannot "
7387 "issue Data: x%x x%x\n",
7388 pmbox->vport ? pmbox->vport->vpi : 0,
7389 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007390 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007391 }
7392
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007393 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007394 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7395 !(hc_copy & HC_MBINT_ENA)) {
7396 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7397 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007398 "(%d):2528 Mailbox command x%x cannot "
7399 "issue Data: x%x x%x\n",
7400 pmbox->vport ? pmbox->vport->vpi : 0,
7401 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007402 goto out_not_finished;
7403 }
James Smart92908312006-03-07 15:04:13 -05007404 }
7405
dea31012005-04-17 16:05:31 -05007406 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7407 /* Polling for a mbox command when another one is already active
7408 * is not allowed in SLI. Also, the driver must have established
7409 * SLI2 mode to queue and process multiple mbox commands.
7410 */
7411
7412 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007413 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007414
7415 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007416 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7417 "(%d):2529 Mailbox command x%x "
7418 "cannot issue Data: x%x x%x\n",
7419 pmbox->vport ? pmbox->vport->vpi : 0,
7420 pmbox->u.mb.mbxCommand,
7421 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007422 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007423 }
7424
James Smart3772a992009-05-22 14:50:54 -04007425 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007426 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007427 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007428 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7429 "(%d):2530 Mailbox command x%x "
7430 "cannot issue Data: x%x x%x\n",
7431 pmbox->vport ? pmbox->vport->vpi : 0,
7432 pmbox->u.mb.mbxCommand,
7433 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007434 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007435 }
7436
dea31012005-04-17 16:05:31 -05007437 /* Another mailbox command is still being processed, queue this
7438 * command to be processed later.
7439 */
7440 lpfc_mbox_put(phba, pmbox);
7441
7442 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007443 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007444 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007445 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007446 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007447 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007448 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007449
7450 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007451 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007452
James Smart858c9f62007-06-17 19:56:39 -05007453 if (pmbox->vport) {
7454 lpfc_debugfs_disc_trc(pmbox->vport,
7455 LPFC_DISC_TRC_MBOX_VPORT,
7456 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007457 (uint32_t)mbx->mbxCommand,
7458 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007459 }
7460 else {
7461 lpfc_debugfs_disc_trc(phba->pport,
7462 LPFC_DISC_TRC_MBOX,
7463 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007464 (uint32_t)mbx->mbxCommand,
7465 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007466 }
7467
James Smart2e0fef82007-06-17 19:56:36 -05007468 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007469 }
7470
dea31012005-04-17 16:05:31 -05007471 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7472
7473 /* If we are not polling, we MUST be in SLI2 mode */
7474 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007475 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007476 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007477 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007478 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007479 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007480 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7481 "(%d):2531 Mailbox command x%x "
7482 "cannot issue Data: x%x x%x\n",
7483 pmbox->vport ? pmbox->vport->vpi : 0,
7484 pmbox->u.mb.mbxCommand,
7485 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007486 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007487 }
7488 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007489 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7490 1000);
7491 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007492 }
7493
7494 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007495 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007496 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007497 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007498 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007499 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007500 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007501
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007502 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007503 if (pmbox->vport) {
7504 lpfc_debugfs_disc_trc(pmbox->vport,
7505 LPFC_DISC_TRC_MBOX_VPORT,
7506 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007507 (uint32_t)mbx->mbxCommand,
7508 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007509 }
7510 else {
7511 lpfc_debugfs_disc_trc(phba->pport,
7512 LPFC_DISC_TRC_MBOX,
7513 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007514 (uint32_t)mbx->mbxCommand,
7515 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007516 }
7517 }
7518
dea31012005-04-17 16:05:31 -05007519 psli->slistat.mbox_cmd++;
7520 evtctr = psli->slistat.mbox_event;
7521
7522 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007523 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007524
James Smart3772a992009-05-22 14:50:54 -04007525 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007526 /* Populate mbox extension offset word. */
7527 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007528 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007529 = (uint8_t *)phba->mbox_ext
7530 - (uint8_t *)phba->mbox;
7531 }
7532
7533 /* Copy the mailbox extension data */
7534 if (pmbox->in_ext_byte_len && pmbox->context2) {
7535 lpfc_sli_pcimem_bcopy(pmbox->context2,
7536 (uint8_t *)phba->mbox_ext,
7537 pmbox->in_ext_byte_len);
7538 }
7539 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007540 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007541 } else {
James Smart7a470272010-03-15 11:25:20 -04007542 /* Populate mbox extension offset word. */
7543 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007544 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007545 = MAILBOX_HBA_EXT_OFFSET;
7546
7547 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007548 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007549 lpfc_memcpy_to_slim(phba->MBslimaddr +
7550 MAILBOX_HBA_EXT_OFFSET,
7551 pmbox->context2, pmbox->in_ext_byte_len);
7552
James Smart895427b2017-02-12 13:52:30 -08007553 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007554 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007555 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7556 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007557
7558 /* First copy mbox command data to HBA SLIM, skip past first
7559 word */
7560 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007561 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007562 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7563
7564 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007565 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007566 to_slim = phba->MBslimaddr;
7567 writel(ldata, to_slim);
7568 readl(to_slim); /* flush */
7569
James Smart895427b2017-02-12 13:52:30 -08007570 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007571 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007572 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007573 }
7574
7575 wmb();
dea31012005-04-17 16:05:31 -05007576
7577 switch (flag) {
7578 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007579 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007580 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007581 /* Interrupt board to do it */
7582 writel(CA_MBATT, phba->CAregaddr);
7583 readl(phba->CAregaddr); /* flush */
7584 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007585 break;
7586
7587 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007588 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007589 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007590 /* Interrupt board to do it */
7591 writel(CA_MBATT, phba->CAregaddr);
7592 readl(phba->CAregaddr); /* flush */
7593
James Smart3772a992009-05-22 14:50:54 -04007594 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007595 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007596 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007597 word0 = le32_to_cpu(word0);
7598 } else {
7599 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007600 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7601 spin_unlock_irqrestore(&phba->hbalock,
7602 drvr_flag);
7603 goto out_not_finished;
7604 }
dea31012005-04-17 16:05:31 -05007605 }
7606
7607 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007608 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7609 spin_unlock_irqrestore(&phba->hbalock,
7610 drvr_flag);
7611 goto out_not_finished;
7612 }
James Smarta183a152011-10-10 21:32:43 -04007613 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7614 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007615 i = 0;
dea31012005-04-17 16:05:31 -05007616 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007617 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7618 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007619 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007620 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007621 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007622 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007623 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007624 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007625 }
7626
7627 /* Check if we took a mbox interrupt while we were
7628 polling */
7629 if (((word0 & OWN_CHIP) != OWN_CHIP)
7630 && (evtctr != psli->slistat.mbox_event))
7631 break;
7632
James Smart09372822008-01-11 01:52:54 -05007633 if (i++ > 10) {
7634 spin_unlock_irqrestore(&phba->hbalock,
7635 drvr_flag);
7636 msleep(1);
7637 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7638 }
dea31012005-04-17 16:05:31 -05007639
James Smart3772a992009-05-22 14:50:54 -04007640 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007641 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007642 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007643 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007644 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007645 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007646 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007647 /* Check real SLIM for any errors */
7648 slimword0 = readl(phba->MBslimaddr);
7649 slimmb = (MAILBOX_t *) & slimword0;
7650 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7651 && slimmb->mbxStatus) {
7652 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007653 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007654 word0 = slimword0;
7655 }
7656 }
7657 } else {
7658 /* First copy command data */
7659 word0 = readl(phba->MBslimaddr);
7660 }
7661 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007662 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7663 spin_unlock_irqrestore(&phba->hbalock,
7664 drvr_flag);
7665 goto out_not_finished;
7666 }
dea31012005-04-17 16:05:31 -05007667 }
7668
James Smart3772a992009-05-22 14:50:54 -04007669 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007670 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007671 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7672 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007673 /* Copy the mailbox extension data */
7674 if (pmbox->out_ext_byte_len && pmbox->context2) {
7675 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7676 pmbox->context2,
7677 pmbox->out_ext_byte_len);
7678 }
dea31012005-04-17 16:05:31 -05007679 } else {
7680 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007681 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007682 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007683 /* Copy the mailbox extension data */
7684 if (pmbox->out_ext_byte_len && pmbox->context2) {
7685 lpfc_memcpy_from_slim(pmbox->context2,
7686 phba->MBslimaddr +
7687 MAILBOX_HBA_EXT_OFFSET,
7688 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007689 }
7690 }
7691
7692 writel(HA_MBATT, phba->HAregaddr);
7693 readl(phba->HAregaddr); /* flush */
7694
7695 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007696 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007697 }
7698
James Smart2e0fef82007-06-17 19:56:36 -05007699 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7700 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007701
7702out_not_finished:
7703 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007704 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007705 lpfc_mbox_cmpl_put(phba, pmbox);
7706 }
7707 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007708}
7709
James Smarte59058c2008-08-24 21:49:00 -04007710/**
James Smartf1126682009-06-10 17:22:44 -04007711 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7712 * @phba: Pointer to HBA context object.
7713 *
7714 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7715 * the driver internal pending mailbox queue. It will then try to wait out the
7716 * possible outstanding mailbox command before return.
7717 *
7718 * Returns:
7719 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7720 * the outstanding mailbox command timed out.
7721 **/
7722static int
7723lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7724{
7725 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007726 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007727 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007728
7729 /* Mark the asynchronous mailbox command posting as blocked */
7730 spin_lock_irq(&phba->hbalock);
7731 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007732 /* Determine how long we might wait for the active mailbox
7733 * command to be gracefully completed by firmware.
7734 */
James Smarta183a152011-10-10 21:32:43 -04007735 if (phba->sli.mbox_active)
7736 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7737 phba->sli.mbox_active) *
7738 1000) + jiffies;
7739 spin_unlock_irq(&phba->hbalock);
7740
James Smarte8d3c3b2013-10-10 12:21:30 -04007741 /* Make sure the mailbox is really active */
7742 if (timeout)
7743 lpfc_sli4_process_missed_mbox_completions(phba);
7744
James Smartf1126682009-06-10 17:22:44 -04007745 /* Wait for the outstnading mailbox command to complete */
7746 while (phba->sli.mbox_active) {
7747 /* Check active mailbox complete status every 2ms */
7748 msleep(2);
7749 if (time_after(jiffies, timeout)) {
7750 /* Timeout, marked the outstanding cmd not complete */
7751 rc = 1;
7752 break;
7753 }
7754 }
7755
7756 /* Can not cleanly block async mailbox command, fails it */
7757 if (rc) {
7758 spin_lock_irq(&phba->hbalock);
7759 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7760 spin_unlock_irq(&phba->hbalock);
7761 }
7762 return rc;
7763}
7764
7765/**
7766 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7767 * @phba: Pointer to HBA context object.
7768 *
7769 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7770 * commands from the driver internal pending mailbox queue. It makes sure
7771 * that there is no outstanding mailbox command before resuming posting
7772 * asynchronous mailbox commands. If, for any reason, there is outstanding
7773 * mailbox command, it will try to wait it out before resuming asynchronous
7774 * mailbox command posting.
7775 **/
7776static void
7777lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7778{
7779 struct lpfc_sli *psli = &phba->sli;
7780
7781 spin_lock_irq(&phba->hbalock);
7782 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7783 /* Asynchronous mailbox posting is not blocked, do nothing */
7784 spin_unlock_irq(&phba->hbalock);
7785 return;
7786 }
7787
7788 /* Outstanding synchronous mailbox command is guaranteed to be done,
7789 * successful or timeout, after timing-out the outstanding mailbox
7790 * command shall always be removed, so just unblock posting async
7791 * mailbox command and resume
7792 */
7793 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7794 spin_unlock_irq(&phba->hbalock);
7795
7796 /* wake up worker thread to post asynchronlous mailbox command */
7797 lpfc_worker_wake_up(phba);
7798}
7799
7800/**
James Smart2d843ed2012-09-29 11:29:06 -04007801 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7802 * @phba: Pointer to HBA context object.
7803 * @mboxq: Pointer to mailbox object.
7804 *
7805 * The function waits for the bootstrap mailbox register ready bit from
7806 * port for twice the regular mailbox command timeout value.
7807 *
7808 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7809 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7810 **/
7811static int
7812lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7813{
7814 uint32_t db_ready;
7815 unsigned long timeout;
7816 struct lpfc_register bmbx_reg;
7817
7818 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7819 * 1000) + jiffies;
7820
7821 do {
7822 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7823 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7824 if (!db_ready)
7825 msleep(2);
7826
7827 if (time_after(jiffies, timeout))
7828 return MBXERR_ERROR;
7829 } while (!db_ready);
7830
7831 return 0;
7832}
7833
7834/**
James Smartda0436e2009-05-22 14:51:39 -04007835 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7836 * @phba: Pointer to HBA context object.
7837 * @mboxq: Pointer to mailbox object.
7838 *
7839 * The function posts a mailbox to the port. The mailbox is expected
7840 * to be comletely filled in and ready for the port to operate on it.
7841 * This routine executes a synchronous completion operation on the
7842 * mailbox by polling for its completion.
7843 *
7844 * The caller must not be holding any locks when calling this routine.
7845 *
7846 * Returns:
7847 * MBX_SUCCESS - mailbox posted successfully
7848 * Any of the MBX error values.
7849 **/
7850static int
7851lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7852{
7853 int rc = MBX_SUCCESS;
7854 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007855 uint32_t mcqe_status;
7856 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007857 struct lpfc_sli *psli = &phba->sli;
7858 struct lpfc_mqe *mb = &mboxq->u.mqe;
7859 struct lpfc_bmbx_create *mbox_rgn;
7860 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007861
7862 /*
7863 * Only one mailbox can be active to the bootstrap mailbox region
7864 * at a time and there is no queueing provided.
7865 */
7866 spin_lock_irqsave(&phba->hbalock, iflag);
7867 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7868 spin_unlock_irqrestore(&phba->hbalock, iflag);
7869 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007870 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007871 "cannot issue Data: x%x x%x\n",
7872 mboxq->vport ? mboxq->vport->vpi : 0,
7873 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007874 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7875 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007876 psli->sli_flag, MBX_POLL);
7877 return MBXERR_ERROR;
7878 }
7879 /* The server grabs the token and owns it until release */
7880 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7881 phba->sli.mbox_active = mboxq;
7882 spin_unlock_irqrestore(&phba->hbalock, iflag);
7883
James Smart2d843ed2012-09-29 11:29:06 -04007884 /* wait for bootstrap mbox register for readyness */
7885 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7886 if (rc)
7887 goto exit;
7888
James Smartda0436e2009-05-22 14:51:39 -04007889 /*
7890 * Initialize the bootstrap memory region to avoid stale data areas
7891 * in the mailbox post. Then copy the caller's mailbox contents to
7892 * the bmbx mailbox region.
7893 */
7894 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7895 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7896 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7897 sizeof(struct lpfc_mqe));
7898
7899 /* Post the high mailbox dma address to the port and wait for ready. */
7900 dma_address = &phba->sli4_hba.bmbx.dma_address;
7901 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7902
James Smart2d843ed2012-09-29 11:29:06 -04007903 /* wait for bootstrap mbox register for hi-address write done */
7904 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7905 if (rc)
7906 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007907
7908 /* Post the low mailbox dma address to the port. */
7909 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007910
James Smart2d843ed2012-09-29 11:29:06 -04007911 /* wait for bootstrap mbox register for low address write done */
7912 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7913 if (rc)
7914 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007915
7916 /*
7917 * Read the CQ to ensure the mailbox has completed.
7918 * If so, update the mailbox status so that the upper layers
7919 * can complete the request normally.
7920 */
7921 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7922 sizeof(struct lpfc_mqe));
7923 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7924 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7925 sizeof(struct lpfc_mcqe));
7926 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007927 /*
7928 * When the CQE status indicates a failure and the mailbox status
7929 * indicates success then copy the CQE status into the mailbox status
7930 * (and prefix it with x4000).
7931 */
James Smartda0436e2009-05-22 14:51:39 -04007932 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007933 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7934 bf_set(lpfc_mqe_status, mb,
7935 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007936 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007937 } else
7938 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007939
7940 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007941 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007942 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7943 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007944 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7945 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7946 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007947 bf_get(lpfc_mqe_status, mb),
7948 mb->un.mb_words[0], mb->un.mb_words[1],
7949 mb->un.mb_words[2], mb->un.mb_words[3],
7950 mb->un.mb_words[4], mb->un.mb_words[5],
7951 mb->un.mb_words[6], mb->un.mb_words[7],
7952 mb->un.mb_words[8], mb->un.mb_words[9],
7953 mb->un.mb_words[10], mb->un.mb_words[11],
7954 mb->un.mb_words[12], mboxq->mcqe.word0,
7955 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7956 mboxq->mcqe.trailer);
7957exit:
7958 /* We are holding the token, no needed for lock when release */
7959 spin_lock_irqsave(&phba->hbalock, iflag);
7960 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7961 phba->sli.mbox_active = NULL;
7962 spin_unlock_irqrestore(&phba->hbalock, iflag);
7963 return rc;
7964}
7965
7966/**
7967 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7968 * @phba: Pointer to HBA context object.
7969 * @pmbox: Pointer to mailbox object.
7970 * @flag: Flag indicating how the mailbox need to be processed.
7971 *
7972 * This function is called by discovery code and HBA management code to submit
7973 * a mailbox command to firmware with SLI-4 interface spec.
7974 *
7975 * Return codes the caller owns the mailbox command after the return of the
7976 * function.
7977 **/
7978static int
7979lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7980 uint32_t flag)
7981{
7982 struct lpfc_sli *psli = &phba->sli;
7983 unsigned long iflags;
7984 int rc;
7985
James Smartb76f2dc2011-07-22 18:37:42 -04007986 /* dump from issue mailbox command if setup */
7987 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7988
James Smart8fa38512009-07-19 10:01:03 -04007989 rc = lpfc_mbox_dev_check(phba);
7990 if (unlikely(rc)) {
7991 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007992 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007993 "cannot issue Data: x%x x%x\n",
7994 mboxq->vport ? mboxq->vport->vpi : 0,
7995 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007996 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7997 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007998 psli->sli_flag, flag);
7999 goto out_not_finished;
8000 }
8001
James Smartda0436e2009-05-22 14:51:39 -04008002 /* Detect polling mode and jump to a handler */
8003 if (!phba->sli4_hba.intr_enable) {
8004 if (flag == MBX_POLL)
8005 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8006 else
8007 rc = -EIO;
8008 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008009 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008010 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008011 "(x%x/x%x) failure: "
8012 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8013 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008014 mboxq->vport ? mboxq->vport->vpi : 0,
8015 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008016 lpfc_sli_config_mbox_subsys_get(phba,
8017 mboxq),
8018 lpfc_sli_config_mbox_opcode_get(phba,
8019 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008020 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8021 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8022 bf_get(lpfc_mcqe_ext_status,
8023 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008024 psli->sli_flag, flag);
8025 return rc;
8026 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008027 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8028 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04008029 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04008030 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008031 mboxq->vport ? mboxq->vport->vpi : 0,
8032 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008033 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8034 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008035 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008036 /* Try to block the asynchronous mailbox posting */
8037 rc = lpfc_sli4_async_mbox_block(phba);
8038 if (!rc) {
8039 /* Successfully blocked, now issue sync mbox cmd */
8040 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8041 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008042 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008043 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008044 "(%d):2597 Sync Mailbox command "
8045 "x%x (x%x/x%x) failure: "
8046 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8047 "Data: x%x x%x\n,",
8048 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008049 mboxq->u.mb.mbxCommand,
8050 lpfc_sli_config_mbox_subsys_get(phba,
8051 mboxq),
8052 lpfc_sli_config_mbox_opcode_get(phba,
8053 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008054 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8055 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8056 bf_get(lpfc_mcqe_ext_status,
8057 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008058 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008059 /* Unblock the async mailbox posting afterward */
8060 lpfc_sli4_async_mbox_unblock(phba);
8061 }
8062 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008063 }
8064
8065 /* Now, interrupt mode asynchrous mailbox command */
8066 rc = lpfc_mbox_cmd_check(phba, mboxq);
8067 if (rc) {
8068 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008069 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008070 "cannot issue Data: x%x x%x\n",
8071 mboxq->vport ? mboxq->vport->vpi : 0,
8072 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008073 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8074 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008075 psli->sli_flag, flag);
8076 goto out_not_finished;
8077 }
James Smartda0436e2009-05-22 14:51:39 -04008078
8079 /* Put the mailbox command to the driver internal FIFO */
8080 psli->slistat.mbox_busy++;
8081 spin_lock_irqsave(&phba->hbalock, iflags);
8082 lpfc_mbox_put(phba, mboxq);
8083 spin_unlock_irqrestore(&phba->hbalock, iflags);
8084 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8085 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008086 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008087 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8088 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008089 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8090 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008091 phba->pport->port_state,
8092 psli->sli_flag, MBX_NOWAIT);
8093 /* Wake up worker thread to transport mailbox command from head */
8094 lpfc_worker_wake_up(phba);
8095
8096 return MBX_BUSY;
8097
8098out_not_finished:
8099 return MBX_NOT_FINISHED;
8100}
8101
8102/**
8103 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8104 * @phba: Pointer to HBA context object.
8105 *
8106 * This function is called by worker thread to send a mailbox command to
8107 * SLI4 HBA firmware.
8108 *
8109 **/
8110int
8111lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8112{
8113 struct lpfc_sli *psli = &phba->sli;
8114 LPFC_MBOXQ_t *mboxq;
8115 int rc = MBX_SUCCESS;
8116 unsigned long iflags;
8117 struct lpfc_mqe *mqe;
8118 uint32_t mbx_cmnd;
8119
8120 /* Check interrupt mode before post async mailbox command */
8121 if (unlikely(!phba->sli4_hba.intr_enable))
8122 return MBX_NOT_FINISHED;
8123
8124 /* Check for mailbox command service token */
8125 spin_lock_irqsave(&phba->hbalock, iflags);
8126 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8127 spin_unlock_irqrestore(&phba->hbalock, iflags);
8128 return MBX_NOT_FINISHED;
8129 }
8130 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8131 spin_unlock_irqrestore(&phba->hbalock, iflags);
8132 return MBX_NOT_FINISHED;
8133 }
8134 if (unlikely(phba->sli.mbox_active)) {
8135 spin_unlock_irqrestore(&phba->hbalock, iflags);
8136 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8137 "0384 There is pending active mailbox cmd\n");
8138 return MBX_NOT_FINISHED;
8139 }
8140 /* Take the mailbox command service token */
8141 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8142
8143 /* Get the next mailbox command from head of queue */
8144 mboxq = lpfc_mbox_get(phba);
8145
8146 /* If no more mailbox command waiting for post, we're done */
8147 if (!mboxq) {
8148 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8149 spin_unlock_irqrestore(&phba->hbalock, iflags);
8150 return MBX_SUCCESS;
8151 }
8152 phba->sli.mbox_active = mboxq;
8153 spin_unlock_irqrestore(&phba->hbalock, iflags);
8154
8155 /* Check device readiness for posting mailbox command */
8156 rc = lpfc_mbox_dev_check(phba);
8157 if (unlikely(rc))
8158 /* Driver clean routine will clean up pending mailbox */
8159 goto out_not_finished;
8160
8161 /* Prepare the mbox command to be posted */
8162 mqe = &mboxq->u.mqe;
8163 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8164
8165 /* Start timer for the mbox_tmo and log some mailbox post messages */
8166 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008167 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008168
8169 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008170 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008171 "x%x x%x\n",
8172 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008173 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8174 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008175 phba->pport->port_state, psli->sli_flag);
8176
8177 if (mbx_cmnd != MBX_HEARTBEAT) {
8178 if (mboxq->vport) {
8179 lpfc_debugfs_disc_trc(mboxq->vport,
8180 LPFC_DISC_TRC_MBOX_VPORT,
8181 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8182 mbx_cmnd, mqe->un.mb_words[0],
8183 mqe->un.mb_words[1]);
8184 } else {
8185 lpfc_debugfs_disc_trc(phba->pport,
8186 LPFC_DISC_TRC_MBOX,
8187 "MBOX Send: cmd:x%x mb:x%x x%x",
8188 mbx_cmnd, mqe->un.mb_words[0],
8189 mqe->un.mb_words[1]);
8190 }
8191 }
8192 psli->slistat.mbox_cmd++;
8193
8194 /* Post the mailbox command to the port */
8195 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8196 if (rc != MBX_SUCCESS) {
8197 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008198 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008199 "cannot issue Data: x%x x%x\n",
8200 mboxq->vport ? mboxq->vport->vpi : 0,
8201 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008202 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8203 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008204 psli->sli_flag, MBX_NOWAIT);
8205 goto out_not_finished;
8206 }
8207
8208 return rc;
8209
8210out_not_finished:
8211 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008212 if (phba->sli.mbox_active) {
8213 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8214 __lpfc_mbox_cmpl_put(phba, mboxq);
8215 /* Release the token */
8216 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8217 phba->sli.mbox_active = NULL;
8218 }
James Smartda0436e2009-05-22 14:51:39 -04008219 spin_unlock_irqrestore(&phba->hbalock, iflags);
8220
8221 return MBX_NOT_FINISHED;
8222}
8223
8224/**
8225 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8226 * @phba: Pointer to HBA context object.
8227 * @pmbox: Pointer to mailbox object.
8228 * @flag: Flag indicating how the mailbox need to be processed.
8229 *
8230 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8231 * the API jump table function pointer from the lpfc_hba struct.
8232 *
8233 * Return codes the caller owns the mailbox command after the return of the
8234 * function.
8235 **/
8236int
8237lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8238{
8239 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8240}
8241
8242/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008243 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008244 * @phba: The hba struct for which this call is being executed.
8245 * @dev_grp: The HBA PCI-Device group number.
8246 *
8247 * This routine sets up the mbox interface API function jump table in @phba
8248 * struct.
8249 * Returns: 0 - success, -ENODEV - failure.
8250 **/
8251int
8252lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8253{
8254
8255 switch (dev_grp) {
8256 case LPFC_PCI_DEV_LP:
8257 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8258 phba->lpfc_sli_handle_slow_ring_event =
8259 lpfc_sli_handle_slow_ring_event_s3;
8260 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8261 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8262 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8263 break;
8264 case LPFC_PCI_DEV_OC:
8265 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8266 phba->lpfc_sli_handle_slow_ring_event =
8267 lpfc_sli_handle_slow_ring_event_s4;
8268 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8269 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8270 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8271 break;
8272 default:
8273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8274 "1420 Invalid HBA PCI-device group: 0x%x\n",
8275 dev_grp);
8276 return -ENODEV;
8277 break;
8278 }
8279 return 0;
8280}
8281
8282/**
James Smart3621a712009-04-06 18:47:14 -04008283 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008284 * @phba: Pointer to HBA context object.
8285 * @pring: Pointer to driver SLI ring object.
8286 * @piocb: Pointer to address of newly added command iocb.
8287 *
8288 * This function is called with hbalock held to add a command
8289 * iocb to the txq when SLI layer cannot submit the command iocb
8290 * to the ring.
8291 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008292void
James Smart92d7f7b2007-06-17 19:56:38 -05008293__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008294 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008295{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008296 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008297 /* Insert the caller's iocb in the txq tail for later processing. */
8298 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008299}
8300
James Smarte59058c2008-08-24 21:49:00 -04008301/**
James Smart3621a712009-04-06 18:47:14 -04008302 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008303 * @phba: Pointer to HBA context object.
8304 * @pring: Pointer to driver SLI ring object.
8305 * @piocb: Pointer to address of newly added command iocb.
8306 *
8307 * This function is called with hbalock held before a new
8308 * iocb is submitted to the firmware. This function checks
8309 * txq to flush the iocbs in txq to Firmware before
8310 * submitting new iocbs to the Firmware.
8311 * If there are iocbs in the txq which need to be submitted
8312 * to firmware, lpfc_sli_next_iocb returns the first element
8313 * of the txq after dequeuing it from txq.
8314 * If there is no iocb in the txq then the function will return
8315 * *piocb and *piocb is set to NULL. Caller needs to check
8316 * *piocb to find if there are more commands in the txq.
8317 **/
dea31012005-04-17 16:05:31 -05008318static struct lpfc_iocbq *
8319lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008320 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008321{
8322 struct lpfc_iocbq * nextiocb;
8323
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008324 lockdep_assert_held(&phba->hbalock);
8325
dea31012005-04-17 16:05:31 -05008326 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8327 if (!nextiocb) {
8328 nextiocb = *piocb;
8329 *piocb = NULL;
8330 }
8331
8332 return nextiocb;
8333}
8334
James Smarte59058c2008-08-24 21:49:00 -04008335/**
James Smart3772a992009-05-22 14:50:54 -04008336 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008337 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008338 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008339 * @piocb: Pointer to command iocb.
8340 * @flag: Flag indicating if this command can be put into txq.
8341 *
James Smart3772a992009-05-22 14:50:54 -04008342 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8343 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8344 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8345 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8346 * this function allows only iocbs for posting buffers. This function finds
8347 * next available slot in the command ring and posts the command to the
8348 * available slot and writes the port attention register to request HBA start
8349 * processing new iocb. If there is no slot available in the ring and
8350 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8351 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008352 *
James Smart3772a992009-05-22 14:50:54 -04008353 * This function is called with hbalock held. The function will return success
8354 * after it successfully submit the iocb to firmware or after adding to the
8355 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008356 **/
James Smart98c9ea52007-10-27 13:37:33 -04008357static int
James Smart3772a992009-05-22 14:50:54 -04008358__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008359 struct lpfc_iocbq *piocb, uint32_t flag)
8360{
8361 struct lpfc_iocbq *nextiocb;
8362 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008363 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008364
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008365 lockdep_assert_held(&phba->hbalock);
8366
James Smart92d7f7b2007-06-17 19:56:38 -05008367 if (piocb->iocb_cmpl && (!piocb->vport) &&
8368 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8369 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8370 lpfc_printf_log(phba, KERN_ERR,
8371 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008372 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008373 piocb->iocb.ulpCommand);
8374 dump_stack();
8375 return IOCB_ERROR;
8376 }
8377
8378
Linas Vepstas8d63f372007-02-14 14:28:36 -06008379 /* If the PCI channel is in offline state, do not post iocbs. */
8380 if (unlikely(pci_channel_offline(phba->pcidev)))
8381 return IOCB_ERROR;
8382
James Smarta257bf92009-04-06 18:48:10 -04008383 /* If HBA has a deferred error attention, fail the iocb. */
8384 if (unlikely(phba->hba_flag & DEFER_ERATT))
8385 return IOCB_ERROR;
8386
dea31012005-04-17 16:05:31 -05008387 /*
8388 * We should never get an IOCB if we are in a < LINK_DOWN state
8389 */
James Smart2e0fef82007-06-17 19:56:36 -05008390 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008391 return IOCB_ERROR;
8392
8393 /*
8394 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008395 * outstanding event.
dea31012005-04-17 16:05:31 -05008396 */
James Smart0b727fe2007-10-27 13:37:25 -04008397 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008398 goto iocb_busy;
8399
James Smart2e0fef82007-06-17 19:56:36 -05008400 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008401 /*
James Smart2680eea2007-04-25 09:52:55 -04008402 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008403 * can be issued if the link is not up.
8404 */
8405 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008406 case CMD_GEN_REQUEST64_CR:
8407 case CMD_GEN_REQUEST64_CX:
8408 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8409 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008410 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008411 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8412 MENLO_TRANSPORT_TYPE))
8413
8414 goto iocb_busy;
8415 break;
dea31012005-04-17 16:05:31 -05008416 case CMD_QUE_RING_BUF_CN:
8417 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008418 /*
8419 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8420 * completion, iocb_cmpl MUST be 0.
8421 */
8422 if (piocb->iocb_cmpl)
8423 piocb->iocb_cmpl = NULL;
8424 /*FALLTHROUGH*/
8425 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008426 case CMD_CLOSE_XRI_CN:
8427 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008428 break;
8429 default:
8430 goto iocb_busy;
8431 }
8432
8433 /*
8434 * For FCP commands, we must be in a state where we can process link
8435 * attention events.
8436 */
James Smart895427b2017-02-12 13:52:30 -08008437 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008438 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008439 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008440 }
dea31012005-04-17 16:05:31 -05008441
dea31012005-04-17 16:05:31 -05008442 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8443 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8444 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8445
8446 if (iocb)
8447 lpfc_sli_update_ring(phba, pring);
8448 else
8449 lpfc_sli_update_full_ring(phba, pring);
8450
8451 if (!piocb)
8452 return IOCB_SUCCESS;
8453
8454 goto out_busy;
8455
8456 iocb_busy:
8457 pring->stats.iocb_cmd_delay++;
8458
8459 out_busy:
8460
8461 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008462 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008463 return IOCB_SUCCESS;
8464 }
8465
8466 return IOCB_BUSY;
8467}
8468
James Smart3772a992009-05-22 14:50:54 -04008469/**
James Smart4f774512009-05-22 14:52:35 -04008470 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8471 * @phba: Pointer to HBA context object.
8472 * @piocb: Pointer to command iocb.
8473 * @sglq: Pointer to the scatter gather queue object.
8474 *
8475 * This routine converts the bpl or bde that is in the IOCB
8476 * to a sgl list for the sli4 hardware. The physical address
8477 * of the bpl/bde is converted back to a virtual address.
8478 * If the IOCB contains a BPL then the list of BDE's is
8479 * converted to sli4_sge's. If the IOCB contains a single
8480 * BDE then it is converted to a single sli_sge.
8481 * The IOCB is still in cpu endianess so the contents of
8482 * the bpl can be used without byte swapping.
8483 *
8484 * Returns valid XRI = Success, NO_XRI = Failure.
8485**/
8486static uint16_t
8487lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8488 struct lpfc_sglq *sglq)
8489{
8490 uint16_t xritag = NO_XRI;
8491 struct ulp_bde64 *bpl = NULL;
8492 struct ulp_bde64 bde;
8493 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008494 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008495 IOCB_t *icmd;
8496 int numBdes = 0;
8497 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008498 uint32_t offset = 0; /* accumulated offset in the sg request list */
8499 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008500
8501 if (!piocbq || !sglq)
8502 return xritag;
8503
8504 sgl = (struct sli4_sge *)sglq->sgl;
8505 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008506 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8507 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008508 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8509 numBdes = icmd->un.genreq64.bdl.bdeSize /
8510 sizeof(struct ulp_bde64);
8511 /* The addrHigh and addrLow fields within the IOCB
8512 * have not been byteswapped yet so there is no
8513 * need to swap them back.
8514 */
James Smart1b511972011-12-13 13:23:09 -05008515 if (piocbq->context3)
8516 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8517 else
8518 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008519
James Smart1b511972011-12-13 13:23:09 -05008520 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008521 if (!bpl)
8522 return xritag;
8523
8524 for (i = 0; i < numBdes; i++) {
8525 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008526 sgl->addr_hi = bpl->addrHigh;
8527 sgl->addr_lo = bpl->addrLow;
8528
James Smart05580562011-05-24 11:40:48 -04008529 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008530 if ((i+1) == numBdes)
8531 bf_set(lpfc_sli4_sge_last, sgl, 1);
8532 else
8533 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008534 /* swap the size field back to the cpu so we
8535 * can assign it to the sgl.
8536 */
8537 bde.tus.w = le32_to_cpu(bpl->tus.w);
8538 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008539 /* The offsets in the sgl need to be accumulated
8540 * separately for the request and reply lists.
8541 * The request is always first, the reply follows.
8542 */
8543 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8544 /* add up the reply sg entries */
8545 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8546 inbound++;
8547 /* first inbound? reset the offset */
8548 if (inbound == 1)
8549 offset = 0;
8550 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008551 bf_set(lpfc_sli4_sge_type, sgl,
8552 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008553 offset += bde.tus.f.bdeSize;
8554 }
James Smart546fc852011-03-11 16:06:29 -05008555 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008556 bpl++;
8557 sgl++;
8558 }
8559 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8560 /* The addrHigh and addrLow fields of the BDE have not
8561 * been byteswapped yet so they need to be swapped
8562 * before putting them in the sgl.
8563 */
8564 sgl->addr_hi =
8565 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8566 sgl->addr_lo =
8567 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008568 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008569 bf_set(lpfc_sli4_sge_last, sgl, 1);
8570 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008571 sgl->sge_len =
8572 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008573 }
8574 return sglq->sli4_xritag;
8575}
8576
8577/**
James Smart4f774512009-05-22 14:52:35 -04008578 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8579 * @phba: Pointer to HBA context object.
8580 * @piocb: Pointer to command iocb.
8581 * @wqe: Pointer to the work queue entry.
8582 *
8583 * This routine converts the iocb command to its Work Queue Entry
8584 * equivalent. The wqe pointer should not have any fields set when
8585 * this routine is called because it will memcpy over them.
8586 * This routine does not set the CQ_ID or the WQEC bits in the
8587 * wqe.
8588 *
8589 * Returns: 0 = Success, IOCB_ERROR = Failure.
8590 **/
8591static int
8592lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8593 union lpfc_wqe *wqe)
8594{
James Smart5ffc2662009-11-18 15:39:44 -05008595 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008596 uint8_t ct = 0;
8597 uint32_t fip;
8598 uint32_t abort_tag;
8599 uint8_t command_type = ELS_COMMAND_NON_FIP;
8600 uint8_t cmnd;
8601 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008602 uint16_t abrt_iotag;
8603 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008604 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008605 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008606 int numBdes, i;
8607 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008608 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008609 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008610 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008611
James Smart45ed1192009-10-02 15:17:02 -04008612 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008613 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008614 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008615 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008616 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008617 command_type = ELS_COMMAND_FIP;
8618 else
8619 command_type = ELS_COMMAND_NON_FIP;
8620
James Smartb5c53952016-03-31 14:12:30 -07008621 if (phba->fcp_embed_io)
8622 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008623 /* Some of the fields are in the right position already */
8624 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008625 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008626 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008627
8628 abort_tag = (uint32_t) iocbq->iotag;
8629 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008630 /* words0-2 bpl convert bde */
8631 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008632 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8633 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008634 bpl = (struct ulp_bde64 *)
8635 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8636 if (!bpl)
8637 return IOCB_ERROR;
8638
8639 /* Should already be byte swapped. */
8640 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8641 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8642 /* swap the size field back to the cpu so we
8643 * can assign it to the sgl.
8644 */
8645 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008646 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8647 total_len = 0;
8648 for (i = 0; i < numBdes; i++) {
8649 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8650 total_len += bde.tus.f.bdeSize;
8651 }
James Smart4f774512009-05-22 14:52:35 -04008652 } else
James Smart5ffc2662009-11-18 15:39:44 -05008653 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008654
8655 iocbq->iocb.ulpIoTag = iocbq->iotag;
8656 cmnd = iocbq->iocb.ulpCommand;
8657
8658 switch (iocbq->iocb.ulpCommand) {
8659 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008660 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8661 ndlp = iocbq->context_un.ndlp;
8662 else
8663 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008664 if (!iocbq->iocb.ulpLe) {
8665 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8666 "2007 Only Limited Edition cmd Format"
8667 " supported 0x%x\n",
8668 iocbq->iocb.ulpCommand);
8669 return IOCB_ERROR;
8670 }
James Smartff78d8f2011-12-13 13:21:35 -05008671
James Smart5ffc2662009-11-18 15:39:44 -05008672 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008673 /* Els_reguest64 has a TMO */
8674 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8675 iocbq->iocb.ulpTimeout);
8676 /* Need a VF for word 4 set the vf bit*/
8677 bf_set(els_req64_vf, &wqe->els_req, 0);
8678 /* And a VFID for word 12 */
8679 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008680 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008681 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8682 iocbq->iocb.ulpContext);
8683 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8684 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008685 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008686 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008687 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8688 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008689 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8690 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008691 if_type = bf_get(lpfc_sli_intf_if_type,
8692 &phba->sli4_hba.sli_intf);
8693 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008694 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008695 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008696 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008697 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008698 *pcmd == ELS_CMD_PLOGI)) {
8699 bf_set(els_req64_sp, &wqe->els_req, 1);
8700 bf_set(els_req64_sid, &wqe->els_req,
8701 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008702 if ((*pcmd == ELS_CMD_FLOGI) &&
8703 !(phba->fc_topology ==
8704 LPFC_TOPOLOGY_LOOP))
8705 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008706 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8707 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008708 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008709 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008710 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8711 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8712 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8713 }
James Smartc8685952009-11-18 15:39:16 -05008714 }
James Smart6d368e52011-05-24 11:44:12 -04008715 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8716 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008717 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8718 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8719 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8720 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8721 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8722 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008723 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008724 break;
James Smart5ffc2662009-11-18 15:39:44 -05008725 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008726 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8727 iocbq->iocb.un.ulpWord[3]);
8728 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008729 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008730 /* The entire sequence is transmitted for this IOCB */
8731 xmit_len = total_len;
8732 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008733 if (phba->link_flag & LS_LOOPBACK_MODE)
8734 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008735 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008736 /* word3 iocb=io_tag32 wqe=reserved */
8737 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008738 /* word4 relative_offset memcpy */
8739 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008740 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8741 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8742 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8743 LPFC_WQE_IOD_WRITE);
8744 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8745 LPFC_WQE_LENLOC_WORD12);
8746 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008747 wqe->xmit_sequence.xmit_len = xmit_len;
8748 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008749 break;
James Smart4f774512009-05-22 14:52:35 -04008750 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008751 /* word3 iocb=iotag32 wqe=seq_payload_len */
8752 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008753 /* word4 iocb=rsvd wqe=rsvd */
8754 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8755 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008756 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008757 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008758 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8759 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8760 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8761 LPFC_WQE_LENLOC_WORD3);
8762 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008763 break;
James Smart4f774512009-05-22 14:52:35 -04008764 case CMD_FCP_IWRITE64_CR:
8765 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008766 /* word3 iocb=iotag wqe=payload_offset_len */
8767 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008768 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8769 xmit_len + sizeof(struct fcp_rsp));
8770 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8771 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008772 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8773 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8774 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8775 iocbq->iocb.ulpFCP2Rcvy);
8776 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8777 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008778 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8779 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8780 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008781 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008782 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008783 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8784 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008785 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8786 if (iocbq->priority) {
8787 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8788 (iocbq->priority << 1));
8789 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008790 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8791 (phba->cfg_XLanePriority << 1));
8792 }
8793 }
James Smartb5c53952016-03-31 14:12:30 -07008794 /* Note, word 10 is already initialized to 0 */
8795
8796 if (phba->fcp_embed_io) {
8797 struct lpfc_scsi_buf *lpfc_cmd;
8798 struct sli4_sge *sgl;
8799 union lpfc_wqe128 *wqe128;
8800 struct fcp_cmnd *fcp_cmnd;
8801 uint32_t *ptr;
8802
8803 /* 128 byte wqe support here */
8804 wqe128 = (union lpfc_wqe128 *)wqe;
8805
8806 lpfc_cmd = iocbq->context1;
8807 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8808 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8809
8810 /* Word 0-2 - FCP_CMND */
8811 wqe128->generic.bde.tus.f.bdeFlags =
8812 BUFF_TYPE_BDE_IMMED;
8813 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8814 wqe128->generic.bde.addrHigh = 0;
8815 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8816
8817 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8818
8819 /* Word 22-29 FCP CMND Payload */
8820 ptr = &wqe128->words[22];
8821 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8822 }
James Smart7851fe22011-07-22 18:36:52 -04008823 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008824 case CMD_FCP_IREAD64_CR:
8825 /* word3 iocb=iotag wqe=payload_offset_len */
8826 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008827 bf_set(payload_offset_len, &wqe->fcp_iread,
8828 xmit_len + sizeof(struct fcp_rsp));
8829 bf_set(cmd_buff_len, &wqe->fcp_iread,
8830 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008831 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8832 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8833 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8834 iocbq->iocb.ulpFCP2Rcvy);
8835 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008836 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008837 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8838 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8839 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008840 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008841 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008842 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8843 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008844 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8845 if (iocbq->priority) {
8846 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8847 (iocbq->priority << 1));
8848 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008849 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8850 (phba->cfg_XLanePriority << 1));
8851 }
8852 }
James Smartb5c53952016-03-31 14:12:30 -07008853 /* Note, word 10 is already initialized to 0 */
8854
8855 if (phba->fcp_embed_io) {
8856 struct lpfc_scsi_buf *lpfc_cmd;
8857 struct sli4_sge *sgl;
8858 union lpfc_wqe128 *wqe128;
8859 struct fcp_cmnd *fcp_cmnd;
8860 uint32_t *ptr;
8861
8862 /* 128 byte wqe support here */
8863 wqe128 = (union lpfc_wqe128 *)wqe;
8864
8865 lpfc_cmd = iocbq->context1;
8866 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8867 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8868
8869 /* Word 0-2 - FCP_CMND */
8870 wqe128->generic.bde.tus.f.bdeFlags =
8871 BUFF_TYPE_BDE_IMMED;
8872 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8873 wqe128->generic.bde.addrHigh = 0;
8874 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8875
8876 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8877
8878 /* Word 22-29 FCP CMND Payload */
8879 ptr = &wqe128->words[22];
8880 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8881 }
James Smart7851fe22011-07-22 18:36:52 -04008882 break;
James Smartf1126682009-06-10 17:22:44 -04008883 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008884 /* word3 iocb=iotag wqe=payload_offset_len */
8885 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8886 bf_set(payload_offset_len, &wqe->fcp_icmd,
8887 xmit_len + sizeof(struct fcp_rsp));
8888 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8889 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008890 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008891 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008892 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008893 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8894 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8895 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8896 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8897 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008898 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8899 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008900 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8901 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008902 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8903 if (iocbq->priority) {
8904 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8905 (iocbq->priority << 1));
8906 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008907 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8908 (phba->cfg_XLanePriority << 1));
8909 }
8910 }
James Smartb5c53952016-03-31 14:12:30 -07008911 /* Note, word 10 is already initialized to 0 */
8912
8913 if (phba->fcp_embed_io) {
8914 struct lpfc_scsi_buf *lpfc_cmd;
8915 struct sli4_sge *sgl;
8916 union lpfc_wqe128 *wqe128;
8917 struct fcp_cmnd *fcp_cmnd;
8918 uint32_t *ptr;
8919
8920 /* 128 byte wqe support here */
8921 wqe128 = (union lpfc_wqe128 *)wqe;
8922
8923 lpfc_cmd = iocbq->context1;
8924 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8925 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8926
8927 /* Word 0-2 - FCP_CMND */
8928 wqe128->generic.bde.tus.f.bdeFlags =
8929 BUFF_TYPE_BDE_IMMED;
8930 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8931 wqe128->generic.bde.addrHigh = 0;
8932 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8933
8934 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8935
8936 /* Word 22-29 FCP CMND Payload */
8937 ptr = &wqe128->words[22];
8938 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8939 }
James Smart7851fe22011-07-22 18:36:52 -04008940 break;
James Smart4f774512009-05-22 14:52:35 -04008941 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008942 /* For this command calculate the xmit length of the
8943 * request bde.
8944 */
8945 xmit_len = 0;
8946 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8947 sizeof(struct ulp_bde64);
8948 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008949 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008950 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8951 break;
James Smart63e801c2010-11-20 23:14:19 -05008952 xmit_len += bde.tus.f.bdeSize;
8953 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008954 /* word3 iocb=IO_TAG wqe=request_payload_len */
8955 wqe->gen_req.request_payload_len = xmit_len;
8956 /* word4 iocb=parameter wqe=relative_offset memcpy */
8957 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008958 /* word6 context tag copied in memcpy */
8959 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8960 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8961 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8962 "2015 Invalid CT %x command 0x%x\n",
8963 ct, iocbq->iocb.ulpCommand);
8964 return IOCB_ERROR;
8965 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008966 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8967 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8968 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8969 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8970 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8971 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8972 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8973 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008974 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008975 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008976 break;
James Smart4f774512009-05-22 14:52:35 -04008977 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008978 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008979 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008980 /* word3 iocb=iotag32 wqe=response_payload_len */
8981 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008982 /* word4 */
8983 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008984 /* word5 iocb=rsvd wge=did */
8985 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008986 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8987
8988 if_type = bf_get(lpfc_sli_intf_if_type,
8989 &phba->sli4_hba.sli_intf);
8990 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8991 if (iocbq->vport->fc_flag & FC_PT2PT) {
8992 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8993 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8994 iocbq->vport->fc_myDID);
8995 if (iocbq->vport->fc_myDID == Fabric_DID) {
8996 bf_set(wqe_els_did,
8997 &wqe->xmit_els_rsp.wqe_dest, 0);
8998 }
8999 }
9000 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009001 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9002 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9003 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9004 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009005 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009006 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009007 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009008 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009009 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9010 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9011 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9012 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9013 LPFC_WQE_LENLOC_WORD3);
9014 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009015 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9016 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009017 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9018 iocbq->context2)->virt);
9019 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009020 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9021 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009022 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009023 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9024 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009025 phba->vpi_ids[phba->pport->vpi]);
9026 }
James Smart4f774512009-05-22 14:52:35 -04009027 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009028 break;
James Smart4f774512009-05-22 14:52:35 -04009029 case CMD_CLOSE_XRI_CN:
9030 case CMD_ABORT_XRI_CN:
9031 case CMD_ABORT_XRI_CX:
9032 /* words 0-2 memcpy should be 0 rserved */
9033 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009034 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9035 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9036 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9037 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9038 } else
9039 fip = 0;
9040
9041 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009042 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009043 * The link is down, or the command was ELS_FIP
9044 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009045 * on the wire.
9046 */
9047 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9048 else
9049 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9050 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009051 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9052 wqe->abort_cmd.rsrvd5 = 0;
9053 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009054 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9055 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009056 /*
9057 * The abort handler will send us CMD_ABORT_XRI_CN or
9058 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9059 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009060 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9061 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9062 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9063 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009064 cmnd = CMD_ABORT_XRI_CX;
9065 command_type = OTHER_COMMAND;
9066 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009067 break;
James Smart6669f9b2009-10-02 15:16:45 -04009068 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009069 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009070 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009071 * we re-construct this WQE here based on information in
9072 * iocbq from scratch.
9073 */
9074 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009075 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009076 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009077 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9078 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009079 LPFC_ABTS_UNSOL_INT) {
9080 /* ABTS sent by initiator to CT exchange, the
9081 * RX_ID field will be filled with the newly
9082 * allocated responder XRI.
9083 */
9084 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9085 iocbq->sli4_xritag);
9086 } else {
9087 /* ABTS sent by responder to CT exchange, the
9088 * RX_ID field will be filled with the responder
9089 * RX_ID from ABTS.
9090 */
9091 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009092 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009093 }
James Smart6669f9b2009-10-02 15:16:45 -04009094 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9095 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009096
9097 /* Use CT=VPI */
9098 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9099 ndlp->nlp_DID);
9100 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9101 iocbq->iocb.ulpContext);
9102 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009103 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009104 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009105 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9106 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9107 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009108 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9109 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009110 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9111 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9112 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9113 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9114 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9115 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9116 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9117 }
9118
James Smart7851fe22011-07-22 18:36:52 -04009119 break;
James Smart4f774512009-05-22 14:52:35 -04009120 case CMD_XRI_ABORTED_CX:
9121 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009122 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9123 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9124 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9125 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9126 default:
9127 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9128 "2014 Invalid command 0x%x\n",
9129 iocbq->iocb.ulpCommand);
9130 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009131 break;
James Smart4f774512009-05-22 14:52:35 -04009132 }
James Smart6d368e52011-05-24 11:44:12 -04009133
James Smart8012cc32012-10-31 14:44:49 -04009134 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9135 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9136 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9137 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9138 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9139 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9140 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9141 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009142 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9143 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9144 wqe->generic.wqe_com.abort_tag = abort_tag;
9145 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9146 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9147 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9148 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009149 return 0;
9150}
9151
9152/**
9153 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9154 * @phba: Pointer to HBA context object.
9155 * @ring_number: SLI ring number to issue iocb on.
9156 * @piocb: Pointer to command iocb.
9157 * @flag: Flag indicating if this command can be put into txq.
9158 *
9159 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9160 * an iocb command to an HBA with SLI-4 interface spec.
9161 *
9162 * This function is called with hbalock held. The function will return success
9163 * after it successfully submit the iocb to firmware or after adding to the
9164 * txq.
9165 **/
9166static int
9167__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9168 struct lpfc_iocbq *piocb, uint32_t flag)
9169{
9170 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009171 union lpfc_wqe *wqe;
9172 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009173 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009174 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009175
James Smart895427b2017-02-12 13:52:30 -08009176 /* Get the WQ */
9177 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9178 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9179 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9180 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9181 else
9182 wq = phba->sli4_hba.oas_wq;
9183 } else {
9184 wq = phba->sli4_hba.els_wq;
9185 }
9186
9187 /* Get corresponding ring */
9188 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009189
James Smartb5c53952016-03-31 14:12:30 -07009190 /*
9191 * The WQE can be either 64 or 128 bytes,
9192 * so allocate space on the stack assuming the largest.
9193 */
9194 wqe = (union lpfc_wqe *)&wqe128;
9195
James Smart895427b2017-02-12 13:52:30 -08009196 lockdep_assert_held(&phba->hbalock);
9197
James Smart4f774512009-05-22 14:52:35 -04009198 if (piocb->sli4_xritag == NO_XRI) {
9199 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009200 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009201 sglq = NULL;
9202 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009203 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009204 if (!(flag & SLI_IOCB_RET_IOCB)) {
9205 __lpfc_sli_ringtx_put(phba,
9206 pring, piocb);
9207 return IOCB_SUCCESS;
9208 } else {
9209 return IOCB_BUSY;
9210 }
9211 } else {
James Smart895427b2017-02-12 13:52:30 -08009212 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009213 if (!sglq) {
9214 if (!(flag & SLI_IOCB_RET_IOCB)) {
9215 __lpfc_sli_ringtx_put(phba,
9216 pring,
9217 piocb);
9218 return IOCB_SUCCESS;
9219 } else
9220 return IOCB_BUSY;
9221 }
9222 }
James Smart4f774512009-05-22 14:52:35 -04009223 }
James Smart2ea259e2017-02-12 13:52:27 -08009224 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009225 /* These IO's already have an XRI and a mapped sgl. */
9226 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009227 else {
James Smart6d368e52011-05-24 11:44:12 -04009228 /*
9229 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009230 * sglq is on the active list
9231 */
James Smartedccdc12013-01-03 15:43:45 -05009232 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009233 if (!sglq)
9234 return IOCB_ERROR;
9235 }
9236
9237 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009238 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009239 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009240 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009241 return IOCB_ERROR;
9242 }
9243
James Smartb5c53952016-03-31 14:12:30 -07009244 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009245 return IOCB_ERROR;
9246
James Smart895427b2017-02-12 13:52:30 -08009247 if (lpfc_sli4_wq_put(wq, wqe))
9248 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009249 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9250
9251 return 0;
9252}
9253
9254/**
James Smart3772a992009-05-22 14:50:54 -04009255 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9256 *
9257 * This routine wraps the actual lockless version for issusing IOCB function
9258 * pointer from the lpfc_hba struct.
9259 *
9260 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009261 * IOCB_ERROR - Error
9262 * IOCB_SUCCESS - Success
9263 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009264 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009265int
James Smart3772a992009-05-22 14:50:54 -04009266__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9267 struct lpfc_iocbq *piocb, uint32_t flag)
9268{
9269 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9270}
9271
9272/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009273 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009274 * @phba: The hba struct for which this call is being executed.
9275 * @dev_grp: The HBA PCI-Device group number.
9276 *
9277 * This routine sets up the SLI interface API function jump table in @phba
9278 * struct.
9279 * Returns: 0 - success, -ENODEV - failure.
9280 **/
9281int
9282lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9283{
9284
9285 switch (dev_grp) {
9286 case LPFC_PCI_DEV_LP:
9287 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9288 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9289 break;
James Smart4f774512009-05-22 14:52:35 -04009290 case LPFC_PCI_DEV_OC:
9291 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9292 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9293 break;
James Smart3772a992009-05-22 14:50:54 -04009294 default:
9295 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9296 "1419 Invalid HBA PCI-device group: 0x%x\n",
9297 dev_grp);
9298 return -ENODEV;
9299 break;
9300 }
9301 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9302 return 0;
9303}
James Smart92d7f7b2007-06-17 19:56:38 -05009304
James Smarta1efe162015-05-21 13:55:20 -04009305/**
James Smart895427b2017-02-12 13:52:30 -08009306 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009307 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009308 * @piocb: Pointer to command iocb.
9309 *
James Smart895427b2017-02-12 13:52:30 -08009310 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9311 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009312 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009313 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009314 */
James Smart895427b2017-02-12 13:52:30 -08009315struct lpfc_sli_ring *
9316lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009317{
James Smart895427b2017-02-12 13:52:30 -08009318 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009319 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009320 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009321 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009322 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009323 /*
James Smart895427b2017-02-12 13:52:30 -08009324 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009325 * be setup based on what work queue we used.
9326 */
9327 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
James Smart895427b2017-02-12 13:52:30 -08009328 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009329 lpfc_sli4_scmd_to_wqidx_distr(phba,
9330 piocb->context1);
James Smart895427b2017-02-12 13:52:30 -08009331 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009332 } else {
9333 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009334 return NULL;
9335 piocb->hba_wqidx = 0;
9336 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009337 }
James Smart895427b2017-02-12 13:52:30 -08009338 } else {
9339 if (unlikely(!phba->sli4_hba.els_wq))
9340 return NULL;
9341 piocb->hba_wqidx = 0;
9342 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009343 }
James Smart9bd2bff52014-09-03 12:57:30 -04009344}
9345
James Smarte59058c2008-08-24 21:49:00 -04009346/**
James Smart3621a712009-04-06 18:47:14 -04009347 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009348 * @phba: Pointer to HBA context object.
9349 * @pring: Pointer to driver SLI ring object.
9350 * @piocb: Pointer to command iocb.
9351 * @flag: Flag indicating if this command can be put into txq.
9352 *
9353 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9354 * function. This function gets the hbalock and calls
9355 * __lpfc_sli_issue_iocb function and will return the error returned
9356 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9357 * functions which do not hold hbalock.
9358 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009359int
James Smart3772a992009-05-22 14:50:54 -04009360lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009361 struct lpfc_iocbq *piocb, uint32_t flag)
9362{
James Smart895427b2017-02-12 13:52:30 -08009363 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009364 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009365 struct lpfc_queue *fpeq;
9366 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009367 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009368 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009369
James Smart7e56aa22012-08-03 12:35:34 -04009370 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009371 pring = lpfc_sli4_calc_ring(phba, piocb);
9372 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009373 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009374
James Smart9bd2bff52014-09-03 12:57:30 -04009375 spin_lock_irqsave(&pring->ring_lock, iflags);
9376 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9377 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009378
James Smart9bd2bff52014-09-03 12:57:30 -04009379 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009380 idx = piocb->hba_wqidx;
9381 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009382
James Smart895427b2017-02-12 13:52:30 -08009383 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009384
James Smart9bd2bff52014-09-03 12:57:30 -04009385 /* Get associated EQ with this index */
9386 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009387
James Smart9bd2bff52014-09-03 12:57:30 -04009388 /* Turn off interrupts from this EQ */
9389 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009390
James Smart9bd2bff52014-09-03 12:57:30 -04009391 /*
9392 * Process all the events on FCP EQ
9393 */
9394 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9395 lpfc_sli4_hba_handle_eqe(phba,
9396 eqe, idx);
9397 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009398 }
James Smartba20c852012-08-03 12:36:52 -04009399
James Smart9bd2bff52014-09-03 12:57:30 -04009400 /* Always clear and re-arm the EQ */
9401 lpfc_sli4_eq_release(fpeq,
9402 LPFC_QUEUE_REARM);
9403 }
James Smart895427b2017-02-12 13:52:30 -08009404 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009405 }
James Smart7e56aa22012-08-03 12:35:34 -04009406 } else {
9407 /* For now, SLI2/3 will still use hbalock */
9408 spin_lock_irqsave(&phba->hbalock, iflags);
9409 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9410 spin_unlock_irqrestore(&phba->hbalock, iflags);
9411 }
James Smart92d7f7b2007-06-17 19:56:38 -05009412 return rc;
9413}
9414
James Smarte59058c2008-08-24 21:49:00 -04009415/**
James Smart3621a712009-04-06 18:47:14 -04009416 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009417 * @phba: Pointer to HBA context object.
9418 *
9419 * This function is called while driver attaches with the
9420 * HBA to setup the extra ring. The extra ring is used
9421 * only when driver needs to support target mode functionality
9422 * or IP over FC functionalities.
9423 *
James Smart895427b2017-02-12 13:52:30 -08009424 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009425 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009426static int
9427lpfc_extra_ring_setup( struct lpfc_hba *phba)
9428{
9429 struct lpfc_sli *psli;
9430 struct lpfc_sli_ring *pring;
9431
9432 psli = &phba->sli;
9433
9434 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009435
9436 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009437 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009438 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9439 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9440 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9441 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009442
James Smarta4bc3372006-12-02 13:34:16 -05009443 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009444 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009445
James Smart7e56aa22012-08-03 12:35:34 -04009446 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9447 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9448 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9449 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009450
9451 /* Setup default profile for this ring */
9452 pring->iotag_max = 4096;
9453 pring->num_mask = 1;
9454 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009455 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9456 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009457 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9458 return 0;
9459}
9460
James Smartcb69f7d2011-12-13 13:21:57 -05009461/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9462 * @phba: Pointer to HBA context object.
9463 * @iocbq: Pointer to iocb object.
9464 *
9465 * The async_event handler calls this routine when it receives
9466 * an ASYNC_STATUS_CN event from the port. The port generates
9467 * this event when an Abort Sequence request to an rport fails
9468 * twice in succession. The abort could be originated by the
9469 * driver or by the port. The ABTS could have been for an ELS
9470 * or FCP IO. The port only generates this event when an ABTS
9471 * fails to complete after one retry.
9472 */
9473static void
9474lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9475 struct lpfc_iocbq *iocbq)
9476{
9477 struct lpfc_nodelist *ndlp = NULL;
9478 uint16_t rpi = 0, vpi = 0;
9479 struct lpfc_vport *vport = NULL;
9480
9481 /* The rpi in the ulpContext is vport-sensitive. */
9482 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9483 rpi = iocbq->iocb.ulpContext;
9484
9485 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9486 "3092 Port generated ABTS async event "
9487 "on vpi %d rpi %d status 0x%x\n",
9488 vpi, rpi, iocbq->iocb.ulpStatus);
9489
9490 vport = lpfc_find_vport_by_vpid(phba, vpi);
9491 if (!vport)
9492 goto err_exit;
9493 ndlp = lpfc_findnode_rpi(vport, rpi);
9494 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9495 goto err_exit;
9496
9497 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9498 lpfc_sli_abts_recover_port(vport, ndlp);
9499 return;
9500
9501 err_exit:
9502 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9503 "3095 Event Context not found, no "
9504 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9505 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9506 vpi, rpi);
9507}
9508
9509/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9510 * @phba: pointer to HBA context object.
9511 * @ndlp: nodelist pointer for the impacted rport.
9512 * @axri: pointer to the wcqe containing the failed exchange.
9513 *
9514 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9515 * port. The port generates this event when an abort exchange request to an
9516 * rport fails twice in succession with no reply. The abort could be originated
9517 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9518 */
9519void
9520lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9521 struct lpfc_nodelist *ndlp,
9522 struct sli4_wcqe_xri_aborted *axri)
9523{
9524 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009525 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009526
James Smart6b5151f2012-01-18 16:24:06 -05009527 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009528 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9529 "3115 Node Context not found, driver "
9530 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009531 return;
9532 }
9533
James Smartcb69f7d2011-12-13 13:21:57 -05009534 vport = ndlp->vport;
9535 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9536 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009537 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009538 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009539 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009540 bf_get(lpfc_wcqe_xa_status, axri),
9541 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009542
James Smart5c1db2a2012-03-01 22:34:36 -05009543 /*
9544 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9545 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9546 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9547 */
James Smarte3d2b802012-08-14 14:25:43 -04009548 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009549 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9550 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009551 lpfc_sli_abts_recover_port(vport, ndlp);
9552}
9553
James Smarte59058c2008-08-24 21:49:00 -04009554/**
James Smart3621a712009-04-06 18:47:14 -04009555 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009556 * @phba: Pointer to HBA context object.
9557 * @pring: Pointer to driver SLI ring object.
9558 * @iocbq: Pointer to iocb object.
9559 *
9560 * This function is called by the slow ring event handler
9561 * function when there is an ASYNC event iocb in the ring.
9562 * This function is called with no lock held.
9563 * Currently this function handles only temperature related
9564 * ASYNC events. The function decodes the temperature sensor
9565 * event message and posts events for the management applications.
9566 **/
James Smart98c9ea52007-10-27 13:37:33 -04009567static void
James Smart57127f12007-10-27 13:37:05 -04009568lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9569 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9570{
9571 IOCB_t *icmd;
9572 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009573 struct temp_event temp_event_data;
9574 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009575 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009576
9577 icmd = &iocbq->iocb;
9578 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009579
James Smartcb69f7d2011-12-13 13:21:57 -05009580 switch (evt_code) {
9581 case ASYNC_TEMP_WARN:
9582 case ASYNC_TEMP_SAFE:
9583 temp_event_data.data = (uint32_t) icmd->ulpContext;
9584 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9585 if (evt_code == ASYNC_TEMP_WARN) {
9586 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9587 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9588 "0347 Adapter is very hot, please take "
9589 "corrective action. temperature : %d Celsius\n",
9590 (uint32_t) icmd->ulpContext);
9591 } else {
9592 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9593 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9594 "0340 Adapter temperature is OK now. "
9595 "temperature : %d Celsius\n",
9596 (uint32_t) icmd->ulpContext);
9597 }
9598
9599 /* Send temperature change event to applications */
9600 shost = lpfc_shost_from_vport(phba->pport);
9601 fc_host_post_vendor_event(shost, fc_get_event_number(),
9602 sizeof(temp_event_data), (char *) &temp_event_data,
9603 LPFC_NL_VENDOR_ID);
9604 break;
9605 case ASYNC_STATUS_CN:
9606 lpfc_sli_abts_err_handler(phba, iocbq);
9607 break;
9608 default:
James Smarta257bf92009-04-06 18:48:10 -04009609 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009610 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009611 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009612 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009613 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9614 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9615 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9616 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009617 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009618 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9619 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9620 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9621 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9622
James Smartcb69f7d2011-12-13 13:21:57 -05009623 break;
James Smart57127f12007-10-27 13:37:05 -04009624 }
James Smart57127f12007-10-27 13:37:05 -04009625}
9626
9627
James Smarte59058c2008-08-24 21:49:00 -04009628/**
James Smart895427b2017-02-12 13:52:30 -08009629 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009630 * @phba: Pointer to HBA context object.
9631 *
9632 * lpfc_sli_setup sets up rings of the SLI interface with
9633 * number of iocbs per ring and iotags. This function is
9634 * called while driver attach to the HBA and before the
9635 * interrupts are enabled. So there is no need for locking.
9636 *
9637 * This function always returns 0.
9638 **/
dea31012005-04-17 16:05:31 -05009639int
James Smart895427b2017-02-12 13:52:30 -08009640lpfc_sli4_setup(struct lpfc_hba *phba)
9641{
9642 struct lpfc_sli_ring *pring;
9643
9644 pring = phba->sli4_hba.els_wq->pring;
9645 pring->num_mask = LPFC_MAX_RING_MASK;
9646 pring->prt[0].profile = 0; /* Mask 0 */
9647 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9648 pring->prt[0].type = FC_TYPE_ELS;
9649 pring->prt[0].lpfc_sli_rcv_unsol_event =
9650 lpfc_els_unsol_event;
9651 pring->prt[1].profile = 0; /* Mask 1 */
9652 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9653 pring->prt[1].type = FC_TYPE_ELS;
9654 pring->prt[1].lpfc_sli_rcv_unsol_event =
9655 lpfc_els_unsol_event;
9656 pring->prt[2].profile = 0; /* Mask 2 */
9657 /* NameServer Inquiry */
9658 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9659 /* NameServer */
9660 pring->prt[2].type = FC_TYPE_CT;
9661 pring->prt[2].lpfc_sli_rcv_unsol_event =
9662 lpfc_ct_unsol_event;
9663 pring->prt[3].profile = 0; /* Mask 3 */
9664 /* NameServer response */
9665 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9666 /* NameServer */
9667 pring->prt[3].type = FC_TYPE_CT;
9668 pring->prt[3].lpfc_sli_rcv_unsol_event =
9669 lpfc_ct_unsol_event;
9670 return 0;
9671}
9672
9673/**
9674 * lpfc_sli_setup - SLI ring setup function
9675 * @phba: Pointer to HBA context object.
9676 *
9677 * lpfc_sli_setup sets up rings of the SLI interface with
9678 * number of iocbs per ring and iotags. This function is
9679 * called while driver attach to the HBA and before the
9680 * interrupts are enabled. So there is no need for locking.
9681 *
9682 * This function always returns 0. SLI3 only.
9683 **/
9684int
dea31012005-04-17 16:05:31 -05009685lpfc_sli_setup(struct lpfc_hba *phba)
9686{
James Smarted957682007-06-17 19:56:37 -05009687 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009688 struct lpfc_sli *psli = &phba->sli;
9689 struct lpfc_sli_ring *pring;
9690
James Smart2a76a282012-08-03 12:35:54 -04009691 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009692 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009693
James Bottomley604a3e32005-10-29 10:28:33 -05009694 psli->iocbq_lookup = NULL;
9695 psli->iocbq_lookup_len = 0;
9696 psli->last_iotag = 0;
9697
dea31012005-04-17 16:05:31 -05009698 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009699 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009700 switch (i) {
9701 case LPFC_FCP_RING: /* ring 0 - FCP */
9702 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009703 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9704 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9705 pring->sli.sli3.numCiocb +=
9706 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9707 pring->sli.sli3.numRiocb +=
9708 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9709 pring->sli.sli3.numCiocb +=
9710 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9711 pring->sli.sli3.numRiocb +=
9712 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9713 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009714 SLI3_IOCB_CMD_SIZE :
9715 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009716 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009717 SLI3_IOCB_RSP_SIZE :
9718 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009719 pring->iotag_ctr = 0;
9720 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009721 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009722 pring->fast_iotag = pring->iotag_max;
9723 pring->num_mask = 0;
9724 break;
James Smarta4bc3372006-12-02 13:34:16 -05009725 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009726 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009727 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9728 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9729 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009730 SLI3_IOCB_CMD_SIZE :
9731 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009732 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009733 SLI3_IOCB_RSP_SIZE :
9734 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009735 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009736 pring->num_mask = 0;
9737 break;
9738 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9739 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009740 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9741 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9742 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009743 SLI3_IOCB_CMD_SIZE :
9744 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009745 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009746 SLI3_IOCB_RSP_SIZE :
9747 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009748 pring->fast_iotag = 0;
9749 pring->iotag_ctr = 0;
9750 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009751 pring->lpfc_sli_rcv_async_status =
9752 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009753 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009754 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009755 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9756 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009757 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009758 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009759 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009760 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9761 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009762 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009763 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009764 pring->prt[2].profile = 0; /* Mask 2 */
9765 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009766 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009767 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009768 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009769 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009770 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009771 pring->prt[3].profile = 0; /* Mask 3 */
9772 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009773 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009774 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009775 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009776 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009777 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009778 break;
9779 }
James Smart7e56aa22012-08-03 12:35:34 -04009780 totiocbsize += (pring->sli.sli3.numCiocb *
9781 pring->sli.sli3.sizeCiocb) +
9782 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009783 }
James Smarted957682007-06-17 19:56:37 -05009784 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009785 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009786 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9787 "SLI2 SLIM Data: x%x x%lx\n",
9788 phba->brd_no, totiocbsize,
9789 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009790 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009791 if (phba->cfg_multi_ring_support == 2)
9792 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009793
9794 return 0;
9795}
9796
James Smarte59058c2008-08-24 21:49:00 -04009797/**
James Smart895427b2017-02-12 13:52:30 -08009798 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009799 * @phba: Pointer to HBA context object.
9800 *
James Smart895427b2017-02-12 13:52:30 -08009801 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009802 * ring. This function also initializes ring indices of each ring.
9803 * This function is called during the initialization of the SLI
9804 * interface of an HBA.
9805 * This function is called with no lock held and always returns
9806 * 1.
9807 **/
James Smart895427b2017-02-12 13:52:30 -08009808void
9809lpfc_sli4_queue_init(struct lpfc_hba *phba)
9810{
9811 struct lpfc_sli *psli;
9812 struct lpfc_sli_ring *pring;
9813 int i;
9814
9815 psli = &phba->sli;
9816 spin_lock_irq(&phba->hbalock);
9817 INIT_LIST_HEAD(&psli->mboxq);
9818 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9819 /* Initialize list headers for txq and txcmplq as double linked lists */
9820 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9821 pring = phba->sli4_hba.fcp_wq[i]->pring;
9822 pring->flag = 0;
9823 pring->ringno = LPFC_FCP_RING;
9824 INIT_LIST_HEAD(&pring->txq);
9825 INIT_LIST_HEAD(&pring->txcmplq);
9826 INIT_LIST_HEAD(&pring->iocb_continueq);
9827 spin_lock_init(&pring->ring_lock);
9828 }
9829 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9830 pring = phba->sli4_hba.nvme_wq[i]->pring;
9831 pring->flag = 0;
9832 pring->ringno = LPFC_FCP_RING;
9833 INIT_LIST_HEAD(&pring->txq);
9834 INIT_LIST_HEAD(&pring->txcmplq);
9835 INIT_LIST_HEAD(&pring->iocb_continueq);
9836 spin_lock_init(&pring->ring_lock);
9837 }
9838 pring = phba->sli4_hba.els_wq->pring;
9839 pring->flag = 0;
9840 pring->ringno = LPFC_ELS_RING;
9841 INIT_LIST_HEAD(&pring->txq);
9842 INIT_LIST_HEAD(&pring->txcmplq);
9843 INIT_LIST_HEAD(&pring->iocb_continueq);
9844 spin_lock_init(&pring->ring_lock);
9845
9846 if (phba->cfg_nvme_io_channel) {
9847 pring = phba->sli4_hba.nvmels_wq->pring;
9848 pring->flag = 0;
9849 pring->ringno = LPFC_ELS_RING;
9850 INIT_LIST_HEAD(&pring->txq);
9851 INIT_LIST_HEAD(&pring->txcmplq);
9852 INIT_LIST_HEAD(&pring->iocb_continueq);
9853 spin_lock_init(&pring->ring_lock);
9854 }
9855
9856 if (phba->cfg_fof) {
9857 pring = phba->sli4_hba.oas_wq->pring;
9858 pring->flag = 0;
9859 pring->ringno = LPFC_FCP_RING;
9860 INIT_LIST_HEAD(&pring->txq);
9861 INIT_LIST_HEAD(&pring->txcmplq);
9862 INIT_LIST_HEAD(&pring->iocb_continueq);
9863 spin_lock_init(&pring->ring_lock);
9864 }
9865
9866 spin_unlock_irq(&phba->hbalock);
9867}
9868
9869/**
9870 * lpfc_sli_queue_init - Queue initialization function
9871 * @phba: Pointer to HBA context object.
9872 *
9873 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9874 * ring. This function also initializes ring indices of each ring.
9875 * This function is called during the initialization of the SLI
9876 * interface of an HBA.
9877 * This function is called with no lock held and always returns
9878 * 1.
9879 **/
9880void
9881lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009882{
9883 struct lpfc_sli *psli;
9884 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009885 int i;
dea31012005-04-17 16:05:31 -05009886
9887 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009888 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009889 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009890 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009891 /* Initialize list headers for txq and txcmplq as double linked lists */
9892 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009893 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009894 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009895 pring->sli.sli3.next_cmdidx = 0;
9896 pring->sli.sli3.local_getidx = 0;
9897 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009898 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009899 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009900 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -08009901 pring->flag = 0;
9902 INIT_LIST_HEAD(&pring->txq);
9903 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -04009904 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009905 }
James Smart2e0fef82007-06-17 19:56:36 -05009906 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009907}
9908
James Smarte59058c2008-08-24 21:49:00 -04009909/**
James Smart04c68492009-05-22 14:52:52 -04009910 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9911 * @phba: Pointer to HBA context object.
9912 *
9913 * This routine flushes the mailbox command subsystem. It will unconditionally
9914 * flush all the mailbox commands in the three possible stages in the mailbox
9915 * command sub-system: pending mailbox command queue; the outstanding mailbox
9916 * command; and completed mailbox command queue. It is caller's responsibility
9917 * to make sure that the driver is in the proper state to flush the mailbox
9918 * command sub-system. Namely, the posting of mailbox commands into the
9919 * pending mailbox command queue from the various clients must be stopped;
9920 * either the HBA is in a state that it will never works on the outstanding
9921 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9922 * mailbox command has been completed.
9923 **/
9924static void
9925lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9926{
9927 LIST_HEAD(completions);
9928 struct lpfc_sli *psli = &phba->sli;
9929 LPFC_MBOXQ_t *pmb;
9930 unsigned long iflag;
9931
9932 /* Flush all the mailbox commands in the mbox system */
9933 spin_lock_irqsave(&phba->hbalock, iflag);
9934 /* The pending mailbox command queue */
9935 list_splice_init(&phba->sli.mboxq, &completions);
9936 /* The outstanding active mailbox command */
9937 if (psli->mbox_active) {
9938 list_add_tail(&psli->mbox_active->list, &completions);
9939 psli->mbox_active = NULL;
9940 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9941 }
9942 /* The completed mailbox command queue */
9943 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9944 spin_unlock_irqrestore(&phba->hbalock, iflag);
9945
9946 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9947 while (!list_empty(&completions)) {
9948 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9949 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9950 if (pmb->mbox_cmpl)
9951 pmb->mbox_cmpl(phba, pmb);
9952 }
9953}
9954
9955/**
James Smart3621a712009-04-06 18:47:14 -04009956 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009957 * @vport: Pointer to virtual port object.
9958 *
9959 * lpfc_sli_host_down is called to clean up the resources
9960 * associated with a vport before destroying virtual
9961 * port data structures.
9962 * This function does following operations:
9963 * - Free discovery resources associated with this virtual
9964 * port.
9965 * - Free iocbs associated with this virtual port in
9966 * the txq.
9967 * - Send abort for all iocb commands associated with this
9968 * vport in txcmplq.
9969 *
9970 * This function is called with no lock held and always returns 1.
9971 **/
dea31012005-04-17 16:05:31 -05009972int
James Smart92d7f7b2007-06-17 19:56:38 -05009973lpfc_sli_host_down(struct lpfc_vport *vport)
9974{
James Smart858c9f62007-06-17 19:56:39 -05009975 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009976 struct lpfc_hba *phba = vport->phba;
9977 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08009978 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -05009979 struct lpfc_sli_ring *pring;
9980 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009981 int i;
9982 unsigned long flags = 0;
9983 uint16_t prev_pring_flag;
9984
9985 lpfc_cleanup_discovery_resources(vport);
9986
9987 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009988
James Smart895427b2017-02-12 13:52:30 -08009989 /*
9990 * Error everything on the txq since these iocbs
9991 * have not been given to the FW yet.
9992 * Also issue ABTS for everything on the txcmplq
9993 */
9994 if (phba->sli_rev != LPFC_SLI_REV4) {
9995 for (i = 0; i < psli->num_rings; i++) {
9996 pring = &psli->sli3_ring[i];
9997 prev_pring_flag = pring->flag;
9998 /* Only slow rings */
9999 if (pring->ringno == LPFC_ELS_RING) {
10000 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10001 /* Set the lpfc data pending flag */
10002 set_bit(LPFC_DATA_READY, &phba->data_flags);
10003 }
10004 list_for_each_entry_safe(iocb, next_iocb,
10005 &pring->txq, list) {
10006 if (iocb->vport != vport)
10007 continue;
10008 list_move_tail(&iocb->list, &completions);
10009 }
10010 list_for_each_entry_safe(iocb, next_iocb,
10011 &pring->txcmplq, list) {
10012 if (iocb->vport != vport)
10013 continue;
10014 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10015 }
10016 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010017 }
James Smart895427b2017-02-12 13:52:30 -080010018 } else {
10019 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10020 pring = qp->pring;
10021 if (!pring)
10022 continue;
10023 if (pring == phba->sli4_hba.els_wq->pring) {
10024 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10025 /* Set the lpfc data pending flag */
10026 set_bit(LPFC_DATA_READY, &phba->data_flags);
10027 }
10028 prev_pring_flag = pring->flag;
10029 spin_lock_irq(&pring->ring_lock);
10030 list_for_each_entry_safe(iocb, next_iocb,
10031 &pring->txq, list) {
10032 if (iocb->vport != vport)
10033 continue;
10034 list_move_tail(&iocb->list, &completions);
10035 }
10036 spin_unlock_irq(&pring->ring_lock);
10037 list_for_each_entry_safe(iocb, next_iocb,
10038 &pring->txcmplq, list) {
10039 if (iocb->vport != vport)
10040 continue;
10041 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10042 }
10043 pring->flag = prev_pring_flag;
10044 }
James Smart92d7f7b2007-06-17 19:56:38 -050010045 }
James Smart92d7f7b2007-06-17 19:56:38 -050010046 spin_unlock_irqrestore(&phba->hbalock, flags);
10047
James Smarta257bf92009-04-06 18:48:10 -040010048 /* Cancel all the IOCBs from the completions list */
10049 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10050 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010051 return 1;
10052}
10053
James Smarte59058c2008-08-24 21:49:00 -040010054/**
James Smart3621a712009-04-06 18:47:14 -040010055 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010056 * @phba: Pointer to HBA context object.
10057 *
10058 * This function cleans up all iocb, buffers, mailbox commands
10059 * while shutting down the HBA. This function is called with no
10060 * lock held and always returns 1.
10061 * This function does the following to cleanup driver resources:
10062 * - Free discovery resources for each virtual port
10063 * - Cleanup any pending fabric iocbs
10064 * - Iterate through the iocb txq and free each entry
10065 * in the list.
10066 * - Free up any buffer posted to the HBA
10067 * - Free mailbox commands in the mailbox queue.
10068 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010069int
James Smart2e0fef82007-06-17 19:56:36 -050010070lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010071{
James Smart2534ba72007-04-25 09:52:20 -040010072 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010073 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010074 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010075 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010076 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010077 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010078 int i;
10079
10080 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010081 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010082
dea31012005-04-17 16:05:31 -050010083 lpfc_hba_down_prep(phba);
10084
James Smart92d7f7b2007-06-17 19:56:38 -050010085 lpfc_fabric_abort_hba(phba);
10086
James Smart2e0fef82007-06-17 19:56:36 -050010087 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010088
James Smart895427b2017-02-12 13:52:30 -080010089 /*
10090 * Error everything on the txq since these iocbs
10091 * have not been given to the FW yet.
10092 */
10093 if (phba->sli_rev != LPFC_SLI_REV4) {
10094 for (i = 0; i < psli->num_rings; i++) {
10095 pring = &psli->sli3_ring[i];
10096 /* Only slow rings */
10097 if (pring->ringno == LPFC_ELS_RING) {
10098 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10099 /* Set the lpfc data pending flag */
10100 set_bit(LPFC_DATA_READY, &phba->data_flags);
10101 }
10102 list_splice_init(&pring->txq, &completions);
10103 }
10104 } else {
10105 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10106 pring = qp->pring;
10107 if (!pring)
10108 continue;
10109 spin_lock_irq(&pring->ring_lock);
10110 list_splice_init(&pring->txq, &completions);
10111 spin_unlock_irq(&pring->ring_lock);
10112 if (pring == phba->sli4_hba.els_wq->pring) {
10113 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10114 /* Set the lpfc data pending flag */
10115 set_bit(LPFC_DATA_READY, &phba->data_flags);
10116 }
10117 }
dea31012005-04-17 16:05:31 -050010118 }
James Smart2e0fef82007-06-17 19:56:36 -050010119 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010120
James Smarta257bf92009-04-06 18:48:10 -040010121 /* Cancel all the IOCBs from the completions list */
10122 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10123 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010124
James Smart0ff10d42008-01-11 01:52:36 -050010125 spin_lock_irqsave(&phba->hbalock, flags);
10126 list_splice_init(&phba->elsbuf, &completions);
10127 phba->elsbuf_cnt = 0;
10128 phba->elsbuf_prev_cnt = 0;
10129 spin_unlock_irqrestore(&phba->hbalock, flags);
10130
10131 while (!list_empty(&completions)) {
10132 list_remove_head(&completions, buf_ptr,
10133 struct lpfc_dmabuf, list);
10134 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10135 kfree(buf_ptr);
10136 }
10137
dea31012005-04-17 16:05:31 -050010138 /* Return any active mbox cmds */
10139 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010140
James Smartda0436e2009-05-22 14:51:39 -040010141 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010142 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010143 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010144
James Smartda0436e2009-05-22 14:51:39 -040010145 return 1;
10146}
James Smart92d7f7b2007-06-17 19:56:38 -050010147
James Smartda0436e2009-05-22 14:51:39 -040010148/**
James Smart3621a712009-04-06 18:47:14 -040010149 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010150 * @srcp: Source memory pointer.
10151 * @destp: Destination memory pointer.
10152 * @cnt: Number of words required to be copied.
10153 *
10154 * This function is used for copying data between driver memory
10155 * and the SLI memory. This function also changes the endianness
10156 * of each word if native endianness is different from SLI
10157 * endianness. This function can be called with or without
10158 * lock.
10159 **/
dea31012005-04-17 16:05:31 -050010160void
10161lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10162{
10163 uint32_t *src = srcp;
10164 uint32_t *dest = destp;
10165 uint32_t ldata;
10166 int i;
10167
10168 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10169 ldata = *src;
10170 ldata = le32_to_cpu(ldata);
10171 *dest = ldata;
10172 src++;
10173 dest++;
10174 }
10175}
10176
James Smarte59058c2008-08-24 21:49:00 -040010177
10178/**
James Smarta0c87cb2009-07-19 10:01:10 -040010179 * lpfc_sli_bemem_bcopy - SLI memory copy function
10180 * @srcp: Source memory pointer.
10181 * @destp: Destination memory pointer.
10182 * @cnt: Number of words required to be copied.
10183 *
10184 * This function is used for copying data between a data structure
10185 * with big endian representation to local endianness.
10186 * This function can be called with or without lock.
10187 **/
10188void
10189lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10190{
10191 uint32_t *src = srcp;
10192 uint32_t *dest = destp;
10193 uint32_t ldata;
10194 int i;
10195
10196 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10197 ldata = *src;
10198 ldata = be32_to_cpu(ldata);
10199 *dest = ldata;
10200 src++;
10201 dest++;
10202 }
10203}
10204
10205/**
James Smart3621a712009-04-06 18:47:14 -040010206 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010207 * @phba: Pointer to HBA context object.
10208 * @pring: Pointer to driver SLI ring object.
10209 * @mp: Pointer to driver buffer object.
10210 *
10211 * This function is called with no lock held.
10212 * It always return zero after adding the buffer to the postbufq
10213 * buffer list.
10214 **/
dea31012005-04-17 16:05:31 -050010215int
James Smart2e0fef82007-06-17 19:56:36 -050010216lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10217 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010218{
10219 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10220 later */
James Smart2e0fef82007-06-17 19:56:36 -050010221 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010222 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010223 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010224 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010225 return 0;
10226}
10227
James Smarte59058c2008-08-24 21:49:00 -040010228/**
James Smart3621a712009-04-06 18:47:14 -040010229 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010230 * @phba: Pointer to HBA context object.
10231 *
10232 * When HBQ is enabled, buffers are searched based on tags. This function
10233 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10234 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10235 * does not conflict with tags of buffer posted for unsolicited events.
10236 * The function returns the allocated tag. The function is called with
10237 * no locks held.
10238 **/
James Smart76bb24e2007-10-27 13:38:00 -040010239uint32_t
10240lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10241{
10242 spin_lock_irq(&phba->hbalock);
10243 phba->buffer_tag_count++;
10244 /*
10245 * Always set the QUE_BUFTAG_BIT to distiguish between
10246 * a tag assigned by HBQ.
10247 */
10248 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10249 spin_unlock_irq(&phba->hbalock);
10250 return phba->buffer_tag_count;
10251}
10252
James Smarte59058c2008-08-24 21:49:00 -040010253/**
James Smart3621a712009-04-06 18:47:14 -040010254 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010255 * @phba: Pointer to HBA context object.
10256 * @pring: Pointer to driver SLI ring object.
10257 * @tag: Buffer tag.
10258 *
10259 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10260 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10261 * iocb is posted to the response ring with the tag of the buffer.
10262 * This function searches the pring->postbufq list using the tag
10263 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10264 * iocb. If the buffer is found then lpfc_dmabuf object of the
10265 * buffer is returned to the caller else NULL is returned.
10266 * This function is called with no lock held.
10267 **/
James Smart76bb24e2007-10-27 13:38:00 -040010268struct lpfc_dmabuf *
10269lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10270 uint32_t tag)
10271{
10272 struct lpfc_dmabuf *mp, *next_mp;
10273 struct list_head *slp = &pring->postbufq;
10274
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010275 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010276 spin_lock_irq(&phba->hbalock);
10277 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10278 if (mp->buffer_tag == tag) {
10279 list_del_init(&mp->list);
10280 pring->postbufq_cnt--;
10281 spin_unlock_irq(&phba->hbalock);
10282 return mp;
10283 }
10284 }
10285
10286 spin_unlock_irq(&phba->hbalock);
10287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010288 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010289 "ring %d Data x%lx x%p x%p x%x\n",
10290 pring->ringno, (unsigned long) tag,
10291 slp->next, slp->prev, pring->postbufq_cnt);
10292
10293 return NULL;
10294}
dea31012005-04-17 16:05:31 -050010295
James Smarte59058c2008-08-24 21:49:00 -040010296/**
James Smart3621a712009-04-06 18:47:14 -040010297 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010298 * @phba: Pointer to HBA context object.
10299 * @pring: Pointer to driver SLI ring object.
10300 * @phys: DMA address of the buffer.
10301 *
10302 * This function searches the buffer list using the dma_address
10303 * of unsolicited event to find the driver's lpfc_dmabuf object
10304 * corresponding to the dma_address. The function returns the
10305 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10306 * This function is called by the ct and els unsolicited event
10307 * handlers to get the buffer associated with the unsolicited
10308 * event.
10309 *
10310 * This function is called with no lock held.
10311 **/
dea31012005-04-17 16:05:31 -050010312struct lpfc_dmabuf *
10313lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10314 dma_addr_t phys)
10315{
10316 struct lpfc_dmabuf *mp, *next_mp;
10317 struct list_head *slp = &pring->postbufq;
10318
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010319 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010320 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010321 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10322 if (mp->phys == phys) {
10323 list_del_init(&mp->list);
10324 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010325 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010326 return mp;
10327 }
10328 }
10329
James Smart2e0fef82007-06-17 19:56:36 -050010330 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010332 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010333 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010334 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010335 slp->next, slp->prev, pring->postbufq_cnt);
10336 return NULL;
10337}
10338
James Smarte59058c2008-08-24 21:49:00 -040010339/**
James Smart3621a712009-04-06 18:47:14 -040010340 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010341 * @phba: Pointer to HBA context object.
10342 * @cmdiocb: Pointer to driver command iocb object.
10343 * @rspiocb: Pointer to driver response iocb object.
10344 *
10345 * This function is the completion handler for the abort iocbs for
10346 * ELS commands. This function is called from the ELS ring event
10347 * handler with no lock held. This function frees memory resources
10348 * associated with the abort iocb.
10349 **/
dea31012005-04-17 16:05:31 -050010350static void
James Smart2e0fef82007-06-17 19:56:36 -050010351lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10352 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010353{
James Smart2e0fef82007-06-17 19:56:36 -050010354 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010355 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010356 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010357
10358 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010359
10360 /*
10361 * Assume that the port already completed and returned, or
10362 * will return the iocb. Just Log the message.
10363 */
James Smart2680eea2007-04-25 09:52:55 -040010364 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10365 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10366
James Smart2e0fef82007-06-17 19:56:36 -050010367 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010368 if (phba->sli_rev < LPFC_SLI_REV4) {
10369 if (abort_iotag != 0 &&
10370 abort_iotag <= phba->sli.last_iotag)
10371 abort_iocb =
10372 phba->sli.iocbq_lookup[abort_iotag];
10373 } else
10374 /* For sli4 the abort_tag is the XRI,
10375 * so the abort routine puts the iotag of the iocb
10376 * being aborted in the context field of the abort
10377 * IOCB.
10378 */
10379 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010380
James Smart2a9bf3d2010-06-07 15:24:45 -040010381 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10382 "0327 Cannot abort els iocb %p "
10383 "with tag %x context %x, abort status %x, "
10384 "abort code %x\n",
10385 abort_iocb, abort_iotag, abort_context,
10386 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010387
James Smartff78d8f2011-12-13 13:21:35 -050010388 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010389 }
James Bottomley604a3e32005-10-29 10:28:33 -050010390 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010391 return;
10392}
10393
James Smarte59058c2008-08-24 21:49:00 -040010394/**
James Smart3621a712009-04-06 18:47:14 -040010395 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010396 * @phba: Pointer to HBA context object.
10397 * @cmdiocb: Pointer to driver command iocb object.
10398 * @rspiocb: Pointer to driver response iocb object.
10399 *
10400 * The function is called from SLI ring event handler with no
10401 * lock held. This function is the completion handler for ELS commands
10402 * which are aborted. The function frees memory resources used for
10403 * the aborted ELS commands.
10404 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010405static void
10406lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10407 struct lpfc_iocbq *rspiocb)
10408{
10409 IOCB_t *irsp = &rspiocb->iocb;
10410
10411 /* ELS cmd tag <ulpIoTag> completes */
10412 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010413 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010414 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010415 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010416 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010417 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10418 lpfc_ct_free_iocb(phba, cmdiocb);
10419 else
10420 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010421 return;
10422}
10423
James Smarte59058c2008-08-24 21:49:00 -040010424/**
James Smart5af5eee2010-10-22 11:06:38 -040010425 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010426 * @phba: Pointer to HBA context object.
10427 * @pring: Pointer to driver SLI ring object.
10428 * @cmdiocb: Pointer to driver command iocb object.
10429 *
James Smart5af5eee2010-10-22 11:06:38 -040010430 * This function issues an abort iocb for the provided command iocb down to
10431 * the port. Other than the case the outstanding command iocb is an abort
10432 * request, this function issues abort out unconditionally. This function is
10433 * called with hbalock held. The function returns 0 when it fails due to
10434 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010435 **/
James Smart5af5eee2010-10-22 11:06:38 -040010436static int
10437lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010438 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010439{
James Smart2e0fef82007-06-17 19:56:36 -050010440 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010441 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010442 IOCB_t *icmd = NULL;
10443 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010444 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010445 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010446
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010447 lockdep_assert_held(&phba->hbalock);
10448
James Smart92d7f7b2007-06-17 19:56:38 -050010449 /*
10450 * There are certain command types we don't want to abort. And we
10451 * don't want to abort commands that are already in the process of
10452 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010453 */
10454 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010455 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010456 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10457 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010458 return 0;
10459
dea31012005-04-17 16:05:31 -050010460 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010461 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010462 if (abtsiocbp == NULL)
10463 return 0;
dea31012005-04-17 16:05:31 -050010464
James Smart07951072007-04-25 09:51:38 -040010465 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010466 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010467 */
10468 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10469
dea31012005-04-17 16:05:31 -050010470 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010471 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10472 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010473 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010474 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010475 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10476 }
James Smartda0436e2009-05-22 14:51:39 -040010477 else
10478 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010479 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010480 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010481
James Smart5ffc2662009-11-18 15:39:44 -050010482 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010483 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010484 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10485 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010486 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10487 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010488
James Smart2e0fef82007-06-17 19:56:36 -050010489 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010490 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10491 else
10492 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10493
10494 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010495 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010496
James Smarte8b62012007-08-02 11:10:09 -040010497 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10498 "0339 Abort xri x%x, original iotag x%x, "
10499 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010500 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010501 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010502 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010503
10504 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010505 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10506 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010507 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010508 /* Note: both hbalock and ring_lock need to be set here */
10509 spin_lock_irqsave(&pring->ring_lock, iflags);
10510 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10511 abtsiocbp, 0);
10512 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10513 } else {
10514 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10515 abtsiocbp, 0);
10516 }
James Smart07951072007-04-25 09:51:38 -040010517
James Smartd7c255b2008-08-24 21:50:00 -040010518 if (retval)
10519 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010520
10521 /*
10522 * Caller to this routine should check for IOCB_ERROR
10523 * and handle it properly. This routine no longer removes
10524 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10525 */
10526 return retval;
10527}
10528
10529/**
10530 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10531 * @phba: Pointer to HBA context object.
10532 * @pring: Pointer to driver SLI ring object.
10533 * @cmdiocb: Pointer to driver command iocb object.
10534 *
10535 * This function issues an abort iocb for the provided command iocb. In case
10536 * of unloading, the abort iocb will not be issued to commands on the ELS
10537 * ring. Instead, the callback function shall be changed to those commands
10538 * so that nothing happens when them finishes. This function is called with
10539 * hbalock held. The function returns 0 when the command iocb is an abort
10540 * request.
10541 **/
10542int
10543lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10544 struct lpfc_iocbq *cmdiocb)
10545{
10546 struct lpfc_vport *vport = cmdiocb->vport;
10547 int retval = IOCB_ERROR;
10548 IOCB_t *icmd = NULL;
10549
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010550 lockdep_assert_held(&phba->hbalock);
10551
James Smart5af5eee2010-10-22 11:06:38 -040010552 /*
10553 * There are certain command types we don't want to abort. And we
10554 * don't want to abort commands that are already in the process of
10555 * being aborted.
10556 */
10557 icmd = &cmdiocb->iocb;
10558 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10559 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10560 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10561 return 0;
10562
10563 /*
10564 * If we're unloading, don't abort iocb on the ELS ring, but change
10565 * the callback so that nothing happens when it finishes.
10566 */
10567 if ((vport->load_flag & FC_UNLOADING) &&
10568 (pring->ringno == LPFC_ELS_RING)) {
10569 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10570 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10571 else
10572 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10573 goto abort_iotag_exit;
10574 }
10575
10576 /* Now, we try to issue the abort to the cmdiocb out */
10577 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10578
James Smart07951072007-04-25 09:51:38 -040010579abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010580 /*
10581 * Caller to this routine should check for IOCB_ERROR
10582 * and handle it properly. This routine no longer removes
10583 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010584 */
James Smart2e0fef82007-06-17 19:56:36 -050010585 return retval;
dea31012005-04-17 16:05:31 -050010586}
10587
James Smarte59058c2008-08-24 21:49:00 -040010588/**
James Smart895427b2017-02-12 13:52:30 -080010589 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10590 * @phba: Pointer to HBA context object.
10591 * @pring: Pointer to driver SLI ring object.
10592 * @cmdiocb: Pointer to driver command iocb object.
10593 *
10594 * This function issues an abort iocb for the provided command iocb down to
10595 * the port. Other than the case the outstanding command iocb is an abort
10596 * request, this function issues abort out unconditionally. This function is
10597 * called with hbalock held. The function returns 0 when it fails due to
10598 * memory allocation failure or when the command iocb is an abort request.
10599 **/
10600static int
10601lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10602 struct lpfc_iocbq *cmdiocb)
10603{
10604 struct lpfc_vport *vport = cmdiocb->vport;
10605 struct lpfc_iocbq *abtsiocbp;
10606 union lpfc_wqe *abts_wqe;
10607 int retval;
10608
10609 /*
10610 * There are certain command types we don't want to abort. And we
10611 * don't want to abort commands that are already in the process of
10612 * being aborted.
10613 */
10614 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10615 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10616 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10617 return 0;
10618
10619 /* issue ABTS for this io based on iotag */
10620 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10621 if (abtsiocbp == NULL)
10622 return 0;
10623
10624 /* This signals the response to set the correct status
10625 * before calling the completion handler
10626 */
10627 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10628
10629 /* Complete prepping the abort wqe and issue to the FW. */
10630 abts_wqe = &abtsiocbp->wqe;
10631 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10632 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10633
10634 /* Explicitly set reserved fields to zero.*/
10635 abts_wqe->abort_cmd.rsrvd4 = 0;
10636 abts_wqe->abort_cmd.rsrvd5 = 0;
10637
10638 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10639 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10640 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10641
10642 /* word 7 */
10643 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10644 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10645 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10646 cmdiocb->iocb.ulpClass);
10647
10648 /* word 8 - tell the FW to abort the IO associated with this
10649 * outstanding exchange ID.
10650 */
10651 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10652
10653 /* word 9 - this is the iotag for the abts_wqe completion. */
10654 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10655 abtsiocbp->iotag);
10656
10657 /* word 10 */
10658 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10659 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10660 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10661
10662 /* word 11 */
10663 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10664 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10665 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10666
10667 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10668 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10669 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010670 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010671 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10672 if (retval == IOCB_ERROR) {
10673 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10674 "6147 Failed abts issue_wqe with status x%x "
10675 "for oxid x%x\n",
10676 retval, cmdiocb->sli4_xritag);
10677 lpfc_sli_release_iocbq(phba, abtsiocbp);
10678 return retval;
10679 }
10680
10681 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10682 "6148 Drv Abort NVME Request Issued for "
10683 "ox_id x%x on reqtag x%x\n",
10684 cmdiocb->sli4_xritag,
10685 abtsiocbp->iotag);
10686
10687 return retval;
10688}
10689
10690/**
James Smart5af5eee2010-10-22 11:06:38 -040010691 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10692 * @phba: pointer to lpfc HBA data structure.
10693 *
10694 * This routine will abort all pending and outstanding iocbs to an HBA.
10695 **/
10696void
10697lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10698{
10699 struct lpfc_sli *psli = &phba->sli;
10700 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010701 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010702 int i;
10703
James Smart895427b2017-02-12 13:52:30 -080010704 if (phba->sli_rev != LPFC_SLI_REV4) {
10705 for (i = 0; i < psli->num_rings; i++) {
10706 pring = &psli->sli3_ring[i];
10707 lpfc_sli_abort_iocb_ring(phba, pring);
10708 }
10709 return;
10710 }
10711 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10712 pring = qp->pring;
10713 if (!pring)
10714 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010715 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010716 }
10717}
10718
10719/**
James Smart3621a712009-04-06 18:47:14 -040010720 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010721 * @iocbq: Pointer to driver iocb object.
10722 * @vport: Pointer to driver virtual port object.
10723 * @tgt_id: SCSI ID of the target.
10724 * @lun_id: LUN ID of the scsi device.
10725 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10726 *
James Smart3621a712009-04-06 18:47:14 -040010727 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010728 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10729 * 0 if the filtering criteria is met for the given iocb and will return
10730 * 1 if the filtering criteria is not met.
10731 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10732 * given iocb is for the SCSI device specified by vport, tgt_id and
10733 * lun_id parameter.
10734 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10735 * given iocb is for the SCSI target specified by vport and tgt_id
10736 * parameters.
10737 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10738 * given iocb is for the SCSI host associated with the given vport.
10739 * This function is called with no locks held.
10740 **/
dea31012005-04-17 16:05:31 -050010741static int
James Smart51ef4c22007-08-02 11:10:31 -040010742lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10743 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010744 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010745{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010746 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010747 int rc = 1;
10748
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010749 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10750 return rc;
10751
James Smart51ef4c22007-08-02 11:10:31 -040010752 if (iocbq->vport != vport)
10753 return rc;
10754
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010755 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010756
James Smart495a7142008-06-14 22:52:59 -040010757 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010758 return rc;
10759
10760 switch (ctx_cmd) {
10761 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010762 if ((lpfc_cmd->rdata->pnode) &&
10763 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10764 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010765 rc = 0;
10766 break;
10767 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010768 if ((lpfc_cmd->rdata->pnode) &&
10769 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010770 rc = 0;
10771 break;
dea31012005-04-17 16:05:31 -050010772 case LPFC_CTX_HOST:
10773 rc = 0;
10774 break;
10775 default:
10776 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010777 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010778 break;
10779 }
10780
10781 return rc;
10782}
10783
James Smarte59058c2008-08-24 21:49:00 -040010784/**
James Smart3621a712009-04-06 18:47:14 -040010785 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010786 * @vport: Pointer to virtual port.
10787 * @tgt_id: SCSI ID of the target.
10788 * @lun_id: LUN ID of the scsi device.
10789 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10790 *
10791 * This function returns number of FCP commands pending for the vport.
10792 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10793 * commands pending on the vport associated with SCSI device specified
10794 * by tgt_id and lun_id parameters.
10795 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10796 * commands pending on the vport associated with SCSI target specified
10797 * by tgt_id parameter.
10798 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10799 * commands pending on the vport.
10800 * This function returns the number of iocbs which satisfy the filter.
10801 * This function is called without any lock held.
10802 **/
dea31012005-04-17 16:05:31 -050010803int
James Smart51ef4c22007-08-02 11:10:31 -040010804lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10805 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010806{
James Smart51ef4c22007-08-02 11:10:31 -040010807 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010808 struct lpfc_iocbq *iocbq;
10809 int sum, i;
dea31012005-04-17 16:05:31 -050010810
Johannes Thumshirn31979002016-07-18 16:06:03 +020010811 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010812 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10813 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010814
James Smart51ef4c22007-08-02 11:10:31 -040010815 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10816 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010817 sum++;
dea31012005-04-17 16:05:31 -050010818 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010819 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010820
dea31012005-04-17 16:05:31 -050010821 return sum;
10822}
10823
James Smarte59058c2008-08-24 21:49:00 -040010824/**
James Smart3621a712009-04-06 18:47:14 -040010825 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010826 * @phba: Pointer to HBA context object
10827 * @cmdiocb: Pointer to command iocb object.
10828 * @rspiocb: Pointer to response iocb object.
10829 *
10830 * This function is called when an aborted FCP iocb completes. This
10831 * function is called by the ring event handler with no lock held.
10832 * This function frees the iocb.
10833 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010834void
James Smart2e0fef82007-06-17 19:56:36 -050010835lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10836 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010837{
James Smartcb69f7d2011-12-13 13:21:57 -050010838 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010839 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010840 "original iotag x%x, abort cmd iotag x%x "
10841 "status 0x%x, reason 0x%x\n",
10842 cmdiocb->iocb.un.acxri.abortContextTag,
10843 cmdiocb->iocb.un.acxri.abortIoTag,
10844 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10845 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010846 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010847 return;
10848}
10849
James Smarte59058c2008-08-24 21:49:00 -040010850/**
James Smart3621a712009-04-06 18:47:14 -040010851 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010852 * @vport: Pointer to virtual port.
10853 * @pring: Pointer to driver SLI ring object.
10854 * @tgt_id: SCSI ID of the target.
10855 * @lun_id: LUN ID of the scsi device.
10856 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10857 *
10858 * This function sends an abort command for every SCSI command
10859 * associated with the given virtual port pending on the ring
10860 * filtered by lpfc_sli_validate_fcp_iocb function.
10861 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10862 * FCP iocbs associated with lun specified by tgt_id and lun_id
10863 * parameters
10864 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10865 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10866 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10867 * FCP iocbs associated with virtual port.
10868 * This function returns number of iocbs it failed to abort.
10869 * This function is called with no locks held.
10870 **/
dea31012005-04-17 16:05:31 -050010871int
James Smart51ef4c22007-08-02 11:10:31 -040010872lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10873 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010874{
James Smart51ef4c22007-08-02 11:10:31 -040010875 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010876 struct lpfc_iocbq *iocbq;
10877 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010878 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010879 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010880 int i;
dea31012005-04-17 16:05:31 -050010881
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010882 for (i = 1; i <= phba->sli.last_iotag; i++) {
10883 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010884
James Smart51ef4c22007-08-02 11:10:31 -040010885 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010886 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010887 continue;
10888
James Smartafbd8d82013-09-06 12:22:13 -040010889 /*
10890 * If the iocbq is already being aborted, don't take a second
10891 * action, but do count it.
10892 */
10893 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10894 continue;
10895
dea31012005-04-17 16:05:31 -050010896 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010897 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010898 if (abtsiocb == NULL) {
10899 errcnt++;
10900 continue;
10901 }
dea31012005-04-17 16:05:31 -050010902
James Smartafbd8d82013-09-06 12:22:13 -040010903 /* indicate the IO is being aborted by the driver. */
10904 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10905
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010906 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010907 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10908 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010909 if (phba->sli_rev == LPFC_SLI_REV4)
10910 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10911 else
10912 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010913 abtsiocb->iocb.ulpLe = 1;
10914 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010915 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010916
James Smart5ffc2662009-11-18 15:39:44 -050010917 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010918 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010919 if (iocbq->iocb_flag & LPFC_IO_FCP)
10920 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010921 if (iocbq->iocb_flag & LPFC_IO_FOF)
10922 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010923
James Smart2e0fef82007-06-17 19:56:36 -050010924 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010925 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10926 else
10927 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10928
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010929 /* Setup callback routine and issue the command. */
10930 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010931 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10932 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010933 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010934 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010935 errcnt++;
10936 continue;
10937 }
10938 }
10939
10940 return errcnt;
10941}
10942
James Smarte59058c2008-08-24 21:49:00 -040010943/**
James Smart98912dda2014-04-04 13:52:31 -040010944 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10945 * @vport: Pointer to virtual port.
10946 * @pring: Pointer to driver SLI ring object.
10947 * @tgt_id: SCSI ID of the target.
10948 * @lun_id: LUN ID of the scsi device.
10949 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10950 *
10951 * This function sends an abort command for every SCSI command
10952 * associated with the given virtual port pending on the ring
10953 * filtered by lpfc_sli_validate_fcp_iocb function.
10954 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10955 * FCP iocbs associated with lun specified by tgt_id and lun_id
10956 * parameters
10957 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10958 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10959 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10960 * FCP iocbs associated with virtual port.
10961 * This function returns number of iocbs it aborted .
10962 * This function is called with no locks held right after a taskmgmt
10963 * command is sent.
10964 **/
10965int
10966lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10967 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10968{
10969 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010970 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010971 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010972 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010973 struct lpfc_iocbq *iocbq;
10974 IOCB_t *icmd;
10975 int sum, i, ret_val;
10976 unsigned long iflags;
10977 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040010978
10979 spin_lock_irq(&phba->hbalock);
10980
10981 /* all I/Os are in process of being flushed */
10982 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10983 spin_unlock_irq(&phba->hbalock);
10984 return 0;
10985 }
10986 sum = 0;
10987
10988 for (i = 1; i <= phba->sli.last_iotag; i++) {
10989 iocbq = phba->sli.iocbq_lookup[i];
10990
10991 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10992 cmd) != 0)
10993 continue;
10994
10995 /*
10996 * If the iocbq is already being aborted, don't take a second
10997 * action, but do count it.
10998 */
10999 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11000 continue;
11001
11002 /* issue ABTS for this IOCB based on iotag */
11003 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11004 if (abtsiocbq == NULL)
11005 continue;
11006
11007 icmd = &iocbq->iocb;
11008 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11009 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11010 if (phba->sli_rev == LPFC_SLI_REV4)
11011 abtsiocbq->iocb.un.acxri.abortIoTag =
11012 iocbq->sli4_xritag;
11013 else
11014 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11015 abtsiocbq->iocb.ulpLe = 1;
11016 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11017 abtsiocbq->vport = vport;
11018
11019 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011020 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011021 if (iocbq->iocb_flag & LPFC_IO_FCP)
11022 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011023 if (iocbq->iocb_flag & LPFC_IO_FOF)
11024 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011025
James Smart8c50d252014-09-03 12:58:16 -040011026 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11027 ndlp = lpfc_cmd->rdata->pnode;
11028
11029 if (lpfc_is_link_up(phba) &&
11030 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011031 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11032 else
11033 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11034
11035 /* Setup callback routine and issue the command. */
11036 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11037
11038 /*
11039 * Indicate the IO is being aborted by the driver and set
11040 * the caller's flag into the aborted IO.
11041 */
11042 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11043
11044 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011045 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11046 if (pring_s4 == NULL)
11047 continue;
James Smart98912dda2014-04-04 13:52:31 -040011048 /* Note: both hbalock and ring_lock must be set here */
11049 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11050 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11051 abtsiocbq, 0);
11052 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11053 } else {
11054 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11055 abtsiocbq, 0);
11056 }
11057
11058
11059 if (ret_val == IOCB_ERROR)
11060 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11061 else
11062 sum++;
11063 }
11064 spin_unlock_irq(&phba->hbalock);
11065 return sum;
11066}
11067
11068/**
James Smart3621a712009-04-06 18:47:14 -040011069 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011070 * @phba: Pointer to HBA context object.
11071 * @cmdiocbq: Pointer to command iocb.
11072 * @rspiocbq: Pointer to response iocb.
11073 *
11074 * This function is the completion handler for iocbs issued using
11075 * lpfc_sli_issue_iocb_wait function. This function is called by the
11076 * ring event handler function without any lock held. This function
11077 * can be called from both worker thread context and interrupt
11078 * context. This function also can be called from other thread which
11079 * cleans up the SLI layer objects.
11080 * This function copy the contents of the response iocb to the
11081 * response iocb memory object provided by the caller of
11082 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11083 * sleeps for the iocb completion.
11084 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011085static void
11086lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11087 struct lpfc_iocbq *cmdiocbq,
11088 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011089{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011090 wait_queue_head_t *pdone_q;
11091 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011092 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011093
James Smart2e0fef82007-06-17 19:56:36 -050011094 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011095 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11096
11097 /*
11098 * A time out has occurred for the iocb. If a time out
11099 * completion handler has been supplied, call it. Otherwise,
11100 * just free the iocbq.
11101 */
11102
11103 spin_unlock_irqrestore(&phba->hbalock, iflags);
11104 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11105 cmdiocbq->wait_iocb_cmpl = NULL;
11106 if (cmdiocbq->iocb_cmpl)
11107 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11108 else
11109 lpfc_sli_release_iocbq(phba, cmdiocbq);
11110 return;
11111 }
11112
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011113 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11114 if (cmdiocbq->context2 && rspiocbq)
11115 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11116 &rspiocbq->iocb, sizeof(IOCB_t));
11117
James Smart0f65ff62010-02-26 14:14:23 -050011118 /* Set the exchange busy flag for task management commands */
11119 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11120 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11121 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11122 cur_iocbq);
11123 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11124 }
11125
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011126 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011127 if (pdone_q)
11128 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011129 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011130 return;
11131}
11132
James Smarte59058c2008-08-24 21:49:00 -040011133/**
James Smartd11e31d2009-06-10 17:23:06 -040011134 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11135 * @phba: Pointer to HBA context object..
11136 * @piocbq: Pointer to command iocb.
11137 * @flag: Flag to test.
11138 *
11139 * This routine grabs the hbalock and then test the iocb_flag to
11140 * see if the passed in flag is set.
11141 * Returns:
11142 * 1 if flag is set.
11143 * 0 if flag is not set.
11144 **/
11145static int
11146lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11147 struct lpfc_iocbq *piocbq, uint32_t flag)
11148{
11149 unsigned long iflags;
11150 int ret;
11151
11152 spin_lock_irqsave(&phba->hbalock, iflags);
11153 ret = piocbq->iocb_flag & flag;
11154 spin_unlock_irqrestore(&phba->hbalock, iflags);
11155 return ret;
11156
11157}
11158
11159/**
James Smart3621a712009-04-06 18:47:14 -040011160 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011161 * @phba: Pointer to HBA context object..
11162 * @pring: Pointer to sli ring.
11163 * @piocb: Pointer to command iocb.
11164 * @prspiocbq: Pointer to response iocb.
11165 * @timeout: Timeout in number of seconds.
11166 *
11167 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011168 * iocb to complete. The iocb_cmpl field of the shall be used
11169 * to handle iocbs which time out. If the field is NULL, the
11170 * function shall free the iocbq structure. If more clean up is
11171 * needed, the caller is expected to provide a completion function
11172 * that will provide the needed clean up. If the iocb command is
11173 * not completed within timeout seconds, the function will either
11174 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11175 * completion function set in the iocb_cmpl field and then return
11176 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11177 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011178 * The function waits for the iocb completion using an
11179 * non-interruptible wait.
11180 * This function will sleep while waiting for iocb completion.
11181 * So, this function should not be called from any context which
11182 * does not allow sleeping. Due to the same reason, this function
11183 * cannot be called with interrupt disabled.
11184 * This function assumes that the iocb completions occur while
11185 * this function sleep. So, this function cannot be called from
11186 * the thread which process iocb completion for this ring.
11187 * This function clears the iocb_flag of the iocb object before
11188 * issuing the iocb and the iocb completion handler sets this
11189 * flag and wakes this thread when the iocb completes.
11190 * The contents of the response iocb will be copied to prspiocbq
11191 * by the completion handler when the command completes.
11192 * This function returns IOCB_SUCCESS when success.
11193 * This function is called with no lock held.
11194 **/
dea31012005-04-17 16:05:31 -050011195int
James Smart2e0fef82007-06-17 19:56:36 -050011196lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011197 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011198 struct lpfc_iocbq *piocb,
11199 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011200 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011201{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011202 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011203 long timeleft, timeout_req = 0;
11204 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011205 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011206 struct lpfc_iocbq *iocb;
11207 int txq_cnt = 0;
11208 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011209 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011210 unsigned long iflags;
11211 bool iocb_completed = true;
11212
James Smart895427b2017-02-12 13:52:30 -080011213 if (phba->sli_rev >= LPFC_SLI_REV4)
11214 pring = lpfc_sli4_calc_ring(phba, piocb);
11215 else
11216 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011217 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011218 * If the caller has provided a response iocbq buffer, then context2
11219 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011220 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011221 if (prspiocbq) {
11222 if (piocb->context2)
11223 return IOCB_ERROR;
11224 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011225 }
11226
James Smart5a0916b2013-07-15 18:31:42 -040011227 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011228 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11229 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011230 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011231
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011232 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011233 if (lpfc_readl(phba->HCregaddr, &creg_val))
11234 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011235 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11236 writel(creg_val, phba->HCregaddr);
11237 readl(phba->HCregaddr); /* flush */
11238 }
11239
James Smart2a9bf3d2010-06-07 15:24:45 -040011240 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11241 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011242 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011243 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011244 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011245 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011246 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011247 spin_lock_irqsave(&phba->hbalock, iflags);
11248 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011249
James Smart5a0916b2013-07-15 18:31:42 -040011250 /*
11251 * IOCB timed out. Inform the wake iocb wait
11252 * completion function and set local status
11253 */
11254
11255 iocb_completed = false;
11256 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11257 }
11258 spin_unlock_irqrestore(&phba->hbalock, iflags);
11259 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011260 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011261 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011262 /* Note: we are not indicating if the IOCB has a success
11263 * status or not - that's for the caller to check.
11264 * IOCB_SUCCESS means just that the command was sent and
11265 * completed. Not that it completed successfully.
11266 * */
James Smart7054a602007-04-25 09:52:34 -040011267 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011268 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011269 "0338 IOCB wait timeout error - no "
11270 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011271 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011272 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011273 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011274 "0330 IOCB wake NOT set, "
11275 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011276 timeout, (timeleft / jiffies));
11277 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011278 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011279 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011280 if (phba->cfg_log_verbose & LOG_SLI) {
11281 list_for_each_entry(iocb, &pring->txq, list) {
11282 txq_cnt++;
11283 }
11284 list_for_each_entry(iocb, &pring->txcmplq, list) {
11285 txcmplq_cnt++;
11286 }
11287 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11288 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11289 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11290 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011291 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011292 } else {
11293 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011294 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011295 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011296 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011297 }
11298
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011299 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011300 if (lpfc_readl(phba->HCregaddr, &creg_val))
11301 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011302 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11303 writel(creg_val, phba->HCregaddr);
11304 readl(phba->HCregaddr); /* flush */
11305 }
11306
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011307 if (prspiocbq)
11308 piocb->context2 = NULL;
11309
11310 piocb->context_un.wait_queue = NULL;
11311 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011312 return retval;
11313}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011314
James Smarte59058c2008-08-24 21:49:00 -040011315/**
James Smart3621a712009-04-06 18:47:14 -040011316 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011317 * @phba: Pointer to HBA context object.
11318 * @pmboxq: Pointer to driver mailbox object.
11319 * @timeout: Timeout in number of seconds.
11320 *
11321 * This function issues the mailbox to firmware and waits for the
11322 * mailbox command to complete. If the mailbox command is not
11323 * completed within timeout seconds, it returns MBX_TIMEOUT.
11324 * The function waits for the mailbox completion using an
11325 * interruptible wait. If the thread is woken up due to a
11326 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11327 * should not free the mailbox resources, if this function returns
11328 * MBX_TIMEOUT.
11329 * This function will sleep while waiting for mailbox completion.
11330 * So, this function should not be called from any context which
11331 * does not allow sleeping. Due to the same reason, this function
11332 * cannot be called with interrupt disabled.
11333 * This function assumes that the mailbox completion occurs while
11334 * this function sleep. So, this function cannot be called from
11335 * the worker thread which processes mailbox completion.
11336 * This function is called in the context of HBA management
11337 * applications.
11338 * This function returns MBX_SUCCESS when successful.
11339 * This function is called with no lock held.
11340 **/
dea31012005-04-17 16:05:31 -050011341int
James Smart2e0fef82007-06-17 19:56:36 -050011342lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011343 uint32_t timeout)
11344{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011345 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011346 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011347 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011348 unsigned long flag;
dea31012005-04-17 16:05:31 -050011349
James Smartb230b8a2013-05-31 17:05:27 -040011350 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011351 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011352 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011353
James Smart495a7142008-06-14 22:52:59 -040011354 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011355 /* setup wake call as IOCB callback */
11356 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11357 /* setup context field to pass wait_queue pointer to wake function */
11358 pmboxq->context1 = &done_q;
11359
dea31012005-04-17 16:05:31 -050011360 /* now issue the command */
11361 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011362 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011363 wait_event_interruptible_timeout(done_q,
11364 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011365 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011366
James Smart858c9f62007-06-17 19:56:39 -050011367 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011368 /* restore the possible extended buffer for free resource */
11369 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011370 /*
11371 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11372 * else do not free the resources.
11373 */
James Smartd7c47992010-06-08 18:31:54 -040011374 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011375 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011376 } else {
James Smart7054a602007-04-25 09:52:34 -040011377 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011378 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11379 }
11380 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011381 } else {
11382 /* restore the possible extended buffer for free resource */
11383 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011384 }
11385
dea31012005-04-17 16:05:31 -050011386 return retval;
11387}
11388
James Smarte59058c2008-08-24 21:49:00 -040011389/**
James Smart3772a992009-05-22 14:50:54 -040011390 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011391 * @phba: Pointer to HBA context.
11392 *
James Smart3772a992009-05-22 14:50:54 -040011393 * This function is called to shutdown the driver's mailbox sub-system.
11394 * It first marks the mailbox sub-system is in a block state to prevent
11395 * the asynchronous mailbox command from issued off the pending mailbox
11396 * command queue. If the mailbox command sub-system shutdown is due to
11397 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11398 * the mailbox sub-system flush routine to forcefully bring down the
11399 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11400 * as with offline or HBA function reset), this routine will wait for the
11401 * outstanding mailbox command to complete before invoking the mailbox
11402 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011403 **/
James Smart3772a992009-05-22 14:50:54 -040011404void
James Smart618a5232012-06-12 13:54:36 -040011405lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011406{
James Smart3772a992009-05-22 14:50:54 -040011407 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011408 unsigned long timeout;
11409
James Smart618a5232012-06-12 13:54:36 -040011410 if (mbx_action == LPFC_MBX_NO_WAIT) {
11411 /* delay 100ms for port state */
11412 msleep(100);
11413 lpfc_sli_mbox_sys_flush(phba);
11414 return;
11415 }
James Smarta183a152011-10-10 21:32:43 -040011416 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011417
James Smart3772a992009-05-22 14:50:54 -040011418 spin_lock_irq(&phba->hbalock);
11419 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011420
11421 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011422 /* Determine how long we might wait for the active mailbox
11423 * command to be gracefully completed by firmware.
11424 */
James Smarta183a152011-10-10 21:32:43 -040011425 if (phba->sli.mbox_active)
11426 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11427 phba->sli.mbox_active) *
11428 1000) + jiffies;
11429 spin_unlock_irq(&phba->hbalock);
11430
James Smart3772a992009-05-22 14:50:54 -040011431 while (phba->sli.mbox_active) {
11432 /* Check active mailbox complete status every 2ms */
11433 msleep(2);
11434 if (time_after(jiffies, timeout))
11435 /* Timeout, let the mailbox flush routine to
11436 * forcefully release active mailbox command
11437 */
11438 break;
11439 }
James Smartd7069f02012-03-01 22:36:29 -050011440 } else
11441 spin_unlock_irq(&phba->hbalock);
11442
James Smart3772a992009-05-22 14:50:54 -040011443 lpfc_sli_mbox_sys_flush(phba);
11444}
11445
11446/**
11447 * lpfc_sli_eratt_read - read sli-3 error attention events
11448 * @phba: Pointer to HBA context.
11449 *
11450 * This function is called to read the SLI3 device error attention registers
11451 * for possible error attention events. The caller must hold the hostlock
11452 * with spin_lock_irq().
11453 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011454 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011455 * Register and returns 0 otherwise.
11456 **/
11457static int
11458lpfc_sli_eratt_read(struct lpfc_hba *phba)
11459{
James Smarted957682007-06-17 19:56:37 -050011460 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011461
James Smart3772a992009-05-22 14:50:54 -040011462 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011463 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11464 goto unplug_err;
11465
James Smart3772a992009-05-22 14:50:54 -040011466 if (ha_copy & HA_ERATT) {
11467 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011468 if (lpfc_sli_read_hs(phba))
11469 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011470
James Smart3772a992009-05-22 14:50:54 -040011471 /* Check if there is a deferred error condition is active */
11472 if ((HS_FFER1 & phba->work_hs) &&
11473 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011474 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011475 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011476 /* Clear all interrupt enable conditions */
11477 writel(0, phba->HCregaddr);
11478 readl(phba->HCregaddr);
11479 }
11480
11481 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011482 phba->work_ha |= HA_ERATT;
11483 /* Indicate polling handles this ERATT */
11484 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011485 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011486 }
James Smart3772a992009-05-22 14:50:54 -040011487 return 0;
James Smart9940b972011-03-11 16:06:12 -050011488
11489unplug_err:
11490 /* Set the driver HS work bitmap */
11491 phba->work_hs |= UNPLUG_ERR;
11492 /* Set the driver HA work bitmap */
11493 phba->work_ha |= HA_ERATT;
11494 /* Indicate polling handles this ERATT */
11495 phba->hba_flag |= HBA_ERATT_HANDLED;
11496 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011497}
11498
James Smarte59058c2008-08-24 21:49:00 -040011499/**
James Smartda0436e2009-05-22 14:51:39 -040011500 * lpfc_sli4_eratt_read - read sli-4 error attention events
11501 * @phba: Pointer to HBA context.
11502 *
11503 * This function is called to read the SLI4 device error attention registers
11504 * for possible error attention events. The caller must hold the hostlock
11505 * with spin_lock_irq().
11506 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011507 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011508 * Register and returns 0 otherwise.
11509 **/
11510static int
11511lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11512{
11513 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011514 uint32_t if_type, portsmphr;
11515 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011516
James Smart2fcee4b2010-12-15 17:57:46 -050011517 /*
11518 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011519 * registers for error attention. This can be changed later.
11520 */
James Smart2fcee4b2010-12-15 17:57:46 -050011521 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11522 switch (if_type) {
11523 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011524 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11525 &uerr_sta_lo) ||
11526 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11527 &uerr_sta_hi)) {
11528 phba->work_hs |= UNPLUG_ERR;
11529 phba->work_ha |= HA_ERATT;
11530 phba->hba_flag |= HBA_ERATT_HANDLED;
11531 return 1;
11532 }
James Smart2fcee4b2010-12-15 17:57:46 -050011533 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11534 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11536 "1423 HBA Unrecoverable error: "
11537 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11538 "ue_mask_lo_reg=0x%x, "
11539 "ue_mask_hi_reg=0x%x\n",
11540 uerr_sta_lo, uerr_sta_hi,
11541 phba->sli4_hba.ue_mask_lo,
11542 phba->sli4_hba.ue_mask_hi);
11543 phba->work_status[0] = uerr_sta_lo;
11544 phba->work_status[1] = uerr_sta_hi;
11545 phba->work_ha |= HA_ERATT;
11546 phba->hba_flag |= HBA_ERATT_HANDLED;
11547 return 1;
11548 }
11549 break;
11550 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011551 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11552 &portstat_reg.word0) ||
11553 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11554 &portsmphr)){
11555 phba->work_hs |= UNPLUG_ERR;
11556 phba->work_ha |= HA_ERATT;
11557 phba->hba_flag |= HBA_ERATT_HANDLED;
11558 return 1;
11559 }
James Smart2fcee4b2010-12-15 17:57:46 -050011560 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11561 phba->work_status[0] =
11562 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11563 phba->work_status[1] =
11564 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011566 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011567 "port status reg 0x%x, "
11568 "port smphr reg 0x%x, "
11569 "error 1=0x%x, error 2=0x%x\n",
11570 portstat_reg.word0,
11571 portsmphr,
11572 phba->work_status[0],
11573 phba->work_status[1]);
11574 phba->work_ha |= HA_ERATT;
11575 phba->hba_flag |= HBA_ERATT_HANDLED;
11576 return 1;
11577 }
11578 break;
11579 case LPFC_SLI_INTF_IF_TYPE_1:
11580 default:
James Smarta747c9c2009-11-18 15:41:10 -050011581 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011582 "2886 HBA Error Attention on unsupported "
11583 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011584 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011585 }
James Smart2fcee4b2010-12-15 17:57:46 -050011586
James Smartda0436e2009-05-22 14:51:39 -040011587 return 0;
11588}
11589
11590/**
James Smart3621a712009-04-06 18:47:14 -040011591 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011592 * @phba: Pointer to HBA context.
11593 *
James Smart3772a992009-05-22 14:50:54 -040011594 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011595 * error attention register bit for error attention events.
11596 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011597 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011598 * Register and returns 0 otherwise.
11599 **/
11600int
11601lpfc_sli_check_eratt(struct lpfc_hba *phba)
11602{
11603 uint32_t ha_copy;
11604
11605 /* If somebody is waiting to handle an eratt, don't process it
11606 * here. The brdkill function will do this.
11607 */
11608 if (phba->link_flag & LS_IGNORE_ERATT)
11609 return 0;
11610
11611 /* Check if interrupt handler handles this ERATT */
11612 spin_lock_irq(&phba->hbalock);
11613 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11614 /* Interrupt handler has handled ERATT */
11615 spin_unlock_irq(&phba->hbalock);
11616 return 0;
11617 }
11618
James Smarta257bf92009-04-06 18:48:10 -040011619 /*
11620 * If there is deferred error attention, do not check for error
11621 * attention
11622 */
11623 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11624 spin_unlock_irq(&phba->hbalock);
11625 return 0;
11626 }
11627
James Smart3772a992009-05-22 14:50:54 -040011628 /* If PCI channel is offline, don't process it */
11629 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011630 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011631 return 0;
11632 }
11633
11634 switch (phba->sli_rev) {
11635 case LPFC_SLI_REV2:
11636 case LPFC_SLI_REV3:
11637 /* Read chip Host Attention (HA) register */
11638 ha_copy = lpfc_sli_eratt_read(phba);
11639 break;
James Smartda0436e2009-05-22 14:51:39 -040011640 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011641 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011642 ha_copy = lpfc_sli4_eratt_read(phba);
11643 break;
James Smart3772a992009-05-22 14:50:54 -040011644 default:
11645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11646 "0299 Invalid SLI revision (%d)\n",
11647 phba->sli_rev);
11648 ha_copy = 0;
11649 break;
James Smart93996272008-08-24 21:50:30 -040011650 }
11651 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011652
11653 return ha_copy;
11654}
11655
11656/**
11657 * lpfc_intr_state_check - Check device state for interrupt handling
11658 * @phba: Pointer to HBA context.
11659 *
11660 * This inline routine checks whether a device or its PCI slot is in a state
11661 * that the interrupt should be handled.
11662 *
11663 * This function returns 0 if the device or the PCI slot is in a state that
11664 * interrupt should be handled, otherwise -EIO.
11665 */
11666static inline int
11667lpfc_intr_state_check(struct lpfc_hba *phba)
11668{
11669 /* If the pci channel is offline, ignore all the interrupts */
11670 if (unlikely(pci_channel_offline(phba->pcidev)))
11671 return -EIO;
11672
11673 /* Update device level interrupt statistics */
11674 phba->sli.slistat.sli_intr++;
11675
11676 /* Ignore all interrupts during initialization. */
11677 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11678 return -EIO;
11679
James Smart93996272008-08-24 21:50:30 -040011680 return 0;
11681}
11682
11683/**
James Smart3772a992009-05-22 14:50:54 -040011684 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011685 * @irq: Interrupt number.
11686 * @dev_id: The device context pointer.
11687 *
James Smart93996272008-08-24 21:50:30 -040011688 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011689 * service routine when device with SLI-3 interface spec is enabled with
11690 * MSI-X multi-message interrupt mode and there are slow-path events in
11691 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11692 * interrupt mode, this function is called as part of the device-level
11693 * interrupt handler. When the PCI slot is in error recovery or the HBA
11694 * is undergoing initialization, the interrupt handler will not process
11695 * the interrupt. The link attention and ELS ring attention events are
11696 * handled by the worker thread. The interrupt handler signals the worker
11697 * thread and returns for these events. This function is called without
11698 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011699 * structures.
11700 *
11701 * This function returns IRQ_HANDLED when interrupt is handled else it
11702 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011703 **/
dea31012005-04-17 16:05:31 -050011704irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011705lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011706{
James Smart2e0fef82007-06-17 19:56:36 -050011707 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011708 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011709 uint32_t work_ha_copy;
11710 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011711 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011712 uint32_t control;
11713
James Smart92d7f7b2007-06-17 19:56:38 -050011714 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011715 struct lpfc_vport *vport;
11716 struct lpfc_nodelist *ndlp;
11717 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011718 LPFC_MBOXQ_t *pmb;
11719 int rc;
11720
dea31012005-04-17 16:05:31 -050011721 /*
11722 * Get the driver's phba structure from the dev_id and
11723 * assume the HBA is not interrupting.
11724 */
James Smart93996272008-08-24 21:50:30 -040011725 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011726
11727 if (unlikely(!phba))
11728 return IRQ_NONE;
11729
dea31012005-04-17 16:05:31 -050011730 /*
James Smart93996272008-08-24 21:50:30 -040011731 * Stuff needs to be attented to when this function is invoked as an
11732 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011733 */
James Smart93996272008-08-24 21:50:30 -040011734 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011735 /* Check device state for handling interrupt */
11736 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011737 return IRQ_NONE;
11738 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011739 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011740 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11741 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011742 /* If somebody is waiting to handle an eratt don't process it
11743 * here. The brdkill function will do this.
11744 */
11745 if (phba->link_flag & LS_IGNORE_ERATT)
11746 ha_copy &= ~HA_ERATT;
11747 /* Check the need for handling ERATT in interrupt handler */
11748 if (ha_copy & HA_ERATT) {
11749 if (phba->hba_flag & HBA_ERATT_HANDLED)
11750 /* ERATT polling has handled ERATT */
11751 ha_copy &= ~HA_ERATT;
11752 else
11753 /* Indicate interrupt handler handles ERATT */
11754 phba->hba_flag |= HBA_ERATT_HANDLED;
11755 }
James Smarta257bf92009-04-06 18:48:10 -040011756
11757 /*
11758 * If there is deferred error attention, do not check for any
11759 * interrupt.
11760 */
11761 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011762 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011763 return IRQ_NONE;
11764 }
11765
James Smart93996272008-08-24 21:50:30 -040011766 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011767 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11768 goto unplug_error;
11769
James Smarta747c9c2009-11-18 15:41:10 -050011770 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11771 HC_LAINT_ENA | HC_ERINT_ENA),
11772 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011773 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11774 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011775 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011776 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011777 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011778 } else
11779 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011780
dea31012005-04-17 16:05:31 -050011781 work_ha_copy = ha_copy & phba->work_ha_mask;
11782
James Smart93996272008-08-24 21:50:30 -040011783 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011784 if (work_ha_copy & HA_LATT) {
11785 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11786 /*
11787 * Turn off Link Attention interrupts
11788 * until CLEAR_LA done
11789 */
James Smart5b75da22008-12-04 22:39:35 -050011790 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011791 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011792 if (lpfc_readl(phba->HCregaddr, &control))
11793 goto unplug_error;
dea31012005-04-17 16:05:31 -050011794 control &= ~HC_LAINT_ENA;
11795 writel(control, phba->HCregaddr);
11796 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011797 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011798 }
11799 else
11800 work_ha_copy &= ~HA_LATT;
11801 }
11802
James Smart93996272008-08-24 21:50:30 -040011803 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011804 /*
11805 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11806 * the only slow ring.
11807 */
11808 status = (work_ha_copy &
11809 (HA_RXMASK << (4*LPFC_ELS_RING)));
11810 status >>= (4*LPFC_ELS_RING);
11811 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011812 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011813 if (lpfc_readl(phba->HCregaddr, &control))
11814 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011815
11816 lpfc_debugfs_slow_ring_trc(phba,
11817 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11818 control, status,
11819 (uint32_t)phba->sli.slistat.sli_intr);
11820
James Smart858c9f62007-06-17 19:56:39 -050011821 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011822 lpfc_debugfs_slow_ring_trc(phba,
11823 "ISR Disable ring:"
11824 "pwork:x%x hawork:x%x wait:x%x",
11825 phba->work_ha, work_ha_copy,
11826 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011827 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011828
James Smart858c9f62007-06-17 19:56:39 -050011829 control &=
11830 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011831 writel(control, phba->HCregaddr);
11832 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011833 }
James Smarta58cbd52007-08-02 11:09:43 -040011834 else {
11835 lpfc_debugfs_slow_ring_trc(phba,
11836 "ISR slow ring: pwork:"
11837 "x%x hawork:x%x wait:x%x",
11838 phba->work_ha, work_ha_copy,
11839 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011840 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011841 }
James Smart5b75da22008-12-04 22:39:35 -050011842 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011843 }
11844 }
James Smart5b75da22008-12-04 22:39:35 -050011845 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011846 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011847 if (lpfc_sli_read_hs(phba))
11848 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011849 /*
11850 * Check if there is a deferred error condition
11851 * is active
11852 */
11853 if ((HS_FFER1 & phba->work_hs) &&
11854 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011855 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11856 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011857 phba->hba_flag |= DEFER_ERATT;
11858 /* Clear all interrupt enable conditions */
11859 writel(0, phba->HCregaddr);
11860 readl(phba->HCregaddr);
11861 }
11862 }
11863
James Smart93996272008-08-24 21:50:30 -040011864 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011865 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011866 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011867 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011868 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011869
11870 /* First check out the status word */
11871 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11872 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011873 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011874 /*
11875 * Stray Mailbox Interrupt, mbxCommand <cmd>
11876 * mbxStatus <status>
11877 */
James Smart09372822008-01-11 01:52:54 -050011878 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011879 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011880 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011881 "Interrupt mbxCommand x%x "
11882 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011883 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011884 pmbox->mbxCommand,
11885 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011886 /* clear mailbox attention bit */
11887 work_ha_copy &= ~HA_MBATT;
11888 } else {
James Smart97eab632008-04-07 10:16:05 -040011889 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011890 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011891 phba->last_completion_time = jiffies;
11892 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011893 if (pmb->mbox_cmpl) {
11894 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11895 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011896 if (pmb->out_ext_byte_len &&
11897 pmb->context2)
11898 lpfc_sli_pcimem_bcopy(
11899 phba->mbox_ext,
11900 pmb->context2,
11901 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011902 }
James Smart09372822008-01-11 01:52:54 -050011903 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11904 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11905
11906 lpfc_debugfs_disc_trc(vport,
11907 LPFC_DISC_TRC_MBOX_VPORT,
11908 "MBOX dflt rpi: : "
11909 "status:x%x rpi:x%x",
11910 (uint32_t)pmbox->mbxStatus,
11911 pmbox->un.varWords[0], 0);
11912
11913 if (!pmbox->mbxStatus) {
11914 mp = (struct lpfc_dmabuf *)
11915 (pmb->context1);
11916 ndlp = (struct lpfc_nodelist *)
11917 pmb->context2;
11918
11919 /* Reg_LOGIN of dflt RPI was
11920 * successful. new lets get
11921 * rid of the RPI using the
11922 * same mbox buffer.
11923 */
11924 lpfc_unreg_login(phba,
11925 vport->vpi,
11926 pmbox->un.varWords[0],
11927 pmb);
11928 pmb->mbox_cmpl =
11929 lpfc_mbx_cmpl_dflt_rpi;
11930 pmb->context1 = mp;
11931 pmb->context2 = ndlp;
11932 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011933 rc = lpfc_sli_issue_mbox(phba,
11934 pmb,
11935 MBX_NOWAIT);
11936 if (rc != MBX_BUSY)
11937 lpfc_printf_log(phba,
11938 KERN_ERR,
11939 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011940 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011941 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011942 if (rc != MBX_NOT_FINISHED)
11943 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011944 }
11945 }
James Smart5b75da22008-12-04 22:39:35 -050011946 spin_lock_irqsave(
11947 &phba->pport->work_port_lock,
11948 iflag);
James Smart09372822008-01-11 01:52:54 -050011949 phba->pport->work_port_events &=
11950 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011951 spin_unlock_irqrestore(
11952 &phba->pport->work_port_lock,
11953 iflag);
James Smart09372822008-01-11 01:52:54 -050011954 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011955 }
James Smart97eab632008-04-07 10:16:05 -040011956 } else
James Smart5b75da22008-12-04 22:39:35 -050011957 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011958
James Smart92d7f7b2007-06-17 19:56:38 -050011959 if ((work_ha_copy & HA_MBATT) &&
11960 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011961send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011962 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011963 do {
11964 rc = lpfc_sli_issue_mbox(phba, NULL,
11965 MBX_NOWAIT);
11966 } while (rc == MBX_NOT_FINISHED);
11967 if (rc != MBX_SUCCESS)
11968 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11969 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011970 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011971 }
11972
James Smart5b75da22008-12-04 22:39:35 -050011973 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011974 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011975 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011976 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011977 }
James Smart93996272008-08-24 21:50:30 -040011978 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011979unplug_error:
11980 spin_unlock_irqrestore(&phba->hbalock, iflag);
11981 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011982
James Smart3772a992009-05-22 14:50:54 -040011983} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011984
11985/**
James Smart3772a992009-05-22 14:50:54 -040011986 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011987 * @irq: Interrupt number.
11988 * @dev_id: The device context pointer.
11989 *
11990 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011991 * service routine when device with SLI-3 interface spec is enabled with
11992 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11993 * ring event in the HBA. However, when the device is enabled with either
11994 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11995 * device-level interrupt handler. When the PCI slot is in error recovery
11996 * or the HBA is undergoing initialization, the interrupt handler will not
11997 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11998 * the intrrupt context. This function is called without any lock held.
11999 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012000 *
12001 * This function returns IRQ_HANDLED when interrupt is handled else it
12002 * returns IRQ_NONE.
12003 **/
12004irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012005lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012006{
12007 struct lpfc_hba *phba;
12008 uint32_t ha_copy;
12009 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012010 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012011 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012012
12013 /* Get the driver's phba structure from the dev_id and
12014 * assume the HBA is not interrupting.
12015 */
12016 phba = (struct lpfc_hba *) dev_id;
12017
12018 if (unlikely(!phba))
12019 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012020
12021 /*
James Smart93996272008-08-24 21:50:30 -040012022 * Stuff needs to be attented to when this function is invoked as an
12023 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012024 */
James Smart93996272008-08-24 21:50:30 -040012025 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012026 /* Check device state for handling interrupt */
12027 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012028 return IRQ_NONE;
12029 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012030 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12031 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012032 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012033 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012034 /*
12035 * If there is deferred error attention, do not check for
12036 * any interrupt.
12037 */
12038 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012039 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012040 return IRQ_NONE;
12041 }
James Smart93996272008-08-24 21:50:30 -040012042 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12043 phba->HAregaddr);
12044 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012045 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012046 } else
12047 ha_copy = phba->ha_copy;
12048
12049 /*
12050 * Process all events on FCP ring. Take the optimized path for FCP IO.
12051 */
12052 ha_copy &= ~(phba->work_ha_mask);
12053
12054 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012055 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012056 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012057 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012058 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012059
12060 if (phba->cfg_multi_ring_support == 2) {
12061 /*
James Smart93996272008-08-24 21:50:30 -040012062 * Process all events on extra ring. Take the optimized path
12063 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012064 */
James Smart93996272008-08-24 21:50:30 -040012065 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012066 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012067 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012068 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012069 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012070 status);
12071 }
12072 }
dea31012005-04-17 16:05:31 -050012073 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012074} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012075
James Smart93996272008-08-24 21:50:30 -040012076/**
James Smart3772a992009-05-22 14:50:54 -040012077 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012078 * @irq: Interrupt number.
12079 * @dev_id: The device context pointer.
12080 *
James Smart3772a992009-05-22 14:50:54 -040012081 * This function is the HBA device-level interrupt handler to device with
12082 * SLI-3 interface spec, called from the PCI layer when either MSI or
12083 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12084 * requires driver attention. This function invokes the slow-path interrupt
12085 * attention handling function and fast-path interrupt attention handling
12086 * function in turn to process the relevant HBA attention events. This
12087 * function is called without any lock held. It gets the hbalock to access
12088 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012089 *
12090 * This function returns IRQ_HANDLED when interrupt is handled, else it
12091 * returns IRQ_NONE.
12092 **/
12093irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012094lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012095{
12096 struct lpfc_hba *phba;
12097 irqreturn_t sp_irq_rc, fp_irq_rc;
12098 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012099 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012100
12101 /*
12102 * Get the driver's phba structure from the dev_id and
12103 * assume the HBA is not interrupting.
12104 */
12105 phba = (struct lpfc_hba *) dev_id;
12106
12107 if (unlikely(!phba))
12108 return IRQ_NONE;
12109
James Smart3772a992009-05-22 14:50:54 -040012110 /* Check device state for handling interrupt */
12111 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012112 return IRQ_NONE;
12113
12114 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012115 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12116 spin_unlock(&phba->hbalock);
12117 return IRQ_HANDLED;
12118 }
12119
James Smart93996272008-08-24 21:50:30 -040012120 if (unlikely(!phba->ha_copy)) {
12121 spin_unlock(&phba->hbalock);
12122 return IRQ_NONE;
12123 } else if (phba->ha_copy & HA_ERATT) {
12124 if (phba->hba_flag & HBA_ERATT_HANDLED)
12125 /* ERATT polling has handled ERATT */
12126 phba->ha_copy &= ~HA_ERATT;
12127 else
12128 /* Indicate interrupt handler handles ERATT */
12129 phba->hba_flag |= HBA_ERATT_HANDLED;
12130 }
12131
James Smarta257bf92009-04-06 18:48:10 -040012132 /*
12133 * If there is deferred error attention, do not check for any interrupt.
12134 */
12135 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012136 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012137 return IRQ_NONE;
12138 }
12139
James Smart93996272008-08-24 21:50:30 -040012140 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012141 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12142 spin_unlock(&phba->hbalock);
12143 return IRQ_HANDLED;
12144 }
James Smarta747c9c2009-11-18 15:41:10 -050012145 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12146 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12147 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012148 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012149 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012150 readl(phba->HAregaddr); /* flush */
12151 spin_unlock(&phba->hbalock);
12152
12153 /*
12154 * Invokes slow-path host attention interrupt handling as appropriate.
12155 */
12156
12157 /* status of events with mailbox and link attention */
12158 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12159
12160 /* status of events with ELS ring */
12161 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12162 status2 >>= (4*LPFC_ELS_RING);
12163
12164 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012165 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012166 else
12167 sp_irq_rc = IRQ_NONE;
12168
12169 /*
12170 * Invoke fast-path host attention interrupt handling as appropriate.
12171 */
12172
12173 /* status of events with FCP ring */
12174 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12175 status1 >>= (4*LPFC_FCP_RING);
12176
12177 /* status of events with extra ring */
12178 if (phba->cfg_multi_ring_support == 2) {
12179 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12180 status2 >>= (4*LPFC_EXTRA_RING);
12181 } else
12182 status2 = 0;
12183
12184 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012185 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012186 else
12187 fp_irq_rc = IRQ_NONE;
12188
12189 /* Return device-level interrupt handling status */
12190 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012191} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012192
12193/**
12194 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12195 * @phba: pointer to lpfc hba data structure.
12196 *
12197 * This routine is invoked by the worker thread to process all the pending
12198 * SLI4 FCP abort XRI events.
12199 **/
12200void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12201{
12202 struct lpfc_cq_event *cq_event;
12203
12204 /* First, declare the fcp xri abort event has been handled */
12205 spin_lock_irq(&phba->hbalock);
12206 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12207 spin_unlock_irq(&phba->hbalock);
12208 /* Now, handle all the fcp xri abort events */
12209 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12210 /* Get the first event from the head of the event queue */
12211 spin_lock_irq(&phba->hbalock);
12212 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12213 cq_event, struct lpfc_cq_event, list);
12214 spin_unlock_irq(&phba->hbalock);
12215 /* Notify aborted XRI for FCP work queue */
12216 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12217 /* Free the event processed back to the free pool */
12218 lpfc_sli4_cq_event_release(phba, cq_event);
12219 }
12220}
12221
12222/**
James Smart318083a2017-03-04 09:30:30 -080012223 * lpfc_sli4_nvme_xri_abort_event_proc - Process nvme xri abort event
12224 * @phba: pointer to lpfc hba data structure.
12225 *
12226 * This routine is invoked by the worker thread to process all the pending
12227 * SLI4 NVME abort XRI events.
12228 **/
12229void lpfc_sli4_nvme_xri_abort_event_proc(struct lpfc_hba *phba)
12230{
12231 struct lpfc_cq_event *cq_event;
12232
12233 /* First, declare the fcp xri abort event has been handled */
12234 spin_lock_irq(&phba->hbalock);
12235 phba->hba_flag &= ~NVME_XRI_ABORT_EVENT;
12236 spin_unlock_irq(&phba->hbalock);
12237 /* Now, handle all the fcp xri abort events */
12238 while (!list_empty(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue)) {
12239 /* Get the first event from the head of the event queue */
12240 spin_lock_irq(&phba->hbalock);
12241 list_remove_head(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
12242 cq_event, struct lpfc_cq_event, list);
12243 spin_unlock_irq(&phba->hbalock);
12244 /* Notify aborted XRI for NVME work queue */
12245 if (phba->nvmet_support) {
12246 lpfc_sli4_nvmet_xri_aborted(phba,
12247 &cq_event->cqe.wcqe_axri);
12248 } else {
12249 lpfc_sli4_nvme_xri_aborted(phba,
12250 &cq_event->cqe.wcqe_axri);
12251 }
12252 /* Free the event processed back to the free pool */
12253 lpfc_sli4_cq_event_release(phba, cq_event);
12254 }
12255}
12256
12257/**
James Smart4f774512009-05-22 14:52:35 -040012258 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12259 * @phba: pointer to lpfc hba data structure.
12260 *
12261 * This routine is invoked by the worker thread to process all the pending
12262 * SLI4 els abort xri events.
12263 **/
12264void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12265{
12266 struct lpfc_cq_event *cq_event;
12267
12268 /* First, declare the els xri abort event has been handled */
12269 spin_lock_irq(&phba->hbalock);
12270 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12271 spin_unlock_irq(&phba->hbalock);
12272 /* Now, handle all the els xri abort events */
12273 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12274 /* Get the first event from the head of the event queue */
12275 spin_lock_irq(&phba->hbalock);
12276 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12277 cq_event, struct lpfc_cq_event, list);
12278 spin_unlock_irq(&phba->hbalock);
12279 /* Notify aborted XRI for ELS work queue */
12280 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12281 /* Free the event processed back to the free pool */
12282 lpfc_sli4_cq_event_release(phba, cq_event);
12283 }
12284}
12285
James Smart341af102010-01-26 23:07:37 -050012286/**
12287 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12288 * @phba: pointer to lpfc hba data structure
12289 * @pIocbIn: pointer to the rspiocbq
12290 * @pIocbOut: pointer to the cmdiocbq
12291 * @wcqe: pointer to the complete wcqe
12292 *
12293 * This routine transfers the fields of a command iocbq to a response iocbq
12294 * by copying all the IOCB fields from command iocbq and transferring the
12295 * completion status information from the complete wcqe.
12296 **/
James Smart4f774512009-05-22 14:52:35 -040012297static void
James Smart341af102010-01-26 23:07:37 -050012298lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12299 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012300 struct lpfc_iocbq *pIocbOut,
12301 struct lpfc_wcqe_complete *wcqe)
12302{
James Smartaf227412013-10-10 12:23:10 -040012303 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012304 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012305 uint32_t status, max_response;
12306 struct lpfc_dmabuf *dmabuf;
12307 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012308 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12309
12310 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12311 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012312 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012313 status = bf_get(lpfc_wcqe_c_status, wcqe);
12314 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012315 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12316 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12317 pIocbIn->iocb.un.fcpi.fcpi_parm =
12318 pIocbOut->iocb.un.fcpi.fcpi_parm -
12319 wcqe->total_data_placed;
12320 else
12321 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012322 else {
James Smart4f774512009-05-22 14:52:35 -040012323 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012324 switch (pIocbOut->iocb.ulpCommand) {
12325 case CMD_ELS_REQUEST64_CR:
12326 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12327 bpl = (struct ulp_bde64 *)dmabuf->virt;
12328 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12329 max_response = bde.tus.f.bdeSize;
12330 break;
12331 case CMD_GEN_REQUEST64_CR:
12332 max_response = 0;
12333 if (!pIocbOut->context3)
12334 break;
12335 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12336 sizeof(struct ulp_bde64);
12337 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12338 bpl = (struct ulp_bde64 *)dmabuf->virt;
12339 for (i = 0; i < numBdes; i++) {
12340 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12341 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12342 max_response += bde.tus.f.bdeSize;
12343 }
12344 break;
12345 default:
12346 max_response = wcqe->total_data_placed;
12347 break;
12348 }
12349 if (max_response < wcqe->total_data_placed)
12350 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12351 else
12352 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12353 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012354 }
James Smart341af102010-01-26 23:07:37 -050012355
James Smartacd68592012-01-18 16:25:09 -050012356 /* Convert BG errors for completion status */
12357 if (status == CQE_STATUS_DI_ERROR) {
12358 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12359
12360 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12361 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12362 else
12363 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12364
12365 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12366 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12367 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12368 BGS_GUARD_ERR_MASK;
12369 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12370 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12371 BGS_APPTAG_ERR_MASK;
12372 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12373 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12374 BGS_REFTAG_ERR_MASK;
12375
12376 /* Check to see if there was any good data before the error */
12377 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12378 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12379 BGS_HI_WATER_MARK_PRESENT_MASK;
12380 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12381 wcqe->total_data_placed;
12382 }
12383
12384 /*
12385 * Set ALL the error bits to indicate we don't know what
12386 * type of error it is.
12387 */
12388 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12389 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12390 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12391 BGS_GUARD_ERR_MASK);
12392 }
12393
James Smart341af102010-01-26 23:07:37 -050012394 /* Pick up HBA exchange busy condition */
12395 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12396 spin_lock_irqsave(&phba->hbalock, iflags);
12397 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12398 spin_unlock_irqrestore(&phba->hbalock, iflags);
12399 }
James Smart4f774512009-05-22 14:52:35 -040012400}
12401
12402/**
James Smart45ed1192009-10-02 15:17:02 -040012403 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12404 * @phba: Pointer to HBA context object.
12405 * @wcqe: Pointer to work-queue completion queue entry.
12406 *
12407 * This routine handles an ELS work-queue completion event and construct
12408 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12409 * discovery engine to handle.
12410 *
12411 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12412 **/
12413static struct lpfc_iocbq *
12414lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12415 struct lpfc_iocbq *irspiocbq)
12416{
James Smart895427b2017-02-12 13:52:30 -080012417 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012418 struct lpfc_iocbq *cmdiocbq;
12419 struct lpfc_wcqe_complete *wcqe;
12420 unsigned long iflags;
12421
James Smart895427b2017-02-12 13:52:30 -080012422 pring = lpfc_phba_elsring(phba);
12423
James Smart45ed1192009-10-02 15:17:02 -040012424 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012425 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012426 pring->stats.iocb_event++;
12427 /* Look up the ELS command IOCB and create pseudo response IOCB */
12428 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12429 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart89533e92016-10-13 15:06:15 -070012430 /* Put the iocb back on the txcmplq */
12431 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
James Smart7e56aa22012-08-03 12:35:34 -040012432 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012433
12434 if (unlikely(!cmdiocbq)) {
12435 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12436 "0386 ELS complete with no corresponding "
12437 "cmdiocb: iotag (%d)\n",
12438 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12439 lpfc_sli_release_iocbq(phba, irspiocbq);
12440 return NULL;
12441 }
12442
12443 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012444 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012445
12446 return irspiocbq;
12447}
12448
12449/**
James Smart04c68492009-05-22 14:52:52 -040012450 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12451 * @phba: Pointer to HBA context object.
12452 * @cqe: Pointer to mailbox completion queue entry.
12453 *
12454 * This routine process a mailbox completion queue entry with asynchrous
12455 * event.
12456 *
12457 * Return: true if work posted to worker thread, otherwise false.
12458 **/
12459static bool
12460lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12461{
12462 struct lpfc_cq_event *cq_event;
12463 unsigned long iflags;
12464
12465 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12466 "0392 Async Event: word0:x%x, word1:x%x, "
12467 "word2:x%x, word3:x%x\n", mcqe->word0,
12468 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12469
12470 /* Allocate a new internal CQ_EVENT entry */
12471 cq_event = lpfc_sli4_cq_event_alloc(phba);
12472 if (!cq_event) {
12473 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12474 "0394 Failed to allocate CQ_EVENT entry\n");
12475 return false;
12476 }
12477
12478 /* Move the CQE into an asynchronous event entry */
12479 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12480 spin_lock_irqsave(&phba->hbalock, iflags);
12481 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12482 /* Set the async event flag */
12483 phba->hba_flag |= ASYNC_EVENT;
12484 spin_unlock_irqrestore(&phba->hbalock, iflags);
12485
12486 return true;
12487}
12488
12489/**
12490 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12491 * @phba: Pointer to HBA context object.
12492 * @cqe: Pointer to mailbox completion queue entry.
12493 *
12494 * This routine process a mailbox completion queue entry with mailbox
12495 * completion event.
12496 *
12497 * Return: true if work posted to worker thread, otherwise false.
12498 **/
12499static bool
12500lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12501{
12502 uint32_t mcqe_status;
12503 MAILBOX_t *mbox, *pmbox;
12504 struct lpfc_mqe *mqe;
12505 struct lpfc_vport *vport;
12506 struct lpfc_nodelist *ndlp;
12507 struct lpfc_dmabuf *mp;
12508 unsigned long iflags;
12509 LPFC_MBOXQ_t *pmb;
12510 bool workposted = false;
12511 int rc;
12512
12513 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12514 if (!bf_get(lpfc_trailer_completed, mcqe))
12515 goto out_no_mqe_complete;
12516
12517 /* Get the reference to the active mbox command */
12518 spin_lock_irqsave(&phba->hbalock, iflags);
12519 pmb = phba->sli.mbox_active;
12520 if (unlikely(!pmb)) {
12521 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12522 "1832 No pending MBOX command to handle\n");
12523 spin_unlock_irqrestore(&phba->hbalock, iflags);
12524 goto out_no_mqe_complete;
12525 }
12526 spin_unlock_irqrestore(&phba->hbalock, iflags);
12527 mqe = &pmb->u.mqe;
12528 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12529 mbox = phba->mbox;
12530 vport = pmb->vport;
12531
12532 /* Reset heartbeat timer */
12533 phba->last_completion_time = jiffies;
12534 del_timer(&phba->sli.mbox_tmo);
12535
12536 /* Move mbox data to caller's mailbox region, do endian swapping */
12537 if (pmb->mbox_cmpl && mbox)
12538 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012539
James Smart73d91e52011-10-10 21:32:10 -040012540 /*
12541 * For mcqe errors, conditionally move a modified error code to
12542 * the mbox so that the error will not be missed.
12543 */
12544 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12545 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12546 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12547 bf_set(lpfc_mqe_status, mqe,
12548 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12549 }
James Smart04c68492009-05-22 14:52:52 -040012550 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12551 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12552 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12553 "MBOX dflt rpi: status:x%x rpi:x%x",
12554 mcqe_status,
12555 pmbox->un.varWords[0], 0);
12556 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12557 mp = (struct lpfc_dmabuf *)(pmb->context1);
12558 ndlp = (struct lpfc_nodelist *)pmb->context2;
12559 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12560 * RID of the PPI using the same mbox buffer.
12561 */
12562 lpfc_unreg_login(phba, vport->vpi,
12563 pmbox->un.varWords[0], pmb);
12564 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12565 pmb->context1 = mp;
12566 pmb->context2 = ndlp;
12567 pmb->vport = vport;
12568 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12569 if (rc != MBX_BUSY)
12570 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12571 LOG_SLI, "0385 rc should "
12572 "have been MBX_BUSY\n");
12573 if (rc != MBX_NOT_FINISHED)
12574 goto send_current_mbox;
12575 }
12576 }
12577 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12578 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12579 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12580
12581 /* There is mailbox completion work to do */
12582 spin_lock_irqsave(&phba->hbalock, iflags);
12583 __lpfc_mbox_cmpl_put(phba, pmb);
12584 phba->work_ha |= HA_MBATT;
12585 spin_unlock_irqrestore(&phba->hbalock, iflags);
12586 workposted = true;
12587
12588send_current_mbox:
12589 spin_lock_irqsave(&phba->hbalock, iflags);
12590 /* Release the mailbox command posting token */
12591 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12592 /* Setting active mailbox pointer need to be in sync to flag clear */
12593 phba->sli.mbox_active = NULL;
12594 spin_unlock_irqrestore(&phba->hbalock, iflags);
12595 /* Wake up worker thread to post the next pending mailbox command */
12596 lpfc_worker_wake_up(phba);
12597out_no_mqe_complete:
12598 if (bf_get(lpfc_trailer_consumed, mcqe))
12599 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12600 return workposted;
12601}
12602
12603/**
12604 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12605 * @phba: Pointer to HBA context object.
12606 * @cqe: Pointer to mailbox completion queue entry.
12607 *
12608 * This routine process a mailbox completion queue entry, it invokes the
12609 * proper mailbox complete handling or asynchrous event handling routine
12610 * according to the MCQE's async bit.
12611 *
12612 * Return: true if work posted to worker thread, otherwise false.
12613 **/
12614static bool
12615lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12616{
12617 struct lpfc_mcqe mcqe;
12618 bool workposted;
12619
12620 /* Copy the mailbox MCQE and convert endian order as needed */
12621 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12622
12623 /* Invoke the proper event handling routine */
12624 if (!bf_get(lpfc_trailer_async, &mcqe))
12625 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12626 else
12627 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12628 return workposted;
12629}
12630
12631/**
James Smart4f774512009-05-22 14:52:35 -040012632 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12633 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012634 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012635 * @wcqe: Pointer to work-queue completion queue entry.
12636 *
12637 * This routine handles an ELS work-queue completion event.
12638 *
12639 * Return: true if work posted to worker thread, otherwise false.
12640 **/
12641static bool
James Smart2a76a282012-08-03 12:35:54 -040012642lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012643 struct lpfc_wcqe_complete *wcqe)
12644{
James Smart4f774512009-05-22 14:52:35 -040012645 struct lpfc_iocbq *irspiocbq;
12646 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012647 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012648 int txq_cnt = 0;
12649 int txcmplq_cnt = 0;
12650 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012651
James Smart45ed1192009-10-02 15:17:02 -040012652 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012653 irspiocbq = lpfc_sli_get_iocbq(phba);
12654 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012655 if (!list_empty(&pring->txq))
12656 txq_cnt++;
12657 if (!list_empty(&pring->txcmplq))
12658 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012659 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012660 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12661 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012662 txq_cnt, phba->iocb_cnt,
12663 fcp_txcmplq_cnt,
12664 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012665 return false;
James Smart4f774512009-05-22 14:52:35 -040012666 }
James Smart4f774512009-05-22 14:52:35 -040012667
James Smart45ed1192009-10-02 15:17:02 -040012668 /* Save off the slow-path queue event for work thread to process */
12669 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012670 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012671 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012672 &phba->sli4_hba.sp_queue_event);
12673 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012674 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012675
James Smart45ed1192009-10-02 15:17:02 -040012676 return true;
James Smart4f774512009-05-22 14:52:35 -040012677}
12678
12679/**
12680 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12681 * @phba: Pointer to HBA context object.
12682 * @wcqe: Pointer to work-queue completion queue entry.
12683 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012684 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012685 * proper WQ release routine to the slow-path WQ.
12686 **/
12687static void
12688lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12689 struct lpfc_wcqe_release *wcqe)
12690{
James Smart2e90f4b2011-12-13 13:22:37 -050012691 /* sanity check on queue memory */
12692 if (unlikely(!phba->sli4_hba.els_wq))
12693 return;
James Smart4f774512009-05-22 14:52:35 -040012694 /* Check for the slow-path ELS work queue */
12695 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12696 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12697 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12698 else
12699 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12700 "2579 Slow-path wqe consume event carries "
12701 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12702 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12703 phba->sli4_hba.els_wq->queue_id);
12704}
12705
12706/**
12707 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12708 * @phba: Pointer to HBA context object.
12709 * @cq: Pointer to a WQ completion queue.
12710 * @wcqe: Pointer to work-queue completion queue entry.
12711 *
12712 * This routine handles an XRI abort event.
12713 *
12714 * Return: true if work posted to worker thread, otherwise false.
12715 **/
12716static bool
12717lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12718 struct lpfc_queue *cq,
12719 struct sli4_wcqe_xri_aborted *wcqe)
12720{
12721 bool workposted = false;
12722 struct lpfc_cq_event *cq_event;
12723 unsigned long iflags;
12724
12725 /* Allocate a new internal CQ_EVENT entry */
12726 cq_event = lpfc_sli4_cq_event_alloc(phba);
12727 if (!cq_event) {
12728 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12729 "0602 Failed to allocate CQ_EVENT entry\n");
12730 return false;
12731 }
12732
12733 /* Move the CQE into the proper xri abort event list */
12734 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12735 switch (cq->subtype) {
12736 case LPFC_FCP:
12737 spin_lock_irqsave(&phba->hbalock, iflags);
12738 list_add_tail(&cq_event->list,
12739 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12740 /* Set the fcp xri abort event flag */
12741 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12742 spin_unlock_irqrestore(&phba->hbalock, iflags);
12743 workposted = true;
12744 break;
12745 case LPFC_ELS:
12746 spin_lock_irqsave(&phba->hbalock, iflags);
12747 list_add_tail(&cq_event->list,
12748 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12749 /* Set the els xri abort event flag */
12750 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12751 spin_unlock_irqrestore(&phba->hbalock, iflags);
12752 workposted = true;
12753 break;
James Smart318083a2017-03-04 09:30:30 -080012754 case LPFC_NVME:
12755 spin_lock_irqsave(&phba->hbalock, iflags);
12756 list_add_tail(&cq_event->list,
12757 &phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
12758 /* Set the nvme xri abort event flag */
12759 phba->hba_flag |= NVME_XRI_ABORT_EVENT;
12760 spin_unlock_irqrestore(&phba->hbalock, iflags);
12761 workposted = true;
12762 break;
James Smart4f774512009-05-22 14:52:35 -040012763 default:
12764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080012765 "0603 Invalid CQ subtype %d: "
12766 "%08x %08x %08x %08x\n",
12767 cq->subtype, wcqe->word0, wcqe->parameter,
12768 wcqe->word2, wcqe->word3);
12769 lpfc_sli4_cq_event_release(phba, cq_event);
James Smart4f774512009-05-22 14:52:35 -040012770 workposted = false;
12771 break;
12772 }
12773 return workposted;
12774}
12775
12776/**
James Smart4d9ab992009-10-02 15:16:39 -040012777 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012778 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012779 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012780 *
James Smart4d9ab992009-10-02 15:16:39 -040012781 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012782 *
12783 * Return: true if work posted to worker thread, otherwise false.
12784 **/
12785static bool
James Smart4d9ab992009-10-02 15:16:39 -040012786lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12787{
12788 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012789 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012790 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12791 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070012792 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040012793 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012794 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012795 unsigned long iflags;
12796
James Smart2e90f4b2011-12-13 13:22:37 -050012797 /* sanity check on queue memory */
12798 if (unlikely(!hrq) || unlikely(!drq))
12799 return workposted;
12800
James Smart7851fe22011-07-22 18:36:52 -040012801 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12802 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12803 else
12804 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12805 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012806 goto out;
12807
12808 status = bf_get(lpfc_rcqe_status, rcqe);
12809 switch (status) {
12810 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12811 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12812 "2537 Receive Frame Truncated!!\n");
12813 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012814 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012815 spin_lock_irqsave(&phba->hbalock, iflags);
12816 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12817 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012818 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012819 spin_unlock_irqrestore(&phba->hbalock, iflags);
12820 goto out;
12821 }
James Smartb84daac2012-08-03 12:35:13 -040012822 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070012823 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040012824 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012825
12826 /* If a NVME LS event (type 0x28), treat it as Fast path */
12827 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12828
James Smart4d9ab992009-10-02 15:16:39 -040012829 /* save off the frame for the word thread to process */
12830 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012831 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012832 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012833 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012834 spin_unlock_irqrestore(&phba->hbalock, iflags);
12835 workposted = true;
12836 break;
James Smart4d9ab992009-10-02 15:16:39 -040012837 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070012838 if (phba->nvmet_support) {
12839 tgtp = phba->targetport->private;
12840 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
12841 "6402 RQE Error x%x, posted %d err_cnt "
12842 "%d: %x %x %x\n",
12843 status, hrq->RQ_buf_posted,
12844 hrq->RQ_no_posted_buf,
12845 atomic_read(&tgtp->rcv_fcp_cmd_in),
12846 atomic_read(&tgtp->rcv_fcp_cmd_out),
12847 atomic_read(&tgtp->xmt_fcp_release));
12848 }
12849 /* fallthrough */
12850
12851 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040012852 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012853 /* Post more buffers if possible */
12854 spin_lock_irqsave(&phba->hbalock, iflags);
12855 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12856 spin_unlock_irqrestore(&phba->hbalock, iflags);
12857 workposted = true;
12858 break;
12859 }
12860out:
12861 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012862}
12863
12864/**
12865 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12866 * @phba: Pointer to HBA context object.
12867 * @cq: Pointer to the completion queue.
12868 * @wcqe: Pointer to a completion queue entry.
12869 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012870 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012871 * entry.
12872 *
12873 * Return: true if work posted to worker thread, otherwise false.
12874 **/
12875static bool
12876lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012877 struct lpfc_cqe *cqe)
12878{
James Smart45ed1192009-10-02 15:17:02 -040012879 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012880 bool workposted = false;
12881
12882 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012883 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012884
12885 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012886 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012887 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012888 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012889 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012890 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012891 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012892 break;
12893 case CQE_CODE_RELEASE_WQE:
12894 /* Process the WQ release event */
12895 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012896 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012897 break;
12898 case CQE_CODE_XRI_ABORTED:
12899 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012900 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012901 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012902 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012903 break;
James Smart4d9ab992009-10-02 15:16:39 -040012904 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012905 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012906 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012907 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012908 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012909 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012910 break;
James Smart4f774512009-05-22 14:52:35 -040012911 default:
12912 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12913 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012914 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012915 break;
12916 }
12917 return workposted;
12918}
12919
12920/**
James Smart4f774512009-05-22 14:52:35 -040012921 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12922 * @phba: Pointer to HBA context object.
12923 * @eqe: Pointer to fast-path event queue entry.
12924 *
12925 * This routine process a event queue entry from the slow-path event queue.
12926 * It will check the MajorCode and MinorCode to determine this is for a
12927 * completion event on a completion queue, if not, an error shall be logged
12928 * and just return. Otherwise, it will get to the corresponding completion
12929 * queue and process all the entries on that completion queue, rearm the
12930 * completion queue, and then return.
12931 *
12932 **/
12933static void
James Smart67d12732012-08-03 12:36:13 -040012934lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12935 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012936{
James Smart67d12732012-08-03 12:36:13 -040012937 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012938 struct lpfc_cqe *cqe;
12939 bool workposted = false;
12940 int ecount = 0;
12941 uint16_t cqid;
12942
James Smart4f774512009-05-22 14:52:35 -040012943 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012944 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012945
James Smart4f774512009-05-22 14:52:35 -040012946 list_for_each_entry(childq, &speq->child_list, list) {
12947 if (childq->queue_id == cqid) {
12948 cq = childq;
12949 break;
12950 }
12951 }
12952 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012953 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12954 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12955 "0365 Slow-path CQ identifier "
12956 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012957 return;
12958 }
12959
James Smart895427b2017-02-12 13:52:30 -080012960 /* Save EQ associated with this CQ */
12961 cq->assoc_qp = speq;
12962
James Smart4f774512009-05-22 14:52:35 -040012963 /* Process all the entries to the CQ */
12964 switch (cq->type) {
12965 case LPFC_MCQ:
12966 while ((cqe = lpfc_sli4_cq_get(cq))) {
12967 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012968 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012969 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012970 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012971 }
12972 break;
12973 case LPFC_WCQ:
12974 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080012975 if ((cq->subtype == LPFC_FCP) ||
12976 (cq->subtype == LPFC_NVME))
12977 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040012978 cqe);
12979 else
12980 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12981 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012982 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012983 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12984 }
James Smartb84daac2012-08-03 12:35:13 -040012985
12986 /* Track the max number of CQEs processed in 1 EQ */
12987 if (ecount > cq->CQ_max_cqe)
12988 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012989 break;
12990 default:
12991 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12992 "0370 Invalid completion queue type (%d)\n",
12993 cq->type);
12994 return;
12995 }
12996
12997 /* Catch the no cq entry condition, log an error */
12998 if (unlikely(ecount == 0))
12999 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13000 "0371 No entry from the CQ: identifier "
13001 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13002
13003 /* In any case, flash and re-arm the RCQ */
13004 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13005
13006 /* wake up worker thread if there are works to be done */
13007 if (workposted)
13008 lpfc_worker_wake_up(phba);
13009}
13010
13011/**
13012 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013013 * @phba: Pointer to HBA context object.
13014 * @cq: Pointer to associated CQ
13015 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013016 *
13017 * This routine process a fast-path work queue completion entry from fast-path
13018 * event queue for FCP command response completion.
13019 **/
13020static void
James Smart2a76a282012-08-03 12:35:54 -040013021lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013022 struct lpfc_wcqe_complete *wcqe)
13023{
James Smart2a76a282012-08-03 12:35:54 -040013024 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013025 struct lpfc_iocbq *cmdiocbq;
13026 struct lpfc_iocbq irspiocbq;
13027 unsigned long iflags;
13028
James Smart4f774512009-05-22 14:52:35 -040013029 /* Check for response status */
13030 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13031 /* If resource errors reported from HBA, reduce queue
13032 * depth of the SCSI device.
13033 */
James Smarte3d2b802012-08-14 14:25:43 -040013034 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13035 IOSTAT_LOCAL_REJECT)) &&
13036 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13037 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013038 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013039
James Smart4f774512009-05-22 14:52:35 -040013040 /* Log the error status */
13041 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13042 "0373 FCP complete error: status=x%x, "
13043 "hw_status=x%x, total_data_specified=%d, "
13044 "parameter=x%x, word3=x%x\n",
13045 bf_get(lpfc_wcqe_c_status, wcqe),
13046 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13047 wcqe->total_data_placed, wcqe->parameter,
13048 wcqe->word3);
13049 }
13050
13051 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013052 spin_lock_irqsave(&pring->ring_lock, iflags);
13053 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013054 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13055 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013056 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013057 if (unlikely(!cmdiocbq)) {
13058 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13059 "0374 FCP complete with no corresponding "
13060 "cmdiocb: iotag (%d)\n",
13061 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13062 return;
13063 }
James Smart895427b2017-02-12 13:52:30 -080013064
13065 if (cq->assoc_qp)
13066 cmdiocbq->isr_timestamp =
13067 cq->assoc_qp->isr_timestamp;
13068
13069 if (cmdiocbq->iocb_cmpl == NULL) {
13070 if (cmdiocbq->wqe_cmpl) {
13071 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13072 spin_lock_irqsave(&phba->hbalock, iflags);
13073 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13074 spin_unlock_irqrestore(&phba->hbalock, iflags);
13075 }
13076
13077 /* Pass the cmd_iocb and the wcqe to the upper layer */
13078 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13079 return;
13080 }
James Smart4f774512009-05-22 14:52:35 -040013081 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13082 "0375 FCP cmdiocb not callback function "
13083 "iotag: (%d)\n",
13084 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13085 return;
13086 }
13087
13088 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013089 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013090
James Smart0f65ff62010-02-26 14:14:23 -050013091 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13092 spin_lock_irqsave(&phba->hbalock, iflags);
13093 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13094 spin_unlock_irqrestore(&phba->hbalock, iflags);
13095 }
13096
James Smart4f774512009-05-22 14:52:35 -040013097 /* Pass the cmd_iocb and the rsp state to the upper layer */
13098 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13099}
13100
13101/**
13102 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13103 * @phba: Pointer to HBA context object.
13104 * @cq: Pointer to completion queue.
13105 * @wcqe: Pointer to work-queue completion queue entry.
13106 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013107 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013108 * proper WQ release routine to the slow-path WQ.
13109 **/
13110static void
13111lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13112 struct lpfc_wcqe_release *wcqe)
13113{
13114 struct lpfc_queue *childwq;
13115 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013116 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013117
13118 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013119 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013120 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013121 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013122 lpfc_sli4_wq_release(childwq,
13123 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13124 wqid_matched = true;
13125 break;
13126 }
13127 }
13128 /* Report warning log message if no match found */
13129 if (wqid_matched != true)
13130 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13131 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013132 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013133}
13134
13135/**
James Smart2d7dbc42017-02-12 13:52:35 -080013136 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13137 * @phba: Pointer to HBA context object.
13138 * @rcqe: Pointer to receive-queue completion queue entry.
13139 *
13140 * This routine process a receive-queue completion queue entry.
13141 *
13142 * Return: true if work posted to worker thread, otherwise false.
13143 **/
13144static bool
13145lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13146 struct lpfc_rcqe *rcqe)
13147{
13148 bool workposted = false;
13149 struct lpfc_queue *hrq;
13150 struct lpfc_queue *drq;
13151 struct rqb_dmabuf *dma_buf;
13152 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013153 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013154 uint32_t status, rq_id;
13155 unsigned long iflags;
13156 uint32_t fctl, idx;
13157
13158 if ((phba->nvmet_support == 0) ||
13159 (phba->sli4_hba.nvmet_cqset == NULL))
13160 return workposted;
13161
13162 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13163 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13164 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13165
13166 /* sanity check on queue memory */
13167 if (unlikely(!hrq) || unlikely(!drq))
13168 return workposted;
13169
13170 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13171 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13172 else
13173 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13174
13175 if ((phba->nvmet_support == 0) ||
13176 (rq_id != hrq->queue_id))
13177 return workposted;
13178
13179 status = bf_get(lpfc_rcqe_status, rcqe);
13180 switch (status) {
13181 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13182 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13183 "6126 Receive Frame Truncated!!\n");
James Smart2d7dbc42017-02-12 13:52:35 -080013184 case FC_STATUS_RQ_SUCCESS:
13185 lpfc_sli4_rq_release(hrq, drq);
13186 spin_lock_irqsave(&phba->hbalock, iflags);
13187 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13188 if (!dma_buf) {
13189 hrq->RQ_no_buf_found++;
13190 spin_unlock_irqrestore(&phba->hbalock, iflags);
13191 goto out;
13192 }
13193 spin_unlock_irqrestore(&phba->hbalock, iflags);
13194 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013195 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013196 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13197
13198 /* Just some basic sanity checks on FCP Command frame */
13199 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13200 fc_hdr->fh_f_ctl[1] << 8 |
13201 fc_hdr->fh_f_ctl[2]);
13202 if (((fctl &
13203 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13204 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13205 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13206 goto drop;
13207
13208 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13209 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013210 lpfc_nvmet_unsol_fcp_event(
13211 phba, phba->sli4_hba.els_wq->pring, dma_buf,
13212 cq->assoc_qp->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013213 return false;
13214 }
13215drop:
13216 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13217 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013218 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013219 if (phba->nvmet_support) {
13220 tgtp = phba->targetport->private;
13221 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13222 "6401 RQE Error x%x, posted %d err_cnt "
13223 "%d: %x %x %x\n",
13224 status, hrq->RQ_buf_posted,
13225 hrq->RQ_no_posted_buf,
13226 atomic_read(&tgtp->rcv_fcp_cmd_in),
13227 atomic_read(&tgtp->rcv_fcp_cmd_out),
13228 atomic_read(&tgtp->xmt_fcp_release));
13229 }
13230 /* fallthrough */
13231
13232 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013233 hrq->RQ_no_posted_buf++;
13234 /* Post more buffers if possible */
13235 spin_lock_irqsave(&phba->hbalock, iflags);
13236 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13237 spin_unlock_irqrestore(&phba->hbalock, iflags);
13238 workposted = true;
13239 break;
13240 }
13241out:
13242 return workposted;
13243}
13244
13245/**
James Smart895427b2017-02-12 13:52:30 -080013246 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013247 * @cq: Pointer to the completion queue.
13248 * @eqe: Pointer to fast-path completion queue entry.
13249 *
13250 * This routine process a fast-path work queue completion entry from fast-path
13251 * event queue for FCP command response completion.
13252 **/
13253static int
James Smart895427b2017-02-12 13:52:30 -080013254lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013255 struct lpfc_cqe *cqe)
13256{
13257 struct lpfc_wcqe_release wcqe;
13258 bool workposted = false;
13259
13260 /* Copy the work queue CQE and convert endian order if needed */
13261 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13262
13263 /* Check and process for different type of WCQE and dispatch */
13264 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13265 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013266 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013267 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013268 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013269 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013270 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13271 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13272 (struct lpfc_wcqe_complete *)&wcqe);
13273 if (cq->subtype == LPFC_NVME_LS)
13274 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013275 (struct lpfc_wcqe_complete *)&wcqe);
13276 break;
13277 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013278 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013279 /* Process the WQ release event */
13280 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13281 (struct lpfc_wcqe_release *)&wcqe);
13282 break;
13283 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013284 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013285 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013286 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013287 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13288 (struct sli4_wcqe_xri_aborted *)&wcqe);
13289 break;
James Smart895427b2017-02-12 13:52:30 -080013290 case CQE_CODE_RECEIVE_V1:
13291 case CQE_CODE_RECEIVE:
13292 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013293 if (cq->subtype == LPFC_NVMET) {
13294 workposted = lpfc_sli4_nvmet_handle_rcqe(
13295 phba, cq, (struct lpfc_rcqe *)&wcqe);
13296 }
James Smart895427b2017-02-12 13:52:30 -080013297 break;
James Smart4f774512009-05-22 14:52:35 -040013298 default:
13299 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013300 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013301 bf_get(lpfc_wcqe_c_code, &wcqe));
13302 break;
13303 }
13304 return workposted;
13305}
13306
13307/**
James Smart67d12732012-08-03 12:36:13 -040013308 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013309 * @phba: Pointer to HBA context object.
13310 * @eqe: Pointer to fast-path event queue entry.
13311 *
13312 * This routine process a event queue entry from the fast-path event queue.
13313 * It will check the MajorCode and MinorCode to determine this is for a
13314 * completion event on a completion queue, if not, an error shall be logged
13315 * and just return. Otherwise, it will get to the corresponding completion
13316 * queue and process all the entries on the completion queue, rearm the
13317 * completion queue, and then return.
13318 **/
13319static void
James Smart67d12732012-08-03 12:36:13 -040013320lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13321 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013322{
James Smart895427b2017-02-12 13:52:30 -080013323 struct lpfc_queue *cq = NULL;
James Smart4f774512009-05-22 14:52:35 -040013324 struct lpfc_cqe *cqe;
13325 bool workposted = false;
James Smart2d7dbc42017-02-12 13:52:35 -080013326 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013327 int ecount = 0;
13328
James Smartcb5172e2010-03-15 11:25:07 -040013329 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013330 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013331 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013332 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013333 bf_get_le32(lpfc_eqe_major_code, eqe),
13334 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040013335 return;
13336 }
13337
James Smart67d12732012-08-03 12:36:13 -040013338 /* Get the reference to the corresponding CQ */
13339 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13340
James Smart2d7dbc42017-02-12 13:52:35 -080013341 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13342 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13343 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13344 /* Process NVMET unsol rcv */
13345 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13346 goto process_cq;
13347 }
13348 }
13349
James Smart895427b2017-02-12 13:52:30 -080013350 if (phba->sli4_hba.nvme_cq_map &&
13351 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013352 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013353 cq = phba->sli4_hba.nvme_cq[qidx];
13354 goto process_cq;
13355 }
13356
13357 if (phba->sli4_hba.fcp_cq_map &&
13358 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13359 /* Process FCP command completion */
13360 cq = phba->sli4_hba.fcp_cq[qidx];
13361 goto process_cq;
13362 }
13363
13364 if (phba->sli4_hba.nvmels_cq &&
13365 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13366 /* Process NVME unsol rcv */
13367 cq = phba->sli4_hba.nvmels_cq;
13368 }
13369
13370 /* Otherwise this is a Slow path event */
13371 if (cq == NULL) {
13372 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013373 return;
13374 }
13375
James Smart895427b2017-02-12 13:52:30 -080013376process_cq:
James Smart4f774512009-05-22 14:52:35 -040013377 if (unlikely(cqid != cq->queue_id)) {
13378 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13379 "0368 Miss-matched fast-path completion "
13380 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13381 cqid, cq->queue_id);
13382 return;
13383 }
13384
James Smart895427b2017-02-12 13:52:30 -080013385 /* Save EQ associated with this CQ */
13386 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13387
James Smart4f774512009-05-22 14:52:35 -040013388 /* Process all the entries to the CQ */
13389 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013390 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013391 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040013392 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13393 }
13394
James Smartb84daac2012-08-03 12:35:13 -040013395 /* Track the max number of CQEs processed in 1 EQ */
13396 if (ecount > cq->CQ_max_cqe)
13397 cq->CQ_max_cqe = ecount;
13398
James Smart4f774512009-05-22 14:52:35 -040013399 /* Catch the no cq entry condition */
13400 if (unlikely(ecount == 0))
13401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13402 "0369 No entry from fast-path completion "
13403 "queue fcpcqid=%d\n", cq->queue_id);
13404
13405 /* In any case, flash and re-arm the CQ */
13406 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13407
13408 /* wake up worker thread if there are works to be done */
13409 if (workposted)
13410 lpfc_worker_wake_up(phba);
13411}
13412
13413static void
13414lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13415{
13416 struct lpfc_eqe *eqe;
13417
13418 /* walk all the EQ entries and drop on the floor */
13419 while ((eqe = lpfc_sli4_eq_get(eq)))
13420 ;
13421
13422 /* Clear and re-arm the EQ */
13423 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13424}
13425
James Smart1ba981f2014-02-20 09:56:45 -050013426
13427/**
13428 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13429 * entry
13430 * @phba: Pointer to HBA context object.
13431 * @eqe: Pointer to fast-path event queue entry.
13432 *
13433 * This routine process a event queue entry from the Flash Optimized Fabric
13434 * event queue. It will check the MajorCode and MinorCode to determine this
13435 * is for a completion event on a completion queue, if not, an error shall be
13436 * logged and just return. Otherwise, it will get to the corresponding
13437 * completion queue and process all the entries on the completion queue, rearm
13438 * the completion queue, and then return.
13439 **/
13440static void
13441lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13442{
13443 struct lpfc_queue *cq;
13444 struct lpfc_cqe *cqe;
13445 bool workposted = false;
13446 uint16_t cqid;
13447 int ecount = 0;
13448
13449 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13450 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13451 "9147 Not a valid completion "
13452 "event: majorcode=x%x, minorcode=x%x\n",
13453 bf_get_le32(lpfc_eqe_major_code, eqe),
13454 bf_get_le32(lpfc_eqe_minor_code, eqe));
13455 return;
13456 }
13457
13458 /* Get the reference to the corresponding CQ */
13459 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13460
13461 /* Next check for OAS */
13462 cq = phba->sli4_hba.oas_cq;
13463 if (unlikely(!cq)) {
13464 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13465 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13466 "9148 OAS completion queue "
13467 "does not exist\n");
13468 return;
13469 }
13470
13471 if (unlikely(cqid != cq->queue_id)) {
13472 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13473 "9149 Miss-matched fast-path compl "
13474 "queue id: eqcqid=%d, fcpcqid=%d\n",
13475 cqid, cq->queue_id);
13476 return;
13477 }
13478
13479 /* Process all the entries to the OAS CQ */
13480 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013481 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart1ba981f2014-02-20 09:56:45 -050013482 if (!(++ecount % cq->entry_repost))
13483 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13484 }
13485
13486 /* Track the max number of CQEs processed in 1 EQ */
13487 if (ecount > cq->CQ_max_cqe)
13488 cq->CQ_max_cqe = ecount;
13489
13490 /* Catch the no cq entry condition */
13491 if (unlikely(ecount == 0))
13492 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13493 "9153 No entry from fast-path completion "
13494 "queue fcpcqid=%d\n", cq->queue_id);
13495
13496 /* In any case, flash and re-arm the CQ */
13497 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13498
13499 /* wake up worker thread if there are works to be done */
13500 if (workposted)
13501 lpfc_worker_wake_up(phba);
13502}
13503
13504/**
13505 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13506 * @irq: Interrupt number.
13507 * @dev_id: The device context pointer.
13508 *
13509 * This function is directly called from the PCI layer as an interrupt
13510 * service routine when device with SLI-4 interface spec is enabled with
13511 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13512 * IOCB ring event in the HBA. However, when the device is enabled with either
13513 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13514 * device-level interrupt handler. When the PCI slot is in error recovery
13515 * or the HBA is undergoing initialization, the interrupt handler will not
13516 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13517 * the intrrupt context. This function is called without any lock held.
13518 * It gets the hbalock to access and update SLI data structures. Note that,
13519 * the EQ to CQ are one-to-one map such that the EQ index is
13520 * equal to that of CQ index.
13521 *
13522 * This function returns IRQ_HANDLED when interrupt is handled else it
13523 * returns IRQ_NONE.
13524 **/
13525irqreturn_t
13526lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13527{
13528 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013529 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013530 struct lpfc_queue *eq;
13531 struct lpfc_eqe *eqe;
13532 unsigned long iflag;
13533 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013534
13535 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013536 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13537 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013538
13539 if (unlikely(!phba))
13540 return IRQ_NONE;
13541
13542 /* Get to the EQ struct associated with this vector */
13543 eq = phba->sli4_hba.fof_eq;
13544 if (unlikely(!eq))
13545 return IRQ_NONE;
13546
13547 /* Check device state for handling interrupt */
13548 if (unlikely(lpfc_intr_state_check(phba))) {
13549 eq->EQ_badstate++;
13550 /* Check again for link_state with lock held */
13551 spin_lock_irqsave(&phba->hbalock, iflag);
13552 if (phba->link_state < LPFC_LINK_DOWN)
13553 /* Flush, clear interrupt, and rearm the EQ */
13554 lpfc_sli4_eq_flush(phba, eq);
13555 spin_unlock_irqrestore(&phba->hbalock, iflag);
13556 return IRQ_NONE;
13557 }
13558
13559 /*
13560 * Process all the event on FCP fast-path EQ
13561 */
13562 while ((eqe = lpfc_sli4_eq_get(eq))) {
13563 lpfc_sli4_fof_handle_eqe(phba, eqe);
13564 if (!(++ecount % eq->entry_repost))
13565 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
13566 eq->EQ_processed++;
13567 }
13568
13569 /* Track the max number of EQEs processed in 1 intr */
13570 if (ecount > eq->EQ_max_eqe)
13571 eq->EQ_max_eqe = ecount;
13572
13573
13574 if (unlikely(ecount == 0)) {
13575 eq->EQ_no_entry++;
13576
13577 if (phba->intr_type == MSIX)
13578 /* MSI-X treated interrupt served as no EQ share INT */
13579 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13580 "9145 MSI-X interrupt with no EQE\n");
13581 else {
13582 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13583 "9146 ISR interrupt with no EQE\n");
13584 /* Non MSI-X treated on interrupt as EQ share INT */
13585 return IRQ_NONE;
13586 }
13587 }
13588 /* Always clear and re-arm the fast-path EQ */
13589 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13590 return IRQ_HANDLED;
13591}
13592
James Smart4f774512009-05-22 14:52:35 -040013593/**
James Smart67d12732012-08-03 12:36:13 -040013594 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013595 * @irq: Interrupt number.
13596 * @dev_id: The device context pointer.
13597 *
13598 * This function is directly called from the PCI layer as an interrupt
13599 * service routine when device with SLI-4 interface spec is enabled with
13600 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13601 * ring event in the HBA. However, when the device is enabled with either
13602 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13603 * device-level interrupt handler. When the PCI slot is in error recovery
13604 * or the HBA is undergoing initialization, the interrupt handler will not
13605 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13606 * the intrrupt context. This function is called without any lock held.
13607 * It gets the hbalock to access and update SLI data structures. Note that,
13608 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13609 * equal to that of FCP CQ index.
13610 *
James Smart67d12732012-08-03 12:36:13 -040013611 * The link attention and ELS ring attention events are handled
13612 * by the worker thread. The interrupt handler signals the worker thread
13613 * and returns for these events. This function is called without any lock
13614 * held. It gets the hbalock to access and update SLI data structures.
13615 *
James Smart4f774512009-05-22 14:52:35 -040013616 * This function returns IRQ_HANDLED when interrupt is handled else it
13617 * returns IRQ_NONE.
13618 **/
13619irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013620lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013621{
13622 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013623 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013624 struct lpfc_queue *fpeq;
13625 struct lpfc_eqe *eqe;
13626 unsigned long iflag;
13627 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013628 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013629
13630 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013631 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13632 phba = hba_eq_hdl->phba;
13633 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013634
13635 if (unlikely(!phba))
13636 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013637 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013638 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013639
13640 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013641 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013642 if (unlikely(!fpeq))
13643 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013644
James Smartbd2cdd52017-02-12 13:52:33 -080013645#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13646 if (phba->ktime_on)
13647 fpeq->isr_timestamp = ktime_get_ns();
13648#endif
13649
James Smartba20c852012-08-03 12:36:52 -040013650 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013651 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013652 lpfc_sli4_eq_clr_intr(fpeq);
13653 else {
James Smart895427b2017-02-12 13:52:30 -080013654 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013655 return IRQ_NONE;
13656 }
13657 }
13658
James Smart4f774512009-05-22 14:52:35 -040013659 /* Check device state for handling interrupt */
13660 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040013661 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040013662 /* Check again for link_state with lock held */
13663 spin_lock_irqsave(&phba->hbalock, iflag);
13664 if (phba->link_state < LPFC_LINK_DOWN)
13665 /* Flush, clear interrupt, and rearm the EQ */
13666 lpfc_sli4_eq_flush(phba, fpeq);
13667 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013668 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013669 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013670 return IRQ_NONE;
13671 }
13672
13673 /*
13674 * Process all the event on FCP fast-path EQ
13675 */
13676 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040013677 if (eqe == NULL)
13678 break;
13679
James Smart895427b2017-02-12 13:52:30 -080013680 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040013681 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040013682 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040013683 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013684 }
13685
James Smartb84daac2012-08-03 12:35:13 -040013686 /* Track the max number of EQEs processed in 1 intr */
13687 if (ecount > fpeq->EQ_max_eqe)
13688 fpeq->EQ_max_eqe = ecount;
13689
James Smart4f774512009-05-22 14:52:35 -040013690 /* Always clear and re-arm the fast-path EQ */
13691 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13692
13693 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013694 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013695
13696 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013697 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013698 return IRQ_NONE;
13699 }
13700
James Smart4f774512009-05-22 14:52:35 -040013701 if (phba->intr_type == MSIX)
13702 /* MSI-X treated interrupt served as no EQ share INT */
13703 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13704 "0358 MSI-X interrupt with no EQE\n");
13705 else
13706 /* Non MSI-X treated on interrupt as EQ share INT */
13707 return IRQ_NONE;
13708 }
13709
James Smartba20c852012-08-03 12:36:52 -040013710 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013711 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13712
James Smart4f774512009-05-22 14:52:35 -040013713 return IRQ_HANDLED;
13714} /* lpfc_sli4_fp_intr_handler */
13715
13716/**
13717 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13718 * @irq: Interrupt number.
13719 * @dev_id: The device context pointer.
13720 *
13721 * This function is the device-level interrupt handler to device with SLI-4
13722 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13723 * interrupt mode is enabled and there is an event in the HBA which requires
13724 * driver attention. This function invokes the slow-path interrupt attention
13725 * handling function and fast-path interrupt attention handling function in
13726 * turn to process the relevant HBA attention events. This function is called
13727 * without any lock held. It gets the hbalock to access and update SLI data
13728 * structures.
13729 *
13730 * This function returns IRQ_HANDLED when interrupt is handled, else it
13731 * returns IRQ_NONE.
13732 **/
13733irqreturn_t
13734lpfc_sli4_intr_handler(int irq, void *dev_id)
13735{
13736 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013737 irqreturn_t hba_irq_rc;
13738 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013739 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013740
13741 /* Get the driver's phba structure from the dev_id */
13742 phba = (struct lpfc_hba *)dev_id;
13743
13744 if (unlikely(!phba))
13745 return IRQ_NONE;
13746
13747 /*
James Smart4f774512009-05-22 14:52:35 -040013748 * Invoke fast-path host attention interrupt handling as appropriate.
13749 */
James Smart895427b2017-02-12 13:52:30 -080013750 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013751 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013752 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013753 if (hba_irq_rc == IRQ_HANDLED)
13754 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013755 }
13756
James Smart1ba981f2014-02-20 09:56:45 -050013757 if (phba->cfg_fof) {
13758 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013759 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013760 if (hba_irq_rc == IRQ_HANDLED)
13761 hba_handled |= true;
13762 }
13763
James Smart67d12732012-08-03 12:36:13 -040013764 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013765} /* lpfc_sli4_intr_handler */
13766
13767/**
13768 * lpfc_sli4_queue_free - free a queue structure and associated memory
13769 * @queue: The queue structure to free.
13770 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013771 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013772 * the host resident queue. This function must be called after destroying the
13773 * queue on the HBA.
13774 **/
13775void
13776lpfc_sli4_queue_free(struct lpfc_queue *queue)
13777{
13778 struct lpfc_dmabuf *dmabuf;
13779
13780 if (!queue)
13781 return;
13782
13783 while (!list_empty(&queue->page_list)) {
13784 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13785 list);
James Smart49198b32010-04-06 15:04:33 -040013786 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040013787 dmabuf->virt, dmabuf->phys);
13788 kfree(dmabuf);
13789 }
James Smart895427b2017-02-12 13:52:30 -080013790 if (queue->rqbp) {
13791 lpfc_free_rq_buffer(queue->phba, queue);
13792 kfree(queue->rqbp);
13793 }
James Smartd1f525a2017-04-21 16:04:55 -070013794
13795 if (!list_empty(&queue->wq_list))
13796 list_del(&queue->wq_list);
13797
James Smart4f774512009-05-22 14:52:35 -040013798 kfree(queue);
13799 return;
13800}
13801
13802/**
13803 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13804 * @phba: The HBA that this queue is being created on.
13805 * @entry_size: The size of each queue entry for this queue.
13806 * @entry count: The number of entries that this queue will handle.
13807 *
13808 * This function allocates a queue structure and the DMAable memory used for
13809 * the host resident queue. This function must be called before creating the
13810 * queue on the HBA.
13811 **/
13812struct lpfc_queue *
13813lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13814 uint32_t entry_count)
13815{
13816 struct lpfc_queue *queue;
13817 struct lpfc_dmabuf *dmabuf;
13818 int x, total_qe_count;
13819 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013820 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013821
James Smartcb5172e2010-03-15 11:25:07 -040013822 if (!phba->sli4_hba.pc_sli4_params.supported)
13823 hw_page_size = SLI4_PAGE_SIZE;
13824
James Smart4f774512009-05-22 14:52:35 -040013825 queue = kzalloc(sizeof(struct lpfc_queue) +
13826 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13827 if (!queue)
13828 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013829 queue->page_count = (ALIGN(entry_size * entry_count,
13830 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013831
13832 /* If needed, Adjust page count to match the max the adapter supports */
13833 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13834 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13835
James Smart4f774512009-05-22 14:52:35 -040013836 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013837 INIT_LIST_HEAD(&queue->wq_list);
James Smart4f774512009-05-22 14:52:35 -040013838 INIT_LIST_HEAD(&queue->page_list);
13839 INIT_LIST_HEAD(&queue->child_list);
13840 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13841 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13842 if (!dmabuf)
13843 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013844 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13845 hw_page_size, &dmabuf->phys,
13846 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013847 if (!dmabuf->virt) {
13848 kfree(dmabuf);
13849 goto out_fail;
13850 }
13851 dmabuf->buffer_tag = x;
13852 list_add_tail(&dmabuf->list, &queue->page_list);
13853 /* initialize queue's entry array */
13854 dma_pointer = dmabuf->virt;
13855 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013856 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013857 total_qe_count++, dma_pointer += entry_size) {
13858 queue->qe[total_qe_count].address = dma_pointer;
13859 }
13860 }
13861 queue->entry_size = entry_size;
13862 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040013863
13864 /*
13865 * entry_repost is calculated based on the number of entries in the
13866 * queue. This works out except for RQs. If buffers are NOT initially
13867 * posted for every RQE, entry_repost should be adjusted accordingly.
13868 */
13869 queue->entry_repost = (entry_count >> 3);
13870 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13871 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013872 queue->phba = phba;
13873
13874 return queue;
13875out_fail:
13876 lpfc_sli4_queue_free(queue);
13877 return NULL;
13878}
13879
13880/**
James Smart962bc512013-01-03 15:44:00 -050013881 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13882 * @phba: HBA structure that indicates port to create a queue on.
13883 * @pci_barset: PCI BAR set flag.
13884 *
13885 * This function shall perform iomap of the specified PCI BAR address to host
13886 * memory address if not already done so and return it. The returned host
13887 * memory address can be NULL.
13888 */
13889static void __iomem *
13890lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13891{
James Smart962bc512013-01-03 15:44:00 -050013892 if (!phba->pcidev)
13893 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013894
13895 switch (pci_barset) {
13896 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013897 return phba->pci_bar0_memmap_p;
13898 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013899 return phba->pci_bar2_memmap_p;
13900 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013901 return phba->pci_bar4_memmap_p;
13902 default:
13903 break;
13904 }
13905 return NULL;
13906}
13907
13908/**
James Smart895427b2017-02-12 13:52:30 -080013909 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040013910 * @phba: HBA structure that indicates port to create a queue on.
13911 * @startq: The starting FCP EQ to modify
13912 *
13913 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080013914 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
13915 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040013916 *
13917 * The @phba struct is used to send mailbox command to HBA. The @startq
13918 * is used to get the starting FCP EQ to change.
13919 * This function is asynchronous and will wait for the mailbox
13920 * command to finish before continuing.
13921 *
13922 * On success this function will return a zero. If unable to allocate enough
13923 * memory this function will return -ENOMEM. If the queue create mailbox command
13924 * fails this function will return -ENXIO.
13925 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013926int
James Smart895427b2017-02-12 13:52:30 -080013927lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013928{
13929 struct lpfc_mbx_modify_eq_delay *eq_delay;
13930 LPFC_MBOXQ_t *mbox;
13931 struct lpfc_queue *eq;
13932 int cnt, rc, length, status = 0;
13933 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013934 uint32_t result;
James Smart895427b2017-02-12 13:52:30 -080013935 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040013936 union lpfc_sli4_cfg_shdr *shdr;
13937 uint16_t dmult;
13938
James Smart895427b2017-02-12 13:52:30 -080013939 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040013940 return 0;
13941
13942 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13943 if (!mbox)
13944 return -ENOMEM;
13945 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13946 sizeof(struct lpfc_sli4_cfg_mhdr));
13947 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13948 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13949 length, LPFC_SLI4_MBX_EMBED);
13950 eq_delay = &mbox->u.mqe.un.eq_delay;
13951
13952 /* Calculate delay multiper from maximum interrupt per second */
James Smart895427b2017-02-12 13:52:30 -080013953 result = phba->cfg_fcp_imax / phba->io_channel_irqs;
13954 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040013955 dmult = 0;
13956 else
13957 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013958
13959 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080013960 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
13961 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040013962 if (!eq)
13963 continue;
13964 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13965 eq_delay->u.request.eq[cnt].phase = 0;
13966 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13967 cnt++;
James Smart43140ca2017-03-04 09:30:34 -080013968 if (cnt >= LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart173edbb2012-06-12 13:54:50 -040013969 break;
13970 }
13971 eq_delay->u.request.num_eq = cnt;
13972
13973 mbox->vport = phba->pport;
13974 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13975 mbox->context1 = NULL;
13976 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13977 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13978 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13979 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13980 if (shdr_status || shdr_add_status || rc) {
13981 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13982 "2512 MODIFY_EQ_DELAY mailbox failed with "
13983 "status x%x add_status x%x, mbx status x%x\n",
13984 shdr_status, shdr_add_status, rc);
13985 status = -ENXIO;
13986 }
13987 mempool_free(mbox, phba->mbox_mem_pool);
13988 return status;
13989}
13990
13991/**
James Smart4f774512009-05-22 14:52:35 -040013992 * lpfc_eq_create - Create an Event Queue on the HBA
13993 * @phba: HBA structure that indicates port to create a queue on.
13994 * @eq: The queue structure to use to create the event queue.
13995 * @imax: The maximum interrupt per second limit.
13996 *
13997 * This function creates an event queue, as detailed in @eq, on a port,
13998 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13999 *
14000 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14001 * is used to get the entry count and entry size that are necessary to
14002 * determine the number of pages to allocate and use for this queue. This
14003 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14004 * event queue. This function is asynchronous and will wait for the mailbox
14005 * command to finish before continuing.
14006 *
14007 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014008 * memory this function will return -ENOMEM. If the queue create mailbox command
14009 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014010 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014011int
James Smartee020062012-09-29 11:28:52 -040014012lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014013{
14014 struct lpfc_mbx_eq_create *eq_create;
14015 LPFC_MBOXQ_t *mbox;
14016 int rc, length, status = 0;
14017 struct lpfc_dmabuf *dmabuf;
14018 uint32_t shdr_status, shdr_add_status;
14019 union lpfc_sli4_cfg_shdr *shdr;
14020 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014021 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14022
James Smart2e90f4b2011-12-13 13:22:37 -050014023 /* sanity check on queue memory */
14024 if (!eq)
14025 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014026 if (!phba->sli4_hba.pc_sli4_params.supported)
14027 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014028
14029 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14030 if (!mbox)
14031 return -ENOMEM;
14032 length = (sizeof(struct lpfc_mbx_eq_create) -
14033 sizeof(struct lpfc_sli4_cfg_mhdr));
14034 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14035 LPFC_MBOX_OPCODE_EQ_CREATE,
14036 length, LPFC_SLI4_MBX_EMBED);
14037 eq_create = &mbox->u.mqe.un.eq_create;
14038 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14039 eq->page_count);
14040 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14041 LPFC_EQE_SIZE);
14042 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040014043 /* don't setup delay multiplier using EQ_CREATE */
14044 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014045 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14046 dmult);
14047 switch (eq->entry_count) {
14048 default:
14049 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14050 "0360 Unsupported EQ count. (%d)\n",
14051 eq->entry_count);
14052 if (eq->entry_count < 256)
14053 return -EINVAL;
14054 /* otherwise default to smallest count (drop through) */
14055 case 256:
14056 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14057 LPFC_EQ_CNT_256);
14058 break;
14059 case 512:
14060 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14061 LPFC_EQ_CNT_512);
14062 break;
14063 case 1024:
14064 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14065 LPFC_EQ_CNT_1024);
14066 break;
14067 case 2048:
14068 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14069 LPFC_EQ_CNT_2048);
14070 break;
14071 case 4096:
14072 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14073 LPFC_EQ_CNT_4096);
14074 break;
14075 }
14076 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014077 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014078 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14079 putPaddrLow(dmabuf->phys);
14080 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14081 putPaddrHigh(dmabuf->phys);
14082 }
14083 mbox->vport = phba->pport;
14084 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14085 mbox->context1 = NULL;
14086 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14087 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14088 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14089 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14090 if (shdr_status || shdr_add_status || rc) {
14091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14092 "2500 EQ_CREATE mailbox failed with "
14093 "status x%x add_status x%x, mbx status x%x\n",
14094 shdr_status, shdr_add_status, rc);
14095 status = -ENXIO;
14096 }
14097 eq->type = LPFC_EQ;
14098 eq->subtype = LPFC_NONE;
14099 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14100 if (eq->queue_id == 0xFFFF)
14101 status = -ENXIO;
14102 eq->host_index = 0;
14103 eq->hba_index = 0;
14104
James Smart8fa38512009-07-19 10:01:03 -040014105 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014106 return status;
14107}
14108
14109/**
14110 * lpfc_cq_create - Create a Completion Queue on the HBA
14111 * @phba: HBA structure that indicates port to create a queue on.
14112 * @cq: The queue structure to use to create the completion queue.
14113 * @eq: The event queue to bind this completion queue to.
14114 *
14115 * This function creates a completion queue, as detailed in @wq, on a port,
14116 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14117 *
14118 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14119 * is used to get the entry count and entry size that are necessary to
14120 * determine the number of pages to allocate and use for this queue. The @eq
14121 * is used to indicate which event queue to bind this completion queue to. This
14122 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14123 * completion queue. This function is asynchronous and will wait for the mailbox
14124 * command to finish before continuing.
14125 *
14126 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014127 * memory this function will return -ENOMEM. If the queue create mailbox command
14128 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014129 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014130int
James Smart4f774512009-05-22 14:52:35 -040014131lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14132 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14133{
14134 struct lpfc_mbx_cq_create *cq_create;
14135 struct lpfc_dmabuf *dmabuf;
14136 LPFC_MBOXQ_t *mbox;
14137 int rc, length, status = 0;
14138 uint32_t shdr_status, shdr_add_status;
14139 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014140 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14141
James Smart2e90f4b2011-12-13 13:22:37 -050014142 /* sanity check on queue memory */
14143 if (!cq || !eq)
14144 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014145 if (!phba->sli4_hba.pc_sli4_params.supported)
14146 hw_page_size = SLI4_PAGE_SIZE;
14147
James Smart4f774512009-05-22 14:52:35 -040014148 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14149 if (!mbox)
14150 return -ENOMEM;
14151 length = (sizeof(struct lpfc_mbx_cq_create) -
14152 sizeof(struct lpfc_sli4_cfg_mhdr));
14153 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14154 LPFC_MBOX_OPCODE_CQ_CREATE,
14155 length, LPFC_SLI4_MBX_EMBED);
14156 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014157 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014158 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14159 cq->page_count);
14160 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14161 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014162 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14163 phba->sli4_hba.pc_sli4_params.cqv);
14164 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040014165 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
14166 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014167 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14168 eq->queue_id);
14169 } else {
14170 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14171 eq->queue_id);
14172 }
James Smart4f774512009-05-22 14:52:35 -040014173 switch (cq->entry_count) {
14174 default:
14175 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014176 "0361 Unsupported CQ count: "
14177 "entry cnt %d sz %d pg cnt %d repost %d\n",
14178 cq->entry_count, cq->entry_size,
14179 cq->page_count, cq->entry_repost);
James Smart4f4c1862012-06-12 13:54:02 -040014180 if (cq->entry_count < 256) {
14181 status = -EINVAL;
14182 goto out;
14183 }
James Smart4f774512009-05-22 14:52:35 -040014184 /* otherwise default to smallest count (drop through) */
14185 case 256:
14186 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14187 LPFC_CQ_CNT_256);
14188 break;
14189 case 512:
14190 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14191 LPFC_CQ_CNT_512);
14192 break;
14193 case 1024:
14194 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14195 LPFC_CQ_CNT_1024);
14196 break;
14197 }
14198 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014199 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014200 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14201 putPaddrLow(dmabuf->phys);
14202 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14203 putPaddrHigh(dmabuf->phys);
14204 }
14205 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14206
14207 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014208 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14209 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14210 if (shdr_status || shdr_add_status || rc) {
14211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14212 "2501 CQ_CREATE mailbox failed with "
14213 "status x%x add_status x%x, mbx status x%x\n",
14214 shdr_status, shdr_add_status, rc);
14215 status = -ENXIO;
14216 goto out;
14217 }
14218 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14219 if (cq->queue_id == 0xFFFF) {
14220 status = -ENXIO;
14221 goto out;
14222 }
14223 /* link the cq onto the parent eq child list */
14224 list_add_tail(&cq->list, &eq->child_list);
14225 /* Set up completion queue's type and subtype */
14226 cq->type = type;
14227 cq->subtype = subtype;
14228 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014229 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014230 cq->host_index = 0;
14231 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040014232
James Smart8fa38512009-07-19 10:01:03 -040014233out:
14234 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014235 return status;
14236}
14237
14238/**
James Smart2d7dbc42017-02-12 13:52:35 -080014239 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14240 * @phba: HBA structure that indicates port to create a queue on.
14241 * @cqp: The queue structure array to use to create the completion queues.
14242 * @eqp: The event queue array to bind these completion queues to.
14243 *
14244 * This function creates a set of completion queue, s to support MRQ
14245 * as detailed in @cqp, on a port,
14246 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14247 *
14248 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14249 * is used to get the entry count and entry size that are necessary to
14250 * determine the number of pages to allocate and use for this queue. The @eq
14251 * is used to indicate which event queue to bind this completion queue to. This
14252 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14253 * completion queue. This function is asynchronous and will wait for the mailbox
14254 * command to finish before continuing.
14255 *
14256 * On success this function will return a zero. If unable to allocate enough
14257 * memory this function will return -ENOMEM. If the queue create mailbox command
14258 * fails this function will return -ENXIO.
14259 **/
14260int
14261lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14262 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14263{
14264 struct lpfc_queue *cq;
14265 struct lpfc_queue *eq;
14266 struct lpfc_mbx_cq_create_set *cq_set;
14267 struct lpfc_dmabuf *dmabuf;
14268 LPFC_MBOXQ_t *mbox;
14269 int rc, length, alloclen, status = 0;
14270 int cnt, idx, numcq, page_idx = 0;
14271 uint32_t shdr_status, shdr_add_status;
14272 union lpfc_sli4_cfg_shdr *shdr;
14273 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14274
14275 /* sanity check on queue memory */
14276 numcq = phba->cfg_nvmet_mrq;
14277 if (!cqp || !eqp || !numcq)
14278 return -ENODEV;
14279 if (!phba->sli4_hba.pc_sli4_params.supported)
14280 hw_page_size = SLI4_PAGE_SIZE;
14281
14282 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14283 if (!mbox)
14284 return -ENOMEM;
14285
14286 length = sizeof(struct lpfc_mbx_cq_create_set);
14287 length += ((numcq * cqp[0]->page_count) *
14288 sizeof(struct dma_address));
14289 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14290 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14291 LPFC_SLI4_MBX_NEMBED);
14292 if (alloclen < length) {
14293 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14294 "3098 Allocated DMA memory size (%d) is "
14295 "less than the requested DMA memory size "
14296 "(%d)\n", alloclen, length);
14297 status = -ENOMEM;
14298 goto out;
14299 }
14300 cq_set = mbox->sge_array->addr[0];
14301 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14302 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14303
14304 for (idx = 0; idx < numcq; idx++) {
14305 cq = cqp[idx];
14306 eq = eqp[idx];
14307 if (!cq || !eq) {
14308 status = -ENOMEM;
14309 goto out;
14310 }
14311
14312 switch (idx) {
14313 case 0:
14314 bf_set(lpfc_mbx_cq_create_set_page_size,
14315 &cq_set->u.request,
14316 (hw_page_size / SLI4_PAGE_SIZE));
14317 bf_set(lpfc_mbx_cq_create_set_num_pages,
14318 &cq_set->u.request, cq->page_count);
14319 bf_set(lpfc_mbx_cq_create_set_evt,
14320 &cq_set->u.request, 1);
14321 bf_set(lpfc_mbx_cq_create_set_valid,
14322 &cq_set->u.request, 1);
14323 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14324 &cq_set->u.request, 0);
14325 bf_set(lpfc_mbx_cq_create_set_num_cq,
14326 &cq_set->u.request, numcq);
14327 switch (cq->entry_count) {
14328 default:
14329 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14330 "3118 Bad CQ count. (%d)\n",
14331 cq->entry_count);
14332 if (cq->entry_count < 256) {
14333 status = -EINVAL;
14334 goto out;
14335 }
14336 /* otherwise default to smallest (drop thru) */
14337 case 256:
14338 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14339 &cq_set->u.request, LPFC_CQ_CNT_256);
14340 break;
14341 case 512:
14342 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14343 &cq_set->u.request, LPFC_CQ_CNT_512);
14344 break;
14345 case 1024:
14346 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14347 &cq_set->u.request, LPFC_CQ_CNT_1024);
14348 break;
14349 }
14350 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14351 &cq_set->u.request, eq->queue_id);
14352 break;
14353 case 1:
14354 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14355 &cq_set->u.request, eq->queue_id);
14356 break;
14357 case 2:
14358 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14359 &cq_set->u.request, eq->queue_id);
14360 break;
14361 case 3:
14362 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14363 &cq_set->u.request, eq->queue_id);
14364 break;
14365 case 4:
14366 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14367 &cq_set->u.request, eq->queue_id);
14368 break;
14369 case 5:
14370 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14371 &cq_set->u.request, eq->queue_id);
14372 break;
14373 case 6:
14374 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14375 &cq_set->u.request, eq->queue_id);
14376 break;
14377 case 7:
14378 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14379 &cq_set->u.request, eq->queue_id);
14380 break;
14381 case 8:
14382 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14383 &cq_set->u.request, eq->queue_id);
14384 break;
14385 case 9:
14386 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14387 &cq_set->u.request, eq->queue_id);
14388 break;
14389 case 10:
14390 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14391 &cq_set->u.request, eq->queue_id);
14392 break;
14393 case 11:
14394 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14395 &cq_set->u.request, eq->queue_id);
14396 break;
14397 case 12:
14398 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14399 &cq_set->u.request, eq->queue_id);
14400 break;
14401 case 13:
14402 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14403 &cq_set->u.request, eq->queue_id);
14404 break;
14405 case 14:
14406 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14407 &cq_set->u.request, eq->queue_id);
14408 break;
14409 case 15:
14410 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14411 &cq_set->u.request, eq->queue_id);
14412 break;
14413 }
14414
14415 /* link the cq onto the parent eq child list */
14416 list_add_tail(&cq->list, &eq->child_list);
14417 /* Set up completion queue's type and subtype */
14418 cq->type = type;
14419 cq->subtype = subtype;
14420 cq->assoc_qid = eq->queue_id;
14421 cq->host_index = 0;
14422 cq->hba_index = 0;
14423
14424 rc = 0;
14425 list_for_each_entry(dmabuf, &cq->page_list, list) {
14426 memset(dmabuf->virt, 0, hw_page_size);
14427 cnt = page_idx + dmabuf->buffer_tag;
14428 cq_set->u.request.page[cnt].addr_lo =
14429 putPaddrLow(dmabuf->phys);
14430 cq_set->u.request.page[cnt].addr_hi =
14431 putPaddrHigh(dmabuf->phys);
14432 rc++;
14433 }
14434 page_idx += rc;
14435 }
14436
14437 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14438
14439 /* The IOCTL status is embedded in the mailbox subheader. */
14440 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14441 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14442 if (shdr_status || shdr_add_status || rc) {
14443 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14444 "3119 CQ_CREATE_SET mailbox failed with "
14445 "status x%x add_status x%x, mbx status x%x\n",
14446 shdr_status, shdr_add_status, rc);
14447 status = -ENXIO;
14448 goto out;
14449 }
14450 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14451 if (rc == 0xFFFF) {
14452 status = -ENXIO;
14453 goto out;
14454 }
14455
14456 for (idx = 0; idx < numcq; idx++) {
14457 cq = cqp[idx];
14458 cq->queue_id = rc + idx;
14459 }
14460
14461out:
14462 lpfc_sli4_mbox_cmd_free(phba, mbox);
14463 return status;
14464}
14465
14466/**
James Smartb19a0612010-04-06 14:48:51 -040014467 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014468 * @phba: HBA structure that indicates port to create a queue on.
14469 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014470 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14471 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014472 *
James Smartb19a0612010-04-06 14:48:51 -040014473 * This function provides failback (fb) functionality when the
14474 * mq_create_ext fails on older FW generations. It's purpose is identical
14475 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014476 *
James Smartb19a0612010-04-06 14:48:51 -040014477 * This routine cannot fail as all attributes were previously accessed and
14478 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014479 **/
James Smartb19a0612010-04-06 14:48:51 -040014480static void
14481lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14482 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014483{
14484 struct lpfc_mbx_mq_create *mq_create;
14485 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014486 int length;
James Smart04c68492009-05-22 14:52:52 -040014487
James Smart04c68492009-05-22 14:52:52 -040014488 length = (sizeof(struct lpfc_mbx_mq_create) -
14489 sizeof(struct lpfc_sli4_cfg_mhdr));
14490 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14491 LPFC_MBOX_OPCODE_MQ_CREATE,
14492 length, LPFC_SLI4_MBX_EMBED);
14493 mq_create = &mbox->u.mqe.un.mq_create;
14494 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014495 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014496 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014497 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014498 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14499 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014500 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014501 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14502 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014503 break;
14504 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014505 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14506 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014507 break;
14508 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014509 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14510 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014511 break;
14512 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014513 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14514 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014515 break;
14516 }
14517 list_for_each_entry(dmabuf, &mq->page_list, list) {
14518 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014519 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014520 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014521 putPaddrHigh(dmabuf->phys);
14522 }
14523}
14524
14525/**
14526 * lpfc_mq_create - Create a mailbox Queue on the HBA
14527 * @phba: HBA structure that indicates port to create a queue on.
14528 * @mq: The queue structure to use to create the mailbox queue.
14529 * @cq: The completion queue to associate with this cq.
14530 * @subtype: The queue's subtype.
14531 *
14532 * This function creates a mailbox queue, as detailed in @mq, on a port,
14533 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14534 *
14535 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14536 * is used to get the entry count and entry size that are necessary to
14537 * determine the number of pages to allocate and use for this queue. This
14538 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14539 * mailbox queue. This function is asynchronous and will wait for the mailbox
14540 * command to finish before continuing.
14541 *
14542 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014543 * memory this function will return -ENOMEM. If the queue create mailbox command
14544 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014545 **/
14546int32_t
14547lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14548 struct lpfc_queue *cq, uint32_t subtype)
14549{
14550 struct lpfc_mbx_mq_create *mq_create;
14551 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14552 struct lpfc_dmabuf *dmabuf;
14553 LPFC_MBOXQ_t *mbox;
14554 int rc, length, status = 0;
14555 uint32_t shdr_status, shdr_add_status;
14556 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014557 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014558
James Smart2e90f4b2011-12-13 13:22:37 -050014559 /* sanity check on queue memory */
14560 if (!mq || !cq)
14561 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014562 if (!phba->sli4_hba.pc_sli4_params.supported)
14563 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014564
14565 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14566 if (!mbox)
14567 return -ENOMEM;
14568 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14569 sizeof(struct lpfc_sli4_cfg_mhdr));
14570 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14571 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14572 length, LPFC_SLI4_MBX_EMBED);
14573
14574 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014575 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014576 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14577 &mq_create_ext->u.request, mq->page_count);
14578 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14579 &mq_create_ext->u.request, 1);
14580 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014581 &mq_create_ext->u.request, 1);
14582 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14583 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014584 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14585 &mq_create_ext->u.request, 1);
14586 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14587 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014588 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014589 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14590 phba->sli4_hba.pc_sli4_params.mqv);
14591 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14592 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14593 cq->queue_id);
14594 else
14595 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14596 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014597 switch (mq->entry_count) {
14598 default:
14599 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14600 "0362 Unsupported MQ count. (%d)\n",
14601 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014602 if (mq->entry_count < 16) {
14603 status = -EINVAL;
14604 goto out;
14605 }
James Smartb19a0612010-04-06 14:48:51 -040014606 /* otherwise default to smallest count (drop through) */
14607 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014608 bf_set(lpfc_mq_context_ring_size,
14609 &mq_create_ext->u.request.context,
14610 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014611 break;
14612 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014613 bf_set(lpfc_mq_context_ring_size,
14614 &mq_create_ext->u.request.context,
14615 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014616 break;
14617 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014618 bf_set(lpfc_mq_context_ring_size,
14619 &mq_create_ext->u.request.context,
14620 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014621 break;
14622 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014623 bf_set(lpfc_mq_context_ring_size,
14624 &mq_create_ext->u.request.context,
14625 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014626 break;
14627 }
14628 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014629 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014630 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14631 putPaddrLow(dmabuf->phys);
14632 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014633 putPaddrHigh(dmabuf->phys);
14634 }
14635 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014636 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14637 &mq_create_ext->u.response);
14638 if (rc != MBX_SUCCESS) {
14639 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14640 "2795 MQ_CREATE_EXT failed with "
14641 "status x%x. Failback to MQ_CREATE.\n",
14642 rc);
14643 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14644 mq_create = &mbox->u.mqe.un.mq_create;
14645 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14646 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14647 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14648 &mq_create->u.response);
14649 }
14650
James Smart04c68492009-05-22 14:52:52 -040014651 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014652 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14653 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14654 if (shdr_status || shdr_add_status || rc) {
14655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14656 "2502 MQ_CREATE mailbox failed with "
14657 "status x%x add_status x%x, mbx status x%x\n",
14658 shdr_status, shdr_add_status, rc);
14659 status = -ENXIO;
14660 goto out;
14661 }
James Smart04c68492009-05-22 14:52:52 -040014662 if (mq->queue_id == 0xFFFF) {
14663 status = -ENXIO;
14664 goto out;
14665 }
14666 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014667 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014668 mq->subtype = subtype;
14669 mq->host_index = 0;
14670 mq->hba_index = 0;
14671
14672 /* link the mq onto the parent cq child list */
14673 list_add_tail(&mq->list, &cq->child_list);
14674out:
James Smart8fa38512009-07-19 10:01:03 -040014675 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014676 return status;
14677}
14678
14679/**
James Smart4f774512009-05-22 14:52:35 -040014680 * lpfc_wq_create - Create a Work Queue on the HBA
14681 * @phba: HBA structure that indicates port to create a queue on.
14682 * @wq: The queue structure to use to create the work queue.
14683 * @cq: The completion queue to bind this work queue to.
14684 * @subtype: The subtype of the work queue indicating its functionality.
14685 *
14686 * This function creates a work queue, as detailed in @wq, on a port, described
14687 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14688 *
14689 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14690 * is used to get the entry count and entry size that are necessary to
14691 * determine the number of pages to allocate and use for this queue. The @cq
14692 * is used to indicate which completion queue to bind this work queue to. This
14693 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14694 * work queue. This function is asynchronous and will wait for the mailbox
14695 * command to finish before continuing.
14696 *
14697 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014698 * memory this function will return -ENOMEM. If the queue create mailbox command
14699 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014700 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014701int
James Smart4f774512009-05-22 14:52:35 -040014702lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14703 struct lpfc_queue *cq, uint32_t subtype)
14704{
14705 struct lpfc_mbx_wq_create *wq_create;
14706 struct lpfc_dmabuf *dmabuf;
14707 LPFC_MBOXQ_t *mbox;
14708 int rc, length, status = 0;
14709 uint32_t shdr_status, shdr_add_status;
14710 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014711 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014712 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014713 void __iomem *bar_memmap_p;
14714 uint32_t db_offset;
14715 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014716
James Smart2e90f4b2011-12-13 13:22:37 -050014717 /* sanity check on queue memory */
14718 if (!wq || !cq)
14719 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014720 if (!phba->sli4_hba.pc_sli4_params.supported)
14721 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014722
14723 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14724 if (!mbox)
14725 return -ENOMEM;
14726 length = (sizeof(struct lpfc_mbx_wq_create) -
14727 sizeof(struct lpfc_sli4_cfg_mhdr));
14728 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14729 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14730 length, LPFC_SLI4_MBX_EMBED);
14731 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014732 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014733 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14734 wq->page_count);
14735 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14736 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014737
14738 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014739 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14740 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014741
James Smart0c651872013-07-15 18:33:23 -040014742 switch (phba->sli4_hba.pc_sli4_params.wqv) {
14743 case LPFC_Q_CREATE_VERSION_0:
14744 switch (wq->entry_size) {
14745 default:
14746 case 64:
14747 /* Nothing to do, version 0 ONLY supports 64 byte */
14748 page = wq_create->u.request.page;
14749 break;
14750 case 128:
14751 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14752 LPFC_WQ_SZ128_SUPPORT)) {
14753 status = -ERANGE;
14754 goto out;
14755 }
14756 /* If we get here the HBA MUST also support V1 and
14757 * we MUST use it
14758 */
14759 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14760 LPFC_Q_CREATE_VERSION_1);
14761
14762 bf_set(lpfc_mbx_wq_create_wqe_count,
14763 &wq_create->u.request_1, wq->entry_count);
14764 bf_set(lpfc_mbx_wq_create_wqe_size,
14765 &wq_create->u.request_1,
14766 LPFC_WQ_WQE_SIZE_128);
14767 bf_set(lpfc_mbx_wq_create_page_size,
14768 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014769 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014770 page = wq_create->u.request_1.page;
14771 break;
14772 }
14773 break;
14774 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014775 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14776 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070014777 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14778 LPFC_Q_CREATE_VERSION_1);
14779
James Smart5a6f1332011-03-11 16:05:35 -050014780 switch (wq->entry_size) {
14781 default:
14782 case 64:
14783 bf_set(lpfc_mbx_wq_create_wqe_size,
14784 &wq_create->u.request_1,
14785 LPFC_WQ_WQE_SIZE_64);
14786 break;
14787 case 128:
James Smart0c651872013-07-15 18:33:23 -040014788 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14789 LPFC_WQ_SZ128_SUPPORT)) {
14790 status = -ERANGE;
14791 goto out;
14792 }
James Smart5a6f1332011-03-11 16:05:35 -050014793 bf_set(lpfc_mbx_wq_create_wqe_size,
14794 &wq_create->u.request_1,
14795 LPFC_WQ_WQE_SIZE_128);
14796 break;
14797 }
James Smart8ea73db2017-02-12 13:52:25 -080014798 bf_set(lpfc_mbx_wq_create_page_size,
14799 &wq_create->u.request_1,
14800 LPFC_WQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014801 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040014802 break;
14803 default:
14804 status = -ERANGE;
14805 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050014806 }
James Smart0c651872013-07-15 18:33:23 -040014807
James Smart4f774512009-05-22 14:52:35 -040014808 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014809 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050014810 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14811 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040014812 }
James Smart962bc512013-01-03 15:44:00 -050014813
14814 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14815 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14816
James Smart4f774512009-05-22 14:52:35 -040014817 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14818 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014819 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14820 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14821 if (shdr_status || shdr_add_status || rc) {
14822 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14823 "2503 WQ_CREATE mailbox failed with "
14824 "status x%x add_status x%x, mbx status x%x\n",
14825 shdr_status, shdr_add_status, rc);
14826 status = -ENXIO;
14827 goto out;
14828 }
14829 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14830 if (wq->queue_id == 0xFFFF) {
14831 status = -ENXIO;
14832 goto out;
14833 }
James Smart962bc512013-01-03 15:44:00 -050014834 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14835 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14836 &wq_create->u.response);
14837 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14838 (wq->db_format != LPFC_DB_RING_FORMAT)) {
14839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14840 "3265 WQ[%d] doorbell format not "
14841 "supported: x%x\n", wq->queue_id,
14842 wq->db_format);
14843 status = -EINVAL;
14844 goto out;
14845 }
14846 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14847 &wq_create->u.response);
14848 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14849 if (!bar_memmap_p) {
14850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14851 "3263 WQ[%d] failed to memmap pci "
14852 "barset:x%x\n", wq->queue_id,
14853 pci_barset);
14854 status = -ENOMEM;
14855 goto out;
14856 }
14857 db_offset = wq_create->u.response.doorbell_offset;
14858 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14859 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14861 "3252 WQ[%d] doorbell offset not "
14862 "supported: x%x\n", wq->queue_id,
14863 db_offset);
14864 status = -EINVAL;
14865 goto out;
14866 }
14867 wq->db_regaddr = bar_memmap_p + db_offset;
14868 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014869 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14870 "format:x%x\n", wq->queue_id, pci_barset,
14871 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014872 } else {
14873 wq->db_format = LPFC_DB_LIST_FORMAT;
14874 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14875 }
James Smart895427b2017-02-12 13:52:30 -080014876 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14877 if (wq->pring == NULL) {
14878 status = -ENOMEM;
14879 goto out;
14880 }
James Smart4f774512009-05-22 14:52:35 -040014881 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050014882 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014883 wq->subtype = subtype;
14884 wq->host_index = 0;
14885 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050014886 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040014887
14888 /* link the wq onto the parent cq child list */
14889 list_add_tail(&wq->list, &cq->child_list);
14890out:
James Smart8fa38512009-07-19 10:01:03 -040014891 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014892 return status;
14893}
14894
14895/**
James Smart73d91e52011-10-10 21:32:10 -040014896 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
14897 * @phba: HBA structure that indicates port to create a queue on.
14898 * @rq: The queue structure to use for the receive queue.
14899 * @qno: The associated HBQ number
14900 *
14901 *
14902 * For SLI4 we need to adjust the RQ repost value based on
14903 * the number of buffers that are initially posted to the RQ.
14904 */
14905void
14906lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
14907{
14908 uint32_t cnt;
14909
James Smart2e90f4b2011-12-13 13:22:37 -050014910 /* sanity check on queue memory */
14911 if (!rq)
14912 return;
James Smart73d91e52011-10-10 21:32:10 -040014913 cnt = lpfc_hbq_defs[qno]->entry_count;
14914
14915 /* Recalc repost for RQs based on buffers initially posted */
14916 cnt = (cnt >> 3);
14917 if (cnt < LPFC_QUEUE_MIN_REPOST)
14918 cnt = LPFC_QUEUE_MIN_REPOST;
14919
14920 rq->entry_repost = cnt;
14921}
14922
14923/**
James Smart4f774512009-05-22 14:52:35 -040014924 * lpfc_rq_create - Create a Receive Queue on the HBA
14925 * @phba: HBA structure that indicates port to create a queue on.
14926 * @hrq: The queue structure to use to create the header receive queue.
14927 * @drq: The queue structure to use to create the data receive queue.
14928 * @cq: The completion queue to bind this work queue to.
14929 *
14930 * This function creates a receive buffer queue pair , as detailed in @hrq and
14931 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14932 * to the HBA.
14933 *
14934 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14935 * struct is used to get the entry count that is necessary to determine the
14936 * number of pages to use for this queue. The @cq is used to indicate which
14937 * completion queue to bind received buffers that are posted to these queues to.
14938 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14939 * receive queue pair. This function is asynchronous and will wait for the
14940 * mailbox command to finish before continuing.
14941 *
14942 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014943 * memory this function will return -ENOMEM. If the queue create mailbox command
14944 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014945 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014946int
James Smart4f774512009-05-22 14:52:35 -040014947lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14948 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14949{
14950 struct lpfc_mbx_rq_create *rq_create;
14951 struct lpfc_dmabuf *dmabuf;
14952 LPFC_MBOXQ_t *mbox;
14953 int rc, length, status = 0;
14954 uint32_t shdr_status, shdr_add_status;
14955 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014956 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050014957 void __iomem *bar_memmap_p;
14958 uint32_t db_offset;
14959 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014960
James Smart2e90f4b2011-12-13 13:22:37 -050014961 /* sanity check on queue memory */
14962 if (!hrq || !drq || !cq)
14963 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014964 if (!phba->sli4_hba.pc_sli4_params.supported)
14965 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014966
14967 if (hrq->entry_count != drq->entry_count)
14968 return -EINVAL;
14969 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14970 if (!mbox)
14971 return -ENOMEM;
14972 length = (sizeof(struct lpfc_mbx_rq_create) -
14973 sizeof(struct lpfc_sli4_cfg_mhdr));
14974 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14975 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14976 length, LPFC_SLI4_MBX_EMBED);
14977 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014978 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14979 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14980 phba->sli4_hba.pc_sli4_params.rqv);
14981 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14982 bf_set(lpfc_rq_context_rqe_count_1,
14983 &rq_create->u.request.context,
14984 hrq->entry_count);
14985 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014986 bf_set(lpfc_rq_context_rqe_size,
14987 &rq_create->u.request.context,
14988 LPFC_RQE_SIZE_8);
14989 bf_set(lpfc_rq_context_page_size,
14990 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080014991 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014992 } else {
14993 switch (hrq->entry_count) {
14994 default:
14995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14996 "2535 Unsupported RQ count. (%d)\n",
14997 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014998 if (hrq->entry_count < 512) {
14999 status = -EINVAL;
15000 goto out;
15001 }
James Smart5a6f1332011-03-11 16:05:35 -050015002 /* otherwise default to smallest count (drop through) */
15003 case 512:
15004 bf_set(lpfc_rq_context_rqe_count,
15005 &rq_create->u.request.context,
15006 LPFC_RQ_RING_SIZE_512);
15007 break;
15008 case 1024:
15009 bf_set(lpfc_rq_context_rqe_count,
15010 &rq_create->u.request.context,
15011 LPFC_RQ_RING_SIZE_1024);
15012 break;
15013 case 2048:
15014 bf_set(lpfc_rq_context_rqe_count,
15015 &rq_create->u.request.context,
15016 LPFC_RQ_RING_SIZE_2048);
15017 break;
15018 case 4096:
15019 bf_set(lpfc_rq_context_rqe_count,
15020 &rq_create->u.request.context,
15021 LPFC_RQ_RING_SIZE_4096);
15022 break;
15023 }
15024 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15025 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015026 }
15027 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15028 cq->queue_id);
15029 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15030 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015031 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015032 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015033 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15034 putPaddrLow(dmabuf->phys);
15035 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15036 putPaddrHigh(dmabuf->phys);
15037 }
James Smart962bc512013-01-03 15:44:00 -050015038 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15039 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15040
James Smart4f774512009-05-22 14:52:35 -040015041 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15042 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015043 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15044 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15045 if (shdr_status || shdr_add_status || rc) {
15046 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15047 "2504 RQ_CREATE mailbox failed with "
15048 "status x%x add_status x%x, mbx status x%x\n",
15049 shdr_status, shdr_add_status, rc);
15050 status = -ENXIO;
15051 goto out;
15052 }
15053 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15054 if (hrq->queue_id == 0xFFFF) {
15055 status = -ENXIO;
15056 goto out;
15057 }
James Smart962bc512013-01-03 15:44:00 -050015058
15059 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15060 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15061 &rq_create->u.response);
15062 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15063 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15065 "3262 RQ [%d] doorbell format not "
15066 "supported: x%x\n", hrq->queue_id,
15067 hrq->db_format);
15068 status = -EINVAL;
15069 goto out;
15070 }
15071
15072 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15073 &rq_create->u.response);
15074 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15075 if (!bar_memmap_p) {
15076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15077 "3269 RQ[%d] failed to memmap pci "
15078 "barset:x%x\n", hrq->queue_id,
15079 pci_barset);
15080 status = -ENOMEM;
15081 goto out;
15082 }
15083
15084 db_offset = rq_create->u.response.doorbell_offset;
15085 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15086 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15088 "3270 RQ[%d] doorbell offset not "
15089 "supported: x%x\n", hrq->queue_id,
15090 db_offset);
15091 status = -EINVAL;
15092 goto out;
15093 }
15094 hrq->db_regaddr = bar_memmap_p + db_offset;
15095 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015096 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15097 "format:x%x\n", hrq->queue_id, pci_barset,
15098 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015099 } else {
15100 hrq->db_format = LPFC_DB_RING_FORMAT;
15101 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15102 }
James Smart4f774512009-05-22 14:52:35 -040015103 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015104 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015105 hrq->subtype = subtype;
15106 hrq->host_index = 0;
15107 hrq->hba_index = 0;
15108
15109 /* now create the data queue */
15110 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15111 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15112 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015113 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15114 phba->sli4_hba.pc_sli4_params.rqv);
15115 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15116 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015117 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050015118 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015119 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15120 LPFC_RQE_SIZE_8);
15121 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15122 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015123 } else {
15124 switch (drq->entry_count) {
15125 default:
15126 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15127 "2536 Unsupported RQ count. (%d)\n",
15128 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015129 if (drq->entry_count < 512) {
15130 status = -EINVAL;
15131 goto out;
15132 }
James Smart5a6f1332011-03-11 16:05:35 -050015133 /* otherwise default to smallest count (drop through) */
15134 case 512:
15135 bf_set(lpfc_rq_context_rqe_count,
15136 &rq_create->u.request.context,
15137 LPFC_RQ_RING_SIZE_512);
15138 break;
15139 case 1024:
15140 bf_set(lpfc_rq_context_rqe_count,
15141 &rq_create->u.request.context,
15142 LPFC_RQ_RING_SIZE_1024);
15143 break;
15144 case 2048:
15145 bf_set(lpfc_rq_context_rqe_count,
15146 &rq_create->u.request.context,
15147 LPFC_RQ_RING_SIZE_2048);
15148 break;
15149 case 4096:
15150 bf_set(lpfc_rq_context_rqe_count,
15151 &rq_create->u.request.context,
15152 LPFC_RQ_RING_SIZE_4096);
15153 break;
15154 }
15155 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15156 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015157 }
15158 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15159 cq->queue_id);
15160 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15161 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015162 list_for_each_entry(dmabuf, &drq->page_list, list) {
15163 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15164 putPaddrLow(dmabuf->phys);
15165 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15166 putPaddrHigh(dmabuf->phys);
15167 }
James Smart962bc512013-01-03 15:44:00 -050015168 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15169 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015170 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15171 /* The IOCTL status is embedded in the mailbox subheader. */
15172 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15173 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15174 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15175 if (shdr_status || shdr_add_status || rc) {
15176 status = -ENXIO;
15177 goto out;
15178 }
15179 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15180 if (drq->queue_id == 0xFFFF) {
15181 status = -ENXIO;
15182 goto out;
15183 }
15184 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015185 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015186 drq->subtype = subtype;
15187 drq->host_index = 0;
15188 drq->hba_index = 0;
15189
15190 /* link the header and data RQs onto the parent cq child list */
15191 list_add_tail(&hrq->list, &cq->child_list);
15192 list_add_tail(&drq->list, &cq->child_list);
15193
15194out:
James Smart8fa38512009-07-19 10:01:03 -040015195 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015196 return status;
15197}
15198
15199/**
James Smart2d7dbc42017-02-12 13:52:35 -080015200 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15201 * @phba: HBA structure that indicates port to create a queue on.
15202 * @hrqp: The queue structure array to use to create the header receive queues.
15203 * @drqp: The queue structure array to use to create the data receive queues.
15204 * @cqp: The completion queue array to bind these receive queues to.
15205 *
15206 * This function creates a receive buffer queue pair , as detailed in @hrq and
15207 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15208 * to the HBA.
15209 *
15210 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15211 * struct is used to get the entry count that is necessary to determine the
15212 * number of pages to use for this queue. The @cq is used to indicate which
15213 * completion queue to bind received buffers that are posted to these queues to.
15214 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15215 * receive queue pair. This function is asynchronous and will wait for the
15216 * mailbox command to finish before continuing.
15217 *
15218 * On success this function will return a zero. If unable to allocate enough
15219 * memory this function will return -ENOMEM. If the queue create mailbox command
15220 * fails this function will return -ENXIO.
15221 **/
15222int
15223lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15224 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15225 uint32_t subtype)
15226{
15227 struct lpfc_queue *hrq, *drq, *cq;
15228 struct lpfc_mbx_rq_create_v2 *rq_create;
15229 struct lpfc_dmabuf *dmabuf;
15230 LPFC_MBOXQ_t *mbox;
15231 int rc, length, alloclen, status = 0;
15232 int cnt, idx, numrq, page_idx = 0;
15233 uint32_t shdr_status, shdr_add_status;
15234 union lpfc_sli4_cfg_shdr *shdr;
15235 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15236
15237 numrq = phba->cfg_nvmet_mrq;
15238 /* sanity check on array memory */
15239 if (!hrqp || !drqp || !cqp || !numrq)
15240 return -ENODEV;
15241 if (!phba->sli4_hba.pc_sli4_params.supported)
15242 hw_page_size = SLI4_PAGE_SIZE;
15243
15244 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15245 if (!mbox)
15246 return -ENOMEM;
15247
15248 length = sizeof(struct lpfc_mbx_rq_create_v2);
15249 length += ((2 * numrq * hrqp[0]->page_count) *
15250 sizeof(struct dma_address));
15251
15252 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15253 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15254 LPFC_SLI4_MBX_NEMBED);
15255 if (alloclen < length) {
15256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15257 "3099 Allocated DMA memory size (%d) is "
15258 "less than the requested DMA memory size "
15259 "(%d)\n", alloclen, length);
15260 status = -ENOMEM;
15261 goto out;
15262 }
15263
15264
15265
15266 rq_create = mbox->sge_array->addr[0];
15267 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15268
15269 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15270 cnt = 0;
15271
15272 for (idx = 0; idx < numrq; idx++) {
15273 hrq = hrqp[idx];
15274 drq = drqp[idx];
15275 cq = cqp[idx];
15276
James Smart2d7dbc42017-02-12 13:52:35 -080015277 /* sanity check on queue memory */
15278 if (!hrq || !drq || !cq) {
15279 status = -ENODEV;
15280 goto out;
15281 }
15282
James Smart7aabe842017-03-04 09:30:22 -080015283 if (hrq->entry_count != drq->entry_count) {
15284 status = -EINVAL;
15285 goto out;
15286 }
15287
James Smart2d7dbc42017-02-12 13:52:35 -080015288 if (idx == 0) {
15289 bf_set(lpfc_mbx_rq_create_num_pages,
15290 &rq_create->u.request,
15291 hrq->page_count);
15292 bf_set(lpfc_mbx_rq_create_rq_cnt,
15293 &rq_create->u.request, (numrq * 2));
15294 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15295 1);
15296 bf_set(lpfc_rq_context_base_cq,
15297 &rq_create->u.request.context,
15298 cq->queue_id);
15299 bf_set(lpfc_rq_context_data_size,
15300 &rq_create->u.request.context,
15301 LPFC_DATA_BUF_SIZE);
15302 bf_set(lpfc_rq_context_hdr_size,
15303 &rq_create->u.request.context,
15304 LPFC_HDR_BUF_SIZE);
15305 bf_set(lpfc_rq_context_rqe_count_1,
15306 &rq_create->u.request.context,
15307 hrq->entry_count);
15308 bf_set(lpfc_rq_context_rqe_size,
15309 &rq_create->u.request.context,
15310 LPFC_RQE_SIZE_8);
15311 bf_set(lpfc_rq_context_page_size,
15312 &rq_create->u.request.context,
15313 (PAGE_SIZE/SLI4_PAGE_SIZE));
15314 }
15315 rc = 0;
15316 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15317 memset(dmabuf->virt, 0, hw_page_size);
15318 cnt = page_idx + dmabuf->buffer_tag;
15319 rq_create->u.request.page[cnt].addr_lo =
15320 putPaddrLow(dmabuf->phys);
15321 rq_create->u.request.page[cnt].addr_hi =
15322 putPaddrHigh(dmabuf->phys);
15323 rc++;
15324 }
15325 page_idx += rc;
15326
15327 rc = 0;
15328 list_for_each_entry(dmabuf, &drq->page_list, list) {
15329 memset(dmabuf->virt, 0, hw_page_size);
15330 cnt = page_idx + dmabuf->buffer_tag;
15331 rq_create->u.request.page[cnt].addr_lo =
15332 putPaddrLow(dmabuf->phys);
15333 rq_create->u.request.page[cnt].addr_hi =
15334 putPaddrHigh(dmabuf->phys);
15335 rc++;
15336 }
15337 page_idx += rc;
15338
15339 hrq->db_format = LPFC_DB_RING_FORMAT;
15340 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15341 hrq->type = LPFC_HRQ;
15342 hrq->assoc_qid = cq->queue_id;
15343 hrq->subtype = subtype;
15344 hrq->host_index = 0;
15345 hrq->hba_index = 0;
15346
15347 drq->db_format = LPFC_DB_RING_FORMAT;
15348 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15349 drq->type = LPFC_DRQ;
15350 drq->assoc_qid = cq->queue_id;
15351 drq->subtype = subtype;
15352 drq->host_index = 0;
15353 drq->hba_index = 0;
15354
15355 list_add_tail(&hrq->list, &cq->child_list);
15356 list_add_tail(&drq->list, &cq->child_list);
15357 }
15358
15359 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15360 /* The IOCTL status is embedded in the mailbox subheader. */
15361 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15362 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15363 if (shdr_status || shdr_add_status || rc) {
15364 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15365 "3120 RQ_CREATE mailbox failed with "
15366 "status x%x add_status x%x, mbx status x%x\n",
15367 shdr_status, shdr_add_status, rc);
15368 status = -ENXIO;
15369 goto out;
15370 }
15371 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15372 if (rc == 0xFFFF) {
15373 status = -ENXIO;
15374 goto out;
15375 }
15376
15377 /* Initialize all RQs with associated queue id */
15378 for (idx = 0; idx < numrq; idx++) {
15379 hrq = hrqp[idx];
15380 hrq->queue_id = rc + (2 * idx);
15381 drq = drqp[idx];
15382 drq->queue_id = rc + (2 * idx) + 1;
15383 }
15384
15385out:
15386 lpfc_sli4_mbox_cmd_free(phba, mbox);
15387 return status;
15388}
15389
15390/**
James Smart4f774512009-05-22 14:52:35 -040015391 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15392 * @eq: The queue structure associated with the queue to destroy.
15393 *
15394 * This function destroys a queue, as detailed in @eq by sending an mailbox
15395 * command, specific to the type of queue, to the HBA.
15396 *
15397 * The @eq struct is used to get the queue ID of the queue to destroy.
15398 *
15399 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015400 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015401 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015402int
James Smart4f774512009-05-22 14:52:35 -040015403lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15404{
15405 LPFC_MBOXQ_t *mbox;
15406 int rc, length, status = 0;
15407 uint32_t shdr_status, shdr_add_status;
15408 union lpfc_sli4_cfg_shdr *shdr;
15409
James Smart2e90f4b2011-12-13 13:22:37 -050015410 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015411 if (!eq)
15412 return -ENODEV;
15413 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15414 if (!mbox)
15415 return -ENOMEM;
15416 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15417 sizeof(struct lpfc_sli4_cfg_mhdr));
15418 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15419 LPFC_MBOX_OPCODE_EQ_DESTROY,
15420 length, LPFC_SLI4_MBX_EMBED);
15421 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15422 eq->queue_id);
15423 mbox->vport = eq->phba->pport;
15424 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15425
15426 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15427 /* The IOCTL status is embedded in the mailbox subheader. */
15428 shdr = (union lpfc_sli4_cfg_shdr *)
15429 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15430 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15431 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15432 if (shdr_status || shdr_add_status || rc) {
15433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15434 "2505 EQ_DESTROY mailbox failed with "
15435 "status x%x add_status x%x, mbx status x%x\n",
15436 shdr_status, shdr_add_status, rc);
15437 status = -ENXIO;
15438 }
15439
15440 /* Remove eq from any list */
15441 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015442 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015443 return status;
15444}
15445
15446/**
15447 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15448 * @cq: The queue structure associated with the queue to destroy.
15449 *
15450 * This function destroys a queue, as detailed in @cq by sending an mailbox
15451 * command, specific to the type of queue, to the HBA.
15452 *
15453 * The @cq struct is used to get the queue ID of the queue to destroy.
15454 *
15455 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015456 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015457 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015458int
James Smart4f774512009-05-22 14:52:35 -040015459lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15460{
15461 LPFC_MBOXQ_t *mbox;
15462 int rc, length, status = 0;
15463 uint32_t shdr_status, shdr_add_status;
15464 union lpfc_sli4_cfg_shdr *shdr;
15465
James Smart2e90f4b2011-12-13 13:22:37 -050015466 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015467 if (!cq)
15468 return -ENODEV;
15469 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15470 if (!mbox)
15471 return -ENOMEM;
15472 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15473 sizeof(struct lpfc_sli4_cfg_mhdr));
15474 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15475 LPFC_MBOX_OPCODE_CQ_DESTROY,
15476 length, LPFC_SLI4_MBX_EMBED);
15477 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15478 cq->queue_id);
15479 mbox->vport = cq->phba->pport;
15480 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15481 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15482 /* The IOCTL status is embedded in the mailbox subheader. */
15483 shdr = (union lpfc_sli4_cfg_shdr *)
15484 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15485 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15486 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15487 if (shdr_status || shdr_add_status || rc) {
15488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15489 "2506 CQ_DESTROY mailbox failed with "
15490 "status x%x add_status x%x, mbx status x%x\n",
15491 shdr_status, shdr_add_status, rc);
15492 status = -ENXIO;
15493 }
15494 /* Remove cq from any list */
15495 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015496 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015497 return status;
15498}
15499
15500/**
James Smart04c68492009-05-22 14:52:52 -040015501 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15502 * @qm: The queue structure associated with the queue to destroy.
15503 *
15504 * This function destroys a queue, as detailed in @mq by sending an mailbox
15505 * command, specific to the type of queue, to the HBA.
15506 *
15507 * The @mq struct is used to get the queue ID of the queue to destroy.
15508 *
15509 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015510 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015511 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015512int
James Smart04c68492009-05-22 14:52:52 -040015513lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15514{
15515 LPFC_MBOXQ_t *mbox;
15516 int rc, length, status = 0;
15517 uint32_t shdr_status, shdr_add_status;
15518 union lpfc_sli4_cfg_shdr *shdr;
15519
James Smart2e90f4b2011-12-13 13:22:37 -050015520 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015521 if (!mq)
15522 return -ENODEV;
15523 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15524 if (!mbox)
15525 return -ENOMEM;
15526 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15527 sizeof(struct lpfc_sli4_cfg_mhdr));
15528 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15529 LPFC_MBOX_OPCODE_MQ_DESTROY,
15530 length, LPFC_SLI4_MBX_EMBED);
15531 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15532 mq->queue_id);
15533 mbox->vport = mq->phba->pport;
15534 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15535 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15536 /* The IOCTL status is embedded in the mailbox subheader. */
15537 shdr = (union lpfc_sli4_cfg_shdr *)
15538 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15539 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15540 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15541 if (shdr_status || shdr_add_status || rc) {
15542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15543 "2507 MQ_DESTROY mailbox failed with "
15544 "status x%x add_status x%x, mbx status x%x\n",
15545 shdr_status, shdr_add_status, rc);
15546 status = -ENXIO;
15547 }
15548 /* Remove mq from any list */
15549 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015550 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015551 return status;
15552}
15553
15554/**
James Smart4f774512009-05-22 14:52:35 -040015555 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15556 * @wq: The queue structure associated with the queue to destroy.
15557 *
15558 * This function destroys a queue, as detailed in @wq by sending an mailbox
15559 * command, specific to the type of queue, to the HBA.
15560 *
15561 * The @wq struct is used to get the queue ID of the queue to destroy.
15562 *
15563 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015564 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015565 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015566int
James Smart4f774512009-05-22 14:52:35 -040015567lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15568{
15569 LPFC_MBOXQ_t *mbox;
15570 int rc, length, status = 0;
15571 uint32_t shdr_status, shdr_add_status;
15572 union lpfc_sli4_cfg_shdr *shdr;
15573
James Smart2e90f4b2011-12-13 13:22:37 -050015574 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015575 if (!wq)
15576 return -ENODEV;
15577 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15578 if (!mbox)
15579 return -ENOMEM;
15580 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15581 sizeof(struct lpfc_sli4_cfg_mhdr));
15582 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15583 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15584 length, LPFC_SLI4_MBX_EMBED);
15585 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15586 wq->queue_id);
15587 mbox->vport = wq->phba->pport;
15588 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15589 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15590 shdr = (union lpfc_sli4_cfg_shdr *)
15591 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15592 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15593 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15594 if (shdr_status || shdr_add_status || rc) {
15595 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15596 "2508 WQ_DESTROY mailbox failed with "
15597 "status x%x add_status x%x, mbx status x%x\n",
15598 shdr_status, shdr_add_status, rc);
15599 status = -ENXIO;
15600 }
15601 /* Remove wq from any list */
15602 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070015603 kfree(wq->pring);
15604 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040015605 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015606 return status;
15607}
15608
15609/**
15610 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15611 * @rq: The queue structure associated with the queue to destroy.
15612 *
15613 * This function destroys a queue, as detailed in @rq by sending an mailbox
15614 * command, specific to the type of queue, to the HBA.
15615 *
15616 * The @rq struct is used to get the queue ID of the queue to destroy.
15617 *
15618 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015619 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015620 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015621int
James Smart4f774512009-05-22 14:52:35 -040015622lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15623 struct lpfc_queue *drq)
15624{
15625 LPFC_MBOXQ_t *mbox;
15626 int rc, length, status = 0;
15627 uint32_t shdr_status, shdr_add_status;
15628 union lpfc_sli4_cfg_shdr *shdr;
15629
James Smart2e90f4b2011-12-13 13:22:37 -050015630 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015631 if (!hrq || !drq)
15632 return -ENODEV;
15633 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15634 if (!mbox)
15635 return -ENOMEM;
15636 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050015637 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040015638 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15639 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15640 length, LPFC_SLI4_MBX_EMBED);
15641 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15642 hrq->queue_id);
15643 mbox->vport = hrq->phba->pport;
15644 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15645 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15646 /* The IOCTL status is embedded in the mailbox subheader. */
15647 shdr = (union lpfc_sli4_cfg_shdr *)
15648 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15649 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15650 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15651 if (shdr_status || shdr_add_status || rc) {
15652 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15653 "2509 RQ_DESTROY mailbox failed with "
15654 "status x%x add_status x%x, mbx status x%x\n",
15655 shdr_status, shdr_add_status, rc);
15656 if (rc != MBX_TIMEOUT)
15657 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15658 return -ENXIO;
15659 }
15660 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15661 drq->queue_id);
15662 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15663 shdr = (union lpfc_sli4_cfg_shdr *)
15664 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15665 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15666 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15667 if (shdr_status || shdr_add_status || rc) {
15668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15669 "2510 RQ_DESTROY mailbox failed with "
15670 "status x%x add_status x%x, mbx status x%x\n",
15671 shdr_status, shdr_add_status, rc);
15672 status = -ENXIO;
15673 }
15674 list_del_init(&hrq->list);
15675 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040015676 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015677 return status;
15678}
15679
15680/**
15681 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15682 * @phba: The virtual port for which this call being executed.
15683 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15684 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15685 * @xritag: the xritag that ties this io to the SGL pages.
15686 *
15687 * This routine will post the sgl pages for the IO that has the xritag
15688 * that is in the iocbq structure. The xritag is assigned during iocbq
15689 * creation and persists for as long as the driver is loaded.
15690 * if the caller has fewer than 256 scatter gather segments to map then
15691 * pdma_phys_addr1 should be 0.
15692 * If the caller needs to map more than 256 scatter gather segment then
15693 * pdma_phys_addr1 should be a valid physical address.
15694 * physical address for SGLs must be 64 byte aligned.
15695 * If you are going to map 2 SGL's then the first one must have 256 entries
15696 * the second sgl can have between 1 and 256 entries.
15697 *
15698 * Return codes:
15699 * 0 - Success
15700 * -ENXIO, -ENOMEM - Failure
15701 **/
15702int
15703lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15704 dma_addr_t pdma_phys_addr0,
15705 dma_addr_t pdma_phys_addr1,
15706 uint16_t xritag)
15707{
15708 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15709 LPFC_MBOXQ_t *mbox;
15710 int rc;
15711 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040015712 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040015713 union lpfc_sli4_cfg_shdr *shdr;
15714
15715 if (xritag == NO_XRI) {
15716 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15717 "0364 Invalid param:\n");
15718 return -EINVAL;
15719 }
15720
15721 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15722 if (!mbox)
15723 return -ENOMEM;
15724
15725 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15726 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15727 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050015728 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040015729
15730 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15731 &mbox->u.mqe.un.post_sgl_pages;
15732 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15733 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15734
15735 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15736 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15737 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15738 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15739
15740 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15741 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15742 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15743 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15744 if (!phba->sli4_hba.intr_enable)
15745 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040015746 else {
James Smarta183a152011-10-10 21:32:43 -040015747 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040015748 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15749 }
James Smart4f774512009-05-22 14:52:35 -040015750 /* The IOCTL status is embedded in the mailbox subheader. */
15751 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15752 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15753 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15754 if (rc != MBX_TIMEOUT)
15755 mempool_free(mbox, phba->mbox_mem_pool);
15756 if (shdr_status || shdr_add_status || rc) {
15757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15758 "2511 POST_SGL mailbox failed with "
15759 "status x%x add_status x%x, mbx status x%x\n",
15760 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040015761 }
15762 return 0;
15763}
James Smart4f774512009-05-22 14:52:35 -040015764
15765/**
James Smart88a2cfb2011-07-22 18:36:33 -040015766 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040015767 * @phba: pointer to lpfc hba data structure.
15768 *
15769 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040015770 * HBA consistent with the SLI-4 interface spec. This routine
15771 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15772 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040015773 *
James Smart88a2cfb2011-07-22 18:36:33 -040015774 * Returns
15775 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15776 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15777 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015778static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015779lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15780{
15781 unsigned long xri;
15782
15783 /*
15784 * Fetch the next logical xri. Because this index is logical,
15785 * the driver starts at 0 each time.
15786 */
15787 spin_lock_irq(&phba->hbalock);
15788 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15789 phba->sli4_hba.max_cfg_param.max_xri, 0);
15790 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15791 spin_unlock_irq(&phba->hbalock);
15792 return NO_XRI;
15793 } else {
15794 set_bit(xri, phba->sli4_hba.xri_bmask);
15795 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015796 }
James Smart6d368e52011-05-24 11:44:12 -040015797 spin_unlock_irq(&phba->hbalock);
15798 return xri;
15799}
15800
15801/**
15802 * lpfc_sli4_free_xri - Release an xri for reuse.
15803 * @phba: pointer to lpfc hba data structure.
15804 *
15805 * This routine is invoked to release an xri to the pool of
15806 * available rpis maintained by the driver.
15807 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015808static void
James Smart6d368e52011-05-24 11:44:12 -040015809__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15810{
15811 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040015812 phba->sli4_hba.max_cfg_param.xri_used--;
15813 }
15814}
15815
15816/**
15817 * lpfc_sli4_free_xri - Release an xri for reuse.
15818 * @phba: pointer to lpfc hba data structure.
15819 *
15820 * This routine is invoked to release an xri to the pool of
15821 * available rpis maintained by the driver.
15822 **/
15823void
15824lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15825{
15826 spin_lock_irq(&phba->hbalock);
15827 __lpfc_sli4_free_xri(phba, xri);
15828 spin_unlock_irq(&phba->hbalock);
15829}
15830
15831/**
James Smart4f774512009-05-22 14:52:35 -040015832 * lpfc_sli4_next_xritag - Get an xritag for the io
15833 * @phba: Pointer to HBA context object.
15834 *
15835 * This function gets an xritag for the iocb. If there is no unused xritag
15836 * it will return 0xffff.
15837 * The function returns the allocated xritag if successful, else returns zero.
15838 * Zero is not a valid xritag.
15839 * The caller is not required to hold any lock.
15840 **/
15841uint16_t
15842lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15843{
James Smart6d368e52011-05-24 11:44:12 -040015844 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040015845
James Smart6d368e52011-05-24 11:44:12 -040015846 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040015847 if (xri_index == NO_XRI)
15848 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15849 "2004 Failed to allocate XRI.last XRITAG is %d"
15850 " Max XRI is %d, Used XRI is %d\n",
15851 xri_index,
15852 phba->sli4_hba.max_cfg_param.max_xri,
15853 phba->sli4_hba.max_cfg_param.xri_used);
15854 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040015855}
15856
15857/**
James Smart895427b2017-02-12 13:52:30 -080015858 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040015859 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040015860 * @post_sgl_list: pointer to els sgl entry list.
15861 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040015862 *
15863 * This routine is invoked to post a block of driver's sgl pages to the
15864 * HBA using non-embedded mailbox command. No Lock is held. This routine
15865 * is only called when the driver is loading and after all IO has been
15866 * stopped.
15867 **/
James Smart8a9d2e82012-05-09 21:16:12 -040015868static int
James Smart895427b2017-02-12 13:52:30 -080015869lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040015870 struct list_head *post_sgl_list,
15871 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040015872{
James Smart8a9d2e82012-05-09 21:16:12 -040015873 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040015874 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15875 struct sgl_page_pairs *sgl_pg_pairs;
15876 void *viraddr;
15877 LPFC_MBOXQ_t *mbox;
15878 uint32_t reqlen, alloclen, pg_pairs;
15879 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040015880 uint16_t xritag_start = 0;
15881 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040015882 uint32_t shdr_status, shdr_add_status;
15883 union lpfc_sli4_cfg_shdr *shdr;
15884
James Smart895427b2017-02-12 13:52:30 -080015885 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015886 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015887 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080015888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040015889 "2559 Block sgl registration required DMA "
15890 "size (%d) great than a page\n", reqlen);
15891 return -ENOMEM;
15892 }
James Smart895427b2017-02-12 13:52:30 -080015893
James Smart4f774512009-05-22 14:52:35 -040015894 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040015895 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040015896 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040015897
15898 /* Allocate DMA memory and set up the non-embedded mailbox command */
15899 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15900 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15901 LPFC_SLI4_MBX_NEMBED);
15902
15903 if (alloclen < reqlen) {
15904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15905 "0285 Allocated DMA memory size (%d) is "
15906 "less than the requested DMA memory "
15907 "size (%d)\n", alloclen, reqlen);
15908 lpfc_sli4_mbox_cmd_free(phba, mbox);
15909 return -ENOMEM;
15910 }
James Smart4f774512009-05-22 14:52:35 -040015911 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040015912 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040015913 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15914 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15915
James Smart8a9d2e82012-05-09 21:16:12 -040015916 pg_pairs = 0;
15917 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040015918 /* Set up the sge entry */
15919 sgl_pg_pairs->sgl_pg0_addr_lo =
15920 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15921 sgl_pg_pairs->sgl_pg0_addr_hi =
15922 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15923 sgl_pg_pairs->sgl_pg1_addr_lo =
15924 cpu_to_le32(putPaddrLow(0));
15925 sgl_pg_pairs->sgl_pg1_addr_hi =
15926 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040015927
James Smart4f774512009-05-22 14:52:35 -040015928 /* Keep the first xritag on the list */
15929 if (pg_pairs == 0)
15930 xritag_start = sglq_entry->sli4_xritag;
15931 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040015932 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040015933 }
James Smart6d368e52011-05-24 11:44:12 -040015934
15935 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040015936 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080015937 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040015938 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080015939
James Smart4f774512009-05-22 14:52:35 -040015940 if (!phba->sli4_hba.intr_enable)
15941 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15942 else {
James Smarta183a152011-10-10 21:32:43 -040015943 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040015944 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15945 }
15946 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15947 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15948 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15949 if (rc != MBX_TIMEOUT)
15950 lpfc_sli4_mbox_cmd_free(phba, mbox);
15951 if (shdr_status || shdr_add_status || rc) {
15952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15953 "2513 POST_SGL_BLOCK mailbox command failed "
15954 "status x%x add_status x%x mbx status x%x\n",
15955 shdr_status, shdr_add_status, rc);
15956 rc = -ENXIO;
15957 }
15958 return rc;
15959}
15960
15961/**
15962 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
15963 * @phba: pointer to lpfc hba data structure.
15964 * @sblist: pointer to scsi buffer list.
15965 * @count: number of scsi buffers on the list.
15966 *
15967 * This routine is invoked to post a block of @count scsi sgl pages from a
15968 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
15969 * No Lock is held.
15970 *
15971 **/
15972int
James Smart8a9d2e82012-05-09 21:16:12 -040015973lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
15974 struct list_head *sblist,
15975 int count)
James Smart4f774512009-05-22 14:52:35 -040015976{
15977 struct lpfc_scsi_buf *psb;
15978 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15979 struct sgl_page_pairs *sgl_pg_pairs;
15980 void *viraddr;
15981 LPFC_MBOXQ_t *mbox;
15982 uint32_t reqlen, alloclen, pg_pairs;
15983 uint32_t mbox_tmo;
15984 uint16_t xritag_start = 0;
15985 int rc = 0;
15986 uint32_t shdr_status, shdr_add_status;
15987 dma_addr_t pdma_phys_bpl1;
15988 union lpfc_sli4_cfg_shdr *shdr;
15989
15990 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040015991 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015992 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015993 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040015994 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
15995 "0217 Block sgl registration required DMA "
15996 "size (%d) great than a page\n", reqlen);
15997 return -ENOMEM;
15998 }
15999 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16000 if (!mbox) {
16001 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16002 "0283 Failed to allocate mbox cmd memory\n");
16003 return -ENOMEM;
16004 }
16005
16006 /* Allocate DMA memory and set up the non-embedded mailbox command */
16007 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16008 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16009 LPFC_SLI4_MBX_NEMBED);
16010
16011 if (alloclen < reqlen) {
16012 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16013 "2561 Allocated DMA memory size (%d) is "
16014 "less than the requested DMA memory "
16015 "size (%d)\n", alloclen, reqlen);
16016 lpfc_sli4_mbox_cmd_free(phba, mbox);
16017 return -ENOMEM;
16018 }
James Smart6d368e52011-05-24 11:44:12 -040016019
James Smart4f774512009-05-22 14:52:35 -040016020 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016021 viraddr = mbox->sge_array->addr[0];
16022
16023 /* Set up the SGL pages in the non-embedded DMA pages */
16024 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16025 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16026
16027 pg_pairs = 0;
16028 list_for_each_entry(psb, sblist, list) {
16029 /* Set up the sge entry */
16030 sgl_pg_pairs->sgl_pg0_addr_lo =
16031 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16032 sgl_pg_pairs->sgl_pg0_addr_hi =
16033 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16034 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16035 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16036 else
16037 pdma_phys_bpl1 = 0;
16038 sgl_pg_pairs->sgl_pg1_addr_lo =
16039 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16040 sgl_pg_pairs->sgl_pg1_addr_hi =
16041 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16042 /* Keep the first xritag on the list */
16043 if (pg_pairs == 0)
16044 xritag_start = psb->cur_iocbq.sli4_xritag;
16045 sgl_pg_pairs++;
16046 pg_pairs++;
16047 }
16048 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16049 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16050 /* Perform endian conversion if necessary */
16051 sgl->word0 = cpu_to_le32(sgl->word0);
16052
16053 if (!phba->sli4_hba.intr_enable)
16054 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16055 else {
James Smarta183a152011-10-10 21:32:43 -040016056 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016057 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16058 }
16059 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16060 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16061 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16062 if (rc != MBX_TIMEOUT)
16063 lpfc_sli4_mbox_cmd_free(phba, mbox);
16064 if (shdr_status || shdr_add_status || rc) {
16065 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16066 "2564 POST_SGL_BLOCK mailbox command failed "
16067 "status x%x add_status x%x mbx status x%x\n",
16068 shdr_status, shdr_add_status, rc);
16069 rc = -ENXIO;
16070 }
16071 return rc;
16072}
16073
James Smart2ea259e2017-02-12 13:52:27 -080016074static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
16075static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
16076
James Smart4f774512009-05-22 14:52:35 -040016077/**
16078 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16079 * @phba: pointer to lpfc_hba struct that the frame was received on
16080 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16081 *
16082 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16083 * valid type of frame that the LPFC driver will handle. This function will
16084 * return a zero if the frame is a valid frame or a non zero value when the
16085 * frame does not pass the check.
16086 **/
16087static int
16088lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16089{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016090 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016091 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016092 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016093
16094 switch (fc_hdr->fh_r_ctl) {
16095 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16096 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16097 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16098 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16099 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16100 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16101 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16102 case FC_RCTL_DD_CMD_STATUS: /* command status */
16103 case FC_RCTL_ELS_REQ: /* extended link services request */
16104 case FC_RCTL_ELS_REP: /* extended link services reply */
16105 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16106 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16107 case FC_RCTL_BA_NOP: /* basic link service NOP */
16108 case FC_RCTL_BA_ABTS: /* basic link service abort */
16109 case FC_RCTL_BA_RMC: /* remove connection */
16110 case FC_RCTL_BA_ACC: /* basic accept */
16111 case FC_RCTL_BA_RJT: /* basic reject */
16112 case FC_RCTL_BA_PRMT:
16113 case FC_RCTL_ACK_1: /* acknowledge_1 */
16114 case FC_RCTL_ACK_0: /* acknowledge_0 */
16115 case FC_RCTL_P_RJT: /* port reject */
16116 case FC_RCTL_F_RJT: /* fabric reject */
16117 case FC_RCTL_P_BSY: /* port busy */
16118 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16119 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16120 case FC_RCTL_LCR: /* link credit reset */
16121 case FC_RCTL_END: /* end */
16122 break;
16123 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16124 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16125 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16126 return lpfc_fc_frame_check(phba, fc_hdr);
16127 default:
16128 goto drop;
16129 }
16130 switch (fc_hdr->fh_type) {
16131 case FC_TYPE_BLS:
16132 case FC_TYPE_ELS:
16133 case FC_TYPE_FCP:
16134 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016135 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040016136 break;
16137 case FC_TYPE_IP:
16138 case FC_TYPE_ILS:
16139 default:
16140 goto drop;
16141 }
James Smart546fc852011-03-11 16:06:29 -050016142
James Smart4f774512009-05-22 14:52:35 -040016143 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040016144 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
16145 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart2ea259e2017-02-12 13:52:27 -080016146 lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
16147 lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050016148 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16149 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040016150 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16151 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016152 return 0;
16153drop:
16154 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16155 "2539 Dropped frame rctl:%s type:%s\n",
James Smart2ea259e2017-02-12 13:52:27 -080016156 lpfc_rctl_names[fc_hdr->fh_r_ctl],
16157 lpfc_type_names[fc_hdr->fh_type]);
James Smart4f774512009-05-22 14:52:35 -040016158 return 1;
16159}
16160
16161/**
16162 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16163 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16164 *
16165 * This function processes the FC header to retrieve the VFI from the VF
16166 * header, if one exists. This function will return the VFI if one exists
16167 * or 0 if no VSAN Header exists.
16168 **/
16169static uint32_t
16170lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16171{
16172 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16173
16174 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16175 return 0;
16176 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16177}
16178
16179/**
16180 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16181 * @phba: Pointer to the HBA structure to search for the vport on
16182 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16183 * @fcfi: The FC Fabric ID that the frame came from
16184 *
16185 * This function searches the @phba for a vport that matches the content of the
16186 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16187 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16188 * returns the matching vport pointer or NULL if unable to match frame to a
16189 * vport.
16190 **/
16191static struct lpfc_vport *
16192lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016193 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016194{
16195 struct lpfc_vport **vports;
16196 struct lpfc_vport *vport = NULL;
16197 int i;
James Smart939723a2012-05-09 21:19:03 -040016198
James Smartbf086112011-08-21 21:48:13 -040016199 if (did == Fabric_DID)
16200 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016201 if ((phba->pport->fc_flag & FC_PT2PT) &&
16202 !(phba->link_state == LPFC_HBA_READY))
16203 return phba->pport;
16204
James Smart4f774512009-05-22 14:52:35 -040016205 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016206 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016207 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16208 if (phba->fcf.fcfi == fcfi &&
16209 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16210 vports[i]->fc_myDID == did) {
16211 vport = vports[i];
16212 break;
16213 }
16214 }
James Smart895427b2017-02-12 13:52:30 -080016215 }
James Smart4f774512009-05-22 14:52:35 -040016216 lpfc_destroy_vport_work_array(phba, vports);
16217 return vport;
16218}
16219
16220/**
James Smart45ed1192009-10-02 15:17:02 -040016221 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16222 * @vport: The vport to work on.
16223 *
16224 * This function updates the receive sequence time stamp for this vport. The
16225 * receive sequence time stamp indicates the time that the last frame of the
16226 * the sequence that has been idle for the longest amount of time was received.
16227 * the driver uses this time stamp to indicate if any received sequences have
16228 * timed out.
16229 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016230static void
James Smart45ed1192009-10-02 15:17:02 -040016231lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16232{
16233 struct lpfc_dmabuf *h_buf;
16234 struct hbq_dmabuf *dmabuf = NULL;
16235
16236 /* get the oldest sequence on the rcv list */
16237 h_buf = list_get_first(&vport->rcv_buffer_list,
16238 struct lpfc_dmabuf, list);
16239 if (!h_buf)
16240 return;
16241 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16242 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16243}
16244
16245/**
16246 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16247 * @vport: The vport that the received sequences were sent to.
16248 *
16249 * This function cleans up all outstanding received sequences. This is called
16250 * by the driver when a link event or user action invalidates all the received
16251 * sequences.
16252 **/
16253void
16254lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16255{
16256 struct lpfc_dmabuf *h_buf, *hnext;
16257 struct lpfc_dmabuf *d_buf, *dnext;
16258 struct hbq_dmabuf *dmabuf = NULL;
16259
16260 /* start with the oldest sequence on the rcv list */
16261 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16262 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16263 list_del_init(&dmabuf->hbuf.list);
16264 list_for_each_entry_safe(d_buf, dnext,
16265 &dmabuf->dbuf.list, list) {
16266 list_del_init(&d_buf->list);
16267 lpfc_in_buf_free(vport->phba, d_buf);
16268 }
16269 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16270 }
16271}
16272
16273/**
16274 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16275 * @vport: The vport that the received sequences were sent to.
16276 *
16277 * This function determines whether any received sequences have timed out by
16278 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16279 * indicates that there is at least one timed out sequence this routine will
16280 * go through the received sequences one at a time from most inactive to most
16281 * active to determine which ones need to be cleaned up. Once it has determined
16282 * that a sequence needs to be cleaned up it will simply free up the resources
16283 * without sending an abort.
16284 **/
16285void
16286lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16287{
16288 struct lpfc_dmabuf *h_buf, *hnext;
16289 struct lpfc_dmabuf *d_buf, *dnext;
16290 struct hbq_dmabuf *dmabuf = NULL;
16291 unsigned long timeout;
16292 int abort_count = 0;
16293
16294 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16295 vport->rcv_buffer_time_stamp);
16296 if (list_empty(&vport->rcv_buffer_list) ||
16297 time_before(jiffies, timeout))
16298 return;
16299 /* start with the oldest sequence on the rcv list */
16300 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16301 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16302 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16303 dmabuf->time_stamp);
16304 if (time_before(jiffies, timeout))
16305 break;
16306 abort_count++;
16307 list_del_init(&dmabuf->hbuf.list);
16308 list_for_each_entry_safe(d_buf, dnext,
16309 &dmabuf->dbuf.list, list) {
16310 list_del_init(&d_buf->list);
16311 lpfc_in_buf_free(vport->phba, d_buf);
16312 }
16313 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16314 }
16315 if (abort_count)
16316 lpfc_update_rcv_time_stamp(vport);
16317}
16318
16319/**
James Smart4f774512009-05-22 14:52:35 -040016320 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16321 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16322 *
16323 * This function searches through the existing incomplete sequences that have
16324 * been sent to this @vport. If the frame matches one of the incomplete
16325 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16326 * make up that sequence. If no sequence is found that matches this frame then
16327 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16328 * This function returns a pointer to the first dmabuf in the sequence list that
16329 * the frame was linked to.
16330 **/
16331static struct hbq_dmabuf *
16332lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16333{
16334 struct fc_frame_header *new_hdr;
16335 struct fc_frame_header *temp_hdr;
16336 struct lpfc_dmabuf *d_buf;
16337 struct lpfc_dmabuf *h_buf;
16338 struct hbq_dmabuf *seq_dmabuf = NULL;
16339 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016340 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016341
James Smart4d9ab992009-10-02 15:16:39 -040016342 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016343 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016344 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016345
James Smart4f774512009-05-22 14:52:35 -040016346 /* Use the hdr_buf to find the sequence that this frame belongs to */
16347 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16348 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16349 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16350 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16351 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16352 continue;
16353 /* found a pending sequence that matches this frame */
16354 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16355 break;
16356 }
16357 if (!seq_dmabuf) {
16358 /*
16359 * This indicates first frame received for this sequence.
16360 * Queue the buffer on the vport's rcv_buffer_list.
16361 */
16362 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016363 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016364 return dmabuf;
16365 }
16366 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016367 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16368 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016369 list_del_init(&seq_dmabuf->hbuf.list);
16370 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16371 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016372 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016373 return dmabuf;
16374 }
James Smart45ed1192009-10-02 15:17:02 -040016375 /* move this sequence to the tail to indicate a young sequence */
16376 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16377 seq_dmabuf->time_stamp = jiffies;
16378 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016379 if (list_empty(&seq_dmabuf->dbuf.list)) {
16380 temp_hdr = dmabuf->hbuf.virt;
16381 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16382 return seq_dmabuf;
16383 }
James Smart4f774512009-05-22 14:52:35 -040016384 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016385 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16386 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016387 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16388 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16389 /*
16390 * If the frame's sequence count is greater than the frame on
16391 * the list then insert the frame right after this frame
16392 */
James Smarteeead812009-12-21 17:01:23 -050016393 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16394 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016395 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016396 found = 1;
16397 break;
James Smart4f774512009-05-22 14:52:35 -040016398 }
James Smart4360ca92015-12-16 18:12:04 -050016399
16400 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16401 break;
16402 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016403 }
James Smart4360ca92015-12-16 18:12:04 -050016404
16405 if (found)
16406 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016407 return NULL;
16408}
16409
16410/**
James Smart6669f9b2009-10-02 15:16:45 -040016411 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16412 * @vport: pointer to a vitural port
16413 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16414 *
16415 * This function tries to abort from the partially assembed sequence, described
16416 * by the information from basic abbort @dmabuf. It checks to see whether such
16417 * partially assembled sequence held by the driver. If so, it shall free up all
16418 * the frames from the partially assembled sequence.
16419 *
16420 * Return
16421 * true -- if there is matching partially assembled sequence present and all
16422 * the frames freed with the sequence;
16423 * false -- if there is no matching partially assembled sequence present so
16424 * nothing got aborted in the lower layer driver
16425 **/
16426static bool
16427lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16428 struct hbq_dmabuf *dmabuf)
16429{
16430 struct fc_frame_header *new_hdr;
16431 struct fc_frame_header *temp_hdr;
16432 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16433 struct hbq_dmabuf *seq_dmabuf = NULL;
16434
16435 /* Use the hdr_buf to find the sequence that matches this frame */
16436 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16437 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16438 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16439 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16440 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16441 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16442 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16443 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16444 continue;
16445 /* found a pending sequence that matches this frame */
16446 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16447 break;
16448 }
16449
16450 /* Free up all the frames from the partially assembled sequence */
16451 if (seq_dmabuf) {
16452 list_for_each_entry_safe(d_buf, n_buf,
16453 &seq_dmabuf->dbuf.list, list) {
16454 list_del_init(&d_buf->list);
16455 lpfc_in_buf_free(vport->phba, d_buf);
16456 }
16457 return true;
16458 }
16459 return false;
16460}
16461
16462/**
James Smart6dd9e312013-01-03 15:43:37 -050016463 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16464 * @vport: pointer to a vitural port
16465 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16466 *
16467 * This function tries to abort from the assembed sequence from upper level
16468 * protocol, described by the information from basic abbort @dmabuf. It
16469 * checks to see whether such pending context exists at upper level protocol.
16470 * If so, it shall clean up the pending context.
16471 *
16472 * Return
16473 * true -- if there is matching pending context of the sequence cleaned
16474 * at ulp;
16475 * false -- if there is no matching pending context of the sequence present
16476 * at ulp.
16477 **/
16478static bool
16479lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16480{
16481 struct lpfc_hba *phba = vport->phba;
16482 int handled;
16483
16484 /* Accepting abort at ulp with SLI4 only */
16485 if (phba->sli_rev < LPFC_SLI_REV4)
16486 return false;
16487
16488 /* Register all caring upper level protocols to attend abort */
16489 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16490 if (handled)
16491 return true;
16492
16493 return false;
16494}
16495
16496/**
James Smart546fc852011-03-11 16:06:29 -050016497 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016498 * @phba: Pointer to HBA context object.
16499 * @cmd_iocbq: pointer to the command iocbq structure.
16500 * @rsp_iocbq: pointer to the response iocbq structure.
16501 *
James Smart546fc852011-03-11 16:06:29 -050016502 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016503 * event. It properly releases the memory allocated to the sequence abort
16504 * accept iocb.
16505 **/
16506static void
James Smart546fc852011-03-11 16:06:29 -050016507lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016508 struct lpfc_iocbq *cmd_iocbq,
16509 struct lpfc_iocbq *rsp_iocbq)
16510{
James Smart6dd9e312013-01-03 15:43:37 -050016511 struct lpfc_nodelist *ndlp;
16512
16513 if (cmd_iocbq) {
16514 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16515 lpfc_nlp_put(ndlp);
16516 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016517 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016518 }
James Smart6b5151f2012-01-18 16:24:06 -050016519
16520 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16521 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16522 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16523 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16524 rsp_iocbq->iocb.ulpStatus,
16525 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016526}
16527
16528/**
James Smart6d368e52011-05-24 11:44:12 -040016529 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16530 * @phba: Pointer to HBA context object.
16531 * @xri: xri id in transaction.
16532 *
16533 * This function validates the xri maps to the known range of XRIs allocated an
16534 * used by the driver.
16535 **/
James Smart7851fe22011-07-22 18:36:52 -040016536uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016537lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16538 uint16_t xri)
16539{
James Smarta2fc4aef2014-09-03 12:57:55 -040016540 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016541
16542 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16543 if (xri == phba->sli4_hba.xri_ids[i])
16544 return i;
16545 }
16546 return NO_XRI;
16547}
16548
James Smart6d368e52011-05-24 11:44:12 -040016549/**
James Smart546fc852011-03-11 16:06:29 -050016550 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016551 * @phba: Pointer to HBA context object.
16552 * @fc_hdr: pointer to a FC frame header.
16553 *
James Smart546fc852011-03-11 16:06:29 -050016554 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016555 * event after aborting the sequence handling.
16556 **/
James Smart86c67372017-04-21 16:05:04 -070016557void
James Smart6dd9e312013-01-03 15:43:37 -050016558lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16559 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016560{
James Smart6dd9e312013-01-03 15:43:37 -050016561 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040016562 struct lpfc_iocbq *ctiocb = NULL;
16563 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040016564 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050016565 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040016566 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050016567 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040016568
16569 if (!lpfc_is_link_up(phba))
16570 return;
16571
16572 sid = sli4_sid_from_fc_hdr(fc_hdr);
16573 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050016574 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040016575
James Smart6dd9e312013-01-03 15:43:37 -050016576 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040016577 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070016578 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050016579 if (!ndlp) {
16580 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16581 "1268 Failed to allocate ndlp for "
16582 "oxid:x%x SID:x%x\n", oxid, sid);
16583 return;
16584 }
James Smart6dd9e312013-01-03 15:43:37 -050016585 /* Put ndlp onto pport node list */
16586 lpfc_enqueue_node(vport, ndlp);
16587 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16588 /* re-setup ndlp without removing from node list */
16589 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16590 if (!ndlp) {
16591 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16592 "3275 Failed to active ndlp found "
16593 "for oxid:x%x SID:x%x\n", oxid, sid);
16594 return;
16595 }
James Smart6669f9b2009-10-02 15:16:45 -040016596 }
16597
James Smart546fc852011-03-11 16:06:29 -050016598 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040016599 ctiocb = lpfc_sli_get_iocbq(phba);
16600 if (!ctiocb)
16601 return;
16602
James Smart5ffc2662009-11-18 15:39:44 -050016603 /* Extract the F_CTL field from FC_HDR */
16604 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16605
James Smart6669f9b2009-10-02 15:16:45 -040016606 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040016607 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050016608 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040016609 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16610 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16611 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16612
16613 /* Fill in the rest of iocb fields */
16614 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16615 icmd->ulpBdeCount = 0;
16616 icmd->ulpLe = 1;
16617 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040016618 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050016619 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016620
James Smart6669f9b2009-10-02 15:16:45 -040016621 ctiocb->iocb_cmpl = NULL;
16622 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050016623 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040016624 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050016625 ctiocb->sli4_xritag = NO_XRI;
16626
James Smartee0f4fe2012-05-09 21:19:14 -040016627 if (fctl & FC_FC_EX_CTX)
16628 /* Exchange responder sent the abort so we
16629 * own the oxid.
16630 */
16631 xri = oxid;
16632 else
16633 xri = rxid;
16634 lxri = lpfc_sli4_xri_inrange(phba, xri);
16635 if (lxri != NO_XRI)
16636 lpfc_set_rrq_active(phba, ndlp, lxri,
16637 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050016638 /* For BA_ABTS from exchange responder, if the logical xri with
16639 * the oxid maps to the FCP XRI range, the port no longer has
16640 * that exchange context, send a BLS_RJT. Override the IOCB for
16641 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050016642 */
James Smart6dd9e312013-01-03 15:43:37 -050016643 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080016644 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050016645 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16646 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16647 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16648 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16649 }
16650
16651 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16652 * the driver no longer has that exchange, send a BLS_RJT. Override
16653 * the IOCB for a BA_RJT.
16654 */
16655 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050016656 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16657 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16658 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16659 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16660 }
James Smart6669f9b2009-10-02 15:16:45 -040016661
James Smart5ffc2662009-11-18 15:39:44 -050016662 if (fctl & FC_FC_EX_CTX) {
16663 /* ABTS sent by responder to CT exchange, construction
16664 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16665 * field and RX_ID from ABTS for RX_ID field.
16666 */
James Smart546fc852011-03-11 16:06:29 -050016667 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050016668 } else {
16669 /* ABTS sent by initiator to CT exchange, construction
16670 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050016671 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050016672 */
James Smart546fc852011-03-11 16:06:29 -050016673 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050016674 }
James Smartf09c3ac2012-03-01 22:33:29 -050016675 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050016676 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050016677
James Smart546fc852011-03-11 16:06:29 -050016678 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050016679 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16680 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16681 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050016682
16683 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16684 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050016685 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16686 "2925 Failed to issue CT ABTS RSP x%x on "
16687 "xri x%x, Data x%x\n",
16688 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16689 phba->link_state);
16690 lpfc_nlp_put(ndlp);
16691 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050016692 lpfc_sli_release_iocbq(phba, ctiocb);
16693 }
James Smart6669f9b2009-10-02 15:16:45 -040016694}
16695
16696/**
16697 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16698 * @vport: Pointer to the vport on which this sequence was received
16699 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16700 *
16701 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16702 * receive sequence is only partially assembed by the driver, it shall abort
16703 * the partially assembled frames for the sequence. Otherwise, if the
16704 * unsolicited receive sequence has been completely assembled and passed to
16705 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16706 * unsolicited sequence has been aborted. After that, it will issue a basic
16707 * accept to accept the abort.
16708 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016709static void
James Smart6669f9b2009-10-02 15:16:45 -040016710lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16711 struct hbq_dmabuf *dmabuf)
16712{
16713 struct lpfc_hba *phba = vport->phba;
16714 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050016715 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050016716 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040016717
James Smart6669f9b2009-10-02 15:16:45 -040016718 /* Make a copy of fc_hdr before the dmabuf being released */
16719 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050016720 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040016721
James Smart5ffc2662009-11-18 15:39:44 -050016722 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050016723 /* ABTS by responder to exchange, no cleanup needed */
16724 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050016725 } else {
James Smart6dd9e312013-01-03 15:43:37 -050016726 /* ABTS by initiator to exchange, need to do cleanup */
16727 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16728 if (aborted == false)
16729 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050016730 }
James Smart6dd9e312013-01-03 15:43:37 -050016731 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16732
James Smart86c67372017-04-21 16:05:04 -070016733 if (phba->nvmet_support) {
16734 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16735 return;
16736 }
16737
James Smart6dd9e312013-01-03 15:43:37 -050016738 /* Respond with BA_ACC or BA_RJT accordingly */
16739 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040016740}
16741
16742/**
James Smart4f774512009-05-22 14:52:35 -040016743 * lpfc_seq_complete - Indicates if a sequence is complete
16744 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16745 *
16746 * This function checks the sequence, starting with the frame described by
16747 * @dmabuf, to see if all the frames associated with this sequence are present.
16748 * the frames associated with this sequence are linked to the @dmabuf using the
16749 * dbuf list. This function looks for two major things. 1) That the first frame
16750 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16751 * set. 3) That there are no holes in the sequence count. The function will
16752 * return 1 when the sequence is complete, otherwise it will return 0.
16753 **/
16754static int
16755lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16756{
16757 struct fc_frame_header *hdr;
16758 struct lpfc_dmabuf *d_buf;
16759 struct hbq_dmabuf *seq_dmabuf;
16760 uint32_t fctl;
16761 int seq_count = 0;
16762
16763 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16764 /* make sure first fame of sequence has a sequence count of zero */
16765 if (hdr->fh_seq_cnt != seq_count)
16766 return 0;
16767 fctl = (hdr->fh_f_ctl[0] << 16 |
16768 hdr->fh_f_ctl[1] << 8 |
16769 hdr->fh_f_ctl[2]);
16770 /* If last frame of sequence we can return success. */
16771 if (fctl & FC_FC_END_SEQ)
16772 return 1;
16773 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16774 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16775 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16776 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050016777 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040016778 return 0;
16779 fctl = (hdr->fh_f_ctl[0] << 16 |
16780 hdr->fh_f_ctl[1] << 8 |
16781 hdr->fh_f_ctl[2]);
16782 /* If last frame of sequence we can return success. */
16783 if (fctl & FC_FC_END_SEQ)
16784 return 1;
16785 }
16786 return 0;
16787}
16788
16789/**
16790 * lpfc_prep_seq - Prep sequence for ULP processing
16791 * @vport: Pointer to the vport on which this sequence was received
16792 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16793 *
16794 * This function takes a sequence, described by a list of frames, and creates
16795 * a list of iocbq structures to describe the sequence. This iocbq list will be
16796 * used to issue to the generic unsolicited sequence handler. This routine
16797 * returns a pointer to the first iocbq in the list. If the function is unable
16798 * to allocate an iocbq then it throw out the received frames that were not
16799 * able to be described and return a pointer to the first iocbq. If unable to
16800 * allocate any iocbqs (including the first) this function will return NULL.
16801 **/
16802static struct lpfc_iocbq *
16803lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16804{
James Smart7851fe22011-07-22 18:36:52 -040016805 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016806 struct lpfc_dmabuf *d_buf, *n_buf;
16807 struct lpfc_iocbq *first_iocbq, *iocbq;
16808 struct fc_frame_header *fc_hdr;
16809 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040016810 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050016811 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040016812
16813 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16814 /* remove from receive buffer list */
16815 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016816 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016817 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040016818 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040016819 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016820 /* Get an iocbq struct to fill in. */
16821 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16822 if (first_iocbq) {
16823 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040016824 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016825 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080016826 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040016827
16828 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16829 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16830 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16831 first_iocbq->iocb.un.rcvels.parmRo =
16832 sli4_did_from_fc_hdr(fc_hdr);
16833 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16834 } else
16835 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040016836 first_iocbq->iocb.ulpContext = NO_XRI;
16837 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16838 be16_to_cpu(fc_hdr->fh_ox_id);
16839 /* iocbq is prepped for internal consumption. Physical vpi. */
16840 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16841 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040016842 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040016843 tot_len = bf_get(lpfc_rcqe_length,
16844 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16845
James Smart4f774512009-05-22 14:52:35 -040016846 first_iocbq->context2 = &seq_dmabuf->dbuf;
16847 first_iocbq->context3 = NULL;
16848 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040016849 if (tot_len > LPFC_DATA_BUF_SIZE)
16850 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040016851 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040016852 else
16853 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16854
James Smart4f774512009-05-22 14:52:35 -040016855 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040016856
James Smart7851fe22011-07-22 18:36:52 -040016857 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040016858 }
16859 iocbq = first_iocbq;
16860 /*
16861 * Each IOCBq can have two Buffers assigned, so go through the list
16862 * of buffers for this sequence and save two buffers in each IOCBq
16863 */
16864 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16865 if (!iocbq) {
16866 lpfc_in_buf_free(vport->phba, d_buf);
16867 continue;
16868 }
16869 if (!iocbq->context3) {
16870 iocbq->context3 = d_buf;
16871 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040016872 /* We need to get the size out of the right CQE */
16873 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16874 len = bf_get(lpfc_rcqe_length,
16875 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016876 pbde = (struct ulp_bde64 *)
16877 &iocbq->iocb.unsli3.sli3Words[4];
16878 if (len > LPFC_DATA_BUF_SIZE)
16879 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16880 else
16881 pbde->tus.f.bdeSize = len;
16882
James Smart7851fe22011-07-22 18:36:52 -040016883 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16884 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040016885 } else {
16886 iocbq = lpfc_sli_get_iocbq(vport->phba);
16887 if (!iocbq) {
16888 if (first_iocbq) {
16889 first_iocbq->iocb.ulpStatus =
16890 IOSTAT_FCP_RSP_ERROR;
16891 first_iocbq->iocb.un.ulpWord[4] =
16892 IOERR_NO_RESOURCES;
16893 }
16894 lpfc_in_buf_free(vport->phba, d_buf);
16895 continue;
16896 }
James Smart7851fe22011-07-22 18:36:52 -040016897 /* We need to get the size out of the right CQE */
16898 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16899 len = bf_get(lpfc_rcqe_length,
16900 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016901 iocbq->context2 = d_buf;
16902 iocbq->context3 = NULL;
16903 iocbq->iocb.ulpBdeCount = 1;
16904 if (len > LPFC_DATA_BUF_SIZE)
16905 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16906 LPFC_DATA_BUF_SIZE;
16907 else
16908 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
16909
James Smart7851fe22011-07-22 18:36:52 -040016910 tot_len += len;
16911 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16912
James Smart4f774512009-05-22 14:52:35 -040016913 iocbq->iocb.un.rcvels.remoteID = sid;
16914 list_add_tail(&iocbq->list, &first_iocbq->list);
16915 }
16916 }
16917 return first_iocbq;
16918}
16919
James Smart6669f9b2009-10-02 15:16:45 -040016920static void
16921lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16922 struct hbq_dmabuf *seq_dmabuf)
16923{
16924 struct fc_frame_header *fc_hdr;
16925 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16926 struct lpfc_hba *phba = vport->phba;
16927
16928 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16929 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16930 if (!iocbq) {
16931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16932 "2707 Ring %d handler: Failed to allocate "
16933 "iocb Rctl x%x Type x%x received\n",
16934 LPFC_ELS_RING,
16935 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16936 return;
16937 }
16938 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080016939 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040016940 iocbq, fc_hdr->fh_r_ctl,
16941 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040016942 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040016943 "2540 Ring %d handler: unexpected Rctl "
16944 "x%x Type x%x received\n",
16945 LPFC_ELS_RING,
16946 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16947
16948 /* Free iocb created in lpfc_prep_seq */
16949 list_for_each_entry_safe(curr_iocb, next_iocb,
16950 &iocbq->list, list) {
16951 list_del_init(&curr_iocb->list);
16952 lpfc_sli_release_iocbq(phba, curr_iocb);
16953 }
16954 lpfc_sli_release_iocbq(phba, iocbq);
16955}
16956
James Smart4f774512009-05-22 14:52:35 -040016957/**
16958 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
16959 * @phba: Pointer to HBA context object.
16960 *
16961 * This function is called with no lock held. This function processes all
16962 * the received buffers and gives it to upper layers when a received buffer
16963 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080016964 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040016965 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
16966 * appropriate receive function when the final frame in a sequence is received.
16967 **/
James Smart4d9ab992009-10-02 15:16:39 -040016968void
16969lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
16970 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040016971{
James Smart4d9ab992009-10-02 15:16:39 -040016972 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016973 struct fc_frame_header *fc_hdr;
16974 struct lpfc_vport *vport;
16975 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040016976 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040016977
James Smart4f774512009-05-22 14:52:35 -040016978 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040016979 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080016980
James Smart4d9ab992009-10-02 15:16:39 -040016981 /* check to see if this a valid type of frame */
16982 if (lpfc_fc_frame_check(phba, fc_hdr)) {
16983 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16984 return;
16985 }
James Smart2ea259e2017-02-12 13:52:27 -080016986
James Smart7851fe22011-07-22 18:36:52 -040016987 if ((bf_get(lpfc_cqe_code,
16988 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
16989 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
16990 &dmabuf->cq_event.cqe.rcqe_cmpl);
16991 else
16992 fcfi = bf_get(lpfc_rcqe_fcf_id,
16993 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040016994
James Smart895427b2017-02-12 13:52:30 -080016995 /* d_id this frame is directed to */
16996 did = sli4_did_from_fc_hdr(fc_hdr);
16997
16998 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040016999 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017000 /* throw out the frame */
17001 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17002 return;
17003 }
James Smart939723a2012-05-09 21:19:03 -040017004
James Smart939723a2012-05-09 21:19:03 -040017005 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17006 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17007 (did != Fabric_DID)) {
17008 /*
17009 * Throw out the frame if we are not pt2pt.
17010 * The pt2pt protocol allows for discovery frames
17011 * to be received without a registered VPI.
17012 */
17013 if (!(vport->fc_flag & FC_PT2PT) ||
17014 (phba->link_state == LPFC_HBA_READY)) {
17015 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17016 return;
17017 }
17018 }
17019
James Smart6669f9b2009-10-02 15:16:45 -040017020 /* Handle the basic abort sequence (BA_ABTS) event */
17021 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17022 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17023 return;
17024 }
17025
James Smart4d9ab992009-10-02 15:16:39 -040017026 /* Link this frame */
17027 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17028 if (!seq_dmabuf) {
17029 /* unable to add frame to vport - throw it out */
17030 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17031 return;
17032 }
17033 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017034 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017035 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017036
James Smart6669f9b2009-10-02 15:16:45 -040017037 /* Send the complete sequence to the upper layer protocol */
17038 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017039}
James Smart6fb120a2009-05-22 14:52:59 -040017040
17041/**
17042 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17043 * @phba: pointer to lpfc hba data structure.
17044 *
17045 * This routine is invoked to post rpi header templates to the
17046 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017047 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17048 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017049 *
17050 * This routine does not require any locks. It's usage is expected
17051 * to be driver load or reset recovery when the driver is
17052 * sequential.
17053 *
17054 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017055 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017056 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017057 * When this error occurs, the driver is not guaranteed
17058 * to have any rpi regions posted to the device and
17059 * must either attempt to repost the regions or take a
17060 * fatal error.
17061 **/
17062int
17063lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17064{
17065 struct lpfc_rpi_hdr *rpi_page;
17066 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017067 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017068
James Smart6d368e52011-05-24 11:44:12 -040017069 /* SLI4 ports that support extents do not require RPI headers. */
17070 if (!phba->sli4_hba.rpi_hdrs_in_use)
17071 goto exit;
17072 if (phba->sli4_hba.extents_in_use)
17073 return -EIO;
17074
James Smart6fb120a2009-05-22 14:52:59 -040017075 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017076 /*
17077 * Assign the rpi headers a physical rpi only if the driver
17078 * has not initialized those resources. A port reset only
17079 * needs the headers posted.
17080 */
17081 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17082 LPFC_RPI_RSRC_RDY)
17083 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17084
James Smart6fb120a2009-05-22 14:52:59 -040017085 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17086 if (rc != MBX_SUCCESS) {
17087 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17088 "2008 Error %d posting all rpi "
17089 "headers\n", rc);
17090 rc = -EIO;
17091 break;
17092 }
17093 }
17094
James Smart6d368e52011-05-24 11:44:12 -040017095 exit:
17096 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17097 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017098 return rc;
17099}
17100
17101/**
17102 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17103 * @phba: pointer to lpfc hba data structure.
17104 * @rpi_page: pointer to the rpi memory region.
17105 *
17106 * This routine is invoked to post a single rpi header to the
17107 * HBA consistent with the SLI-4 interface spec. This memory region
17108 * maps up to 64 rpi context regions.
17109 *
17110 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017111 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017112 * -ENOMEM - No available memory
17113 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017114 **/
17115int
17116lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17117{
17118 LPFC_MBOXQ_t *mboxq;
17119 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17120 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017121 uint32_t shdr_status, shdr_add_status;
17122 union lpfc_sli4_cfg_shdr *shdr;
17123
James Smart6d368e52011-05-24 11:44:12 -040017124 /* SLI4 ports that support extents do not require RPI headers. */
17125 if (!phba->sli4_hba.rpi_hdrs_in_use)
17126 return rc;
17127 if (phba->sli4_hba.extents_in_use)
17128 return -EIO;
17129
James Smart6fb120a2009-05-22 14:52:59 -040017130 /* The port is notified of the header region via a mailbox command. */
17131 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17132 if (!mboxq) {
17133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17134 "2001 Unable to allocate memory for issuing "
17135 "SLI_CONFIG_SPECIAL mailbox command\n");
17136 return -ENOMEM;
17137 }
17138
17139 /* Post all rpi memory regions to the port. */
17140 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017141 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17142 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17143 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017144 sizeof(struct lpfc_sli4_cfg_mhdr),
17145 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017146
17147
17148 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017149 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17150 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017151 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17152 hdr_tmpl, rpi_page->page_count);
17153
James Smart6fb120a2009-05-22 14:52:59 -040017154 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17155 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017156 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017157 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17158 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17159 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17160 if (rc != MBX_TIMEOUT)
17161 mempool_free(mboxq, phba->mbox_mem_pool);
17162 if (shdr_status || shdr_add_status || rc) {
17163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17164 "2514 POST_RPI_HDR mailbox failed with "
17165 "status x%x add_status x%x, mbx status x%x\n",
17166 shdr_status, shdr_add_status, rc);
17167 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017168 } else {
17169 /*
17170 * The next_rpi stores the next logical module-64 rpi value used
17171 * to post physical rpis in subsequent rpi postings.
17172 */
17173 spin_lock_irq(&phba->hbalock);
17174 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17175 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017176 }
17177 return rc;
17178}
17179
17180/**
17181 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17182 * @phba: pointer to lpfc hba data structure.
17183 *
17184 * This routine is invoked to post rpi header templates to the
17185 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017186 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17187 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017188 *
17189 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017190 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017191 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17192 **/
17193int
17194lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17195{
James Smart6d368e52011-05-24 11:44:12 -040017196 unsigned long rpi;
17197 uint16_t max_rpi, rpi_limit;
17198 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017199 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017200 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017201
James Smart6fb120a2009-05-22 14:52:59 -040017202 /*
James Smart6d368e52011-05-24 11:44:12 -040017203 * Fetch the next logical rpi. Because this index is logical,
17204 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017205 */
James Smart4902b382013-10-10 12:20:35 -040017206 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017207 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17208 rpi_limit = phba->sli4_hba.next_rpi;
17209
James Smart6d368e52011-05-24 11:44:12 -040017210 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17211 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017212 rpi = LPFC_RPI_ALLOC_ERROR;
17213 else {
17214 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17215 phba->sli4_hba.max_cfg_param.rpi_used++;
17216 phba->sli4_hba.rpi_count++;
17217 }
James Smartbe6bb942015-04-07 15:07:22 -040017218 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17219 "0001 rpi:%x max:%x lim:%x\n",
17220 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017221
17222 /*
17223 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017224 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017225 */
17226 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17227 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017228 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017229 return rpi;
17230 }
17231
17232 /*
James Smart6d368e52011-05-24 11:44:12 -040017233 * RPI header postings are not required for SLI4 ports capable of
17234 * extents.
17235 */
17236 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017237 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017238 return rpi;
17239 }
17240
17241 /*
James Smart6fb120a2009-05-22 14:52:59 -040017242 * If the driver is running low on rpi resources, allocate another
17243 * page now. Note that the next_rpi value is used because
17244 * it represents how many are actually in use whereas max_rpi notes
17245 * how many are supported max by the device.
17246 */
James Smart6d368e52011-05-24 11:44:12 -040017247 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017248 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017249 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17250 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17251 if (!rpi_hdr) {
17252 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17253 "2002 Error Could not grow rpi "
17254 "count\n");
17255 } else {
James Smart6d368e52011-05-24 11:44:12 -040017256 lrpi = rpi_hdr->start_rpi;
17257 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017258 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17259 }
17260 }
17261
17262 return rpi;
17263}
17264
17265/**
17266 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17267 * @phba: pointer to lpfc hba data structure.
17268 *
17269 * This routine is invoked to release an rpi to the pool of
17270 * available rpis maintained by the driver.
17271 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017272static void
James Smartd7c47992010-06-08 18:31:54 -040017273__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17274{
17275 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17276 phba->sli4_hba.rpi_count--;
17277 phba->sli4_hba.max_cfg_param.rpi_used--;
17278 }
17279}
17280
17281/**
17282 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17283 * @phba: pointer to lpfc hba data structure.
17284 *
17285 * This routine is invoked to release an rpi to the pool of
17286 * available rpis maintained by the driver.
17287 **/
17288void
James Smart6fb120a2009-05-22 14:52:59 -040017289lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17290{
17291 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017292 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017293 spin_unlock_irq(&phba->hbalock);
17294}
17295
17296/**
17297 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17298 * @phba: pointer to lpfc hba data structure.
17299 *
17300 * This routine is invoked to remove the memory region that
17301 * provided rpi via a bitmask.
17302 **/
17303void
17304lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17305{
17306 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017307 kfree(phba->sli4_hba.rpi_ids);
17308 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017309}
17310
17311/**
17312 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17313 * @phba: pointer to lpfc hba data structure.
17314 *
17315 * This routine is invoked to remove the memory region that
17316 * provided rpi via a bitmask.
17317 **/
17318int
James Smart6b5151f2012-01-18 16:24:06 -050017319lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17320 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017321{
17322 LPFC_MBOXQ_t *mboxq;
17323 struct lpfc_hba *phba = ndlp->phba;
17324 int rc;
17325
17326 /* The port is notified of the header region via a mailbox command. */
17327 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17328 if (!mboxq)
17329 return -ENOMEM;
17330
17331 /* Post all rpi memory regions to the port. */
17332 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017333 if (cmpl) {
17334 mboxq->mbox_cmpl = cmpl;
17335 mboxq->context1 = arg;
17336 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017337 } else
17338 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017339 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017340 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17341 if (rc == MBX_NOT_FINISHED) {
17342 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17343 "2010 Resume RPI Mailbox failed "
17344 "status %d, mbxStatus x%x\n", rc,
17345 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17346 mempool_free(mboxq, phba->mbox_mem_pool);
17347 return -EIO;
17348 }
17349 return 0;
17350}
17351
17352/**
17353 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017354 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017355 *
James Smart76a95d72010-11-20 23:11:48 -050017356 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017357 *
17358 * Returns:
17359 * 0 success
17360 * -Evalue otherwise
17361 **/
17362int
James Smart76a95d72010-11-20 23:11:48 -050017363lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017364{
17365 LPFC_MBOXQ_t *mboxq;
17366 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017367 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017368 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017369 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017370 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17371 if (!mboxq)
17372 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017373 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017374 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017375 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017376 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017377 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017378 "2022 INIT VPI Mailbox failed "
17379 "status %d, mbxStatus x%x\n", rc,
17380 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017381 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017382 }
James Smart6a9c52c2009-10-02 15:16:51 -040017383 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017384 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017385
17386 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017387}
17388
17389/**
17390 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17391 * @phba: pointer to lpfc hba data structure.
17392 * @mboxq: Pointer to mailbox object.
17393 *
17394 * This routine is invoked to manually add a single FCF record. The caller
17395 * must pass a completely initialized FCF_Record. This routine takes
17396 * care of the nonembedded mailbox operations.
17397 **/
17398static void
17399lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17400{
17401 void *virt_addr;
17402 union lpfc_sli4_cfg_shdr *shdr;
17403 uint32_t shdr_status, shdr_add_status;
17404
17405 virt_addr = mboxq->sge_array->addr[0];
17406 /* The IOCTL status is embedded in the mailbox subheader. */
17407 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17408 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17409 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17410
17411 if ((shdr_status || shdr_add_status) &&
17412 (shdr_status != STATUS_FCF_IN_USE))
17413 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17414 "2558 ADD_FCF_RECORD mailbox failed with "
17415 "status x%x add_status x%x\n",
17416 shdr_status, shdr_add_status);
17417
17418 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17419}
17420
17421/**
17422 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17423 * @phba: pointer to lpfc hba data structure.
17424 * @fcf_record: pointer to the initialized fcf record to add.
17425 *
17426 * This routine is invoked to manually add a single FCF record. The caller
17427 * must pass a completely initialized FCF_Record. This routine takes
17428 * care of the nonembedded mailbox operations.
17429 **/
17430int
17431lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17432{
17433 int rc = 0;
17434 LPFC_MBOXQ_t *mboxq;
17435 uint8_t *bytep;
17436 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017437 struct lpfc_mbx_sge sge;
17438 uint32_t alloc_len, req_len;
17439 uint32_t fcfindex;
17440
17441 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17442 if (!mboxq) {
17443 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17444 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17445 return -ENOMEM;
17446 }
17447
17448 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17449 sizeof(uint32_t);
17450
17451 /* Allocate DMA memory and set up the non-embedded mailbox command */
17452 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17453 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17454 req_len, LPFC_SLI4_MBX_NEMBED);
17455 if (alloc_len < req_len) {
17456 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17457 "2523 Allocated DMA memory size (x%x) is "
17458 "less than the requested DMA memory "
17459 "size (x%x)\n", alloc_len, req_len);
17460 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17461 return -ENOMEM;
17462 }
17463
17464 /*
17465 * Get the first SGE entry from the non-embedded DMA memory. This
17466 * routine only uses a single SGE.
17467 */
17468 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040017469 virt_addr = mboxq->sge_array->addr[0];
17470 /*
17471 * Configure the FCF record for FCFI 0. This is the driver's
17472 * hardcoded default and gets used in nonFIP mode.
17473 */
17474 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17475 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17476 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17477
17478 /*
17479 * Copy the fcf_index and the FCF Record Data. The data starts after
17480 * the FCoE header plus word10. The data copy needs to be endian
17481 * correct.
17482 */
17483 bytep += sizeof(uint32_t);
17484 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17485 mboxq->vport = phba->pport;
17486 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17487 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17488 if (rc == MBX_NOT_FINISHED) {
17489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17490 "2515 ADD_FCF_RECORD mailbox failed with "
17491 "status 0x%x\n", rc);
17492 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17493 rc = -EIO;
17494 } else
17495 rc = 0;
17496
17497 return rc;
17498}
17499
17500/**
17501 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17502 * @phba: pointer to lpfc hba data structure.
17503 * @fcf_record: pointer to the fcf record to write the default data.
17504 * @fcf_index: FCF table entry index.
17505 *
17506 * This routine is invoked to build the driver's default FCF record. The
17507 * values used are hardcoded. This routine handles memory initialization.
17508 *
17509 **/
17510void
17511lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17512 struct fcf_record *fcf_record,
17513 uint16_t fcf_index)
17514{
17515 memset(fcf_record, 0, sizeof(struct fcf_record));
17516 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17517 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17518 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17519 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17520 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17521 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17522 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17523 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17524 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17525 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17526 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17527 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17528 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040017529 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040017530 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17531 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17532 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17533 /* Set the VLAN bit map */
17534 if (phba->valid_vlan) {
17535 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17536 = 1 << (phba->vlan_id % 8);
17537 }
17538}
17539
17540/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017541 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040017542 * @phba: pointer to lpfc hba data structure.
17543 * @fcf_index: FCF table entry offset.
17544 *
James Smart0c9ab6f2010-02-26 14:15:57 -050017545 * This routine is invoked to scan the entire FCF table by reading FCF
17546 * record and processing it one at a time starting from the @fcf_index
17547 * for initial FCF discovery or fast FCF failover rediscovery.
17548 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017549 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017550 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040017551 **/
17552int
James Smart0c9ab6f2010-02-26 14:15:57 -050017553lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040017554{
17555 int rc = 0, error;
17556 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040017557
James Smart32b97932009-07-19 10:01:21 -040017558 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050017559 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040017560 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17561 if (!mboxq) {
17562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17563 "2000 Failed to allocate mbox for "
17564 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040017565 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050017566 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017567 }
James Smartecfd03c2010-02-12 14:41:27 -050017568 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050017569 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050017570 if (rc) {
17571 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050017572 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017573 }
James Smartecfd03c2010-02-12 14:41:27 -050017574 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040017575 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050017576 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040017577
17578 spin_lock_irq(&phba->hbalock);
17579 phba->hba_flag |= FCF_TS_INPROG;
17580 spin_unlock_irq(&phba->hbalock);
17581
James Smart6fb120a2009-05-22 14:52:59 -040017582 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050017583 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040017584 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050017585 else {
James Smart38b92ef2010-08-04 16:11:39 -040017586 /* Reset eligible FCF count for new scan */
17587 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040017588 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017589 error = 0;
James Smart32b97932009-07-19 10:01:21 -040017590 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017591fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040017592 if (error) {
17593 if (mboxq)
17594 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040017595 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040017596 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040017597 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040017598 spin_unlock_irq(&phba->hbalock);
17599 }
James Smart6fb120a2009-05-22 14:52:59 -040017600 return error;
17601}
James Smarta0c87cb2009-07-19 10:01:10 -040017602
17603/**
James Smarta93ff372010-10-22 11:06:08 -040017604 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050017605 * @phba: pointer to lpfc hba data structure.
17606 * @fcf_index: FCF table entry offset.
17607 *
17608 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040017609 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050017610 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017611 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017612 * otherwise.
17613 **/
17614int
17615lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17616{
17617 int rc = 0, error;
17618 LPFC_MBOXQ_t *mboxq;
17619
17620 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17621 if (!mboxq) {
17622 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17623 "2763 Failed to allocate mbox for "
17624 "READ_FCF cmd\n");
17625 error = -ENOMEM;
17626 goto fail_fcf_read;
17627 }
17628 /* Construct the read FCF record mailbox command */
17629 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17630 if (rc) {
17631 error = -EINVAL;
17632 goto fail_fcf_read;
17633 }
17634 /* Issue the mailbox command asynchronously */
17635 mboxq->vport = phba->pport;
17636 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17637 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17638 if (rc == MBX_NOT_FINISHED)
17639 error = -EIO;
17640 else
17641 error = 0;
17642
17643fail_fcf_read:
17644 if (error && mboxq)
17645 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17646 return error;
17647}
17648
17649/**
17650 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17651 * @phba: pointer to lpfc hba data structure.
17652 * @fcf_index: FCF table entry offset.
17653 *
17654 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040017655 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050017656 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017657 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017658 * otherwise.
17659 **/
17660int
17661lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17662{
17663 int rc = 0, error;
17664 LPFC_MBOXQ_t *mboxq;
17665
17666 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17667 if (!mboxq) {
17668 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17669 "2758 Failed to allocate mbox for "
17670 "READ_FCF cmd\n");
17671 error = -ENOMEM;
17672 goto fail_fcf_read;
17673 }
17674 /* Construct the read FCF record mailbox command */
17675 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17676 if (rc) {
17677 error = -EINVAL;
17678 goto fail_fcf_read;
17679 }
17680 /* Issue the mailbox command asynchronously */
17681 mboxq->vport = phba->pport;
17682 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17683 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17684 if (rc == MBX_NOT_FINISHED)
17685 error = -EIO;
17686 else
17687 error = 0;
17688
17689fail_fcf_read:
17690 if (error && mboxq)
17691 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17692 return error;
17693}
17694
17695/**
James Smartf5cb5302015-12-16 18:11:52 -050017696 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040017697 * phba pointer to the lpfc_hba struct for this port.
17698 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17699 * routine when the rr_bmask is empty. The FCF indecies are put into the
17700 * rr_bmask based on their priority level. Starting from the highest priority
17701 * to the lowest. The most likely FCF candidate will be in the highest
17702 * priority group. When this routine is called it searches the fcf_pri list for
17703 * next lowest priority group and repopulates the rr_bmask with only those
17704 * fcf_indexes.
17705 * returns:
17706 * 1=success 0=failure
17707 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017708static int
James Smart7d791df2011-07-22 18:37:52 -040017709lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17710{
17711 uint16_t next_fcf_pri;
17712 uint16_t last_index;
17713 struct lpfc_fcf_pri *fcf_pri;
17714 int rc;
17715 int ret = 0;
17716
17717 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17718 LPFC_SLI4_FCF_TBL_INDX_MAX);
17719 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17720 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050017721
17722 /* Verify the priority list has 2 or more entries */
17723 spin_lock_irq(&phba->hbalock);
17724 if (list_empty(&phba->fcf.fcf_pri_list) ||
17725 list_is_singular(&phba->fcf.fcf_pri_list)) {
17726 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040017727 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17728 "3061 Last IDX %d\n", last_index);
17729 return 0; /* Empty rr list */
17730 }
James Smart25626692013-03-01 16:36:54 -050017731 spin_unlock_irq(&phba->hbalock);
17732
James Smart7d791df2011-07-22 18:37:52 -040017733 next_fcf_pri = 0;
17734 /*
17735 * Clear the rr_bmask and set all of the bits that are at this
17736 * priority.
17737 */
17738 memset(phba->fcf.fcf_rr_bmask, 0,
17739 sizeof(*phba->fcf.fcf_rr_bmask));
17740 spin_lock_irq(&phba->hbalock);
17741 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17742 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
17743 continue;
17744 /*
17745 * the 1st priority that has not FLOGI failed
17746 * will be the highest.
17747 */
17748 if (!next_fcf_pri)
17749 next_fcf_pri = fcf_pri->fcf_rec.priority;
17750 spin_unlock_irq(&phba->hbalock);
17751 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17752 rc = lpfc_sli4_fcf_rr_index_set(phba,
17753 fcf_pri->fcf_rec.fcf_index);
17754 if (rc)
17755 return 0;
17756 }
17757 spin_lock_irq(&phba->hbalock);
17758 }
17759 /*
17760 * if next_fcf_pri was not set above and the list is not empty then
17761 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070017762 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040017763 */
17764 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
17765 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17766 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
17767 /*
17768 * the 1st priority that has not FLOGI failed
17769 * will be the highest.
17770 */
17771 if (!next_fcf_pri)
17772 next_fcf_pri = fcf_pri->fcf_rec.priority;
17773 spin_unlock_irq(&phba->hbalock);
17774 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17775 rc = lpfc_sli4_fcf_rr_index_set(phba,
17776 fcf_pri->fcf_rec.fcf_index);
17777 if (rc)
17778 return 0;
17779 }
17780 spin_lock_irq(&phba->hbalock);
17781 }
17782 } else
17783 ret = 1;
17784 spin_unlock_irq(&phba->hbalock);
17785
17786 return ret;
17787}
17788/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017789 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17790 * @phba: pointer to lpfc hba data structure.
17791 *
17792 * This routine is to get the next eligible FCF record index in a round
17793 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040017794 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050017795 * shall be returned, otherwise, the next eligible FCF record's index
17796 * shall be returned.
17797 **/
17798uint16_t
17799lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17800{
17801 uint16_t next_fcf_index;
17802
James Smart421c6622013-01-03 15:44:16 -050017803initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040017804 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050017805 next_fcf_index = phba->fcf.current_rec.fcf_indx;
17806
James Smart7d791df2011-07-22 18:37:52 -040017807next_priority:
James Smart421c6622013-01-03 15:44:16 -050017808 /* Determine the next fcf index to check */
17809 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050017810 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17811 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040017812 next_fcf_index);
17813
James Smart0c9ab6f2010-02-26 14:15:57 -050017814 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040017815 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17816 /*
17817 * If we have wrapped then we need to clear the bits that
17818 * have been tested so that we can detect when we should
17819 * change the priority level.
17820 */
James Smart0c9ab6f2010-02-26 14:15:57 -050017821 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17822 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040017823 }
17824
James Smart0c9ab6f2010-02-26 14:15:57 -050017825
James Smart3804dc82010-07-14 15:31:37 -040017826 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040017827 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17828 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17829 /*
17830 * If next fcf index is not found check if there are lower
17831 * Priority level fcf's in the fcf_priority list.
17832 * Set up the rr_bmask with all of the avaiable fcf bits
17833 * at that level and continue the selection process.
17834 */
17835 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050017836 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040017837 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17838 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040017839 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
17840 return LPFC_FCOE_FCF_NEXT_NONE;
17841 else {
17842 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17843 "3063 Only FCF available idx %d, flag %x\n",
17844 next_fcf_index,
17845 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
17846 return next_fcf_index;
17847 }
James Smart3804dc82010-07-14 15:31:37 -040017848 }
17849
James Smart7d791df2011-07-22 18:37:52 -040017850 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
17851 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050017852 LPFC_FCF_FLOGI_FAILED) {
17853 if (list_is_singular(&phba->fcf.fcf_pri_list))
17854 return LPFC_FCOE_FCF_NEXT_NONE;
17855
James Smart7d791df2011-07-22 18:37:52 -040017856 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050017857 }
James Smart7d791df2011-07-22 18:37:52 -040017858
James Smart3804dc82010-07-14 15:31:37 -040017859 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017860 "2845 Get next roundrobin failover FCF (x%x)\n",
17861 next_fcf_index);
17862
James Smart0c9ab6f2010-02-26 14:15:57 -050017863 return next_fcf_index;
17864}
17865
17866/**
17867 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
17868 * @phba: pointer to lpfc hba data structure.
17869 *
17870 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017871 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017872 * does not go beyond the range of the driver allocated bmask dimension
17873 * before setting the bit.
17874 *
17875 * Returns 0 if the index bit successfully set, otherwise, it returns
17876 * -EINVAL.
17877 **/
17878int
17879lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
17880{
17881 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17882 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017883 "2610 FCF (x%x) reached driver's book "
17884 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017885 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17886 return -EINVAL;
17887 }
17888 /* Set the eligible FCF record index bmask */
17889 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17890
James Smart3804dc82010-07-14 15:31:37 -040017891 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017892 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040017893 "bmask\n", fcf_index);
17894
James Smart0c9ab6f2010-02-26 14:15:57 -050017895 return 0;
17896}
17897
17898/**
James Smart3804dc82010-07-14 15:31:37 -040017899 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050017900 * @phba: pointer to lpfc hba data structure.
17901 *
17902 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017903 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017904 * does not go beyond the range of the driver allocated bmask dimension
17905 * before clearing the bit.
17906 **/
17907void
17908lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
17909{
James Smart9a803a72013-09-06 12:17:56 -040017910 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050017911 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17912 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017913 "2762 FCF (x%x) reached driver's book "
17914 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017915 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17916 return;
17917 }
17918 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040017919 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040017920 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
17921 list) {
James Smart7d791df2011-07-22 18:37:52 -040017922 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
17923 list_del_init(&fcf_pri->list);
17924 break;
17925 }
17926 }
17927 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017928 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040017929
17930 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017931 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040017932 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050017933}
17934
17935/**
James Smartecfd03c2010-02-12 14:41:27 -050017936 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
17937 * @phba: pointer to lpfc hba data structure.
17938 *
17939 * This routine is the completion routine for the rediscover FCF table mailbox
17940 * command. If the mailbox command returned failure, it will try to stop the
17941 * FCF rediscover wait timer.
17942 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017943static void
James Smartecfd03c2010-02-12 14:41:27 -050017944lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
17945{
17946 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17947 uint32_t shdr_status, shdr_add_status;
17948
17949 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17950
17951 shdr_status = bf_get(lpfc_mbox_hdr_status,
17952 &redisc_fcf->header.cfg_shdr.response);
17953 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
17954 &redisc_fcf->header.cfg_shdr.response);
17955 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050017956 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050017957 "2746 Requesting for FCF rediscovery failed "
17958 "status x%x add_status x%x\n",
17959 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050017960 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050017961 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017962 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050017963 spin_unlock_irq(&phba->hbalock);
17964 /*
17965 * CVL event triggered FCF rediscover request failed,
17966 * last resort to re-try current registered FCF entry.
17967 */
17968 lpfc_retry_pport_discovery(phba);
17969 } else {
17970 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017971 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050017972 spin_unlock_irq(&phba->hbalock);
17973 /*
17974 * DEAD FCF event triggered FCF rediscover request
17975 * failed, last resort to fail over as a link down
17976 * to FCF registration.
17977 */
17978 lpfc_sli4_fcf_dead_failthrough(phba);
17979 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017980 } else {
17981 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017982 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050017983 /*
17984 * Start FCF rediscovery wait timer for pending FCF
17985 * before rescan FCF record table.
17986 */
17987 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050017988 }
James Smartecfd03c2010-02-12 14:41:27 -050017989
17990 mempool_free(mbox, phba->mbox_mem_pool);
17991}
17992
17993/**
James Smart3804dc82010-07-14 15:31:37 -040017994 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050017995 * @phba: pointer to lpfc hba data structure.
17996 *
17997 * This routine is invoked to request for rediscovery of the entire FCF table
17998 * by the port.
17999 **/
18000int
18001lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18002{
18003 LPFC_MBOXQ_t *mbox;
18004 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18005 int rc, length;
18006
James Smart0c9ab6f2010-02-26 14:15:57 -050018007 /* Cancel retry delay timers to all vports before FCF rediscover */
18008 lpfc_cancel_all_vport_retry_delay_timer(phba);
18009
James Smartecfd03c2010-02-12 14:41:27 -050018010 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18011 if (!mbox) {
18012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18013 "2745 Failed to allocate mbox for "
18014 "requesting FCF rediscover.\n");
18015 return -ENOMEM;
18016 }
18017
18018 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18019 sizeof(struct lpfc_sli4_cfg_mhdr));
18020 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18021 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18022 length, LPFC_SLI4_MBX_EMBED);
18023
18024 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18025 /* Set count to 0 for invalidating the entire FCF database */
18026 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18027
18028 /* Issue the mailbox command asynchronously */
18029 mbox->vport = phba->pport;
18030 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18031 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18032
18033 if (rc == MBX_NOT_FINISHED) {
18034 mempool_free(mbox, phba->mbox_mem_pool);
18035 return -EIO;
18036 }
18037 return 0;
18038}
18039
18040/**
James Smartfc2b9892010-02-26 14:15:29 -050018041 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18042 * @phba: pointer to lpfc hba data structure.
18043 *
18044 * This function is the failover routine as a last resort to the FCF DEAD
18045 * event when driver failed to perform fast FCF failover.
18046 **/
18047void
18048lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18049{
18050 uint32_t link_state;
18051
18052 /*
18053 * Last resort as FCF DEAD event failover will treat this as
18054 * a link down, but save the link state because we don't want
18055 * it to be changed to Link Down unless it is already down.
18056 */
18057 link_state = phba->link_state;
18058 lpfc_linkdown(phba);
18059 phba->link_state = link_state;
18060
18061 /* Unregister FCF if no devices connected to it */
18062 lpfc_unregister_unused_fcf(phba);
18063}
18064
18065/**
James Smart026abb82011-12-13 13:20:45 -050018066 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018067 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018068 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018069 *
James Smart026abb82011-12-13 13:20:45 -050018070 * This function gets SLI3 port configure region 23 data through memory dump
18071 * mailbox command. When it successfully retrieves data, the size of the data
18072 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018073 **/
James Smart026abb82011-12-13 13:20:45 -050018074static uint32_t
18075lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018076{
18077 LPFC_MBOXQ_t *pmb = NULL;
18078 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018079 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018080 int rc;
18081
James Smart026abb82011-12-13 13:20:45 -050018082 if (!rgn23_data)
18083 return 0;
18084
James Smarta0c87cb2009-07-19 10:01:10 -040018085 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18086 if (!pmb) {
18087 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018088 "2600 failed to allocate mailbox memory\n");
18089 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018090 }
18091 mb = &pmb->u.mb;
18092
James Smarta0c87cb2009-07-19 10:01:10 -040018093 do {
18094 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18095 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18096
18097 if (rc != MBX_SUCCESS) {
18098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018099 "2601 failed to read config "
18100 "region 23, rc 0x%x Status 0x%x\n",
18101 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018102 mb->un.varDmp.word_cnt = 0;
18103 }
18104 /*
18105 * dump mem may return a zero when finished or we got a
18106 * mailbox error, either way we are done.
18107 */
18108 if (mb->un.varDmp.word_cnt == 0)
18109 break;
18110 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18111 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18112
18113 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018114 rgn23_data + offset,
18115 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018116 offset += mb->un.varDmp.word_cnt;
18117 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18118
James Smart026abb82011-12-13 13:20:45 -050018119 mempool_free(pmb, phba->mbox_mem_pool);
18120 return offset;
18121}
18122
18123/**
18124 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18125 * @phba: pointer to lpfc hba data structure.
18126 * @rgn23_data: pointer to configure region 23 data.
18127 *
18128 * This function gets SLI4 port configure region 23 data through memory dump
18129 * mailbox command. When it successfully retrieves data, the size of the data
18130 * will be returned, otherwise, 0 will be returned.
18131 **/
18132static uint32_t
18133lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18134{
18135 LPFC_MBOXQ_t *mboxq = NULL;
18136 struct lpfc_dmabuf *mp = NULL;
18137 struct lpfc_mqe *mqe;
18138 uint32_t data_length = 0;
18139 int rc;
18140
18141 if (!rgn23_data)
18142 return 0;
18143
18144 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18145 if (!mboxq) {
18146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18147 "3105 failed to allocate mailbox memory\n");
18148 return 0;
18149 }
18150
18151 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18152 goto out;
18153 mqe = &mboxq->u.mqe;
18154 mp = (struct lpfc_dmabuf *) mboxq->context1;
18155 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18156 if (rc)
18157 goto out;
18158 data_length = mqe->un.mb_words[5];
18159 if (data_length == 0)
18160 goto out;
18161 if (data_length > DMP_RGN23_SIZE) {
18162 data_length = 0;
18163 goto out;
18164 }
18165 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18166out:
18167 mempool_free(mboxq, phba->mbox_mem_pool);
18168 if (mp) {
18169 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18170 kfree(mp);
18171 }
18172 return data_length;
18173}
18174
18175/**
18176 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18177 * @phba: pointer to lpfc hba data structure.
18178 *
18179 * This function read region 23 and parse TLV for port status to
18180 * decide if the user disaled the port. If the TLV indicates the
18181 * port is disabled, the hba_flag is set accordingly.
18182 **/
18183void
18184lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18185{
18186 uint8_t *rgn23_data = NULL;
18187 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18188 uint32_t offset = 0;
18189
18190 /* Get adapter Region 23 data */
18191 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18192 if (!rgn23_data)
18193 goto out;
18194
18195 if (phba->sli_rev < LPFC_SLI_REV4)
18196 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18197 else {
18198 if_type = bf_get(lpfc_sli_intf_if_type,
18199 &phba->sli4_hba.sli_intf);
18200 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18201 goto out;
18202 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18203 }
James Smarta0c87cb2009-07-19 10:01:10 -040018204
18205 if (!data_size)
18206 goto out;
18207
18208 /* Check the region signature first */
18209 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18211 "2619 Config region 23 has bad signature\n");
18212 goto out;
18213 }
18214 offset += 4;
18215
18216 /* Check the data structure version */
18217 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18218 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18219 "2620 Config region 23 has bad version\n");
18220 goto out;
18221 }
18222 offset += 4;
18223
18224 /* Parse TLV entries in the region */
18225 while (offset < data_size) {
18226 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18227 break;
18228 /*
18229 * If the TLV is not driver specific TLV or driver id is
18230 * not linux driver id, skip the record.
18231 */
18232 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18233 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18234 (rgn23_data[offset + 3] != 0)) {
18235 offset += rgn23_data[offset + 1] * 4 + 4;
18236 continue;
18237 }
18238
18239 /* Driver found a driver specific TLV in the config region */
18240 sub_tlv_len = rgn23_data[offset + 1] * 4;
18241 offset += 4;
18242 tlv_offset = 0;
18243
18244 /*
18245 * Search for configured port state sub-TLV.
18246 */
18247 while ((offset < data_size) &&
18248 (tlv_offset < sub_tlv_len)) {
18249 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18250 offset += 4;
18251 tlv_offset += 4;
18252 break;
18253 }
18254 if (rgn23_data[offset] != PORT_STE_TYPE) {
18255 offset += rgn23_data[offset + 1] * 4 + 4;
18256 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18257 continue;
18258 }
18259
18260 /* This HBA contains PORT_STE configured */
18261 if (!rgn23_data[offset + 2])
18262 phba->hba_flag |= LINK_DISABLED;
18263
18264 goto out;
18265 }
18266 }
James Smart026abb82011-12-13 13:20:45 -050018267
James Smarta0c87cb2009-07-19 10:01:10 -040018268out:
James Smarta0c87cb2009-07-19 10:01:10 -040018269 kfree(rgn23_data);
18270 return;
18271}
James Smart695a8142010-01-26 23:08:03 -050018272
18273/**
James Smart52d52442011-05-24 11:42:45 -040018274 * lpfc_wr_object - write an object to the firmware
18275 * @phba: HBA structure that indicates port to create a queue on.
18276 * @dmabuf_list: list of dmabufs to write to the port.
18277 * @size: the total byte value of the objects to write to the port.
18278 * @offset: the current offset to be used to start the transfer.
18279 *
18280 * This routine will create a wr_object mailbox command to send to the port.
18281 * the mailbox command will be constructed using the dma buffers described in
18282 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18283 * BDEs that the imbedded mailbox can support. The @offset variable will be
18284 * used to indicate the starting offset of the transfer and will also return
18285 * the offset after the write object mailbox has completed. @size is used to
18286 * determine the end of the object and whether the eof bit should be set.
18287 *
18288 * Return 0 is successful and offset will contain the the new offset to use
18289 * for the next write.
18290 * Return negative value for error cases.
18291 **/
18292int
18293lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18294 uint32_t size, uint32_t *offset)
18295{
18296 struct lpfc_mbx_wr_object *wr_object;
18297 LPFC_MBOXQ_t *mbox;
18298 int rc = 0, i = 0;
18299 uint32_t shdr_status, shdr_add_status;
18300 uint32_t mbox_tmo;
18301 union lpfc_sli4_cfg_shdr *shdr;
18302 struct lpfc_dmabuf *dmabuf;
18303 uint32_t written = 0;
18304
18305 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18306 if (!mbox)
18307 return -ENOMEM;
18308
18309 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18310 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18311 sizeof(struct lpfc_mbx_wr_object) -
18312 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18313
18314 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18315 wr_object->u.request.write_offset = *offset;
18316 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18317 wr_object->u.request.object_name[0] =
18318 cpu_to_le32(wr_object->u.request.object_name[0]);
18319 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18320 list_for_each_entry(dmabuf, dmabuf_list, list) {
18321 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18322 break;
18323 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18324 wr_object->u.request.bde[i].addrHigh =
18325 putPaddrHigh(dmabuf->phys);
18326 if (written + SLI4_PAGE_SIZE >= size) {
18327 wr_object->u.request.bde[i].tus.f.bdeSize =
18328 (size - written);
18329 written += (size - written);
18330 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18331 } else {
18332 wr_object->u.request.bde[i].tus.f.bdeSize =
18333 SLI4_PAGE_SIZE;
18334 written += SLI4_PAGE_SIZE;
18335 }
18336 i++;
18337 }
18338 wr_object->u.request.bde_count = i;
18339 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18340 if (!phba->sli4_hba.intr_enable)
18341 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18342 else {
James Smarta183a152011-10-10 21:32:43 -040018343 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018344 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18345 }
18346 /* The IOCTL status is embedded in the mailbox subheader. */
18347 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18348 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18349 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18350 if (rc != MBX_TIMEOUT)
18351 mempool_free(mbox, phba->mbox_mem_pool);
18352 if (shdr_status || shdr_add_status || rc) {
18353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18354 "3025 Write Object mailbox failed with "
18355 "status x%x add_status x%x, mbx status x%x\n",
18356 shdr_status, shdr_add_status, rc);
18357 rc = -ENXIO;
18358 } else
18359 *offset += wr_object->u.response.actual_write_length;
18360 return rc;
18361}
18362
18363/**
James Smart695a8142010-01-26 23:08:03 -050018364 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18365 * @vport: pointer to vport data structure.
18366 *
18367 * This function iterate through the mailboxq and clean up all REG_LOGIN
18368 * and REG_VPI mailbox commands associated with the vport. This function
18369 * is called when driver want to restart discovery of the vport due to
18370 * a Clear Virtual Link event.
18371 **/
18372void
18373lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18374{
18375 struct lpfc_hba *phba = vport->phba;
18376 LPFC_MBOXQ_t *mb, *nextmb;
18377 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018378 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018379 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018380 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018381 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018382 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018383
James Smartd439d282010-09-29 11:18:45 -040018384 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018385 spin_lock_irq(&phba->hbalock);
18386 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18387 if (mb->vport != vport)
18388 continue;
18389
18390 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18391 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18392 continue;
18393
James Smartd439d282010-09-29 11:18:45 -040018394 list_del(&mb->list);
18395 list_add_tail(&mb->list, &mbox_cmd_list);
18396 }
18397 /* Clean up active mailbox command with the vport */
18398 mb = phba->sli.mbox_active;
18399 if (mb && (mb->vport == vport)) {
18400 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18401 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18402 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18403 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18404 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18405 /* Put reference count for delayed processing */
18406 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18407 /* Unregister the RPI when mailbox complete */
18408 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18409 }
18410 }
James Smart63e801c2010-11-20 23:14:19 -050018411 /* Cleanup any mailbox completions which are not yet processed */
18412 do {
18413 restart_loop = 0;
18414 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18415 /*
18416 * If this mailox is already processed or it is
18417 * for another vport ignore it.
18418 */
18419 if ((mb->vport != vport) ||
18420 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18421 continue;
18422
18423 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18424 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18425 continue;
18426
18427 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18428 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18429 ndlp = (struct lpfc_nodelist *)mb->context2;
18430 /* Unregister the RPI when mailbox complete */
18431 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18432 restart_loop = 1;
18433 spin_unlock_irq(&phba->hbalock);
18434 spin_lock(shost->host_lock);
18435 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18436 spin_unlock(shost->host_lock);
18437 spin_lock_irq(&phba->hbalock);
18438 break;
18439 }
18440 }
18441 } while (restart_loop);
18442
James Smartd439d282010-09-29 11:18:45 -040018443 spin_unlock_irq(&phba->hbalock);
18444
18445 /* Release the cleaned-up mailbox commands */
18446 while (!list_empty(&mbox_cmd_list)) {
18447 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018448 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18449 mp = (struct lpfc_dmabuf *) (mb->context1);
18450 if (mp) {
18451 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18452 kfree(mp);
18453 }
James Smart78730cf2010-04-06 15:06:30 -040018454 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018455 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018456 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018457 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018458 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018459 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018460 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018461 }
James Smart695a8142010-01-26 23:08:03 -050018462 }
James Smart695a8142010-01-26 23:08:03 -050018463 mempool_free(mb, phba->mbox_mem_pool);
18464 }
James Smartd439d282010-09-29 11:18:45 -040018465
18466 /* Release the ndlp with the cleaned-up active mailbox command */
18467 if (act_mbx_ndlp) {
18468 spin_lock(shost->host_lock);
18469 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18470 spin_unlock(shost->host_lock);
18471 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050018472 }
James Smart695a8142010-01-26 23:08:03 -050018473}
18474
James Smart2a9bf3d2010-06-07 15:24:45 -040018475/**
18476 * lpfc_drain_txq - Drain the txq
18477 * @phba: Pointer to HBA context object.
18478 *
18479 * This function attempt to submit IOCBs on the txq
18480 * to the adapter. For SLI4 adapters, the txq contains
18481 * ELS IOCBs that have been deferred because the there
18482 * are no SGLs. This congestion can occur with large
18483 * vport counts during node discovery.
18484 **/
18485
18486uint32_t
18487lpfc_drain_txq(struct lpfc_hba *phba)
18488{
18489 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080018490 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090018491 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040018492 unsigned long iflags = 0;
18493 char *fail_msg = NULL;
18494 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080018495 union lpfc_wqe128 wqe128;
18496 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040018497 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040018498
James Smart895427b2017-02-12 13:52:30 -080018499 pring = lpfc_phba_elsring(phba);
18500
James Smart398d81c2013-05-31 17:04:19 -040018501 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050018502 list_for_each_entry(piocbq, &pring->txq, list) {
18503 txq_cnt++;
18504 }
18505
18506 if (txq_cnt > pring->txq_max)
18507 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018508
James Smart398d81c2013-05-31 17:04:19 -040018509 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018510
James Smart0e9bb8d2013-03-01 16:35:12 -050018511 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040018512 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018513
James Smart19ca7602010-11-20 23:11:55 -050018514 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040018515 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040018516 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040018517 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18518 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050018519 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040018520 break;
18521 }
James Smart895427b2017-02-12 13:52:30 -080018522 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040018523 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050018524 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040018525 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018526 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040018527 }
James Smart0e9bb8d2013-03-01 16:35:12 -050018528 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040018529
18530 /* The xri and iocb resources secured,
18531 * attempt to issue request
18532 */
James Smart6d368e52011-05-24 11:44:12 -040018533 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040018534 piocbq->sli4_xritag = sglq->sli4_xritag;
18535 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18536 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080018537 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018538 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080018539 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018540 fail_msg = " - Wq is full";
18541 else
18542 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18543
18544 if (fail_msg) {
18545 /* Failed means we can't issue and need to cancel */
18546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18547 "2822 IOCB failed %s iotag 0x%x "
18548 "xri 0x%x\n",
18549 fail_msg,
18550 piocbq->iotag, piocbq->sli4_xritag);
18551 list_add_tail(&piocbq->list, &completions);
18552 }
James Smart398d81c2013-05-31 17:04:19 -040018553 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018554 }
18555
James Smart2a9bf3d2010-06-07 15:24:45 -040018556 /* Cancel all the IOCBs that cannot be issued */
18557 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18558 IOERR_SLI_ABORTED);
18559
James Smart0e9bb8d2013-03-01 16:35:12 -050018560 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018561}
James Smart895427b2017-02-12 13:52:30 -080018562
18563/**
18564 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18565 * @phba: Pointer to HBA context object.
18566 * @pwqe: Pointer to command WQE.
18567 * @sglq: Pointer to the scatter gather queue object.
18568 *
18569 * This routine converts the bpl or bde that is in the WQE
18570 * to a sgl list for the sli4 hardware. The physical address
18571 * of the bpl/bde is converted back to a virtual address.
18572 * If the WQE contains a BPL then the list of BDE's is
18573 * converted to sli4_sge's. If the WQE contains a single
18574 * BDE then it is converted to a single sli_sge.
18575 * The WQE is still in cpu endianness so the contents of
18576 * the bpl can be used without byte swapping.
18577 *
18578 * Returns valid XRI = Success, NO_XRI = Failure.
18579 */
18580static uint16_t
18581lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18582 struct lpfc_sglq *sglq)
18583{
18584 uint16_t xritag = NO_XRI;
18585 struct ulp_bde64 *bpl = NULL;
18586 struct ulp_bde64 bde;
18587 struct sli4_sge *sgl = NULL;
18588 struct lpfc_dmabuf *dmabuf;
18589 union lpfc_wqe *wqe;
18590 int numBdes = 0;
18591 int i = 0;
18592 uint32_t offset = 0; /* accumulated offset in the sg request list */
18593 int inbound = 0; /* number of sg reply entries inbound from firmware */
18594 uint32_t cmd;
18595
18596 if (!pwqeq || !sglq)
18597 return xritag;
18598
18599 sgl = (struct sli4_sge *)sglq->sgl;
18600 wqe = &pwqeq->wqe;
18601 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18602
18603 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18604 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18605 return sglq->sli4_xritag;
18606 numBdes = pwqeq->rsvd2;
18607 if (numBdes) {
18608 /* The addrHigh and addrLow fields within the WQE
18609 * have not been byteswapped yet so there is no
18610 * need to swap them back.
18611 */
18612 if (pwqeq->context3)
18613 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18614 else
18615 return xritag;
18616
18617 bpl = (struct ulp_bde64 *)dmabuf->virt;
18618 if (!bpl)
18619 return xritag;
18620
18621 for (i = 0; i < numBdes; i++) {
18622 /* Should already be byte swapped. */
18623 sgl->addr_hi = bpl->addrHigh;
18624 sgl->addr_lo = bpl->addrLow;
18625
18626 sgl->word2 = le32_to_cpu(sgl->word2);
18627 if ((i+1) == numBdes)
18628 bf_set(lpfc_sli4_sge_last, sgl, 1);
18629 else
18630 bf_set(lpfc_sli4_sge_last, sgl, 0);
18631 /* swap the size field back to the cpu so we
18632 * can assign it to the sgl.
18633 */
18634 bde.tus.w = le32_to_cpu(bpl->tus.w);
18635 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18636 /* The offsets in the sgl need to be accumulated
18637 * separately for the request and reply lists.
18638 * The request is always first, the reply follows.
18639 */
18640 switch (cmd) {
18641 case CMD_GEN_REQUEST64_WQE:
18642 /* add up the reply sg entries */
18643 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18644 inbound++;
18645 /* first inbound? reset the offset */
18646 if (inbound == 1)
18647 offset = 0;
18648 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18649 bf_set(lpfc_sli4_sge_type, sgl,
18650 LPFC_SGE_TYPE_DATA);
18651 offset += bde.tus.f.bdeSize;
18652 break;
18653 case CMD_FCP_TRSP64_WQE:
18654 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18655 bf_set(lpfc_sli4_sge_type, sgl,
18656 LPFC_SGE_TYPE_DATA);
18657 break;
18658 case CMD_FCP_TSEND64_WQE:
18659 case CMD_FCP_TRECEIVE64_WQE:
18660 bf_set(lpfc_sli4_sge_type, sgl,
18661 bpl->tus.f.bdeFlags);
18662 if (i < 3)
18663 offset = 0;
18664 else
18665 offset += bde.tus.f.bdeSize;
18666 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18667 break;
18668 }
18669 sgl->word2 = cpu_to_le32(sgl->word2);
18670 bpl++;
18671 sgl++;
18672 }
18673 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18674 /* The addrHigh and addrLow fields of the BDE have not
18675 * been byteswapped yet so they need to be swapped
18676 * before putting them in the sgl.
18677 */
18678 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18679 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18680 sgl->word2 = le32_to_cpu(sgl->word2);
18681 bf_set(lpfc_sli4_sge_last, sgl, 1);
18682 sgl->word2 = cpu_to_le32(sgl->word2);
18683 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18684 }
18685 return sglq->sli4_xritag;
18686}
18687
18688/**
18689 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18690 * @phba: Pointer to HBA context object.
18691 * @ring_number: Base sli ring number
18692 * @pwqe: Pointer to command WQE.
18693 **/
18694int
18695lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18696 struct lpfc_iocbq *pwqe)
18697{
18698 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080018699 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080018700 struct lpfc_queue *wq;
18701 struct lpfc_sglq *sglq;
18702 struct lpfc_sli_ring *pring;
18703 unsigned long iflags;
18704
18705 /* NVME_LS and NVME_LS ABTS requests. */
18706 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18707 pring = phba->sli4_hba.nvmels_wq->pring;
18708 spin_lock_irqsave(&pring->ring_lock, iflags);
18709 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18710 if (!sglq) {
18711 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18712 return WQE_BUSY;
18713 }
18714 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18715 pwqe->sli4_xritag = sglq->sli4_xritag;
18716 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18717 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18718 return WQE_ERROR;
18719 }
18720 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18721 pwqe->sli4_xritag);
18722 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
18723 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18724 return WQE_ERROR;
18725 }
18726 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18727 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18728 return 0;
18729 }
18730
18731 /* NVME_FCREQ and NVME_ABTS requests */
18732 if (pwqe->iocb_flag & LPFC_IO_NVME) {
18733 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18734 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18735
18736 spin_lock_irqsave(&pring->ring_lock, iflags);
18737 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18738 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18739 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18740 if (lpfc_sli4_wq_put(wq, wqe)) {
18741 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18742 return WQE_ERROR;
18743 }
18744 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18745 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18746 return 0;
18747 }
18748
James Smartf358dd02017-02-12 13:52:34 -080018749 /* NVMET requests */
18750 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
18751 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18752 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18753
18754 spin_lock_irqsave(&pring->ring_lock, iflags);
18755 ctxp = pwqe->context2;
18756 sglq = ctxp->rqb_buffer->sglq;
18757 if (pwqe->sli4_xritag == NO_XRI) {
18758 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18759 pwqe->sli4_xritag = sglq->sli4_xritag;
18760 }
18761 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18762 pwqe->sli4_xritag);
18763 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18764 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18765 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18766 if (lpfc_sli4_wq_put(wq, wqe)) {
18767 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18768 return WQE_ERROR;
18769 }
18770 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18771 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18772 return 0;
18773 }
James Smart895427b2017-02-12 13:52:30 -080018774 return WQE_ERROR;
18775}