blob: 903c06ff828a37b88bbb43bf3c8b964981cab2e4 [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 Smart61f3d4b2017-05-15 15:20:41 -0700482 temp_hrqe = hq->qe[put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500483 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;
James Smart61f3d4b2017-05-15 15:20:41 -0700487 if (put_index != dq->host_index)
James Smart4f774512009-05-22 14:52:35 -0400488 return -EINVAL;
489 /* If the host has not yet processed the next entry then we are done */
James Smart61f3d4b2017-05-15 15:20:41 -0700490 if (((put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400491 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 */
James Smart61f3d4b2017-05-15 15:20:41 -0700496 hq->host_index = ((put_index + 1) % hq->entry_count);
James Smart4f774512009-05-22 14:52:35 -0400497 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700498 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400499
500 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400501 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400502 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500503 if (hq->db_format == LPFC_DB_RING_FORMAT) {
504 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
505 hq->entry_repost);
506 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
507 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
508 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
509 hq->entry_repost);
510 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
511 hq->host_index);
512 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
513 } else {
514 return -EINVAL;
515 }
516 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 Smarta8cf5df2017-05-15 15:20:46 -07006516int
James Smart6c621a22017-05-15 15:20:45 -07006517lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006518 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006519{
6520 int rc, i;
6521 struct lpfc_rqe hrqe;
6522 struct lpfc_rqe drqe;
6523 struct lpfc_rqb *rqbp;
6524 struct rqb_dmabuf *rqb_buffer;
6525 LIST_HEAD(rqb_buf_list);
6526
6527 rqbp = hrq->rqbp;
6528 for (i = 0; i < count; i++) {
6529 /* IF RQ is already full, don't bother */
6530 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6531 break;
6532 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6533 if (!rqb_buffer)
6534 break;
6535 rqb_buffer->hrq = hrq;
6536 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006537 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006538 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6539 }
6540 while (!list_empty(&rqb_buf_list)) {
6541 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6542 hbuf.list);
6543
6544 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6545 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6546 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6547 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6548 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6549 if (rc < 0) {
6550 rqbp->rqb_free_buffer(phba, rqb_buffer);
6551 } else {
6552 list_add_tail(&rqb_buffer->hbuf.list,
6553 &rqbp->rqb_buffer_list);
6554 rqbp->buffer_count++;
6555 }
6556 }
6557 return 1;
6558}
6559
James Smart8a9d2e82012-05-09 21:16:12 -04006560/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006561 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006562 * @phba: Pointer to HBA context object.
6563 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006564 * This function is the main SLI4 device initialization PCI function. This
6565 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006566 * HBA error attention handler code. Caller is not required to hold any
6567 * locks.
6568 **/
6569int
6570lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6571{
James Smart6c621a22017-05-15 15:20:45 -07006572 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006573 LPFC_MBOXQ_t *mboxq;
6574 struct lpfc_mqe *mqe;
6575 uint8_t *vpd;
6576 uint32_t vpd_size;
6577 uint32_t ftr_rsp = 0;
6578 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6579 struct lpfc_vport *vport = phba->pport;
6580 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006581 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006582
6583 /* Perform a PCI function reset to start from clean */
6584 rc = lpfc_pci_function_reset(phba);
6585 if (unlikely(rc))
6586 return -ENODEV;
6587
6588 /* Check the HBA Host Status Register for readyness */
6589 rc = lpfc_sli4_post_status_check(phba);
6590 if (unlikely(rc))
6591 return -ENODEV;
6592 else {
6593 spin_lock_irq(&phba->hbalock);
6594 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6595 spin_unlock_irq(&phba->hbalock);
6596 }
6597
6598 /*
6599 * Allocate a single mailbox container for initializing the
6600 * port.
6601 */
6602 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6603 if (!mboxq)
6604 return -ENOMEM;
6605
James Smartda0436e2009-05-22 14:51:39 -04006606 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006607 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006608 vpd = kzalloc(vpd_size, GFP_KERNEL);
6609 if (!vpd) {
6610 rc = -ENOMEM;
6611 goto out_free_mbox;
6612 }
6613
6614 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006615 if (unlikely(rc)) {
6616 kfree(vpd);
6617 goto out_free_mbox;
6618 }
James Smart572709e2013-07-15 18:32:43 -04006619
James Smartda0436e2009-05-22 14:51:39 -04006620 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006621 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006622 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006623 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006624 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6625 } else {
James Smart76a95d72010-11-20 23:11:48 -05006626 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006627 }
James Smart45ed1192009-10-02 15:17:02 -04006628
6629 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6630 LPFC_DCBX_CEE_MODE)
6631 phba->hba_flag |= HBA_FIP_SUPPORT;
6632 else
6633 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6634
James Smart4f2e66c2012-05-09 21:17:07 -04006635 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6636
James Smartc31098c2011-04-16 11:03:33 -04006637 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006638 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6639 "0376 READ_REV Error. SLI Level %d "
6640 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006641 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006642 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006643 kfree(vpd);
6644 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006645 }
James Smartcd1c8302011-10-10 21:33:25 -04006646
6647 /*
James Smartff78d8f2011-12-13 13:21:35 -05006648 * Continue initialization with default values even if driver failed
6649 * to read FCoE param config regions, only read parameters if the
6650 * board is FCoE
6651 */
6652 if (phba->hba_flag & HBA_FCOE_MODE &&
6653 lpfc_sli4_read_fcoe_params(phba))
6654 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6655 "2570 Failed to read FCoE parameters\n");
6656
6657 /*
James Smartcd1c8302011-10-10 21:33:25 -04006658 * Retrieve sli4 device physical port name, failure of doing it
6659 * is considered as non-fatal.
6660 */
6661 rc = lpfc_sli4_retrieve_pport_name(phba);
6662 if (!rc)
6663 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6664 "3080 Successful retrieving SLI4 device "
6665 "physical port name: %s.\n", phba->Port);
6666
James Smartda0436e2009-05-22 14:51:39 -04006667 /*
6668 * Evaluate the read rev and vpd data. Populate the driver
6669 * state with the results. If this routine fails, the failure
6670 * is not fatal as the driver will use generic values.
6671 */
6672 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6673 if (unlikely(!rc)) {
6674 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6675 "0377 Error %d parsing vpd. "
6676 "Using defaults.\n", rc);
6677 rc = 0;
6678 }
James Smart76a95d72010-11-20 23:11:48 -05006679 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006680
James Smartf1126682009-06-10 17:22:44 -04006681 /* Save information as VPD data */
6682 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6683 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6684 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6685 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6686 &mqe->un.read_rev);
6687 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6688 &mqe->un.read_rev);
6689 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6690 &mqe->un.read_rev);
6691 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6692 &mqe->un.read_rev);
6693 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6694 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6695 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6696 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6697 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6698 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6699 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6700 "(%d):0380 READ_REV Status x%x "
6701 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6702 mboxq->vport ? mboxq->vport->vpi : 0,
6703 bf_get(lpfc_mqe_status, mqe),
6704 phba->vpd.rev.opFwName,
6705 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6706 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006707
James Smart572709e2013-07-15 18:32:43 -04006708 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6709 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6710 if (phba->pport->cfg_lun_queue_depth > rc) {
6711 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6712 "3362 LUN queue depth changed from %d to %d\n",
6713 phba->pport->cfg_lun_queue_depth, rc);
6714 phba->pport->cfg_lun_queue_depth = rc;
6715 }
6716
James Smart65791f12016-07-06 12:35:56 -07006717 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006718 LPFC_SLI_INTF_IF_TYPE_0) {
6719 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6720 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6721 if (rc == MBX_SUCCESS) {
6722 phba->hba_flag |= HBA_RECOVERABLE_UE;
6723 /* Set 1Sec interval to detect UE */
6724 phba->eratt_poll_interval = 1;
6725 phba->sli4_hba.ue_to_sr = bf_get(
6726 lpfc_mbx_set_feature_UESR,
6727 &mboxq->u.mqe.un.set_feature);
6728 phba->sli4_hba.ue_to_rp = bf_get(
6729 lpfc_mbx_set_feature_UERP,
6730 &mboxq->u.mqe.un.set_feature);
6731 }
6732 }
6733
6734 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6735 /* Enable MDS Diagnostics only if the SLI Port supports it */
6736 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6737 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6738 if (rc != MBX_SUCCESS)
6739 phba->mds_diags_support = 0;
6740 }
James Smart572709e2013-07-15 18:32:43 -04006741
James Smartda0436e2009-05-22 14:51:39 -04006742 /*
6743 * Discover the port's supported feature set and match it against the
6744 * hosts requests.
6745 */
6746 lpfc_request_features(phba, mboxq);
6747 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6748 if (unlikely(rc)) {
6749 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006750 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006751 }
6752
6753 /*
6754 * The port must support FCP initiator mode as this is the
6755 * only mode running in the host.
6756 */
6757 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6758 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6759 "0378 No support for fcpi mode.\n");
6760 ftr_rsp++;
6761 }
James Smartfedd3b72011-02-16 12:39:24 -05006762 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6763 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6764 else
6765 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006766 /*
6767 * If the port cannot support the host's requested features
6768 * then turn off the global config parameters to disable the
6769 * feature in the driver. This is not a fatal error.
6770 */
James Smartbf086112011-08-21 21:48:13 -04006771 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6772 if (phba->cfg_enable_bg) {
6773 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6774 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6775 else
6776 ftr_rsp++;
6777 }
James Smartda0436e2009-05-22 14:51:39 -04006778
6779 if (phba->max_vpi && phba->cfg_enable_npiv &&
6780 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6781 ftr_rsp++;
6782
6783 if (ftr_rsp) {
6784 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6785 "0379 Feature Mismatch Data: x%08x %08x "
6786 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6787 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6788 phba->cfg_enable_npiv, phba->max_vpi);
6789 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6790 phba->cfg_enable_bg = 0;
6791 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6792 phba->cfg_enable_npiv = 0;
6793 }
6794
6795 /* These SLI3 features are assumed in SLI4 */
6796 spin_lock_irq(&phba->hbalock);
6797 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6798 spin_unlock_irq(&phba->hbalock);
6799
James Smart6d368e52011-05-24 11:44:12 -04006800 /*
6801 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6802 * calls depends on these resources to complete port setup.
6803 */
6804 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6805 if (rc) {
6806 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6807 "2920 Failed to alloc Resource IDs "
6808 "rc = x%x\n", rc);
6809 goto out_free_mbox;
6810 }
6811
James Smart61bda8f2016-10-13 15:06:05 -07006812 lpfc_set_host_data(phba, mboxq);
6813
6814 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6815 if (rc) {
6816 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6817 "2134 Failed to set host os driver version %x",
6818 rc);
6819 }
6820
James Smartda0436e2009-05-22 14:51:39 -04006821 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006822 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6823 if (rc) {
6824 phba->link_state = LPFC_HBA_ERROR;
6825 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006826 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006827 }
6828
James Smartda0436e2009-05-22 14:51:39 -04006829 mboxq->vport = vport;
6830 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6831 mp = (struct lpfc_dmabuf *) mboxq->context1;
6832 if (rc == MBX_SUCCESS) {
6833 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6834 rc = 0;
6835 }
6836
6837 /*
6838 * This memory was allocated by the lpfc_read_sparam routine. Release
6839 * it to the mbuf pool.
6840 */
6841 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6842 kfree(mp);
6843 mboxq->context1 = NULL;
6844 if (unlikely(rc)) {
6845 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6846 "0382 READ_SPARAM command failed "
6847 "status %d, mbxStatus x%x\n",
6848 rc, bf_get(lpfc_mqe_status, mqe));
6849 phba->link_state = LPFC_HBA_ERROR;
6850 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006851 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006852 }
6853
James Smart05580562011-05-24 11:40:48 -04006854 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006855
6856 /* Update the fc_host data structures with new wwn. */
6857 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6858 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6859
James Smart895427b2017-02-12 13:52:30 -08006860 /* Create all the SLI4 queues */
6861 rc = lpfc_sli4_queue_create(phba);
6862 if (rc) {
6863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6864 "3089 Failed to allocate queues\n");
6865 rc = -ENODEV;
6866 goto out_free_mbox;
6867 }
6868 /* Set up all the queues to the device */
6869 rc = lpfc_sli4_queue_setup(phba);
6870 if (unlikely(rc)) {
6871 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6872 "0381 Error %d during queue setup.\n ", rc);
6873 goto out_stop_timers;
6874 }
6875 /* Initialize the driver internal SLI layer lists. */
6876 lpfc_sli4_setup(phba);
6877 lpfc_sli4_queue_init(phba);
6878
6879 /* update host els xri-sgl sizes and mappings */
6880 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006881 if (unlikely(rc)) {
6882 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6883 "1400 Failed to update xri-sgl size and "
6884 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08006885 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006886 }
6887
James Smart8a9d2e82012-05-09 21:16:12 -04006888 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08006889 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6890 phba->sli4_hba.els_xri_cnt);
6891 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04006892 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6893 "0582 Error %d during els sgl post "
6894 "operation\n", rc);
6895 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006896 goto out_destroy_queue;
6897 }
6898 phba->sli4_hba.els_xri_cnt = rc;
6899
James Smartf358dd02017-02-12 13:52:34 -08006900 if (phba->nvmet_support) {
6901 /* update host nvmet xri-sgl sizes and mappings */
6902 rc = lpfc_sli4_nvmet_sgl_update(phba);
6903 if (unlikely(rc)) {
6904 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6905 "6308 Failed to update nvmet-sgl size "
6906 "and mapping: %d\n", rc);
6907 goto out_destroy_queue;
6908 }
6909
6910 /* register the nvmet sgl pool to the port */
6911 rc = lpfc_sli4_repost_sgl_list(
6912 phba,
6913 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6914 phba->sli4_hba.nvmet_xri_cnt);
6915 if (unlikely(rc < 0)) {
6916 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6917 "3117 Error %d during nvmet "
6918 "sgl post\n", rc);
6919 rc = -ENODEV;
6920 goto out_destroy_queue;
6921 }
6922 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07006923
6924 cnt = phba->cfg_iocb_cnt * 1024;
6925 /* We need 1 iocbq for every SGL, for IO processing */
6926 cnt += phba->sli4_hba.nvmet_xri_cnt;
6927 /* Initialize and populate the iocb list per host */
6928 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6929 "2821 initialize iocb list %d total %d\n",
6930 phba->cfg_iocb_cnt, cnt);
6931 rc = lpfc_init_iocb_list(phba, cnt);
6932 if (rc) {
6933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6934 "1413 Failed to init iocb list.\n");
6935 goto out_destroy_queue;
6936 }
6937
James Smartd613b6a2017-02-12 13:52:37 -08006938 lpfc_nvmet_create_targetport(phba);
James Smartf358dd02017-02-12 13:52:34 -08006939 } else {
James Smart895427b2017-02-12 13:52:30 -08006940 /* update host scsi xri-sgl sizes and mappings */
6941 rc = lpfc_sli4_scsi_sgl_update(phba);
6942 if (unlikely(rc)) {
6943 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6944 "6309 Failed to update scsi-sgl size "
6945 "and mapping: %d\n", rc);
6946 goto out_destroy_queue;
6947 }
6948
6949 /* update host nvme xri-sgl sizes and mappings */
6950 rc = lpfc_sli4_nvme_sgl_update(phba);
6951 if (unlikely(rc)) {
6952 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6953 "6082 Failed to update nvme-sgl size "
6954 "and mapping: %d\n", rc);
6955 goto out_destroy_queue;
6956 }
James Smart6c621a22017-05-15 15:20:45 -07006957
6958 cnt = phba->cfg_iocb_cnt * 1024;
6959 /* Initialize and populate the iocb list per host */
6960 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6961 "2820 initialize iocb list %d total %d\n",
6962 phba->cfg_iocb_cnt, cnt);
6963 rc = lpfc_init_iocb_list(phba, cnt);
6964 if (rc) {
6965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6966 "6301 Failed to init iocb list.\n");
6967 goto out_destroy_queue;
6968 }
James Smart8a9d2e82012-05-09 21:16:12 -04006969 }
6970
James Smart2d7dbc42017-02-12 13:52:35 -08006971 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08006972 /* Post initial buffers to all RQs created */
6973 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6974 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6975 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6976 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6977 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07006978 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08006979 rqbp->buffer_count = 0;
6980
James Smart2d7dbc42017-02-12 13:52:35 -08006981 lpfc_post_rq_buffer(
6982 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6983 phba->sli4_hba.nvmet_mrq_data[i],
James Smarta8cf5df2017-05-15 15:20:46 -07006984 LPFC_NVMET_RQE_DEF_COUNT, i);
James Smart2d7dbc42017-02-12 13:52:35 -08006985 }
6986 }
6987
James Smart895427b2017-02-12 13:52:30 -08006988 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6989 /* register the allocated scsi sgl pool to the port */
6990 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6991 if (unlikely(rc)) {
6992 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6993 "0383 Error %d during scsi sgl post "
6994 "operation\n", rc);
6995 /* Some Scsi buffers were moved to abort scsi list */
6996 /* A pci function reset will repost them */
6997 rc = -ENODEV;
6998 goto out_destroy_queue;
6999 }
James Smartda0436e2009-05-22 14:51:39 -04007000 }
7001
James Smart01649562017-02-12 13:52:32 -08007002 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7003 (phba->nvmet_support == 0)) {
7004
7005 /* register the allocated nvme sgl pool to the port */
7006 rc = lpfc_repost_nvme_sgl_list(phba);
7007 if (unlikely(rc)) {
7008 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7009 "6116 Error %d during nvme sgl post "
7010 "operation\n", rc);
7011 /* Some NVME buffers were moved to abort nvme list */
7012 /* A pci function reset will repost them */
7013 rc = -ENODEV;
7014 goto out_destroy_queue;
7015 }
James Smartda0436e2009-05-22 14:51:39 -04007016 }
7017
7018 /* Post the rpi header region to the device. */
7019 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7020 if (unlikely(rc)) {
7021 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7022 "0393 Error %d during rpi post operation\n",
7023 rc);
7024 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007025 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007026 }
James Smart97f2ecf2012-03-01 22:35:23 -05007027 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007028
James Smart895427b2017-02-12 13:52:30 -08007029 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007030 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007031 /*
7032 * The FC Port needs to register FCFI (index 0)
7033 */
7034 lpfc_reg_fcfi(phba, mboxq);
7035 mboxq->vport = phba->pport;
7036 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7037 if (rc != MBX_SUCCESS)
7038 goto out_unset_queue;
7039 rc = 0;
7040 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7041 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007042 } else {
7043 /* We are a NVME Target mode with MRQ > 1 */
7044
7045 /* First register the FCFI */
7046 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7047 mboxq->vport = phba->pport;
7048 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7049 if (rc != MBX_SUCCESS)
7050 goto out_unset_queue;
7051 rc = 0;
7052 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7053 &mboxq->u.mqe.un.reg_fcfi_mrq);
7054
7055 /* Next register the MRQs */
7056 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7057 mboxq->vport = phba->pport;
7058 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7059 if (rc != MBX_SUCCESS)
7060 goto out_unset_queue;
7061 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007062 }
7063 /* Check if the port is configured to be disabled */
7064 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007065 }
7066
7067 /* Arm the CQs and then EQs on device */
7068 lpfc_sli4_arm_cqeq_intr(phba);
7069
7070 /* Indicate device interrupt mode */
7071 phba->sli4_hba.intr_enable = 1;
7072
7073 /* Allow asynchronous mailbox command to go through */
7074 spin_lock_irq(&phba->hbalock);
7075 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7076 spin_unlock_irq(&phba->hbalock);
7077
7078 /* Post receive buffers to the device */
7079 lpfc_sli4_rb_setup(phba);
7080
James Smartfc2b9892010-02-26 14:15:29 -05007081 /* Reset HBA FCF states after HBA reset */
7082 phba->fcf.fcf_flag = 0;
7083 phba->fcf.current_rec.flag = 0;
7084
James Smartda0436e2009-05-22 14:51:39 -04007085 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007086 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007087 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007088
7089 /* Start heart beat timer */
7090 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007091 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007092 phba->hb_outstanding = 0;
7093 phba->last_completion_time = jiffies;
7094
7095 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007096 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007097 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007098
James Smart75baf692010-06-08 18:31:21 -04007099 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7100 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7101 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7102 if (!rc) {
7103 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7104 "2829 This device supports "
7105 "Advanced Error Reporting (AER)\n");
7106 spin_lock_irq(&phba->hbalock);
7107 phba->hba_flag |= HBA_AER_ENABLED;
7108 spin_unlock_irq(&phba->hbalock);
7109 } else {
7110 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7111 "2830 This device does not support "
7112 "Advanced Error Reporting (AER)\n");
7113 phba->cfg_aer_support = 0;
7114 }
James Smart0a96e972011-07-22 18:37:28 -04007115 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007116 }
7117
James Smartda0436e2009-05-22 14:51:39 -04007118 /*
7119 * The port is ready, set the host's link state to LINK_DOWN
7120 * in preparation for link interrupts.
7121 */
James Smartda0436e2009-05-22 14:51:39 -04007122 spin_lock_irq(&phba->hbalock);
7123 phba->link_state = LPFC_LINK_DOWN;
7124 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007125 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7126 (phba->hba_flag & LINK_DISABLED)) {
7127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7128 "3103 Adapter Link is disabled.\n");
7129 lpfc_down_link(phba, mboxq);
7130 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7131 if (rc != MBX_SUCCESS) {
7132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7133 "3104 Adapter failed to issue "
7134 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7135 goto out_unset_queue;
7136 }
7137 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007138 /* don't perform init_link on SLI4 FC port loopback test */
7139 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7140 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7141 if (rc)
7142 goto out_unset_queue;
7143 }
James Smart5350d872011-10-10 21:33:49 -04007144 }
7145 mempool_free(mboxq, phba->mbox_mem_pool);
7146 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007147out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007148 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007149 lpfc_sli4_queue_unset(phba);
7150out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007151 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007152 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007153out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007154 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007155out_free_mbox:
7156 mempool_free(mboxq, phba->mbox_mem_pool);
7157 return rc;
7158}
James Smarte59058c2008-08-24 21:49:00 -04007159
7160/**
James Smart3621a712009-04-06 18:47:14 -04007161 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007162 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007163 *
James Smarte59058c2008-08-24 21:49:00 -04007164 * This is the callback function for mailbox timer. The mailbox
7165 * timer is armed when a new mailbox command is issued and the timer
7166 * is deleted when the mailbox complete. The function is called by
7167 * the kernel timer code when a mailbox does not complete within
7168 * expected time. This function wakes up the worker thread to
7169 * process the mailbox timeout and returns. All the processing is
7170 * done by the worker thread function lpfc_mbox_timeout_handler.
7171 **/
dea31012005-04-17 16:05:31 -05007172void
7173lpfc_mbox_timeout(unsigned long ptr)
7174{
James Smart92d7f7b2007-06-17 19:56:38 -05007175 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05007176 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007177 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007178
James Smart2e0fef82007-06-17 19:56:36 -05007179 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007180 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007181 if (!tmo_posted)
7182 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7183 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7184
James Smart5e9d9b82008-06-14 22:52:53 -04007185 if (!tmo_posted)
7186 lpfc_worker_wake_up(phba);
7187 return;
dea31012005-04-17 16:05:31 -05007188}
7189
James Smarte8d3c3b2013-10-10 12:21:30 -04007190/**
7191 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7192 * are pending
7193 * @phba: Pointer to HBA context object.
7194 *
7195 * This function checks if any mailbox completions are present on the mailbox
7196 * completion queue.
7197 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007198static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007199lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7200{
7201
7202 uint32_t idx;
7203 struct lpfc_queue *mcq;
7204 struct lpfc_mcqe *mcqe;
7205 bool pending_completions = false;
7206
7207 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7208 return false;
7209
7210 /* Check for completions on mailbox completion queue */
7211
7212 mcq = phba->sli4_hba.mbx_cq;
7213 idx = mcq->hba_index;
7214 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7215 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7216 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7217 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7218 pending_completions = true;
7219 break;
7220 }
7221 idx = (idx + 1) % mcq->entry_count;
7222 if (mcq->hba_index == idx)
7223 break;
7224 }
7225 return pending_completions;
7226
7227}
7228
7229/**
7230 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7231 * that were missed.
7232 * @phba: Pointer to HBA context object.
7233 *
7234 * For sli4, it is possible to miss an interrupt. As such mbox completions
7235 * maybe missed causing erroneous mailbox timeouts to occur. This function
7236 * checks to see if mbox completions are on the mailbox completion queue
7237 * and will process all the completions associated with the eq for the
7238 * mailbox completion queue.
7239 **/
7240bool
7241lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7242{
7243
7244 uint32_t eqidx;
7245 struct lpfc_queue *fpeq = NULL;
7246 struct lpfc_eqe *eqe;
7247 bool mbox_pending;
7248
7249 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7250 return false;
7251
7252 /* Find the eq associated with the mcq */
7253
7254 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007255 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007256 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7257 phba->sli4_hba.mbx_cq->assoc_qid) {
7258 fpeq = phba->sli4_hba.hba_eq[eqidx];
7259 break;
7260 }
7261 if (!fpeq)
7262 return false;
7263
7264 /* Turn off interrupts from this EQ */
7265
7266 lpfc_sli4_eq_clr_intr(fpeq);
7267
7268 /* Check to see if a mbox completion is pending */
7269
7270 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7271
7272 /*
7273 * If a mbox completion is pending, process all the events on EQ
7274 * associated with the mbox completion queue (this could include
7275 * mailbox commands, async events, els commands, receive queue data
7276 * and fcp commands)
7277 */
7278
7279 if (mbox_pending)
7280 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7281 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7282 fpeq->EQ_processed++;
7283 }
7284
7285 /* Always clear and re-arm the EQ */
7286
7287 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7288
7289 return mbox_pending;
7290
7291}
James Smarte59058c2008-08-24 21:49:00 -04007292
7293/**
James Smart3621a712009-04-06 18:47:14 -04007294 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007295 * @phba: Pointer to HBA context object.
7296 *
7297 * This function is called from worker thread when a mailbox command times out.
7298 * The caller is not required to hold any locks. This function will reset the
7299 * HBA and recover all the pending commands.
7300 **/
dea31012005-04-17 16:05:31 -05007301void
7302lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7303{
James Smart2e0fef82007-06-17 19:56:36 -05007304 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007305 MAILBOX_t *mb = NULL;
7306
James Smart1dcb58e2007-04-25 09:51:30 -04007307 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007308
James Smarte8d3c3b2013-10-10 12:21:30 -04007309 /* If the mailbox completed, process the completion and return */
7310 if (lpfc_sli4_process_missed_mbox_completions(phba))
7311 return;
7312
James Smarteb016562014-09-03 12:58:06 -04007313 if (pmbox != NULL)
7314 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007315 /* Check the pmbox pointer first. There is a race condition
7316 * between the mbox timeout handler getting executed in the
7317 * worklist and the mailbox actually completing. When this
7318 * race condition occurs, the mbox_active will be NULL.
7319 */
7320 spin_lock_irq(&phba->hbalock);
7321 if (pmbox == NULL) {
7322 lpfc_printf_log(phba, KERN_WARNING,
7323 LOG_MBOX | LOG_SLI,
7324 "0353 Active Mailbox cleared - mailbox timeout "
7325 "exiting\n");
7326 spin_unlock_irq(&phba->hbalock);
7327 return;
7328 }
7329
dea31012005-04-17 16:05:31 -05007330 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007331 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007332 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007333 mb->mbxCommand,
7334 phba->pport->port_state,
7335 phba->sli.sli_flag,
7336 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007337 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007338
James Smart1dcb58e2007-04-25 09:51:30 -04007339 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7340 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007341 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007342 */
James Smart2e0fef82007-06-17 19:56:36 -05007343 spin_lock_irq(&phba->pport->work_port_lock);
7344 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7345 spin_unlock_irq(&phba->pport->work_port_lock);
7346 spin_lock_irq(&phba->hbalock);
7347 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007348 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007349 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007350
James Smartdb55fba2014-04-04 13:52:02 -04007351 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007352
7353 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007354 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007355
7356 /* Reset the HBA device */
7357 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007358}
7359
James Smarte59058c2008-08-24 21:49:00 -04007360/**
James Smart3772a992009-05-22 14:50:54 -04007361 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007362 * @phba: Pointer to HBA context object.
7363 * @pmbox: Pointer to mailbox object.
7364 * @flag: Flag indicating how the mailbox need to be processed.
7365 *
7366 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007367 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7368 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007369 * The mailbox command can be submitted in polling mode, in which case
7370 * this function will wait in a polling loop for the completion of the
7371 * mailbox.
7372 * If the mailbox is submitted in no_wait mode (not polling) the
7373 * function will submit the command and returns immediately without waiting
7374 * for the mailbox completion. The no_wait is supported only when HBA
7375 * is in SLI2/SLI3 mode - interrupts are enabled.
7376 * The SLI interface allows only one mailbox pending at a time. If the
7377 * mailbox is issued in polling mode and there is already a mailbox
7378 * pending, then the function will return an error. If the mailbox is issued
7379 * in NO_WAIT mode and there is a mailbox pending already, the function
7380 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7381 * The sli layer owns the mailbox object until the completion of mailbox
7382 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7383 * return codes the caller owns the mailbox command after the return of
7384 * the function.
7385 **/
James Smart3772a992009-05-22 14:50:54 -04007386static int
7387lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7388 uint32_t flag)
dea31012005-04-17 16:05:31 -05007389{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007390 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007391 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007392 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007393 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007394 int i;
James Smart09372822008-01-11 01:52:54 -05007395 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007396 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007397 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007398 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007399 int processing_queue = 0;
7400
7401 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7402 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007403 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007404 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007405 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7406 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7407 return MBX_SUCCESS;
7408 }
James Smart58da1ff2008-04-07 10:15:56 -04007409 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007410 pmbox = lpfc_mbox_get(phba);
7411 if (!pmbox) {
7412 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7413 return MBX_SUCCESS;
7414 }
7415 }
dea31012005-04-17 16:05:31 -05007416
James Smarted957682007-06-17 19:56:37 -05007417 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007418 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007419 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007420 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007421 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007422 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007423 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007424 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007425 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007426 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007427 }
7428 }
7429
Linas Vepstas8d63f372007-02-14 14:28:36 -06007430 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007431 if (unlikely(pci_channel_offline(phba->pcidev))) {
7432 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7433 goto out_not_finished;
7434 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007435
James Smarta257bf92009-04-06 18:48:10 -04007436 /* If HBA has a deferred error attention, fail the iocb. */
7437 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7438 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7439 goto out_not_finished;
7440 }
7441
dea31012005-04-17 16:05:31 -05007442 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007443
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007444 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007445 status = MBX_SUCCESS;
7446
James Smart2e0fef82007-06-17 19:56:36 -05007447 if (phba->link_state == LPFC_HBA_ERROR) {
7448 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007449
7450 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007451 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7452 "(%d):0311 Mailbox command x%x cannot "
7453 "issue Data: x%x x%x\n",
7454 pmbox->vport ? pmbox->vport->vpi : 0,
7455 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007456 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007457 }
7458
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007459 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007460 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7461 !(hc_copy & HC_MBINT_ENA)) {
7462 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7463 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007464 "(%d):2528 Mailbox command x%x cannot "
7465 "issue Data: x%x x%x\n",
7466 pmbox->vport ? pmbox->vport->vpi : 0,
7467 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007468 goto out_not_finished;
7469 }
James Smart92908312006-03-07 15:04:13 -05007470 }
7471
dea31012005-04-17 16:05:31 -05007472 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7473 /* Polling for a mbox command when another one is already active
7474 * is not allowed in SLI. Also, the driver must have established
7475 * SLI2 mode to queue and process multiple mbox commands.
7476 */
7477
7478 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007479 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007480
7481 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007482 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7483 "(%d):2529 Mailbox command x%x "
7484 "cannot issue Data: x%x x%x\n",
7485 pmbox->vport ? pmbox->vport->vpi : 0,
7486 pmbox->u.mb.mbxCommand,
7487 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007488 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007489 }
7490
James Smart3772a992009-05-22 14:50:54 -04007491 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007492 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007493 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007494 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7495 "(%d):2530 Mailbox command x%x "
7496 "cannot issue Data: x%x x%x\n",
7497 pmbox->vport ? pmbox->vport->vpi : 0,
7498 pmbox->u.mb.mbxCommand,
7499 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007500 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007501 }
7502
dea31012005-04-17 16:05:31 -05007503 /* Another mailbox command is still being processed, queue this
7504 * command to be processed later.
7505 */
7506 lpfc_mbox_put(phba, pmbox);
7507
7508 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007509 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007510 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007511 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007512 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007513 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007514 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007515
7516 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007517 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007518
James Smart858c9f62007-06-17 19:56:39 -05007519 if (pmbox->vport) {
7520 lpfc_debugfs_disc_trc(pmbox->vport,
7521 LPFC_DISC_TRC_MBOX_VPORT,
7522 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007523 (uint32_t)mbx->mbxCommand,
7524 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007525 }
7526 else {
7527 lpfc_debugfs_disc_trc(phba->pport,
7528 LPFC_DISC_TRC_MBOX,
7529 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007530 (uint32_t)mbx->mbxCommand,
7531 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007532 }
7533
James Smart2e0fef82007-06-17 19:56:36 -05007534 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007535 }
7536
dea31012005-04-17 16:05:31 -05007537 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7538
7539 /* If we are not polling, we MUST be in SLI2 mode */
7540 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007541 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007542 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007543 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007544 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007545 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007546 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7547 "(%d):2531 Mailbox command x%x "
7548 "cannot issue Data: x%x x%x\n",
7549 pmbox->vport ? pmbox->vport->vpi : 0,
7550 pmbox->u.mb.mbxCommand,
7551 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007552 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007553 }
7554 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007555 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7556 1000);
7557 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007558 }
7559
7560 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007561 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007562 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007563 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007564 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007565 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007566 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007567
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007568 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007569 if (pmbox->vport) {
7570 lpfc_debugfs_disc_trc(pmbox->vport,
7571 LPFC_DISC_TRC_MBOX_VPORT,
7572 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007573 (uint32_t)mbx->mbxCommand,
7574 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007575 }
7576 else {
7577 lpfc_debugfs_disc_trc(phba->pport,
7578 LPFC_DISC_TRC_MBOX,
7579 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007580 (uint32_t)mbx->mbxCommand,
7581 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007582 }
7583 }
7584
dea31012005-04-17 16:05:31 -05007585 psli->slistat.mbox_cmd++;
7586 evtctr = psli->slistat.mbox_event;
7587
7588 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007589 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007590
James Smart3772a992009-05-22 14:50:54 -04007591 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007592 /* Populate mbox extension offset word. */
7593 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007594 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007595 = (uint8_t *)phba->mbox_ext
7596 - (uint8_t *)phba->mbox;
7597 }
7598
7599 /* Copy the mailbox extension data */
7600 if (pmbox->in_ext_byte_len && pmbox->context2) {
7601 lpfc_sli_pcimem_bcopy(pmbox->context2,
7602 (uint8_t *)phba->mbox_ext,
7603 pmbox->in_ext_byte_len);
7604 }
7605 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007606 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007607 } else {
James Smart7a470272010-03-15 11:25:20 -04007608 /* Populate mbox extension offset word. */
7609 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007610 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007611 = MAILBOX_HBA_EXT_OFFSET;
7612
7613 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007614 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007615 lpfc_memcpy_to_slim(phba->MBslimaddr +
7616 MAILBOX_HBA_EXT_OFFSET,
7617 pmbox->context2, pmbox->in_ext_byte_len);
7618
James Smart895427b2017-02-12 13:52:30 -08007619 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007620 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007621 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7622 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007623
7624 /* First copy mbox command data to HBA SLIM, skip past first
7625 word */
7626 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007627 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007628 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7629
7630 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007631 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007632 to_slim = phba->MBslimaddr;
7633 writel(ldata, to_slim);
7634 readl(to_slim); /* flush */
7635
James Smart895427b2017-02-12 13:52:30 -08007636 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007637 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007638 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007639 }
7640
7641 wmb();
dea31012005-04-17 16:05:31 -05007642
7643 switch (flag) {
7644 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007645 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007646 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007647 /* Interrupt board to do it */
7648 writel(CA_MBATT, phba->CAregaddr);
7649 readl(phba->CAregaddr); /* flush */
7650 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007651 break;
7652
7653 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007654 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007655 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007656 /* Interrupt board to do it */
7657 writel(CA_MBATT, phba->CAregaddr);
7658 readl(phba->CAregaddr); /* flush */
7659
James Smart3772a992009-05-22 14:50:54 -04007660 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007661 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007662 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007663 word0 = le32_to_cpu(word0);
7664 } else {
7665 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007666 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7667 spin_unlock_irqrestore(&phba->hbalock,
7668 drvr_flag);
7669 goto out_not_finished;
7670 }
dea31012005-04-17 16:05:31 -05007671 }
7672
7673 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007674 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7675 spin_unlock_irqrestore(&phba->hbalock,
7676 drvr_flag);
7677 goto out_not_finished;
7678 }
James Smarta183a152011-10-10 21:32:43 -04007679 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7680 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007681 i = 0;
dea31012005-04-17 16:05:31 -05007682 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007683 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7684 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007685 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007686 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007687 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007688 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007689 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007690 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007691 }
7692
7693 /* Check if we took a mbox interrupt while we were
7694 polling */
7695 if (((word0 & OWN_CHIP) != OWN_CHIP)
7696 && (evtctr != psli->slistat.mbox_event))
7697 break;
7698
James Smart09372822008-01-11 01:52:54 -05007699 if (i++ > 10) {
7700 spin_unlock_irqrestore(&phba->hbalock,
7701 drvr_flag);
7702 msleep(1);
7703 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7704 }
dea31012005-04-17 16:05:31 -05007705
James Smart3772a992009-05-22 14:50:54 -04007706 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007707 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007708 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007709 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007710 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007711 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007712 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007713 /* Check real SLIM for any errors */
7714 slimword0 = readl(phba->MBslimaddr);
7715 slimmb = (MAILBOX_t *) & slimword0;
7716 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7717 && slimmb->mbxStatus) {
7718 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007719 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007720 word0 = slimword0;
7721 }
7722 }
7723 } else {
7724 /* First copy command data */
7725 word0 = readl(phba->MBslimaddr);
7726 }
7727 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007728 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7729 spin_unlock_irqrestore(&phba->hbalock,
7730 drvr_flag);
7731 goto out_not_finished;
7732 }
dea31012005-04-17 16:05:31 -05007733 }
7734
James Smart3772a992009-05-22 14:50:54 -04007735 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007736 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007737 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7738 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007739 /* Copy the mailbox extension data */
7740 if (pmbox->out_ext_byte_len && pmbox->context2) {
7741 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7742 pmbox->context2,
7743 pmbox->out_ext_byte_len);
7744 }
dea31012005-04-17 16:05:31 -05007745 } else {
7746 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007747 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007748 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007749 /* Copy the mailbox extension data */
7750 if (pmbox->out_ext_byte_len && pmbox->context2) {
7751 lpfc_memcpy_from_slim(pmbox->context2,
7752 phba->MBslimaddr +
7753 MAILBOX_HBA_EXT_OFFSET,
7754 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007755 }
7756 }
7757
7758 writel(HA_MBATT, phba->HAregaddr);
7759 readl(phba->HAregaddr); /* flush */
7760
7761 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007762 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007763 }
7764
James Smart2e0fef82007-06-17 19:56:36 -05007765 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7766 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007767
7768out_not_finished:
7769 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007770 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007771 lpfc_mbox_cmpl_put(phba, pmbox);
7772 }
7773 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007774}
7775
James Smarte59058c2008-08-24 21:49:00 -04007776/**
James Smartf1126682009-06-10 17:22:44 -04007777 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7778 * @phba: Pointer to HBA context object.
7779 *
7780 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7781 * the driver internal pending mailbox queue. It will then try to wait out the
7782 * possible outstanding mailbox command before return.
7783 *
7784 * Returns:
7785 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7786 * the outstanding mailbox command timed out.
7787 **/
7788static int
7789lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7790{
7791 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007792 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007793 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007794
7795 /* Mark the asynchronous mailbox command posting as blocked */
7796 spin_lock_irq(&phba->hbalock);
7797 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007798 /* Determine how long we might wait for the active mailbox
7799 * command to be gracefully completed by firmware.
7800 */
James Smarta183a152011-10-10 21:32:43 -04007801 if (phba->sli.mbox_active)
7802 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7803 phba->sli.mbox_active) *
7804 1000) + jiffies;
7805 spin_unlock_irq(&phba->hbalock);
7806
James Smarte8d3c3b2013-10-10 12:21:30 -04007807 /* Make sure the mailbox is really active */
7808 if (timeout)
7809 lpfc_sli4_process_missed_mbox_completions(phba);
7810
James Smartf1126682009-06-10 17:22:44 -04007811 /* Wait for the outstnading mailbox command to complete */
7812 while (phba->sli.mbox_active) {
7813 /* Check active mailbox complete status every 2ms */
7814 msleep(2);
7815 if (time_after(jiffies, timeout)) {
7816 /* Timeout, marked the outstanding cmd not complete */
7817 rc = 1;
7818 break;
7819 }
7820 }
7821
7822 /* Can not cleanly block async mailbox command, fails it */
7823 if (rc) {
7824 spin_lock_irq(&phba->hbalock);
7825 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7826 spin_unlock_irq(&phba->hbalock);
7827 }
7828 return rc;
7829}
7830
7831/**
7832 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7833 * @phba: Pointer to HBA context object.
7834 *
7835 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7836 * commands from the driver internal pending mailbox queue. It makes sure
7837 * that there is no outstanding mailbox command before resuming posting
7838 * asynchronous mailbox commands. If, for any reason, there is outstanding
7839 * mailbox command, it will try to wait it out before resuming asynchronous
7840 * mailbox command posting.
7841 **/
7842static void
7843lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7844{
7845 struct lpfc_sli *psli = &phba->sli;
7846
7847 spin_lock_irq(&phba->hbalock);
7848 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7849 /* Asynchronous mailbox posting is not blocked, do nothing */
7850 spin_unlock_irq(&phba->hbalock);
7851 return;
7852 }
7853
7854 /* Outstanding synchronous mailbox command is guaranteed to be done,
7855 * successful or timeout, after timing-out the outstanding mailbox
7856 * command shall always be removed, so just unblock posting async
7857 * mailbox command and resume
7858 */
7859 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7860 spin_unlock_irq(&phba->hbalock);
7861
7862 /* wake up worker thread to post asynchronlous mailbox command */
7863 lpfc_worker_wake_up(phba);
7864}
7865
7866/**
James Smart2d843ed2012-09-29 11:29:06 -04007867 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7868 * @phba: Pointer to HBA context object.
7869 * @mboxq: Pointer to mailbox object.
7870 *
7871 * The function waits for the bootstrap mailbox register ready bit from
7872 * port for twice the regular mailbox command timeout value.
7873 *
7874 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7875 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7876 **/
7877static int
7878lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7879{
7880 uint32_t db_ready;
7881 unsigned long timeout;
7882 struct lpfc_register bmbx_reg;
7883
7884 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7885 * 1000) + jiffies;
7886
7887 do {
7888 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7889 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7890 if (!db_ready)
7891 msleep(2);
7892
7893 if (time_after(jiffies, timeout))
7894 return MBXERR_ERROR;
7895 } while (!db_ready);
7896
7897 return 0;
7898}
7899
7900/**
James Smartda0436e2009-05-22 14:51:39 -04007901 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7902 * @phba: Pointer to HBA context object.
7903 * @mboxq: Pointer to mailbox object.
7904 *
7905 * The function posts a mailbox to the port. The mailbox is expected
7906 * to be comletely filled in and ready for the port to operate on it.
7907 * This routine executes a synchronous completion operation on the
7908 * mailbox by polling for its completion.
7909 *
7910 * The caller must not be holding any locks when calling this routine.
7911 *
7912 * Returns:
7913 * MBX_SUCCESS - mailbox posted successfully
7914 * Any of the MBX error values.
7915 **/
7916static int
7917lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7918{
7919 int rc = MBX_SUCCESS;
7920 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007921 uint32_t mcqe_status;
7922 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007923 struct lpfc_sli *psli = &phba->sli;
7924 struct lpfc_mqe *mb = &mboxq->u.mqe;
7925 struct lpfc_bmbx_create *mbox_rgn;
7926 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007927
7928 /*
7929 * Only one mailbox can be active to the bootstrap mailbox region
7930 * at a time and there is no queueing provided.
7931 */
7932 spin_lock_irqsave(&phba->hbalock, iflag);
7933 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7934 spin_unlock_irqrestore(&phba->hbalock, iflag);
7935 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007936 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007937 "cannot issue Data: x%x x%x\n",
7938 mboxq->vport ? mboxq->vport->vpi : 0,
7939 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007940 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7941 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007942 psli->sli_flag, MBX_POLL);
7943 return MBXERR_ERROR;
7944 }
7945 /* The server grabs the token and owns it until release */
7946 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7947 phba->sli.mbox_active = mboxq;
7948 spin_unlock_irqrestore(&phba->hbalock, iflag);
7949
James Smart2d843ed2012-09-29 11:29:06 -04007950 /* wait for bootstrap mbox register for readyness */
7951 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7952 if (rc)
7953 goto exit;
7954
James Smartda0436e2009-05-22 14:51:39 -04007955 /*
7956 * Initialize the bootstrap memory region to avoid stale data areas
7957 * in the mailbox post. Then copy the caller's mailbox contents to
7958 * the bmbx mailbox region.
7959 */
7960 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7961 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7962 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7963 sizeof(struct lpfc_mqe));
7964
7965 /* Post the high mailbox dma address to the port and wait for ready. */
7966 dma_address = &phba->sli4_hba.bmbx.dma_address;
7967 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7968
James Smart2d843ed2012-09-29 11:29:06 -04007969 /* wait for bootstrap mbox register for hi-address write done */
7970 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7971 if (rc)
7972 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007973
7974 /* Post the low mailbox dma address to the port. */
7975 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007976
James Smart2d843ed2012-09-29 11:29:06 -04007977 /* wait for bootstrap mbox register for low address write done */
7978 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7979 if (rc)
7980 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007981
7982 /*
7983 * Read the CQ to ensure the mailbox has completed.
7984 * If so, update the mailbox status so that the upper layers
7985 * can complete the request normally.
7986 */
7987 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7988 sizeof(struct lpfc_mqe));
7989 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7990 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7991 sizeof(struct lpfc_mcqe));
7992 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007993 /*
7994 * When the CQE status indicates a failure and the mailbox status
7995 * indicates success then copy the CQE status into the mailbox status
7996 * (and prefix it with x4000).
7997 */
James Smartda0436e2009-05-22 14:51:39 -04007998 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007999 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8000 bf_set(lpfc_mqe_status, mb,
8001 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008002 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008003 } else
8004 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008005
8006 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008007 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008008 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8009 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008010 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8011 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8012 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008013 bf_get(lpfc_mqe_status, mb),
8014 mb->un.mb_words[0], mb->un.mb_words[1],
8015 mb->un.mb_words[2], mb->un.mb_words[3],
8016 mb->un.mb_words[4], mb->un.mb_words[5],
8017 mb->un.mb_words[6], mb->un.mb_words[7],
8018 mb->un.mb_words[8], mb->un.mb_words[9],
8019 mb->un.mb_words[10], mb->un.mb_words[11],
8020 mb->un.mb_words[12], mboxq->mcqe.word0,
8021 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8022 mboxq->mcqe.trailer);
8023exit:
8024 /* We are holding the token, no needed for lock when release */
8025 spin_lock_irqsave(&phba->hbalock, iflag);
8026 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8027 phba->sli.mbox_active = NULL;
8028 spin_unlock_irqrestore(&phba->hbalock, iflag);
8029 return rc;
8030}
8031
8032/**
8033 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8034 * @phba: Pointer to HBA context object.
8035 * @pmbox: Pointer to mailbox object.
8036 * @flag: Flag indicating how the mailbox need to be processed.
8037 *
8038 * This function is called by discovery code and HBA management code to submit
8039 * a mailbox command to firmware with SLI-4 interface spec.
8040 *
8041 * Return codes the caller owns the mailbox command after the return of the
8042 * function.
8043 **/
8044static int
8045lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8046 uint32_t flag)
8047{
8048 struct lpfc_sli *psli = &phba->sli;
8049 unsigned long iflags;
8050 int rc;
8051
James Smartb76f2dc2011-07-22 18:37:42 -04008052 /* dump from issue mailbox command if setup */
8053 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8054
James Smart8fa38512009-07-19 10:01:03 -04008055 rc = lpfc_mbox_dev_check(phba);
8056 if (unlikely(rc)) {
8057 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008058 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008059 "cannot issue Data: x%x x%x\n",
8060 mboxq->vport ? mboxq->vport->vpi : 0,
8061 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008062 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8063 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008064 psli->sli_flag, flag);
8065 goto out_not_finished;
8066 }
8067
James Smartda0436e2009-05-22 14:51:39 -04008068 /* Detect polling mode and jump to a handler */
8069 if (!phba->sli4_hba.intr_enable) {
8070 if (flag == MBX_POLL)
8071 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8072 else
8073 rc = -EIO;
8074 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008075 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008076 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008077 "(x%x/x%x) failure: "
8078 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8079 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008080 mboxq->vport ? mboxq->vport->vpi : 0,
8081 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008082 lpfc_sli_config_mbox_subsys_get(phba,
8083 mboxq),
8084 lpfc_sli_config_mbox_opcode_get(phba,
8085 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008086 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8087 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8088 bf_get(lpfc_mcqe_ext_status,
8089 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008090 psli->sli_flag, flag);
8091 return rc;
8092 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008093 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8094 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04008095 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04008096 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008097 mboxq->vport ? mboxq->vport->vpi : 0,
8098 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008099 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8100 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008101 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008102 /* Try to block the asynchronous mailbox posting */
8103 rc = lpfc_sli4_async_mbox_block(phba);
8104 if (!rc) {
8105 /* Successfully blocked, now issue sync mbox cmd */
8106 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8107 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008108 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008109 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008110 "(%d):2597 Sync Mailbox command "
8111 "x%x (x%x/x%x) failure: "
8112 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8113 "Data: x%x x%x\n,",
8114 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008115 mboxq->u.mb.mbxCommand,
8116 lpfc_sli_config_mbox_subsys_get(phba,
8117 mboxq),
8118 lpfc_sli_config_mbox_opcode_get(phba,
8119 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008120 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8121 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8122 bf_get(lpfc_mcqe_ext_status,
8123 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008124 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008125 /* Unblock the async mailbox posting afterward */
8126 lpfc_sli4_async_mbox_unblock(phba);
8127 }
8128 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008129 }
8130
8131 /* Now, interrupt mode asynchrous mailbox command */
8132 rc = lpfc_mbox_cmd_check(phba, mboxq);
8133 if (rc) {
8134 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008135 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008136 "cannot issue Data: x%x x%x\n",
8137 mboxq->vport ? mboxq->vport->vpi : 0,
8138 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008139 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8140 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008141 psli->sli_flag, flag);
8142 goto out_not_finished;
8143 }
James Smartda0436e2009-05-22 14:51:39 -04008144
8145 /* Put the mailbox command to the driver internal FIFO */
8146 psli->slistat.mbox_busy++;
8147 spin_lock_irqsave(&phba->hbalock, iflags);
8148 lpfc_mbox_put(phba, mboxq);
8149 spin_unlock_irqrestore(&phba->hbalock, iflags);
8150 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8151 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008152 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008153 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8154 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008155 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8156 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008157 phba->pport->port_state,
8158 psli->sli_flag, MBX_NOWAIT);
8159 /* Wake up worker thread to transport mailbox command from head */
8160 lpfc_worker_wake_up(phba);
8161
8162 return MBX_BUSY;
8163
8164out_not_finished:
8165 return MBX_NOT_FINISHED;
8166}
8167
8168/**
8169 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8170 * @phba: Pointer to HBA context object.
8171 *
8172 * This function is called by worker thread to send a mailbox command to
8173 * SLI4 HBA firmware.
8174 *
8175 **/
8176int
8177lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8178{
8179 struct lpfc_sli *psli = &phba->sli;
8180 LPFC_MBOXQ_t *mboxq;
8181 int rc = MBX_SUCCESS;
8182 unsigned long iflags;
8183 struct lpfc_mqe *mqe;
8184 uint32_t mbx_cmnd;
8185
8186 /* Check interrupt mode before post async mailbox command */
8187 if (unlikely(!phba->sli4_hba.intr_enable))
8188 return MBX_NOT_FINISHED;
8189
8190 /* Check for mailbox command service token */
8191 spin_lock_irqsave(&phba->hbalock, iflags);
8192 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8193 spin_unlock_irqrestore(&phba->hbalock, iflags);
8194 return MBX_NOT_FINISHED;
8195 }
8196 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8197 spin_unlock_irqrestore(&phba->hbalock, iflags);
8198 return MBX_NOT_FINISHED;
8199 }
8200 if (unlikely(phba->sli.mbox_active)) {
8201 spin_unlock_irqrestore(&phba->hbalock, iflags);
8202 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8203 "0384 There is pending active mailbox cmd\n");
8204 return MBX_NOT_FINISHED;
8205 }
8206 /* Take the mailbox command service token */
8207 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8208
8209 /* Get the next mailbox command from head of queue */
8210 mboxq = lpfc_mbox_get(phba);
8211
8212 /* If no more mailbox command waiting for post, we're done */
8213 if (!mboxq) {
8214 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8215 spin_unlock_irqrestore(&phba->hbalock, iflags);
8216 return MBX_SUCCESS;
8217 }
8218 phba->sli.mbox_active = mboxq;
8219 spin_unlock_irqrestore(&phba->hbalock, iflags);
8220
8221 /* Check device readiness for posting mailbox command */
8222 rc = lpfc_mbox_dev_check(phba);
8223 if (unlikely(rc))
8224 /* Driver clean routine will clean up pending mailbox */
8225 goto out_not_finished;
8226
8227 /* Prepare the mbox command to be posted */
8228 mqe = &mboxq->u.mqe;
8229 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8230
8231 /* Start timer for the mbox_tmo and log some mailbox post messages */
8232 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008233 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008234
8235 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008236 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008237 "x%x x%x\n",
8238 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008239 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8240 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008241 phba->pport->port_state, psli->sli_flag);
8242
8243 if (mbx_cmnd != MBX_HEARTBEAT) {
8244 if (mboxq->vport) {
8245 lpfc_debugfs_disc_trc(mboxq->vport,
8246 LPFC_DISC_TRC_MBOX_VPORT,
8247 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8248 mbx_cmnd, mqe->un.mb_words[0],
8249 mqe->un.mb_words[1]);
8250 } else {
8251 lpfc_debugfs_disc_trc(phba->pport,
8252 LPFC_DISC_TRC_MBOX,
8253 "MBOX Send: cmd:x%x mb:x%x x%x",
8254 mbx_cmnd, mqe->un.mb_words[0],
8255 mqe->un.mb_words[1]);
8256 }
8257 }
8258 psli->slistat.mbox_cmd++;
8259
8260 /* Post the mailbox command to the port */
8261 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8262 if (rc != MBX_SUCCESS) {
8263 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008264 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008265 "cannot issue Data: x%x x%x\n",
8266 mboxq->vport ? mboxq->vport->vpi : 0,
8267 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008268 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8269 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008270 psli->sli_flag, MBX_NOWAIT);
8271 goto out_not_finished;
8272 }
8273
8274 return rc;
8275
8276out_not_finished:
8277 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008278 if (phba->sli.mbox_active) {
8279 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8280 __lpfc_mbox_cmpl_put(phba, mboxq);
8281 /* Release the token */
8282 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8283 phba->sli.mbox_active = NULL;
8284 }
James Smartda0436e2009-05-22 14:51:39 -04008285 spin_unlock_irqrestore(&phba->hbalock, iflags);
8286
8287 return MBX_NOT_FINISHED;
8288}
8289
8290/**
8291 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8292 * @phba: Pointer to HBA context object.
8293 * @pmbox: Pointer to mailbox object.
8294 * @flag: Flag indicating how the mailbox need to be processed.
8295 *
8296 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8297 * the API jump table function pointer from the lpfc_hba struct.
8298 *
8299 * Return codes the caller owns the mailbox command after the return of the
8300 * function.
8301 **/
8302int
8303lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8304{
8305 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8306}
8307
8308/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008309 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008310 * @phba: The hba struct for which this call is being executed.
8311 * @dev_grp: The HBA PCI-Device group number.
8312 *
8313 * This routine sets up the mbox interface API function jump table in @phba
8314 * struct.
8315 * Returns: 0 - success, -ENODEV - failure.
8316 **/
8317int
8318lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8319{
8320
8321 switch (dev_grp) {
8322 case LPFC_PCI_DEV_LP:
8323 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8324 phba->lpfc_sli_handle_slow_ring_event =
8325 lpfc_sli_handle_slow_ring_event_s3;
8326 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8327 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8328 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8329 break;
8330 case LPFC_PCI_DEV_OC:
8331 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8332 phba->lpfc_sli_handle_slow_ring_event =
8333 lpfc_sli_handle_slow_ring_event_s4;
8334 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8335 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8336 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8337 break;
8338 default:
8339 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8340 "1420 Invalid HBA PCI-device group: 0x%x\n",
8341 dev_grp);
8342 return -ENODEV;
8343 break;
8344 }
8345 return 0;
8346}
8347
8348/**
James Smart3621a712009-04-06 18:47:14 -04008349 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008350 * @phba: Pointer to HBA context object.
8351 * @pring: Pointer to driver SLI ring object.
8352 * @piocb: Pointer to address of newly added command iocb.
8353 *
8354 * This function is called with hbalock held to add a command
8355 * iocb to the txq when SLI layer cannot submit the command iocb
8356 * to the ring.
8357 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008358void
James Smart92d7f7b2007-06-17 19:56:38 -05008359__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008360 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008361{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008362 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008363 /* Insert the caller's iocb in the txq tail for later processing. */
8364 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008365}
8366
James Smarte59058c2008-08-24 21:49:00 -04008367/**
James Smart3621a712009-04-06 18:47:14 -04008368 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008369 * @phba: Pointer to HBA context object.
8370 * @pring: Pointer to driver SLI ring object.
8371 * @piocb: Pointer to address of newly added command iocb.
8372 *
8373 * This function is called with hbalock held before a new
8374 * iocb is submitted to the firmware. This function checks
8375 * txq to flush the iocbs in txq to Firmware before
8376 * submitting new iocbs to the Firmware.
8377 * If there are iocbs in the txq which need to be submitted
8378 * to firmware, lpfc_sli_next_iocb returns the first element
8379 * of the txq after dequeuing it from txq.
8380 * If there is no iocb in the txq then the function will return
8381 * *piocb and *piocb is set to NULL. Caller needs to check
8382 * *piocb to find if there are more commands in the txq.
8383 **/
dea31012005-04-17 16:05:31 -05008384static struct lpfc_iocbq *
8385lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008386 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008387{
8388 struct lpfc_iocbq * nextiocb;
8389
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008390 lockdep_assert_held(&phba->hbalock);
8391
dea31012005-04-17 16:05:31 -05008392 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8393 if (!nextiocb) {
8394 nextiocb = *piocb;
8395 *piocb = NULL;
8396 }
8397
8398 return nextiocb;
8399}
8400
James Smarte59058c2008-08-24 21:49:00 -04008401/**
James Smart3772a992009-05-22 14:50:54 -04008402 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008403 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008404 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008405 * @piocb: Pointer to command iocb.
8406 * @flag: Flag indicating if this command can be put into txq.
8407 *
James Smart3772a992009-05-22 14:50:54 -04008408 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8409 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8410 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8411 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8412 * this function allows only iocbs for posting buffers. This function finds
8413 * next available slot in the command ring and posts the command to the
8414 * available slot and writes the port attention register to request HBA start
8415 * processing new iocb. If there is no slot available in the ring and
8416 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8417 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008418 *
James Smart3772a992009-05-22 14:50:54 -04008419 * This function is called with hbalock held. The function will return success
8420 * after it successfully submit the iocb to firmware or after adding to the
8421 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008422 **/
James Smart98c9ea52007-10-27 13:37:33 -04008423static int
James Smart3772a992009-05-22 14:50:54 -04008424__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008425 struct lpfc_iocbq *piocb, uint32_t flag)
8426{
8427 struct lpfc_iocbq *nextiocb;
8428 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008429 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008430
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008431 lockdep_assert_held(&phba->hbalock);
8432
James Smart92d7f7b2007-06-17 19:56:38 -05008433 if (piocb->iocb_cmpl && (!piocb->vport) &&
8434 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8435 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8436 lpfc_printf_log(phba, KERN_ERR,
8437 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008438 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008439 piocb->iocb.ulpCommand);
8440 dump_stack();
8441 return IOCB_ERROR;
8442 }
8443
8444
Linas Vepstas8d63f372007-02-14 14:28:36 -06008445 /* If the PCI channel is in offline state, do not post iocbs. */
8446 if (unlikely(pci_channel_offline(phba->pcidev)))
8447 return IOCB_ERROR;
8448
James Smarta257bf92009-04-06 18:48:10 -04008449 /* If HBA has a deferred error attention, fail the iocb. */
8450 if (unlikely(phba->hba_flag & DEFER_ERATT))
8451 return IOCB_ERROR;
8452
dea31012005-04-17 16:05:31 -05008453 /*
8454 * We should never get an IOCB if we are in a < LINK_DOWN state
8455 */
James Smart2e0fef82007-06-17 19:56:36 -05008456 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008457 return IOCB_ERROR;
8458
8459 /*
8460 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008461 * outstanding event.
dea31012005-04-17 16:05:31 -05008462 */
James Smart0b727fe2007-10-27 13:37:25 -04008463 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008464 goto iocb_busy;
8465
James Smart2e0fef82007-06-17 19:56:36 -05008466 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008467 /*
James Smart2680eea2007-04-25 09:52:55 -04008468 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008469 * can be issued if the link is not up.
8470 */
8471 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008472 case CMD_GEN_REQUEST64_CR:
8473 case CMD_GEN_REQUEST64_CX:
8474 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8475 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008476 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008477 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8478 MENLO_TRANSPORT_TYPE))
8479
8480 goto iocb_busy;
8481 break;
dea31012005-04-17 16:05:31 -05008482 case CMD_QUE_RING_BUF_CN:
8483 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008484 /*
8485 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8486 * completion, iocb_cmpl MUST be 0.
8487 */
8488 if (piocb->iocb_cmpl)
8489 piocb->iocb_cmpl = NULL;
8490 /*FALLTHROUGH*/
8491 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008492 case CMD_CLOSE_XRI_CN:
8493 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008494 break;
8495 default:
8496 goto iocb_busy;
8497 }
8498
8499 /*
8500 * For FCP commands, we must be in a state where we can process link
8501 * attention events.
8502 */
James Smart895427b2017-02-12 13:52:30 -08008503 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008504 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008505 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008506 }
dea31012005-04-17 16:05:31 -05008507
dea31012005-04-17 16:05:31 -05008508 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8509 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8510 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8511
8512 if (iocb)
8513 lpfc_sli_update_ring(phba, pring);
8514 else
8515 lpfc_sli_update_full_ring(phba, pring);
8516
8517 if (!piocb)
8518 return IOCB_SUCCESS;
8519
8520 goto out_busy;
8521
8522 iocb_busy:
8523 pring->stats.iocb_cmd_delay++;
8524
8525 out_busy:
8526
8527 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008528 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008529 return IOCB_SUCCESS;
8530 }
8531
8532 return IOCB_BUSY;
8533}
8534
James Smart3772a992009-05-22 14:50:54 -04008535/**
James Smart4f774512009-05-22 14:52:35 -04008536 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8537 * @phba: Pointer to HBA context object.
8538 * @piocb: Pointer to command iocb.
8539 * @sglq: Pointer to the scatter gather queue object.
8540 *
8541 * This routine converts the bpl or bde that is in the IOCB
8542 * to a sgl list for the sli4 hardware. The physical address
8543 * of the bpl/bde is converted back to a virtual address.
8544 * If the IOCB contains a BPL then the list of BDE's is
8545 * converted to sli4_sge's. If the IOCB contains a single
8546 * BDE then it is converted to a single sli_sge.
8547 * The IOCB is still in cpu endianess so the contents of
8548 * the bpl can be used without byte swapping.
8549 *
8550 * Returns valid XRI = Success, NO_XRI = Failure.
8551**/
8552static uint16_t
8553lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8554 struct lpfc_sglq *sglq)
8555{
8556 uint16_t xritag = NO_XRI;
8557 struct ulp_bde64 *bpl = NULL;
8558 struct ulp_bde64 bde;
8559 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008560 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008561 IOCB_t *icmd;
8562 int numBdes = 0;
8563 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008564 uint32_t offset = 0; /* accumulated offset in the sg request list */
8565 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008566
8567 if (!piocbq || !sglq)
8568 return xritag;
8569
8570 sgl = (struct sli4_sge *)sglq->sgl;
8571 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008572 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8573 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008574 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8575 numBdes = icmd->un.genreq64.bdl.bdeSize /
8576 sizeof(struct ulp_bde64);
8577 /* The addrHigh and addrLow fields within the IOCB
8578 * have not been byteswapped yet so there is no
8579 * need to swap them back.
8580 */
James Smart1b511972011-12-13 13:23:09 -05008581 if (piocbq->context3)
8582 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8583 else
8584 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008585
James Smart1b511972011-12-13 13:23:09 -05008586 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008587 if (!bpl)
8588 return xritag;
8589
8590 for (i = 0; i < numBdes; i++) {
8591 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008592 sgl->addr_hi = bpl->addrHigh;
8593 sgl->addr_lo = bpl->addrLow;
8594
James Smart05580562011-05-24 11:40:48 -04008595 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008596 if ((i+1) == numBdes)
8597 bf_set(lpfc_sli4_sge_last, sgl, 1);
8598 else
8599 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008600 /* swap the size field back to the cpu so we
8601 * can assign it to the sgl.
8602 */
8603 bde.tus.w = le32_to_cpu(bpl->tus.w);
8604 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008605 /* The offsets in the sgl need to be accumulated
8606 * separately for the request and reply lists.
8607 * The request is always first, the reply follows.
8608 */
8609 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8610 /* add up the reply sg entries */
8611 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8612 inbound++;
8613 /* first inbound? reset the offset */
8614 if (inbound == 1)
8615 offset = 0;
8616 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008617 bf_set(lpfc_sli4_sge_type, sgl,
8618 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008619 offset += bde.tus.f.bdeSize;
8620 }
James Smart546fc852011-03-11 16:06:29 -05008621 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008622 bpl++;
8623 sgl++;
8624 }
8625 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8626 /* The addrHigh and addrLow fields of the BDE have not
8627 * been byteswapped yet so they need to be swapped
8628 * before putting them in the sgl.
8629 */
8630 sgl->addr_hi =
8631 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8632 sgl->addr_lo =
8633 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008634 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008635 bf_set(lpfc_sli4_sge_last, sgl, 1);
8636 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008637 sgl->sge_len =
8638 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008639 }
8640 return sglq->sli4_xritag;
8641}
8642
8643/**
James Smart4f774512009-05-22 14:52:35 -04008644 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8645 * @phba: Pointer to HBA context object.
8646 * @piocb: Pointer to command iocb.
8647 * @wqe: Pointer to the work queue entry.
8648 *
8649 * This routine converts the iocb command to its Work Queue Entry
8650 * equivalent. The wqe pointer should not have any fields set when
8651 * this routine is called because it will memcpy over them.
8652 * This routine does not set the CQ_ID or the WQEC bits in the
8653 * wqe.
8654 *
8655 * Returns: 0 = Success, IOCB_ERROR = Failure.
8656 **/
8657static int
8658lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8659 union lpfc_wqe *wqe)
8660{
James Smart5ffc2662009-11-18 15:39:44 -05008661 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008662 uint8_t ct = 0;
8663 uint32_t fip;
8664 uint32_t abort_tag;
8665 uint8_t command_type = ELS_COMMAND_NON_FIP;
8666 uint8_t cmnd;
8667 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008668 uint16_t abrt_iotag;
8669 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008670 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008671 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008672 int numBdes, i;
8673 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008674 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008675 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008676 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008677
James Smart45ed1192009-10-02 15:17:02 -04008678 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008679 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008680 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008681 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008682 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008683 command_type = ELS_COMMAND_FIP;
8684 else
8685 command_type = ELS_COMMAND_NON_FIP;
8686
James Smartb5c53952016-03-31 14:12:30 -07008687 if (phba->fcp_embed_io)
8688 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008689 /* Some of the fields are in the right position already */
8690 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008691 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008692 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008693
8694 abort_tag = (uint32_t) iocbq->iotag;
8695 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008696 /* words0-2 bpl convert bde */
8697 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008698 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8699 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008700 bpl = (struct ulp_bde64 *)
8701 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8702 if (!bpl)
8703 return IOCB_ERROR;
8704
8705 /* Should already be byte swapped. */
8706 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8707 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8708 /* swap the size field back to the cpu so we
8709 * can assign it to the sgl.
8710 */
8711 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008712 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8713 total_len = 0;
8714 for (i = 0; i < numBdes; i++) {
8715 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8716 total_len += bde.tus.f.bdeSize;
8717 }
James Smart4f774512009-05-22 14:52:35 -04008718 } else
James Smart5ffc2662009-11-18 15:39:44 -05008719 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008720
8721 iocbq->iocb.ulpIoTag = iocbq->iotag;
8722 cmnd = iocbq->iocb.ulpCommand;
8723
8724 switch (iocbq->iocb.ulpCommand) {
8725 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008726 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8727 ndlp = iocbq->context_un.ndlp;
8728 else
8729 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008730 if (!iocbq->iocb.ulpLe) {
8731 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8732 "2007 Only Limited Edition cmd Format"
8733 " supported 0x%x\n",
8734 iocbq->iocb.ulpCommand);
8735 return IOCB_ERROR;
8736 }
James Smartff78d8f2011-12-13 13:21:35 -05008737
James Smart5ffc2662009-11-18 15:39:44 -05008738 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008739 /* Els_reguest64 has a TMO */
8740 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8741 iocbq->iocb.ulpTimeout);
8742 /* Need a VF for word 4 set the vf bit*/
8743 bf_set(els_req64_vf, &wqe->els_req, 0);
8744 /* And a VFID for word 12 */
8745 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008746 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008747 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8748 iocbq->iocb.ulpContext);
8749 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8750 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008751 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008752 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008753 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8754 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008755 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8756 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008757 if_type = bf_get(lpfc_sli_intf_if_type,
8758 &phba->sli4_hba.sli_intf);
8759 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008760 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008761 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008762 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008763 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008764 *pcmd == ELS_CMD_PLOGI)) {
8765 bf_set(els_req64_sp, &wqe->els_req, 1);
8766 bf_set(els_req64_sid, &wqe->els_req,
8767 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008768 if ((*pcmd == ELS_CMD_FLOGI) &&
8769 !(phba->fc_topology ==
8770 LPFC_TOPOLOGY_LOOP))
8771 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008772 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8773 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008774 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008775 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008776 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8777 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8778 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8779 }
James Smartc8685952009-11-18 15:39:16 -05008780 }
James Smart6d368e52011-05-24 11:44:12 -04008781 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8782 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008783 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8784 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8785 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8786 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8787 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8788 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008789 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008790 break;
James Smart5ffc2662009-11-18 15:39:44 -05008791 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008792 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8793 iocbq->iocb.un.ulpWord[3]);
8794 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008795 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008796 /* The entire sequence is transmitted for this IOCB */
8797 xmit_len = total_len;
8798 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008799 if (phba->link_flag & LS_LOOPBACK_MODE)
8800 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008801 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008802 /* word3 iocb=io_tag32 wqe=reserved */
8803 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008804 /* word4 relative_offset memcpy */
8805 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008806 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8807 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8808 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8809 LPFC_WQE_IOD_WRITE);
8810 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8811 LPFC_WQE_LENLOC_WORD12);
8812 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008813 wqe->xmit_sequence.xmit_len = xmit_len;
8814 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008815 break;
James Smart4f774512009-05-22 14:52:35 -04008816 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008817 /* word3 iocb=iotag32 wqe=seq_payload_len */
8818 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008819 /* word4 iocb=rsvd wqe=rsvd */
8820 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8821 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008822 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008823 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008824 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8825 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8826 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8827 LPFC_WQE_LENLOC_WORD3);
8828 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008829 break;
James Smart4f774512009-05-22 14:52:35 -04008830 case CMD_FCP_IWRITE64_CR:
8831 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008832 /* word3 iocb=iotag wqe=payload_offset_len */
8833 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008834 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8835 xmit_len + sizeof(struct fcp_rsp));
8836 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8837 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008838 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8839 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8840 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8841 iocbq->iocb.ulpFCP2Rcvy);
8842 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8843 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008844 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8845 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8846 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008847 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008848 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008849 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8850 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008851 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8852 if (iocbq->priority) {
8853 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8854 (iocbq->priority << 1));
8855 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008856 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8857 (phba->cfg_XLanePriority << 1));
8858 }
8859 }
James Smartb5c53952016-03-31 14:12:30 -07008860 /* Note, word 10 is already initialized to 0 */
8861
8862 if (phba->fcp_embed_io) {
8863 struct lpfc_scsi_buf *lpfc_cmd;
8864 struct sli4_sge *sgl;
8865 union lpfc_wqe128 *wqe128;
8866 struct fcp_cmnd *fcp_cmnd;
8867 uint32_t *ptr;
8868
8869 /* 128 byte wqe support here */
8870 wqe128 = (union lpfc_wqe128 *)wqe;
8871
8872 lpfc_cmd = iocbq->context1;
8873 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8874 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8875
8876 /* Word 0-2 - FCP_CMND */
8877 wqe128->generic.bde.tus.f.bdeFlags =
8878 BUFF_TYPE_BDE_IMMED;
8879 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8880 wqe128->generic.bde.addrHigh = 0;
8881 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8882
8883 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8884
8885 /* Word 22-29 FCP CMND Payload */
8886 ptr = &wqe128->words[22];
8887 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8888 }
James Smart7851fe22011-07-22 18:36:52 -04008889 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008890 case CMD_FCP_IREAD64_CR:
8891 /* word3 iocb=iotag wqe=payload_offset_len */
8892 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008893 bf_set(payload_offset_len, &wqe->fcp_iread,
8894 xmit_len + sizeof(struct fcp_rsp));
8895 bf_set(cmd_buff_len, &wqe->fcp_iread,
8896 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008897 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8898 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8899 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8900 iocbq->iocb.ulpFCP2Rcvy);
8901 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008902 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008903 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8904 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8905 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008906 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008907 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008908 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8909 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008910 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8911 if (iocbq->priority) {
8912 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8913 (iocbq->priority << 1));
8914 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008915 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8916 (phba->cfg_XLanePriority << 1));
8917 }
8918 }
James Smartb5c53952016-03-31 14:12:30 -07008919 /* Note, word 10 is already initialized to 0 */
8920
8921 if (phba->fcp_embed_io) {
8922 struct lpfc_scsi_buf *lpfc_cmd;
8923 struct sli4_sge *sgl;
8924 union lpfc_wqe128 *wqe128;
8925 struct fcp_cmnd *fcp_cmnd;
8926 uint32_t *ptr;
8927
8928 /* 128 byte wqe support here */
8929 wqe128 = (union lpfc_wqe128 *)wqe;
8930
8931 lpfc_cmd = iocbq->context1;
8932 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8933 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8934
8935 /* Word 0-2 - FCP_CMND */
8936 wqe128->generic.bde.tus.f.bdeFlags =
8937 BUFF_TYPE_BDE_IMMED;
8938 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8939 wqe128->generic.bde.addrHigh = 0;
8940 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8941
8942 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8943
8944 /* Word 22-29 FCP CMND Payload */
8945 ptr = &wqe128->words[22];
8946 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8947 }
James Smart7851fe22011-07-22 18:36:52 -04008948 break;
James Smartf1126682009-06-10 17:22:44 -04008949 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008950 /* word3 iocb=iotag wqe=payload_offset_len */
8951 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8952 bf_set(payload_offset_len, &wqe->fcp_icmd,
8953 xmit_len + sizeof(struct fcp_rsp));
8954 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8955 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008956 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008957 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008958 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008959 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8960 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8961 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8962 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8963 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008964 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8965 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008966 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8967 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008968 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8969 if (iocbq->priority) {
8970 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8971 (iocbq->priority << 1));
8972 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008973 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8974 (phba->cfg_XLanePriority << 1));
8975 }
8976 }
James Smartb5c53952016-03-31 14:12:30 -07008977 /* Note, word 10 is already initialized to 0 */
8978
8979 if (phba->fcp_embed_io) {
8980 struct lpfc_scsi_buf *lpfc_cmd;
8981 struct sli4_sge *sgl;
8982 union lpfc_wqe128 *wqe128;
8983 struct fcp_cmnd *fcp_cmnd;
8984 uint32_t *ptr;
8985
8986 /* 128 byte wqe support here */
8987 wqe128 = (union lpfc_wqe128 *)wqe;
8988
8989 lpfc_cmd = iocbq->context1;
8990 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8991 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8992
8993 /* Word 0-2 - FCP_CMND */
8994 wqe128->generic.bde.tus.f.bdeFlags =
8995 BUFF_TYPE_BDE_IMMED;
8996 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8997 wqe128->generic.bde.addrHigh = 0;
8998 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8999
9000 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9001
9002 /* Word 22-29 FCP CMND Payload */
9003 ptr = &wqe128->words[22];
9004 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9005 }
James Smart7851fe22011-07-22 18:36:52 -04009006 break;
James Smart4f774512009-05-22 14:52:35 -04009007 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009008 /* For this command calculate the xmit length of the
9009 * request bde.
9010 */
9011 xmit_len = 0;
9012 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9013 sizeof(struct ulp_bde64);
9014 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009015 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009016 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9017 break;
James Smart63e801c2010-11-20 23:14:19 -05009018 xmit_len += bde.tus.f.bdeSize;
9019 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009020 /* word3 iocb=IO_TAG wqe=request_payload_len */
9021 wqe->gen_req.request_payload_len = xmit_len;
9022 /* word4 iocb=parameter wqe=relative_offset memcpy */
9023 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009024 /* word6 context tag copied in memcpy */
9025 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9026 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9027 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9028 "2015 Invalid CT %x command 0x%x\n",
9029 ct, iocbq->iocb.ulpCommand);
9030 return IOCB_ERROR;
9031 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009032 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9033 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9034 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9035 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9036 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9037 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9038 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9039 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009040 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009041 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009042 break;
James Smart4f774512009-05-22 14:52:35 -04009043 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009044 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009045 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009046 /* word3 iocb=iotag32 wqe=response_payload_len */
9047 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009048 /* word4 */
9049 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009050 /* word5 iocb=rsvd wge=did */
9051 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009052 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9053
9054 if_type = bf_get(lpfc_sli_intf_if_type,
9055 &phba->sli4_hba.sli_intf);
9056 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9057 if (iocbq->vport->fc_flag & FC_PT2PT) {
9058 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9059 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9060 iocbq->vport->fc_myDID);
9061 if (iocbq->vport->fc_myDID == Fabric_DID) {
9062 bf_set(wqe_els_did,
9063 &wqe->xmit_els_rsp.wqe_dest, 0);
9064 }
9065 }
9066 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009067 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9068 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9069 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9070 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009071 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009072 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009073 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009074 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009075 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9076 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9077 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9078 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9079 LPFC_WQE_LENLOC_WORD3);
9080 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009081 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9082 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009083 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9084 iocbq->context2)->virt);
9085 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009086 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9087 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009088 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009089 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9090 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009091 phba->vpi_ids[phba->pport->vpi]);
9092 }
James Smart4f774512009-05-22 14:52:35 -04009093 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009094 break;
James Smart4f774512009-05-22 14:52:35 -04009095 case CMD_CLOSE_XRI_CN:
9096 case CMD_ABORT_XRI_CN:
9097 case CMD_ABORT_XRI_CX:
9098 /* words 0-2 memcpy should be 0 rserved */
9099 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009100 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9101 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9102 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9103 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9104 } else
9105 fip = 0;
9106
9107 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009108 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009109 * The link is down, or the command was ELS_FIP
9110 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009111 * on the wire.
9112 */
9113 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9114 else
9115 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9116 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009117 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9118 wqe->abort_cmd.rsrvd5 = 0;
9119 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009120 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9121 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009122 /*
9123 * The abort handler will send us CMD_ABORT_XRI_CN or
9124 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9125 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009126 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9127 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9128 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9129 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009130 cmnd = CMD_ABORT_XRI_CX;
9131 command_type = OTHER_COMMAND;
9132 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009133 break;
James Smart6669f9b2009-10-02 15:16:45 -04009134 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009135 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009136 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009137 * we re-construct this WQE here based on information in
9138 * iocbq from scratch.
9139 */
9140 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009141 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009142 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009143 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9144 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009145 LPFC_ABTS_UNSOL_INT) {
9146 /* ABTS sent by initiator to CT exchange, the
9147 * RX_ID field will be filled with the newly
9148 * allocated responder XRI.
9149 */
9150 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9151 iocbq->sli4_xritag);
9152 } else {
9153 /* ABTS sent by responder to CT exchange, the
9154 * RX_ID field will be filled with the responder
9155 * RX_ID from ABTS.
9156 */
9157 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009158 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009159 }
James Smart6669f9b2009-10-02 15:16:45 -04009160 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9161 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009162
9163 /* Use CT=VPI */
9164 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9165 ndlp->nlp_DID);
9166 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9167 iocbq->iocb.ulpContext);
9168 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009169 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009170 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009171 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9172 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9173 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009174 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9175 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009176 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9177 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9178 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9179 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9180 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9181 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9182 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9183 }
9184
James Smart7851fe22011-07-22 18:36:52 -04009185 break;
James Smart4f774512009-05-22 14:52:35 -04009186 case CMD_XRI_ABORTED_CX:
9187 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009188 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9189 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9190 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9191 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9192 default:
9193 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9194 "2014 Invalid command 0x%x\n",
9195 iocbq->iocb.ulpCommand);
9196 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009197 break;
James Smart4f774512009-05-22 14:52:35 -04009198 }
James Smart6d368e52011-05-24 11:44:12 -04009199
James Smart8012cc32012-10-31 14:44:49 -04009200 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9201 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9202 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9203 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9204 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9205 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9206 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9207 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009208 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9209 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9210 wqe->generic.wqe_com.abort_tag = abort_tag;
9211 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9212 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9213 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9214 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009215 return 0;
9216}
9217
9218/**
9219 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9220 * @phba: Pointer to HBA context object.
9221 * @ring_number: SLI ring number to issue iocb on.
9222 * @piocb: Pointer to command iocb.
9223 * @flag: Flag indicating if this command can be put into txq.
9224 *
9225 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9226 * an iocb command to an HBA with SLI-4 interface spec.
9227 *
9228 * This function is called with hbalock held. The function will return success
9229 * after it successfully submit the iocb to firmware or after adding to the
9230 * txq.
9231 **/
9232static int
9233__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9234 struct lpfc_iocbq *piocb, uint32_t flag)
9235{
9236 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009237 union lpfc_wqe *wqe;
9238 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009239 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009240 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009241
James Smart895427b2017-02-12 13:52:30 -08009242 /* Get the WQ */
9243 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9244 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9245 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9246 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9247 else
9248 wq = phba->sli4_hba.oas_wq;
9249 } else {
9250 wq = phba->sli4_hba.els_wq;
9251 }
9252
9253 /* Get corresponding ring */
9254 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009255
James Smartb5c53952016-03-31 14:12:30 -07009256 /*
9257 * The WQE can be either 64 or 128 bytes,
9258 * so allocate space on the stack assuming the largest.
9259 */
9260 wqe = (union lpfc_wqe *)&wqe128;
9261
James Smart895427b2017-02-12 13:52:30 -08009262 lockdep_assert_held(&phba->hbalock);
9263
James Smart4f774512009-05-22 14:52:35 -04009264 if (piocb->sli4_xritag == NO_XRI) {
9265 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009266 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009267 sglq = NULL;
9268 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009269 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009270 if (!(flag & SLI_IOCB_RET_IOCB)) {
9271 __lpfc_sli_ringtx_put(phba,
9272 pring, piocb);
9273 return IOCB_SUCCESS;
9274 } else {
9275 return IOCB_BUSY;
9276 }
9277 } else {
James Smart895427b2017-02-12 13:52:30 -08009278 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009279 if (!sglq) {
9280 if (!(flag & SLI_IOCB_RET_IOCB)) {
9281 __lpfc_sli_ringtx_put(phba,
9282 pring,
9283 piocb);
9284 return IOCB_SUCCESS;
9285 } else
9286 return IOCB_BUSY;
9287 }
9288 }
James Smart4f774512009-05-22 14:52:35 -04009289 }
James Smart2ea259e2017-02-12 13:52:27 -08009290 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009291 /* These IO's already have an XRI and a mapped sgl. */
9292 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009293 else {
James Smart6d368e52011-05-24 11:44:12 -04009294 /*
9295 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009296 * sglq is on the active list
9297 */
James Smartedccdc12013-01-03 15:43:45 -05009298 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009299 if (!sglq)
9300 return IOCB_ERROR;
9301 }
9302
9303 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009304 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009305 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009306 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009307 return IOCB_ERROR;
9308 }
9309
James Smartb5c53952016-03-31 14:12:30 -07009310 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009311 return IOCB_ERROR;
9312
James Smart895427b2017-02-12 13:52:30 -08009313 if (lpfc_sli4_wq_put(wq, wqe))
9314 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009315 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9316
9317 return 0;
9318}
9319
9320/**
James Smart3772a992009-05-22 14:50:54 -04009321 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9322 *
9323 * This routine wraps the actual lockless version for issusing IOCB function
9324 * pointer from the lpfc_hba struct.
9325 *
9326 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009327 * IOCB_ERROR - Error
9328 * IOCB_SUCCESS - Success
9329 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009330 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009331int
James Smart3772a992009-05-22 14:50:54 -04009332__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9333 struct lpfc_iocbq *piocb, uint32_t flag)
9334{
9335 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9336}
9337
9338/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009339 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009340 * @phba: The hba struct for which this call is being executed.
9341 * @dev_grp: The HBA PCI-Device group number.
9342 *
9343 * This routine sets up the SLI interface API function jump table in @phba
9344 * struct.
9345 * Returns: 0 - success, -ENODEV - failure.
9346 **/
9347int
9348lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9349{
9350
9351 switch (dev_grp) {
9352 case LPFC_PCI_DEV_LP:
9353 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9354 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9355 break;
James Smart4f774512009-05-22 14:52:35 -04009356 case LPFC_PCI_DEV_OC:
9357 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9358 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9359 break;
James Smart3772a992009-05-22 14:50:54 -04009360 default:
9361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9362 "1419 Invalid HBA PCI-device group: 0x%x\n",
9363 dev_grp);
9364 return -ENODEV;
9365 break;
9366 }
9367 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9368 return 0;
9369}
James Smart92d7f7b2007-06-17 19:56:38 -05009370
James Smarta1efe162015-05-21 13:55:20 -04009371/**
James Smart895427b2017-02-12 13:52:30 -08009372 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009373 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009374 * @piocb: Pointer to command iocb.
9375 *
James Smart895427b2017-02-12 13:52:30 -08009376 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9377 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009378 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009379 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009380 */
James Smart895427b2017-02-12 13:52:30 -08009381struct lpfc_sli_ring *
9382lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009383{
James Smart895427b2017-02-12 13:52:30 -08009384 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009385 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009386 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009387 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009388 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009389 /*
James Smart895427b2017-02-12 13:52:30 -08009390 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009391 * be setup based on what work queue we used.
9392 */
9393 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
James Smart895427b2017-02-12 13:52:30 -08009394 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009395 lpfc_sli4_scmd_to_wqidx_distr(phba,
9396 piocb->context1);
James Smart895427b2017-02-12 13:52:30 -08009397 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009398 } else {
9399 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009400 return NULL;
9401 piocb->hba_wqidx = 0;
9402 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009403 }
James Smart895427b2017-02-12 13:52:30 -08009404 } else {
9405 if (unlikely(!phba->sli4_hba.els_wq))
9406 return NULL;
9407 piocb->hba_wqidx = 0;
9408 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009409 }
James Smart9bd2bff52014-09-03 12:57:30 -04009410}
9411
James Smarte59058c2008-08-24 21:49:00 -04009412/**
James Smart3621a712009-04-06 18:47:14 -04009413 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009414 * @phba: Pointer to HBA context object.
9415 * @pring: Pointer to driver SLI ring object.
9416 * @piocb: Pointer to command iocb.
9417 * @flag: Flag indicating if this command can be put into txq.
9418 *
9419 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9420 * function. This function gets the hbalock and calls
9421 * __lpfc_sli_issue_iocb function and will return the error returned
9422 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9423 * functions which do not hold hbalock.
9424 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009425int
James Smart3772a992009-05-22 14:50:54 -04009426lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009427 struct lpfc_iocbq *piocb, uint32_t flag)
9428{
James Smart895427b2017-02-12 13:52:30 -08009429 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009430 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009431 struct lpfc_queue *fpeq;
9432 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009433 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009434 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009435
James Smart7e56aa22012-08-03 12:35:34 -04009436 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009437 pring = lpfc_sli4_calc_ring(phba, piocb);
9438 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009439 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009440
James Smart9bd2bff52014-09-03 12:57:30 -04009441 spin_lock_irqsave(&pring->ring_lock, iflags);
9442 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9443 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009444
James Smart9bd2bff52014-09-03 12:57:30 -04009445 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009446 idx = piocb->hba_wqidx;
9447 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009448
James Smart895427b2017-02-12 13:52:30 -08009449 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009450
James Smart9bd2bff52014-09-03 12:57:30 -04009451 /* Get associated EQ with this index */
9452 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009453
James Smart9bd2bff52014-09-03 12:57:30 -04009454 /* Turn off interrupts from this EQ */
9455 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009456
James Smart9bd2bff52014-09-03 12:57:30 -04009457 /*
9458 * Process all the events on FCP EQ
9459 */
9460 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9461 lpfc_sli4_hba_handle_eqe(phba,
9462 eqe, idx);
9463 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009464 }
James Smartba20c852012-08-03 12:36:52 -04009465
James Smart9bd2bff52014-09-03 12:57:30 -04009466 /* Always clear and re-arm the EQ */
9467 lpfc_sli4_eq_release(fpeq,
9468 LPFC_QUEUE_REARM);
9469 }
James Smart895427b2017-02-12 13:52:30 -08009470 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009471 }
James Smart7e56aa22012-08-03 12:35:34 -04009472 } else {
9473 /* For now, SLI2/3 will still use hbalock */
9474 spin_lock_irqsave(&phba->hbalock, iflags);
9475 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9476 spin_unlock_irqrestore(&phba->hbalock, iflags);
9477 }
James Smart92d7f7b2007-06-17 19:56:38 -05009478 return rc;
9479}
9480
James Smarte59058c2008-08-24 21:49:00 -04009481/**
James Smart3621a712009-04-06 18:47:14 -04009482 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009483 * @phba: Pointer to HBA context object.
9484 *
9485 * This function is called while driver attaches with the
9486 * HBA to setup the extra ring. The extra ring is used
9487 * only when driver needs to support target mode functionality
9488 * or IP over FC functionalities.
9489 *
James Smart895427b2017-02-12 13:52:30 -08009490 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009491 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009492static int
9493lpfc_extra_ring_setup( struct lpfc_hba *phba)
9494{
9495 struct lpfc_sli *psli;
9496 struct lpfc_sli_ring *pring;
9497
9498 psli = &phba->sli;
9499
9500 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009501
9502 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009503 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009504 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9505 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9506 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9507 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009508
James Smarta4bc3372006-12-02 13:34:16 -05009509 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009510 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009511
James Smart7e56aa22012-08-03 12:35:34 -04009512 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9513 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9514 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9515 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009516
9517 /* Setup default profile for this ring */
9518 pring->iotag_max = 4096;
9519 pring->num_mask = 1;
9520 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009521 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9522 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009523 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9524 return 0;
9525}
9526
James Smartcb69f7d2011-12-13 13:21:57 -05009527/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9528 * @phba: Pointer to HBA context object.
9529 * @iocbq: Pointer to iocb object.
9530 *
9531 * The async_event handler calls this routine when it receives
9532 * an ASYNC_STATUS_CN event from the port. The port generates
9533 * this event when an Abort Sequence request to an rport fails
9534 * twice in succession. The abort could be originated by the
9535 * driver or by the port. The ABTS could have been for an ELS
9536 * or FCP IO. The port only generates this event when an ABTS
9537 * fails to complete after one retry.
9538 */
9539static void
9540lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9541 struct lpfc_iocbq *iocbq)
9542{
9543 struct lpfc_nodelist *ndlp = NULL;
9544 uint16_t rpi = 0, vpi = 0;
9545 struct lpfc_vport *vport = NULL;
9546
9547 /* The rpi in the ulpContext is vport-sensitive. */
9548 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9549 rpi = iocbq->iocb.ulpContext;
9550
9551 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9552 "3092 Port generated ABTS async event "
9553 "on vpi %d rpi %d status 0x%x\n",
9554 vpi, rpi, iocbq->iocb.ulpStatus);
9555
9556 vport = lpfc_find_vport_by_vpid(phba, vpi);
9557 if (!vport)
9558 goto err_exit;
9559 ndlp = lpfc_findnode_rpi(vport, rpi);
9560 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9561 goto err_exit;
9562
9563 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9564 lpfc_sli_abts_recover_port(vport, ndlp);
9565 return;
9566
9567 err_exit:
9568 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9569 "3095 Event Context not found, no "
9570 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9571 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9572 vpi, rpi);
9573}
9574
9575/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9576 * @phba: pointer to HBA context object.
9577 * @ndlp: nodelist pointer for the impacted rport.
9578 * @axri: pointer to the wcqe containing the failed exchange.
9579 *
9580 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9581 * port. The port generates this event when an abort exchange request to an
9582 * rport fails twice in succession with no reply. The abort could be originated
9583 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9584 */
9585void
9586lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9587 struct lpfc_nodelist *ndlp,
9588 struct sli4_wcqe_xri_aborted *axri)
9589{
9590 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009591 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009592
James Smart6b5151f2012-01-18 16:24:06 -05009593 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009594 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9595 "3115 Node Context not found, driver "
9596 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009597 return;
9598 }
9599
James Smartcb69f7d2011-12-13 13:21:57 -05009600 vport = ndlp->vport;
9601 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9602 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009603 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009604 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009605 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009606 bf_get(lpfc_wcqe_xa_status, axri),
9607 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009608
James Smart5c1db2a2012-03-01 22:34:36 -05009609 /*
9610 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9611 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9612 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9613 */
James Smarte3d2b802012-08-14 14:25:43 -04009614 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009615 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9616 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009617 lpfc_sli_abts_recover_port(vport, ndlp);
9618}
9619
James Smarte59058c2008-08-24 21:49:00 -04009620/**
James Smart3621a712009-04-06 18:47:14 -04009621 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009622 * @phba: Pointer to HBA context object.
9623 * @pring: Pointer to driver SLI ring object.
9624 * @iocbq: Pointer to iocb object.
9625 *
9626 * This function is called by the slow ring event handler
9627 * function when there is an ASYNC event iocb in the ring.
9628 * This function is called with no lock held.
9629 * Currently this function handles only temperature related
9630 * ASYNC events. The function decodes the temperature sensor
9631 * event message and posts events for the management applications.
9632 **/
James Smart98c9ea52007-10-27 13:37:33 -04009633static void
James Smart57127f12007-10-27 13:37:05 -04009634lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9635 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9636{
9637 IOCB_t *icmd;
9638 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009639 struct temp_event temp_event_data;
9640 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009641 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009642
9643 icmd = &iocbq->iocb;
9644 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009645
James Smartcb69f7d2011-12-13 13:21:57 -05009646 switch (evt_code) {
9647 case ASYNC_TEMP_WARN:
9648 case ASYNC_TEMP_SAFE:
9649 temp_event_data.data = (uint32_t) icmd->ulpContext;
9650 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9651 if (evt_code == ASYNC_TEMP_WARN) {
9652 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9653 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9654 "0347 Adapter is very hot, please take "
9655 "corrective action. temperature : %d Celsius\n",
9656 (uint32_t) icmd->ulpContext);
9657 } else {
9658 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9659 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9660 "0340 Adapter temperature is OK now. "
9661 "temperature : %d Celsius\n",
9662 (uint32_t) icmd->ulpContext);
9663 }
9664
9665 /* Send temperature change event to applications */
9666 shost = lpfc_shost_from_vport(phba->pport);
9667 fc_host_post_vendor_event(shost, fc_get_event_number(),
9668 sizeof(temp_event_data), (char *) &temp_event_data,
9669 LPFC_NL_VENDOR_ID);
9670 break;
9671 case ASYNC_STATUS_CN:
9672 lpfc_sli_abts_err_handler(phba, iocbq);
9673 break;
9674 default:
James Smarta257bf92009-04-06 18:48:10 -04009675 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009677 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009678 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009679 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9680 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9681 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9682 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009683 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009684 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9685 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9686 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9687 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9688
James Smartcb69f7d2011-12-13 13:21:57 -05009689 break;
James Smart57127f12007-10-27 13:37:05 -04009690 }
James Smart57127f12007-10-27 13:37:05 -04009691}
9692
9693
James Smarte59058c2008-08-24 21:49:00 -04009694/**
James Smart895427b2017-02-12 13:52:30 -08009695 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009696 * @phba: Pointer to HBA context object.
9697 *
9698 * lpfc_sli_setup sets up rings of the SLI interface with
9699 * number of iocbs per ring and iotags. This function is
9700 * called while driver attach to the HBA and before the
9701 * interrupts are enabled. So there is no need for locking.
9702 *
9703 * This function always returns 0.
9704 **/
dea31012005-04-17 16:05:31 -05009705int
James Smart895427b2017-02-12 13:52:30 -08009706lpfc_sli4_setup(struct lpfc_hba *phba)
9707{
9708 struct lpfc_sli_ring *pring;
9709
9710 pring = phba->sli4_hba.els_wq->pring;
9711 pring->num_mask = LPFC_MAX_RING_MASK;
9712 pring->prt[0].profile = 0; /* Mask 0 */
9713 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9714 pring->prt[0].type = FC_TYPE_ELS;
9715 pring->prt[0].lpfc_sli_rcv_unsol_event =
9716 lpfc_els_unsol_event;
9717 pring->prt[1].profile = 0; /* Mask 1 */
9718 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9719 pring->prt[1].type = FC_TYPE_ELS;
9720 pring->prt[1].lpfc_sli_rcv_unsol_event =
9721 lpfc_els_unsol_event;
9722 pring->prt[2].profile = 0; /* Mask 2 */
9723 /* NameServer Inquiry */
9724 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9725 /* NameServer */
9726 pring->prt[2].type = FC_TYPE_CT;
9727 pring->prt[2].lpfc_sli_rcv_unsol_event =
9728 lpfc_ct_unsol_event;
9729 pring->prt[3].profile = 0; /* Mask 3 */
9730 /* NameServer response */
9731 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9732 /* NameServer */
9733 pring->prt[3].type = FC_TYPE_CT;
9734 pring->prt[3].lpfc_sli_rcv_unsol_event =
9735 lpfc_ct_unsol_event;
9736 return 0;
9737}
9738
9739/**
9740 * lpfc_sli_setup - SLI ring setup function
9741 * @phba: Pointer to HBA context object.
9742 *
9743 * lpfc_sli_setup sets up rings of the SLI interface with
9744 * number of iocbs per ring and iotags. This function is
9745 * called while driver attach to the HBA and before the
9746 * interrupts are enabled. So there is no need for locking.
9747 *
9748 * This function always returns 0. SLI3 only.
9749 **/
9750int
dea31012005-04-17 16:05:31 -05009751lpfc_sli_setup(struct lpfc_hba *phba)
9752{
James Smarted957682007-06-17 19:56:37 -05009753 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009754 struct lpfc_sli *psli = &phba->sli;
9755 struct lpfc_sli_ring *pring;
9756
James Smart2a76a282012-08-03 12:35:54 -04009757 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009758 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009759
James Bottomley604a3e32005-10-29 10:28:33 -05009760 psli->iocbq_lookup = NULL;
9761 psli->iocbq_lookup_len = 0;
9762 psli->last_iotag = 0;
9763
dea31012005-04-17 16:05:31 -05009764 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009765 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009766 switch (i) {
9767 case LPFC_FCP_RING: /* ring 0 - FCP */
9768 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009769 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9770 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9771 pring->sli.sli3.numCiocb +=
9772 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9773 pring->sli.sli3.numRiocb +=
9774 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9775 pring->sli.sli3.numCiocb +=
9776 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9777 pring->sli.sli3.numRiocb +=
9778 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9779 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009780 SLI3_IOCB_CMD_SIZE :
9781 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009782 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009783 SLI3_IOCB_RSP_SIZE :
9784 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009785 pring->iotag_ctr = 0;
9786 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009787 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009788 pring->fast_iotag = pring->iotag_max;
9789 pring->num_mask = 0;
9790 break;
James Smarta4bc3372006-12-02 13:34:16 -05009791 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009792 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009793 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9794 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9795 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009796 SLI3_IOCB_CMD_SIZE :
9797 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009798 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009799 SLI3_IOCB_RSP_SIZE :
9800 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009801 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009802 pring->num_mask = 0;
9803 break;
9804 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9805 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009806 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9807 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9808 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009809 SLI3_IOCB_CMD_SIZE :
9810 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009811 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009812 SLI3_IOCB_RSP_SIZE :
9813 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009814 pring->fast_iotag = 0;
9815 pring->iotag_ctr = 0;
9816 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009817 pring->lpfc_sli_rcv_async_status =
9818 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009819 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009820 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009821 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9822 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009823 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009824 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009825 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009826 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9827 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009828 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009829 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009830 pring->prt[2].profile = 0; /* Mask 2 */
9831 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009832 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009833 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009834 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009835 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009836 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009837 pring->prt[3].profile = 0; /* Mask 3 */
9838 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009839 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009840 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009841 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009842 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009843 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009844 break;
9845 }
James Smart7e56aa22012-08-03 12:35:34 -04009846 totiocbsize += (pring->sli.sli3.numCiocb *
9847 pring->sli.sli3.sizeCiocb) +
9848 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009849 }
James Smarted957682007-06-17 19:56:37 -05009850 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009851 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009852 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9853 "SLI2 SLIM Data: x%x x%lx\n",
9854 phba->brd_no, totiocbsize,
9855 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009856 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009857 if (phba->cfg_multi_ring_support == 2)
9858 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009859
9860 return 0;
9861}
9862
James Smarte59058c2008-08-24 21:49:00 -04009863/**
James Smart895427b2017-02-12 13:52:30 -08009864 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009865 * @phba: Pointer to HBA context object.
9866 *
James Smart895427b2017-02-12 13:52:30 -08009867 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009868 * ring. This function also initializes ring indices of each ring.
9869 * This function is called during the initialization of the SLI
9870 * interface of an HBA.
9871 * This function is called with no lock held and always returns
9872 * 1.
9873 **/
James Smart895427b2017-02-12 13:52:30 -08009874void
9875lpfc_sli4_queue_init(struct lpfc_hba *phba)
9876{
9877 struct lpfc_sli *psli;
9878 struct lpfc_sli_ring *pring;
9879 int i;
9880
9881 psli = &phba->sli;
9882 spin_lock_irq(&phba->hbalock);
9883 INIT_LIST_HEAD(&psli->mboxq);
9884 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9885 /* Initialize list headers for txq and txcmplq as double linked lists */
9886 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9887 pring = phba->sli4_hba.fcp_wq[i]->pring;
9888 pring->flag = 0;
9889 pring->ringno = LPFC_FCP_RING;
9890 INIT_LIST_HEAD(&pring->txq);
9891 INIT_LIST_HEAD(&pring->txcmplq);
9892 INIT_LIST_HEAD(&pring->iocb_continueq);
9893 spin_lock_init(&pring->ring_lock);
9894 }
9895 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9896 pring = phba->sli4_hba.nvme_wq[i]->pring;
9897 pring->flag = 0;
9898 pring->ringno = LPFC_FCP_RING;
9899 INIT_LIST_HEAD(&pring->txq);
9900 INIT_LIST_HEAD(&pring->txcmplq);
9901 INIT_LIST_HEAD(&pring->iocb_continueq);
9902 spin_lock_init(&pring->ring_lock);
9903 }
9904 pring = phba->sli4_hba.els_wq->pring;
9905 pring->flag = 0;
9906 pring->ringno = LPFC_ELS_RING;
9907 INIT_LIST_HEAD(&pring->txq);
9908 INIT_LIST_HEAD(&pring->txcmplq);
9909 INIT_LIST_HEAD(&pring->iocb_continueq);
9910 spin_lock_init(&pring->ring_lock);
9911
9912 if (phba->cfg_nvme_io_channel) {
9913 pring = phba->sli4_hba.nvmels_wq->pring;
9914 pring->flag = 0;
9915 pring->ringno = LPFC_ELS_RING;
9916 INIT_LIST_HEAD(&pring->txq);
9917 INIT_LIST_HEAD(&pring->txcmplq);
9918 INIT_LIST_HEAD(&pring->iocb_continueq);
9919 spin_lock_init(&pring->ring_lock);
9920 }
9921
9922 if (phba->cfg_fof) {
9923 pring = phba->sli4_hba.oas_wq->pring;
9924 pring->flag = 0;
9925 pring->ringno = LPFC_FCP_RING;
9926 INIT_LIST_HEAD(&pring->txq);
9927 INIT_LIST_HEAD(&pring->txcmplq);
9928 INIT_LIST_HEAD(&pring->iocb_continueq);
9929 spin_lock_init(&pring->ring_lock);
9930 }
9931
9932 spin_unlock_irq(&phba->hbalock);
9933}
9934
9935/**
9936 * lpfc_sli_queue_init - Queue initialization function
9937 * @phba: Pointer to HBA context object.
9938 *
9939 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9940 * ring. This function also initializes ring indices of each ring.
9941 * This function is called during the initialization of the SLI
9942 * interface of an HBA.
9943 * This function is called with no lock held and always returns
9944 * 1.
9945 **/
9946void
9947lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009948{
9949 struct lpfc_sli *psli;
9950 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009951 int i;
dea31012005-04-17 16:05:31 -05009952
9953 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009954 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009955 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009956 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009957 /* Initialize list headers for txq and txcmplq as double linked lists */
9958 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009959 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009960 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009961 pring->sli.sli3.next_cmdidx = 0;
9962 pring->sli.sli3.local_getidx = 0;
9963 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009964 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009965 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009966 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -08009967 pring->flag = 0;
9968 INIT_LIST_HEAD(&pring->txq);
9969 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -04009970 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009971 }
James Smart2e0fef82007-06-17 19:56:36 -05009972 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009973}
9974
James Smarte59058c2008-08-24 21:49:00 -04009975/**
James Smart04c68492009-05-22 14:52:52 -04009976 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9977 * @phba: Pointer to HBA context object.
9978 *
9979 * This routine flushes the mailbox command subsystem. It will unconditionally
9980 * flush all the mailbox commands in the three possible stages in the mailbox
9981 * command sub-system: pending mailbox command queue; the outstanding mailbox
9982 * command; and completed mailbox command queue. It is caller's responsibility
9983 * to make sure that the driver is in the proper state to flush the mailbox
9984 * command sub-system. Namely, the posting of mailbox commands into the
9985 * pending mailbox command queue from the various clients must be stopped;
9986 * either the HBA is in a state that it will never works on the outstanding
9987 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9988 * mailbox command has been completed.
9989 **/
9990static void
9991lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9992{
9993 LIST_HEAD(completions);
9994 struct lpfc_sli *psli = &phba->sli;
9995 LPFC_MBOXQ_t *pmb;
9996 unsigned long iflag;
9997
9998 /* Flush all the mailbox commands in the mbox system */
9999 spin_lock_irqsave(&phba->hbalock, iflag);
10000 /* The pending mailbox command queue */
10001 list_splice_init(&phba->sli.mboxq, &completions);
10002 /* The outstanding active mailbox command */
10003 if (psli->mbox_active) {
10004 list_add_tail(&psli->mbox_active->list, &completions);
10005 psli->mbox_active = NULL;
10006 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10007 }
10008 /* The completed mailbox command queue */
10009 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10010 spin_unlock_irqrestore(&phba->hbalock, iflag);
10011
10012 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10013 while (!list_empty(&completions)) {
10014 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10015 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10016 if (pmb->mbox_cmpl)
10017 pmb->mbox_cmpl(phba, pmb);
10018 }
10019}
10020
10021/**
James Smart3621a712009-04-06 18:47:14 -040010022 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010023 * @vport: Pointer to virtual port object.
10024 *
10025 * lpfc_sli_host_down is called to clean up the resources
10026 * associated with a vport before destroying virtual
10027 * port data structures.
10028 * This function does following operations:
10029 * - Free discovery resources associated with this virtual
10030 * port.
10031 * - Free iocbs associated with this virtual port in
10032 * the txq.
10033 * - Send abort for all iocb commands associated with this
10034 * vport in txcmplq.
10035 *
10036 * This function is called with no lock held and always returns 1.
10037 **/
dea31012005-04-17 16:05:31 -050010038int
James Smart92d7f7b2007-06-17 19:56:38 -050010039lpfc_sli_host_down(struct lpfc_vport *vport)
10040{
James Smart858c9f62007-06-17 19:56:39 -050010041 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010042 struct lpfc_hba *phba = vport->phba;
10043 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010044 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010045 struct lpfc_sli_ring *pring;
10046 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010047 int i;
10048 unsigned long flags = 0;
10049 uint16_t prev_pring_flag;
10050
10051 lpfc_cleanup_discovery_resources(vport);
10052
10053 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010054
James Smart895427b2017-02-12 13:52:30 -080010055 /*
10056 * Error everything on the txq since these iocbs
10057 * have not been given to the FW yet.
10058 * Also issue ABTS for everything on the txcmplq
10059 */
10060 if (phba->sli_rev != LPFC_SLI_REV4) {
10061 for (i = 0; i < psli->num_rings; i++) {
10062 pring = &psli->sli3_ring[i];
10063 prev_pring_flag = pring->flag;
10064 /* Only slow rings */
10065 if (pring->ringno == LPFC_ELS_RING) {
10066 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10067 /* Set the lpfc data pending flag */
10068 set_bit(LPFC_DATA_READY, &phba->data_flags);
10069 }
10070 list_for_each_entry_safe(iocb, next_iocb,
10071 &pring->txq, list) {
10072 if (iocb->vport != vport)
10073 continue;
10074 list_move_tail(&iocb->list, &completions);
10075 }
10076 list_for_each_entry_safe(iocb, next_iocb,
10077 &pring->txcmplq, list) {
10078 if (iocb->vport != vport)
10079 continue;
10080 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10081 }
10082 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010083 }
James Smart895427b2017-02-12 13:52:30 -080010084 } else {
10085 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10086 pring = qp->pring;
10087 if (!pring)
10088 continue;
10089 if (pring == phba->sli4_hba.els_wq->pring) {
10090 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10091 /* Set the lpfc data pending flag */
10092 set_bit(LPFC_DATA_READY, &phba->data_flags);
10093 }
10094 prev_pring_flag = pring->flag;
10095 spin_lock_irq(&pring->ring_lock);
10096 list_for_each_entry_safe(iocb, next_iocb,
10097 &pring->txq, list) {
10098 if (iocb->vport != vport)
10099 continue;
10100 list_move_tail(&iocb->list, &completions);
10101 }
10102 spin_unlock_irq(&pring->ring_lock);
10103 list_for_each_entry_safe(iocb, next_iocb,
10104 &pring->txcmplq, list) {
10105 if (iocb->vport != vport)
10106 continue;
10107 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10108 }
10109 pring->flag = prev_pring_flag;
10110 }
James Smart92d7f7b2007-06-17 19:56:38 -050010111 }
James Smart92d7f7b2007-06-17 19:56:38 -050010112 spin_unlock_irqrestore(&phba->hbalock, flags);
10113
James Smarta257bf92009-04-06 18:48:10 -040010114 /* Cancel all the IOCBs from the completions list */
10115 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10116 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010117 return 1;
10118}
10119
James Smarte59058c2008-08-24 21:49:00 -040010120/**
James Smart3621a712009-04-06 18:47:14 -040010121 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010122 * @phba: Pointer to HBA context object.
10123 *
10124 * This function cleans up all iocb, buffers, mailbox commands
10125 * while shutting down the HBA. This function is called with no
10126 * lock held and always returns 1.
10127 * This function does the following to cleanup driver resources:
10128 * - Free discovery resources for each virtual port
10129 * - Cleanup any pending fabric iocbs
10130 * - Iterate through the iocb txq and free each entry
10131 * in the list.
10132 * - Free up any buffer posted to the HBA
10133 * - Free mailbox commands in the mailbox queue.
10134 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010135int
James Smart2e0fef82007-06-17 19:56:36 -050010136lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010137{
James Smart2534ba72007-04-25 09:52:20 -040010138 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010139 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010140 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010141 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010142 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010143 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010144 int i;
10145
10146 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010147 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010148
dea31012005-04-17 16:05:31 -050010149 lpfc_hba_down_prep(phba);
10150
James Smart92d7f7b2007-06-17 19:56:38 -050010151 lpfc_fabric_abort_hba(phba);
10152
James Smart2e0fef82007-06-17 19:56:36 -050010153 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010154
James Smart895427b2017-02-12 13:52:30 -080010155 /*
10156 * Error everything on the txq since these iocbs
10157 * have not been given to the FW yet.
10158 */
10159 if (phba->sli_rev != LPFC_SLI_REV4) {
10160 for (i = 0; i < psli->num_rings; i++) {
10161 pring = &psli->sli3_ring[i];
10162 /* Only slow rings */
10163 if (pring->ringno == LPFC_ELS_RING) {
10164 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10165 /* Set the lpfc data pending flag */
10166 set_bit(LPFC_DATA_READY, &phba->data_flags);
10167 }
10168 list_splice_init(&pring->txq, &completions);
10169 }
10170 } else {
10171 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10172 pring = qp->pring;
10173 if (!pring)
10174 continue;
10175 spin_lock_irq(&pring->ring_lock);
10176 list_splice_init(&pring->txq, &completions);
10177 spin_unlock_irq(&pring->ring_lock);
10178 if (pring == phba->sli4_hba.els_wq->pring) {
10179 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10180 /* Set the lpfc data pending flag */
10181 set_bit(LPFC_DATA_READY, &phba->data_flags);
10182 }
10183 }
dea31012005-04-17 16:05:31 -050010184 }
James Smart2e0fef82007-06-17 19:56:36 -050010185 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010186
James Smarta257bf92009-04-06 18:48:10 -040010187 /* Cancel all the IOCBs from the completions list */
10188 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10189 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010190
James Smart0ff10d42008-01-11 01:52:36 -050010191 spin_lock_irqsave(&phba->hbalock, flags);
10192 list_splice_init(&phba->elsbuf, &completions);
10193 phba->elsbuf_cnt = 0;
10194 phba->elsbuf_prev_cnt = 0;
10195 spin_unlock_irqrestore(&phba->hbalock, flags);
10196
10197 while (!list_empty(&completions)) {
10198 list_remove_head(&completions, buf_ptr,
10199 struct lpfc_dmabuf, list);
10200 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10201 kfree(buf_ptr);
10202 }
10203
dea31012005-04-17 16:05:31 -050010204 /* Return any active mbox cmds */
10205 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010206
James Smartda0436e2009-05-22 14:51:39 -040010207 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010208 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010209 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010210
James Smartda0436e2009-05-22 14:51:39 -040010211 return 1;
10212}
James Smart92d7f7b2007-06-17 19:56:38 -050010213
James Smartda0436e2009-05-22 14:51:39 -040010214/**
James Smart3621a712009-04-06 18:47:14 -040010215 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010216 * @srcp: Source memory pointer.
10217 * @destp: Destination memory pointer.
10218 * @cnt: Number of words required to be copied.
10219 *
10220 * This function is used for copying data between driver memory
10221 * and the SLI memory. This function also changes the endianness
10222 * of each word if native endianness is different from SLI
10223 * endianness. This function can be called with or without
10224 * lock.
10225 **/
dea31012005-04-17 16:05:31 -050010226void
10227lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10228{
10229 uint32_t *src = srcp;
10230 uint32_t *dest = destp;
10231 uint32_t ldata;
10232 int i;
10233
10234 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10235 ldata = *src;
10236 ldata = le32_to_cpu(ldata);
10237 *dest = ldata;
10238 src++;
10239 dest++;
10240 }
10241}
10242
James Smarte59058c2008-08-24 21:49:00 -040010243
10244/**
James Smarta0c87cb2009-07-19 10:01:10 -040010245 * lpfc_sli_bemem_bcopy - SLI memory copy function
10246 * @srcp: Source memory pointer.
10247 * @destp: Destination memory pointer.
10248 * @cnt: Number of words required to be copied.
10249 *
10250 * This function is used for copying data between a data structure
10251 * with big endian representation to local endianness.
10252 * This function can be called with or without lock.
10253 **/
10254void
10255lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10256{
10257 uint32_t *src = srcp;
10258 uint32_t *dest = destp;
10259 uint32_t ldata;
10260 int i;
10261
10262 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10263 ldata = *src;
10264 ldata = be32_to_cpu(ldata);
10265 *dest = ldata;
10266 src++;
10267 dest++;
10268 }
10269}
10270
10271/**
James Smart3621a712009-04-06 18:47:14 -040010272 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010273 * @phba: Pointer to HBA context object.
10274 * @pring: Pointer to driver SLI ring object.
10275 * @mp: Pointer to driver buffer object.
10276 *
10277 * This function is called with no lock held.
10278 * It always return zero after adding the buffer to the postbufq
10279 * buffer list.
10280 **/
dea31012005-04-17 16:05:31 -050010281int
James Smart2e0fef82007-06-17 19:56:36 -050010282lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10283 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010284{
10285 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10286 later */
James Smart2e0fef82007-06-17 19:56:36 -050010287 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010288 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010289 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010290 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010291 return 0;
10292}
10293
James Smarte59058c2008-08-24 21:49:00 -040010294/**
James Smart3621a712009-04-06 18:47:14 -040010295 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010296 * @phba: Pointer to HBA context object.
10297 *
10298 * When HBQ is enabled, buffers are searched based on tags. This function
10299 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10300 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10301 * does not conflict with tags of buffer posted for unsolicited events.
10302 * The function returns the allocated tag. The function is called with
10303 * no locks held.
10304 **/
James Smart76bb24e2007-10-27 13:38:00 -040010305uint32_t
10306lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10307{
10308 spin_lock_irq(&phba->hbalock);
10309 phba->buffer_tag_count++;
10310 /*
10311 * Always set the QUE_BUFTAG_BIT to distiguish between
10312 * a tag assigned by HBQ.
10313 */
10314 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10315 spin_unlock_irq(&phba->hbalock);
10316 return phba->buffer_tag_count;
10317}
10318
James Smarte59058c2008-08-24 21:49:00 -040010319/**
James Smart3621a712009-04-06 18:47:14 -040010320 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010321 * @phba: Pointer to HBA context object.
10322 * @pring: Pointer to driver SLI ring object.
10323 * @tag: Buffer tag.
10324 *
10325 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10326 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10327 * iocb is posted to the response ring with the tag of the buffer.
10328 * This function searches the pring->postbufq list using the tag
10329 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10330 * iocb. If the buffer is found then lpfc_dmabuf object of the
10331 * buffer is returned to the caller else NULL is returned.
10332 * This function is called with no lock held.
10333 **/
James Smart76bb24e2007-10-27 13:38:00 -040010334struct lpfc_dmabuf *
10335lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10336 uint32_t tag)
10337{
10338 struct lpfc_dmabuf *mp, *next_mp;
10339 struct list_head *slp = &pring->postbufq;
10340
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010341 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010342 spin_lock_irq(&phba->hbalock);
10343 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10344 if (mp->buffer_tag == tag) {
10345 list_del_init(&mp->list);
10346 pring->postbufq_cnt--;
10347 spin_unlock_irq(&phba->hbalock);
10348 return mp;
10349 }
10350 }
10351
10352 spin_unlock_irq(&phba->hbalock);
10353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010354 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010355 "ring %d Data x%lx x%p x%p x%x\n",
10356 pring->ringno, (unsigned long) tag,
10357 slp->next, slp->prev, pring->postbufq_cnt);
10358
10359 return NULL;
10360}
dea31012005-04-17 16:05:31 -050010361
James Smarte59058c2008-08-24 21:49:00 -040010362/**
James Smart3621a712009-04-06 18:47:14 -040010363 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010364 * @phba: Pointer to HBA context object.
10365 * @pring: Pointer to driver SLI ring object.
10366 * @phys: DMA address of the buffer.
10367 *
10368 * This function searches the buffer list using the dma_address
10369 * of unsolicited event to find the driver's lpfc_dmabuf object
10370 * corresponding to the dma_address. The function returns the
10371 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10372 * This function is called by the ct and els unsolicited event
10373 * handlers to get the buffer associated with the unsolicited
10374 * event.
10375 *
10376 * This function is called with no lock held.
10377 **/
dea31012005-04-17 16:05:31 -050010378struct lpfc_dmabuf *
10379lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10380 dma_addr_t phys)
10381{
10382 struct lpfc_dmabuf *mp, *next_mp;
10383 struct list_head *slp = &pring->postbufq;
10384
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010385 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010386 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010387 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10388 if (mp->phys == phys) {
10389 list_del_init(&mp->list);
10390 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010391 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010392 return mp;
10393 }
10394 }
10395
James Smart2e0fef82007-06-17 19:56:36 -050010396 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010398 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010399 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010400 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010401 slp->next, slp->prev, pring->postbufq_cnt);
10402 return NULL;
10403}
10404
James Smarte59058c2008-08-24 21:49:00 -040010405/**
James Smart3621a712009-04-06 18:47:14 -040010406 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010407 * @phba: Pointer to HBA context object.
10408 * @cmdiocb: Pointer to driver command iocb object.
10409 * @rspiocb: Pointer to driver response iocb object.
10410 *
10411 * This function is the completion handler for the abort iocbs for
10412 * ELS commands. This function is called from the ELS ring event
10413 * handler with no lock held. This function frees memory resources
10414 * associated with the abort iocb.
10415 **/
dea31012005-04-17 16:05:31 -050010416static void
James Smart2e0fef82007-06-17 19:56:36 -050010417lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10418 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010419{
James Smart2e0fef82007-06-17 19:56:36 -050010420 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010421 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010422 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010423
10424 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010425
10426 /*
10427 * Assume that the port already completed and returned, or
10428 * will return the iocb. Just Log the message.
10429 */
James Smart2680eea2007-04-25 09:52:55 -040010430 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10431 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10432
James Smart2e0fef82007-06-17 19:56:36 -050010433 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010434 if (phba->sli_rev < LPFC_SLI_REV4) {
10435 if (abort_iotag != 0 &&
10436 abort_iotag <= phba->sli.last_iotag)
10437 abort_iocb =
10438 phba->sli.iocbq_lookup[abort_iotag];
10439 } else
10440 /* For sli4 the abort_tag is the XRI,
10441 * so the abort routine puts the iotag of the iocb
10442 * being aborted in the context field of the abort
10443 * IOCB.
10444 */
10445 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010446
James Smart2a9bf3d2010-06-07 15:24:45 -040010447 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10448 "0327 Cannot abort els iocb %p "
10449 "with tag %x context %x, abort status %x, "
10450 "abort code %x\n",
10451 abort_iocb, abort_iotag, abort_context,
10452 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010453
James Smartff78d8f2011-12-13 13:21:35 -050010454 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010455 }
James Bottomley604a3e32005-10-29 10:28:33 -050010456 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010457 return;
10458}
10459
James Smarte59058c2008-08-24 21:49:00 -040010460/**
James Smart3621a712009-04-06 18:47:14 -040010461 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010462 * @phba: Pointer to HBA context object.
10463 * @cmdiocb: Pointer to driver command iocb object.
10464 * @rspiocb: Pointer to driver response iocb object.
10465 *
10466 * The function is called from SLI ring event handler with no
10467 * lock held. This function is the completion handler for ELS commands
10468 * which are aborted. The function frees memory resources used for
10469 * the aborted ELS commands.
10470 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010471static void
10472lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10473 struct lpfc_iocbq *rspiocb)
10474{
10475 IOCB_t *irsp = &rspiocb->iocb;
10476
10477 /* ELS cmd tag <ulpIoTag> completes */
10478 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010479 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010480 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010481 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010482 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010483 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10484 lpfc_ct_free_iocb(phba, cmdiocb);
10485 else
10486 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010487 return;
10488}
10489
James Smarte59058c2008-08-24 21:49:00 -040010490/**
James Smart5af5eee2010-10-22 11:06:38 -040010491 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010492 * @phba: Pointer to HBA context object.
10493 * @pring: Pointer to driver SLI ring object.
10494 * @cmdiocb: Pointer to driver command iocb object.
10495 *
James Smart5af5eee2010-10-22 11:06:38 -040010496 * This function issues an abort iocb for the provided command iocb down to
10497 * the port. Other than the case the outstanding command iocb is an abort
10498 * request, this function issues abort out unconditionally. This function is
10499 * called with hbalock held. The function returns 0 when it fails due to
10500 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010501 **/
James Smart5af5eee2010-10-22 11:06:38 -040010502static int
10503lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010504 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010505{
James Smart2e0fef82007-06-17 19:56:36 -050010506 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010507 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010508 IOCB_t *icmd = NULL;
10509 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010510 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010511 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010512
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010513 lockdep_assert_held(&phba->hbalock);
10514
James Smart92d7f7b2007-06-17 19:56:38 -050010515 /*
10516 * There are certain command types we don't want to abort. And we
10517 * don't want to abort commands that are already in the process of
10518 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010519 */
10520 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010521 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010522 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10523 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010524 return 0;
10525
dea31012005-04-17 16:05:31 -050010526 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010527 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010528 if (abtsiocbp == NULL)
10529 return 0;
dea31012005-04-17 16:05:31 -050010530
James Smart07951072007-04-25 09:51:38 -040010531 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010532 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010533 */
10534 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10535
dea31012005-04-17 16:05:31 -050010536 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010537 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10538 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010539 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010540 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010541 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10542 }
James Smartda0436e2009-05-22 14:51:39 -040010543 else
10544 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010545 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010546 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010547
James Smart5ffc2662009-11-18 15:39:44 -050010548 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010549 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010550 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10551 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010552 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10553 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010554
James Smart2e0fef82007-06-17 19:56:36 -050010555 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010556 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10557 else
10558 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10559
10560 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010561 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010562
James Smarte8b62012007-08-02 11:10:09 -040010563 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10564 "0339 Abort xri x%x, original iotag x%x, "
10565 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010566 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010567 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010568 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010569
10570 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010571 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10572 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010573 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010574 /* Note: both hbalock and ring_lock need to be set here */
10575 spin_lock_irqsave(&pring->ring_lock, iflags);
10576 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10577 abtsiocbp, 0);
10578 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10579 } else {
10580 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10581 abtsiocbp, 0);
10582 }
James Smart07951072007-04-25 09:51:38 -040010583
James Smartd7c255b2008-08-24 21:50:00 -040010584 if (retval)
10585 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010586
10587 /*
10588 * Caller to this routine should check for IOCB_ERROR
10589 * and handle it properly. This routine no longer removes
10590 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10591 */
10592 return retval;
10593}
10594
10595/**
10596 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10597 * @phba: Pointer to HBA context object.
10598 * @pring: Pointer to driver SLI ring object.
10599 * @cmdiocb: Pointer to driver command iocb object.
10600 *
10601 * This function issues an abort iocb for the provided command iocb. In case
10602 * of unloading, the abort iocb will not be issued to commands on the ELS
10603 * ring. Instead, the callback function shall be changed to those commands
10604 * so that nothing happens when them finishes. This function is called with
10605 * hbalock held. The function returns 0 when the command iocb is an abort
10606 * request.
10607 **/
10608int
10609lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10610 struct lpfc_iocbq *cmdiocb)
10611{
10612 struct lpfc_vport *vport = cmdiocb->vport;
10613 int retval = IOCB_ERROR;
10614 IOCB_t *icmd = NULL;
10615
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010616 lockdep_assert_held(&phba->hbalock);
10617
James Smart5af5eee2010-10-22 11:06:38 -040010618 /*
10619 * There are certain command types we don't want to abort. And we
10620 * don't want to abort commands that are already in the process of
10621 * being aborted.
10622 */
10623 icmd = &cmdiocb->iocb;
10624 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10625 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10626 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10627 return 0;
10628
10629 /*
10630 * If we're unloading, don't abort iocb on the ELS ring, but change
10631 * the callback so that nothing happens when it finishes.
10632 */
10633 if ((vport->load_flag & FC_UNLOADING) &&
10634 (pring->ringno == LPFC_ELS_RING)) {
10635 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10636 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10637 else
10638 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10639 goto abort_iotag_exit;
10640 }
10641
10642 /* Now, we try to issue the abort to the cmdiocb out */
10643 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10644
James Smart07951072007-04-25 09:51:38 -040010645abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010646 /*
10647 * Caller to this routine should check for IOCB_ERROR
10648 * and handle it properly. This routine no longer removes
10649 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010650 */
James Smart2e0fef82007-06-17 19:56:36 -050010651 return retval;
dea31012005-04-17 16:05:31 -050010652}
10653
James Smarte59058c2008-08-24 21:49:00 -040010654/**
James Smart895427b2017-02-12 13:52:30 -080010655 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10656 * @phba: Pointer to HBA context object.
10657 * @pring: Pointer to driver SLI ring object.
10658 * @cmdiocb: Pointer to driver command iocb object.
10659 *
10660 * This function issues an abort iocb for the provided command iocb down to
10661 * the port. Other than the case the outstanding command iocb is an abort
10662 * request, this function issues abort out unconditionally. This function is
10663 * called with hbalock held. The function returns 0 when it fails due to
10664 * memory allocation failure or when the command iocb is an abort request.
10665 **/
10666static int
10667lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10668 struct lpfc_iocbq *cmdiocb)
10669{
10670 struct lpfc_vport *vport = cmdiocb->vport;
10671 struct lpfc_iocbq *abtsiocbp;
10672 union lpfc_wqe *abts_wqe;
10673 int retval;
10674
10675 /*
10676 * There are certain command types we don't want to abort. And we
10677 * don't want to abort commands that are already in the process of
10678 * being aborted.
10679 */
10680 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10681 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10682 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10683 return 0;
10684
10685 /* issue ABTS for this io based on iotag */
10686 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10687 if (abtsiocbp == NULL)
10688 return 0;
10689
10690 /* This signals the response to set the correct status
10691 * before calling the completion handler
10692 */
10693 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10694
10695 /* Complete prepping the abort wqe and issue to the FW. */
10696 abts_wqe = &abtsiocbp->wqe;
10697 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10698 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10699
10700 /* Explicitly set reserved fields to zero.*/
10701 abts_wqe->abort_cmd.rsrvd4 = 0;
10702 abts_wqe->abort_cmd.rsrvd5 = 0;
10703
10704 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10705 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10706 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10707
10708 /* word 7 */
10709 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10710 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10711 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10712 cmdiocb->iocb.ulpClass);
10713
10714 /* word 8 - tell the FW to abort the IO associated with this
10715 * outstanding exchange ID.
10716 */
10717 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10718
10719 /* word 9 - this is the iotag for the abts_wqe completion. */
10720 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10721 abtsiocbp->iotag);
10722
10723 /* word 10 */
10724 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10725 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10726 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10727
10728 /* word 11 */
10729 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10730 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10731 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10732
10733 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10734 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10735 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010736 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010737 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10738 if (retval == IOCB_ERROR) {
10739 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10740 "6147 Failed abts issue_wqe with status x%x "
10741 "for oxid x%x\n",
10742 retval, cmdiocb->sli4_xritag);
10743 lpfc_sli_release_iocbq(phba, abtsiocbp);
10744 return retval;
10745 }
10746
10747 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10748 "6148 Drv Abort NVME Request Issued for "
10749 "ox_id x%x on reqtag x%x\n",
10750 cmdiocb->sli4_xritag,
10751 abtsiocbp->iotag);
10752
10753 return retval;
10754}
10755
10756/**
James Smart5af5eee2010-10-22 11:06:38 -040010757 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10758 * @phba: pointer to lpfc HBA data structure.
10759 *
10760 * This routine will abort all pending and outstanding iocbs to an HBA.
10761 **/
10762void
10763lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10764{
10765 struct lpfc_sli *psli = &phba->sli;
10766 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010767 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010768 int i;
10769
James Smart895427b2017-02-12 13:52:30 -080010770 if (phba->sli_rev != LPFC_SLI_REV4) {
10771 for (i = 0; i < psli->num_rings; i++) {
10772 pring = &psli->sli3_ring[i];
10773 lpfc_sli_abort_iocb_ring(phba, pring);
10774 }
10775 return;
10776 }
10777 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10778 pring = qp->pring;
10779 if (!pring)
10780 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010781 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010782 }
10783}
10784
10785/**
James Smart3621a712009-04-06 18:47:14 -040010786 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010787 * @iocbq: Pointer to driver iocb object.
10788 * @vport: Pointer to driver virtual port object.
10789 * @tgt_id: SCSI ID of the target.
10790 * @lun_id: LUN ID of the scsi device.
10791 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10792 *
James Smart3621a712009-04-06 18:47:14 -040010793 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010794 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10795 * 0 if the filtering criteria is met for the given iocb and will return
10796 * 1 if the filtering criteria is not met.
10797 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10798 * given iocb is for the SCSI device specified by vport, tgt_id and
10799 * lun_id parameter.
10800 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10801 * given iocb is for the SCSI target specified by vport and tgt_id
10802 * parameters.
10803 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10804 * given iocb is for the SCSI host associated with the given vport.
10805 * This function is called with no locks held.
10806 **/
dea31012005-04-17 16:05:31 -050010807static int
James Smart51ef4c22007-08-02 11:10:31 -040010808lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10809 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010810 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010811{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010812 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010813 int rc = 1;
10814
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010815 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10816 return rc;
10817
James Smart51ef4c22007-08-02 11:10:31 -040010818 if (iocbq->vport != vport)
10819 return rc;
10820
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010821 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010822
James Smart495a7142008-06-14 22:52:59 -040010823 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010824 return rc;
10825
10826 switch (ctx_cmd) {
10827 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010828 if ((lpfc_cmd->rdata->pnode) &&
10829 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10830 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010831 rc = 0;
10832 break;
10833 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010834 if ((lpfc_cmd->rdata->pnode) &&
10835 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010836 rc = 0;
10837 break;
dea31012005-04-17 16:05:31 -050010838 case LPFC_CTX_HOST:
10839 rc = 0;
10840 break;
10841 default:
10842 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010843 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010844 break;
10845 }
10846
10847 return rc;
10848}
10849
James Smarte59058c2008-08-24 21:49:00 -040010850/**
James Smart3621a712009-04-06 18:47:14 -040010851 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010852 * @vport: Pointer to virtual port.
10853 * @tgt_id: SCSI ID of the target.
10854 * @lun_id: LUN ID of the scsi device.
10855 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10856 *
10857 * This function returns number of FCP commands pending for the vport.
10858 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10859 * commands pending on the vport associated with SCSI device specified
10860 * by tgt_id and lun_id parameters.
10861 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10862 * commands pending on the vport associated with SCSI target specified
10863 * by tgt_id parameter.
10864 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10865 * commands pending on the vport.
10866 * This function returns the number of iocbs which satisfy the filter.
10867 * This function is called without any lock held.
10868 **/
dea31012005-04-17 16:05:31 -050010869int
James Smart51ef4c22007-08-02 11:10:31 -040010870lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10871 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010872{
James Smart51ef4c22007-08-02 11:10:31 -040010873 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010874 struct lpfc_iocbq *iocbq;
10875 int sum, i;
dea31012005-04-17 16:05:31 -050010876
Johannes Thumshirn31979002016-07-18 16:06:03 +020010877 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010878 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10879 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010880
James Smart51ef4c22007-08-02 11:10:31 -040010881 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10882 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010883 sum++;
dea31012005-04-17 16:05:31 -050010884 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010885 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010886
dea31012005-04-17 16:05:31 -050010887 return sum;
10888}
10889
James Smarte59058c2008-08-24 21:49:00 -040010890/**
James Smart3621a712009-04-06 18:47:14 -040010891 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010892 * @phba: Pointer to HBA context object
10893 * @cmdiocb: Pointer to command iocb object.
10894 * @rspiocb: Pointer to response iocb object.
10895 *
10896 * This function is called when an aborted FCP iocb completes. This
10897 * function is called by the ring event handler with no lock held.
10898 * This function frees the iocb.
10899 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010900void
James Smart2e0fef82007-06-17 19:56:36 -050010901lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10902 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010903{
James Smartcb69f7d2011-12-13 13:21:57 -050010904 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010905 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010906 "original iotag x%x, abort cmd iotag x%x "
10907 "status 0x%x, reason 0x%x\n",
10908 cmdiocb->iocb.un.acxri.abortContextTag,
10909 cmdiocb->iocb.un.acxri.abortIoTag,
10910 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10911 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010912 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010913 return;
10914}
10915
James Smarte59058c2008-08-24 21:49:00 -040010916/**
James Smart3621a712009-04-06 18:47:14 -040010917 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010918 * @vport: Pointer to virtual port.
10919 * @pring: Pointer to driver SLI ring object.
10920 * @tgt_id: SCSI ID of the target.
10921 * @lun_id: LUN ID of the scsi device.
10922 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10923 *
10924 * This function sends an abort command for every SCSI command
10925 * associated with the given virtual port pending on the ring
10926 * filtered by lpfc_sli_validate_fcp_iocb function.
10927 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10928 * FCP iocbs associated with lun specified by tgt_id and lun_id
10929 * parameters
10930 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10931 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10932 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10933 * FCP iocbs associated with virtual port.
10934 * This function returns number of iocbs it failed to abort.
10935 * This function is called with no locks held.
10936 **/
dea31012005-04-17 16:05:31 -050010937int
James Smart51ef4c22007-08-02 11:10:31 -040010938lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10939 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010940{
James Smart51ef4c22007-08-02 11:10:31 -040010941 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010942 struct lpfc_iocbq *iocbq;
10943 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010944 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010945 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010946 int i;
dea31012005-04-17 16:05:31 -050010947
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010948 for (i = 1; i <= phba->sli.last_iotag; i++) {
10949 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010950
James Smart51ef4c22007-08-02 11:10:31 -040010951 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010952 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010953 continue;
10954
James Smartafbd8d82013-09-06 12:22:13 -040010955 /*
10956 * If the iocbq is already being aborted, don't take a second
10957 * action, but do count it.
10958 */
10959 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10960 continue;
10961
dea31012005-04-17 16:05:31 -050010962 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010963 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010964 if (abtsiocb == NULL) {
10965 errcnt++;
10966 continue;
10967 }
dea31012005-04-17 16:05:31 -050010968
James Smartafbd8d82013-09-06 12:22:13 -040010969 /* indicate the IO is being aborted by the driver. */
10970 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10971
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010972 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010973 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10974 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010975 if (phba->sli_rev == LPFC_SLI_REV4)
10976 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10977 else
10978 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010979 abtsiocb->iocb.ulpLe = 1;
10980 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010981 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010982
James Smart5ffc2662009-11-18 15:39:44 -050010983 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010984 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010985 if (iocbq->iocb_flag & LPFC_IO_FCP)
10986 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010987 if (iocbq->iocb_flag & LPFC_IO_FOF)
10988 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010989
James Smart2e0fef82007-06-17 19:56:36 -050010990 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010991 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10992 else
10993 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10994
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010995 /* Setup callback routine and issue the command. */
10996 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010997 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10998 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010999 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011000 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011001 errcnt++;
11002 continue;
11003 }
11004 }
11005
11006 return errcnt;
11007}
11008
James Smarte59058c2008-08-24 21:49:00 -040011009/**
James Smart98912dda2014-04-04 13:52:31 -040011010 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11011 * @vport: Pointer to virtual port.
11012 * @pring: Pointer to driver SLI ring object.
11013 * @tgt_id: SCSI ID of the target.
11014 * @lun_id: LUN ID of the scsi device.
11015 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11016 *
11017 * This function sends an abort command for every SCSI command
11018 * associated with the given virtual port pending on the ring
11019 * filtered by lpfc_sli_validate_fcp_iocb function.
11020 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11021 * FCP iocbs associated with lun specified by tgt_id and lun_id
11022 * parameters
11023 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11024 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11025 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11026 * FCP iocbs associated with virtual port.
11027 * This function returns number of iocbs it aborted .
11028 * This function is called with no locks held right after a taskmgmt
11029 * command is sent.
11030 **/
11031int
11032lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11033 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11034{
11035 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011036 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011037 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011038 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011039 struct lpfc_iocbq *iocbq;
11040 IOCB_t *icmd;
11041 int sum, i, ret_val;
11042 unsigned long iflags;
11043 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011044
11045 spin_lock_irq(&phba->hbalock);
11046
11047 /* all I/Os are in process of being flushed */
11048 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11049 spin_unlock_irq(&phba->hbalock);
11050 return 0;
11051 }
11052 sum = 0;
11053
11054 for (i = 1; i <= phba->sli.last_iotag; i++) {
11055 iocbq = phba->sli.iocbq_lookup[i];
11056
11057 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11058 cmd) != 0)
11059 continue;
11060
11061 /*
11062 * If the iocbq is already being aborted, don't take a second
11063 * action, but do count it.
11064 */
11065 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11066 continue;
11067
11068 /* issue ABTS for this IOCB based on iotag */
11069 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11070 if (abtsiocbq == NULL)
11071 continue;
11072
11073 icmd = &iocbq->iocb;
11074 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11075 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11076 if (phba->sli_rev == LPFC_SLI_REV4)
11077 abtsiocbq->iocb.un.acxri.abortIoTag =
11078 iocbq->sli4_xritag;
11079 else
11080 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11081 abtsiocbq->iocb.ulpLe = 1;
11082 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11083 abtsiocbq->vport = vport;
11084
11085 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011086 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011087 if (iocbq->iocb_flag & LPFC_IO_FCP)
11088 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011089 if (iocbq->iocb_flag & LPFC_IO_FOF)
11090 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011091
James Smart8c50d252014-09-03 12:58:16 -040011092 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11093 ndlp = lpfc_cmd->rdata->pnode;
11094
11095 if (lpfc_is_link_up(phba) &&
11096 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011097 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11098 else
11099 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11100
11101 /* Setup callback routine and issue the command. */
11102 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11103
11104 /*
11105 * Indicate the IO is being aborted by the driver and set
11106 * the caller's flag into the aborted IO.
11107 */
11108 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11109
11110 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011111 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11112 if (pring_s4 == NULL)
11113 continue;
James Smart98912dda2014-04-04 13:52:31 -040011114 /* Note: both hbalock and ring_lock must be set here */
11115 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11116 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11117 abtsiocbq, 0);
11118 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11119 } else {
11120 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11121 abtsiocbq, 0);
11122 }
11123
11124
11125 if (ret_val == IOCB_ERROR)
11126 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11127 else
11128 sum++;
11129 }
11130 spin_unlock_irq(&phba->hbalock);
11131 return sum;
11132}
11133
11134/**
James Smart3621a712009-04-06 18:47:14 -040011135 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011136 * @phba: Pointer to HBA context object.
11137 * @cmdiocbq: Pointer to command iocb.
11138 * @rspiocbq: Pointer to response iocb.
11139 *
11140 * This function is the completion handler for iocbs issued using
11141 * lpfc_sli_issue_iocb_wait function. This function is called by the
11142 * ring event handler function without any lock held. This function
11143 * can be called from both worker thread context and interrupt
11144 * context. This function also can be called from other thread which
11145 * cleans up the SLI layer objects.
11146 * This function copy the contents of the response iocb to the
11147 * response iocb memory object provided by the caller of
11148 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11149 * sleeps for the iocb completion.
11150 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011151static void
11152lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11153 struct lpfc_iocbq *cmdiocbq,
11154 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011155{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011156 wait_queue_head_t *pdone_q;
11157 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011158 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011159
James Smart2e0fef82007-06-17 19:56:36 -050011160 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011161 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11162
11163 /*
11164 * A time out has occurred for the iocb. If a time out
11165 * completion handler has been supplied, call it. Otherwise,
11166 * just free the iocbq.
11167 */
11168
11169 spin_unlock_irqrestore(&phba->hbalock, iflags);
11170 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11171 cmdiocbq->wait_iocb_cmpl = NULL;
11172 if (cmdiocbq->iocb_cmpl)
11173 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11174 else
11175 lpfc_sli_release_iocbq(phba, cmdiocbq);
11176 return;
11177 }
11178
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011179 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11180 if (cmdiocbq->context2 && rspiocbq)
11181 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11182 &rspiocbq->iocb, sizeof(IOCB_t));
11183
James Smart0f65ff62010-02-26 14:14:23 -050011184 /* Set the exchange busy flag for task management commands */
11185 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11186 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11187 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11188 cur_iocbq);
11189 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11190 }
11191
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011192 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011193 if (pdone_q)
11194 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011195 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011196 return;
11197}
11198
James Smarte59058c2008-08-24 21:49:00 -040011199/**
James Smartd11e31d2009-06-10 17:23:06 -040011200 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11201 * @phba: Pointer to HBA context object..
11202 * @piocbq: Pointer to command iocb.
11203 * @flag: Flag to test.
11204 *
11205 * This routine grabs the hbalock and then test the iocb_flag to
11206 * see if the passed in flag is set.
11207 * Returns:
11208 * 1 if flag is set.
11209 * 0 if flag is not set.
11210 **/
11211static int
11212lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11213 struct lpfc_iocbq *piocbq, uint32_t flag)
11214{
11215 unsigned long iflags;
11216 int ret;
11217
11218 spin_lock_irqsave(&phba->hbalock, iflags);
11219 ret = piocbq->iocb_flag & flag;
11220 spin_unlock_irqrestore(&phba->hbalock, iflags);
11221 return ret;
11222
11223}
11224
11225/**
James Smart3621a712009-04-06 18:47:14 -040011226 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011227 * @phba: Pointer to HBA context object..
11228 * @pring: Pointer to sli ring.
11229 * @piocb: Pointer to command iocb.
11230 * @prspiocbq: Pointer to response iocb.
11231 * @timeout: Timeout in number of seconds.
11232 *
11233 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011234 * iocb to complete. The iocb_cmpl field of the shall be used
11235 * to handle iocbs which time out. If the field is NULL, the
11236 * function shall free the iocbq structure. If more clean up is
11237 * needed, the caller is expected to provide a completion function
11238 * that will provide the needed clean up. If the iocb command is
11239 * not completed within timeout seconds, the function will either
11240 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11241 * completion function set in the iocb_cmpl field and then return
11242 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11243 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011244 * The function waits for the iocb completion using an
11245 * non-interruptible wait.
11246 * This function will sleep while waiting for iocb completion.
11247 * So, this function should not be called from any context which
11248 * does not allow sleeping. Due to the same reason, this function
11249 * cannot be called with interrupt disabled.
11250 * This function assumes that the iocb completions occur while
11251 * this function sleep. So, this function cannot be called from
11252 * the thread which process iocb completion for this ring.
11253 * This function clears the iocb_flag of the iocb object before
11254 * issuing the iocb and the iocb completion handler sets this
11255 * flag and wakes this thread when the iocb completes.
11256 * The contents of the response iocb will be copied to prspiocbq
11257 * by the completion handler when the command completes.
11258 * This function returns IOCB_SUCCESS when success.
11259 * This function is called with no lock held.
11260 **/
dea31012005-04-17 16:05:31 -050011261int
James Smart2e0fef82007-06-17 19:56:36 -050011262lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011263 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011264 struct lpfc_iocbq *piocb,
11265 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011266 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011267{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011268 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011269 long timeleft, timeout_req = 0;
11270 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011271 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011272 struct lpfc_iocbq *iocb;
11273 int txq_cnt = 0;
11274 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011275 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011276 unsigned long iflags;
11277 bool iocb_completed = true;
11278
James Smart895427b2017-02-12 13:52:30 -080011279 if (phba->sli_rev >= LPFC_SLI_REV4)
11280 pring = lpfc_sli4_calc_ring(phba, piocb);
11281 else
11282 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011283 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011284 * If the caller has provided a response iocbq buffer, then context2
11285 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011286 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011287 if (prspiocbq) {
11288 if (piocb->context2)
11289 return IOCB_ERROR;
11290 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011291 }
11292
James Smart5a0916b2013-07-15 18:31:42 -040011293 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011294 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11295 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011296 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011297
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011298 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011299 if (lpfc_readl(phba->HCregaddr, &creg_val))
11300 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011301 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11302 writel(creg_val, phba->HCregaddr);
11303 readl(phba->HCregaddr); /* flush */
11304 }
11305
James Smart2a9bf3d2010-06-07 15:24:45 -040011306 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11307 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011308 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011309 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011310 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011311 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011312 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011313 spin_lock_irqsave(&phba->hbalock, iflags);
11314 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011315
James Smart5a0916b2013-07-15 18:31:42 -040011316 /*
11317 * IOCB timed out. Inform the wake iocb wait
11318 * completion function and set local status
11319 */
11320
11321 iocb_completed = false;
11322 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11323 }
11324 spin_unlock_irqrestore(&phba->hbalock, iflags);
11325 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011326 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011327 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011328 /* Note: we are not indicating if the IOCB has a success
11329 * status or not - that's for the caller to check.
11330 * IOCB_SUCCESS means just that the command was sent and
11331 * completed. Not that it completed successfully.
11332 * */
James Smart7054a602007-04-25 09:52:34 -040011333 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011334 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011335 "0338 IOCB wait timeout error - no "
11336 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011337 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011338 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011339 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011340 "0330 IOCB wake NOT set, "
11341 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011342 timeout, (timeleft / jiffies));
11343 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011344 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011345 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011346 if (phba->cfg_log_verbose & LOG_SLI) {
11347 list_for_each_entry(iocb, &pring->txq, list) {
11348 txq_cnt++;
11349 }
11350 list_for_each_entry(iocb, &pring->txcmplq, list) {
11351 txcmplq_cnt++;
11352 }
11353 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11354 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11355 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11356 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011357 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011358 } else {
11359 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011360 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011361 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011362 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011363 }
11364
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011365 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011366 if (lpfc_readl(phba->HCregaddr, &creg_val))
11367 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011368 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11369 writel(creg_val, phba->HCregaddr);
11370 readl(phba->HCregaddr); /* flush */
11371 }
11372
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011373 if (prspiocbq)
11374 piocb->context2 = NULL;
11375
11376 piocb->context_un.wait_queue = NULL;
11377 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011378 return retval;
11379}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011380
James Smarte59058c2008-08-24 21:49:00 -040011381/**
James Smart3621a712009-04-06 18:47:14 -040011382 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011383 * @phba: Pointer to HBA context object.
11384 * @pmboxq: Pointer to driver mailbox object.
11385 * @timeout: Timeout in number of seconds.
11386 *
11387 * This function issues the mailbox to firmware and waits for the
11388 * mailbox command to complete. If the mailbox command is not
11389 * completed within timeout seconds, it returns MBX_TIMEOUT.
11390 * The function waits for the mailbox completion using an
11391 * interruptible wait. If the thread is woken up due to a
11392 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11393 * should not free the mailbox resources, if this function returns
11394 * MBX_TIMEOUT.
11395 * This function will sleep while waiting for mailbox completion.
11396 * So, this function should not be called from any context which
11397 * does not allow sleeping. Due to the same reason, this function
11398 * cannot be called with interrupt disabled.
11399 * This function assumes that the mailbox completion occurs while
11400 * this function sleep. So, this function cannot be called from
11401 * the worker thread which processes mailbox completion.
11402 * This function is called in the context of HBA management
11403 * applications.
11404 * This function returns MBX_SUCCESS when successful.
11405 * This function is called with no lock held.
11406 **/
dea31012005-04-17 16:05:31 -050011407int
James Smart2e0fef82007-06-17 19:56:36 -050011408lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011409 uint32_t timeout)
11410{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011411 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011412 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011413 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011414 unsigned long flag;
dea31012005-04-17 16:05:31 -050011415
James Smartb230b8a2013-05-31 17:05:27 -040011416 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011417 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011418 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011419
James Smart495a7142008-06-14 22:52:59 -040011420 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011421 /* setup wake call as IOCB callback */
11422 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11423 /* setup context field to pass wait_queue pointer to wake function */
11424 pmboxq->context1 = &done_q;
11425
dea31012005-04-17 16:05:31 -050011426 /* now issue the command */
11427 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011428 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011429 wait_event_interruptible_timeout(done_q,
11430 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011431 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011432
James Smart858c9f62007-06-17 19:56:39 -050011433 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011434 /* restore the possible extended buffer for free resource */
11435 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011436 /*
11437 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11438 * else do not free the resources.
11439 */
James Smartd7c47992010-06-08 18:31:54 -040011440 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011441 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011442 } else {
James Smart7054a602007-04-25 09:52:34 -040011443 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011444 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11445 }
11446 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011447 } else {
11448 /* restore the possible extended buffer for free resource */
11449 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011450 }
11451
dea31012005-04-17 16:05:31 -050011452 return retval;
11453}
11454
James Smarte59058c2008-08-24 21:49:00 -040011455/**
James Smart3772a992009-05-22 14:50:54 -040011456 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011457 * @phba: Pointer to HBA context.
11458 *
James Smart3772a992009-05-22 14:50:54 -040011459 * This function is called to shutdown the driver's mailbox sub-system.
11460 * It first marks the mailbox sub-system is in a block state to prevent
11461 * the asynchronous mailbox command from issued off the pending mailbox
11462 * command queue. If the mailbox command sub-system shutdown is due to
11463 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11464 * the mailbox sub-system flush routine to forcefully bring down the
11465 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11466 * as with offline or HBA function reset), this routine will wait for the
11467 * outstanding mailbox command to complete before invoking the mailbox
11468 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011469 **/
James Smart3772a992009-05-22 14:50:54 -040011470void
James Smart618a5232012-06-12 13:54:36 -040011471lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011472{
James Smart3772a992009-05-22 14:50:54 -040011473 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011474 unsigned long timeout;
11475
James Smart618a5232012-06-12 13:54:36 -040011476 if (mbx_action == LPFC_MBX_NO_WAIT) {
11477 /* delay 100ms for port state */
11478 msleep(100);
11479 lpfc_sli_mbox_sys_flush(phba);
11480 return;
11481 }
James Smarta183a152011-10-10 21:32:43 -040011482 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011483
James Smart3772a992009-05-22 14:50:54 -040011484 spin_lock_irq(&phba->hbalock);
11485 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011486
11487 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011488 /* Determine how long we might wait for the active mailbox
11489 * command to be gracefully completed by firmware.
11490 */
James Smarta183a152011-10-10 21:32:43 -040011491 if (phba->sli.mbox_active)
11492 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11493 phba->sli.mbox_active) *
11494 1000) + jiffies;
11495 spin_unlock_irq(&phba->hbalock);
11496
James Smart3772a992009-05-22 14:50:54 -040011497 while (phba->sli.mbox_active) {
11498 /* Check active mailbox complete status every 2ms */
11499 msleep(2);
11500 if (time_after(jiffies, timeout))
11501 /* Timeout, let the mailbox flush routine to
11502 * forcefully release active mailbox command
11503 */
11504 break;
11505 }
James Smartd7069f02012-03-01 22:36:29 -050011506 } else
11507 spin_unlock_irq(&phba->hbalock);
11508
James Smart3772a992009-05-22 14:50:54 -040011509 lpfc_sli_mbox_sys_flush(phba);
11510}
11511
11512/**
11513 * lpfc_sli_eratt_read - read sli-3 error attention events
11514 * @phba: Pointer to HBA context.
11515 *
11516 * This function is called to read the SLI3 device error attention registers
11517 * for possible error attention events. The caller must hold the hostlock
11518 * with spin_lock_irq().
11519 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011520 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011521 * Register and returns 0 otherwise.
11522 **/
11523static int
11524lpfc_sli_eratt_read(struct lpfc_hba *phba)
11525{
James Smarted957682007-06-17 19:56:37 -050011526 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011527
James Smart3772a992009-05-22 14:50:54 -040011528 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011529 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11530 goto unplug_err;
11531
James Smart3772a992009-05-22 14:50:54 -040011532 if (ha_copy & HA_ERATT) {
11533 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011534 if (lpfc_sli_read_hs(phba))
11535 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011536
James Smart3772a992009-05-22 14:50:54 -040011537 /* Check if there is a deferred error condition is active */
11538 if ((HS_FFER1 & phba->work_hs) &&
11539 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011540 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011541 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011542 /* Clear all interrupt enable conditions */
11543 writel(0, phba->HCregaddr);
11544 readl(phba->HCregaddr);
11545 }
11546
11547 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011548 phba->work_ha |= HA_ERATT;
11549 /* Indicate polling handles this ERATT */
11550 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011551 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011552 }
James Smart3772a992009-05-22 14:50:54 -040011553 return 0;
James Smart9940b972011-03-11 16:06:12 -050011554
11555unplug_err:
11556 /* Set the driver HS work bitmap */
11557 phba->work_hs |= UNPLUG_ERR;
11558 /* Set the driver HA work bitmap */
11559 phba->work_ha |= HA_ERATT;
11560 /* Indicate polling handles this ERATT */
11561 phba->hba_flag |= HBA_ERATT_HANDLED;
11562 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011563}
11564
James Smarte59058c2008-08-24 21:49:00 -040011565/**
James Smartda0436e2009-05-22 14:51:39 -040011566 * lpfc_sli4_eratt_read - read sli-4 error attention events
11567 * @phba: Pointer to HBA context.
11568 *
11569 * This function is called to read the SLI4 device error attention registers
11570 * for possible error attention events. The caller must hold the hostlock
11571 * with spin_lock_irq().
11572 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011573 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011574 * Register and returns 0 otherwise.
11575 **/
11576static int
11577lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11578{
11579 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011580 uint32_t if_type, portsmphr;
11581 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011582
James Smart2fcee4b2010-12-15 17:57:46 -050011583 /*
11584 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011585 * registers for error attention. This can be changed later.
11586 */
James Smart2fcee4b2010-12-15 17:57:46 -050011587 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11588 switch (if_type) {
11589 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011590 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11591 &uerr_sta_lo) ||
11592 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11593 &uerr_sta_hi)) {
11594 phba->work_hs |= UNPLUG_ERR;
11595 phba->work_ha |= HA_ERATT;
11596 phba->hba_flag |= HBA_ERATT_HANDLED;
11597 return 1;
11598 }
James Smart2fcee4b2010-12-15 17:57:46 -050011599 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11600 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11602 "1423 HBA Unrecoverable error: "
11603 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11604 "ue_mask_lo_reg=0x%x, "
11605 "ue_mask_hi_reg=0x%x\n",
11606 uerr_sta_lo, uerr_sta_hi,
11607 phba->sli4_hba.ue_mask_lo,
11608 phba->sli4_hba.ue_mask_hi);
11609 phba->work_status[0] = uerr_sta_lo;
11610 phba->work_status[1] = uerr_sta_hi;
11611 phba->work_ha |= HA_ERATT;
11612 phba->hba_flag |= HBA_ERATT_HANDLED;
11613 return 1;
11614 }
11615 break;
11616 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011617 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11618 &portstat_reg.word0) ||
11619 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11620 &portsmphr)){
11621 phba->work_hs |= UNPLUG_ERR;
11622 phba->work_ha |= HA_ERATT;
11623 phba->hba_flag |= HBA_ERATT_HANDLED;
11624 return 1;
11625 }
James Smart2fcee4b2010-12-15 17:57:46 -050011626 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11627 phba->work_status[0] =
11628 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11629 phba->work_status[1] =
11630 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011632 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011633 "port status reg 0x%x, "
11634 "port smphr reg 0x%x, "
11635 "error 1=0x%x, error 2=0x%x\n",
11636 portstat_reg.word0,
11637 portsmphr,
11638 phba->work_status[0],
11639 phba->work_status[1]);
11640 phba->work_ha |= HA_ERATT;
11641 phba->hba_flag |= HBA_ERATT_HANDLED;
11642 return 1;
11643 }
11644 break;
11645 case LPFC_SLI_INTF_IF_TYPE_1:
11646 default:
James Smarta747c9c2009-11-18 15:41:10 -050011647 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011648 "2886 HBA Error Attention on unsupported "
11649 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011650 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011651 }
James Smart2fcee4b2010-12-15 17:57:46 -050011652
James Smartda0436e2009-05-22 14:51:39 -040011653 return 0;
11654}
11655
11656/**
James Smart3621a712009-04-06 18:47:14 -040011657 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011658 * @phba: Pointer to HBA context.
11659 *
James Smart3772a992009-05-22 14:50:54 -040011660 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011661 * error attention register bit for error attention events.
11662 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011663 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011664 * Register and returns 0 otherwise.
11665 **/
11666int
11667lpfc_sli_check_eratt(struct lpfc_hba *phba)
11668{
11669 uint32_t ha_copy;
11670
11671 /* If somebody is waiting to handle an eratt, don't process it
11672 * here. The brdkill function will do this.
11673 */
11674 if (phba->link_flag & LS_IGNORE_ERATT)
11675 return 0;
11676
11677 /* Check if interrupt handler handles this ERATT */
11678 spin_lock_irq(&phba->hbalock);
11679 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11680 /* Interrupt handler has handled ERATT */
11681 spin_unlock_irq(&phba->hbalock);
11682 return 0;
11683 }
11684
James Smarta257bf92009-04-06 18:48:10 -040011685 /*
11686 * If there is deferred error attention, do not check for error
11687 * attention
11688 */
11689 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11690 spin_unlock_irq(&phba->hbalock);
11691 return 0;
11692 }
11693
James Smart3772a992009-05-22 14:50:54 -040011694 /* If PCI channel is offline, don't process it */
11695 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011696 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011697 return 0;
11698 }
11699
11700 switch (phba->sli_rev) {
11701 case LPFC_SLI_REV2:
11702 case LPFC_SLI_REV3:
11703 /* Read chip Host Attention (HA) register */
11704 ha_copy = lpfc_sli_eratt_read(phba);
11705 break;
James Smartda0436e2009-05-22 14:51:39 -040011706 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011707 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011708 ha_copy = lpfc_sli4_eratt_read(phba);
11709 break;
James Smart3772a992009-05-22 14:50:54 -040011710 default:
11711 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11712 "0299 Invalid SLI revision (%d)\n",
11713 phba->sli_rev);
11714 ha_copy = 0;
11715 break;
James Smart93996272008-08-24 21:50:30 -040011716 }
11717 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011718
11719 return ha_copy;
11720}
11721
11722/**
11723 * lpfc_intr_state_check - Check device state for interrupt handling
11724 * @phba: Pointer to HBA context.
11725 *
11726 * This inline routine checks whether a device or its PCI slot is in a state
11727 * that the interrupt should be handled.
11728 *
11729 * This function returns 0 if the device or the PCI slot is in a state that
11730 * interrupt should be handled, otherwise -EIO.
11731 */
11732static inline int
11733lpfc_intr_state_check(struct lpfc_hba *phba)
11734{
11735 /* If the pci channel is offline, ignore all the interrupts */
11736 if (unlikely(pci_channel_offline(phba->pcidev)))
11737 return -EIO;
11738
11739 /* Update device level interrupt statistics */
11740 phba->sli.slistat.sli_intr++;
11741
11742 /* Ignore all interrupts during initialization. */
11743 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11744 return -EIO;
11745
James Smart93996272008-08-24 21:50:30 -040011746 return 0;
11747}
11748
11749/**
James Smart3772a992009-05-22 14:50:54 -040011750 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011751 * @irq: Interrupt number.
11752 * @dev_id: The device context pointer.
11753 *
James Smart93996272008-08-24 21:50:30 -040011754 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011755 * service routine when device with SLI-3 interface spec is enabled with
11756 * MSI-X multi-message interrupt mode and there are slow-path events in
11757 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11758 * interrupt mode, this function is called as part of the device-level
11759 * interrupt handler. When the PCI slot is in error recovery or the HBA
11760 * is undergoing initialization, the interrupt handler will not process
11761 * the interrupt. The link attention and ELS ring attention events are
11762 * handled by the worker thread. The interrupt handler signals the worker
11763 * thread and returns for these events. This function is called without
11764 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011765 * structures.
11766 *
11767 * This function returns IRQ_HANDLED when interrupt is handled else it
11768 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011769 **/
dea31012005-04-17 16:05:31 -050011770irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011771lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011772{
James Smart2e0fef82007-06-17 19:56:36 -050011773 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011774 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011775 uint32_t work_ha_copy;
11776 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011777 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011778 uint32_t control;
11779
James Smart92d7f7b2007-06-17 19:56:38 -050011780 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011781 struct lpfc_vport *vport;
11782 struct lpfc_nodelist *ndlp;
11783 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011784 LPFC_MBOXQ_t *pmb;
11785 int rc;
11786
dea31012005-04-17 16:05:31 -050011787 /*
11788 * Get the driver's phba structure from the dev_id and
11789 * assume the HBA is not interrupting.
11790 */
James Smart93996272008-08-24 21:50:30 -040011791 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011792
11793 if (unlikely(!phba))
11794 return IRQ_NONE;
11795
dea31012005-04-17 16:05:31 -050011796 /*
James Smart93996272008-08-24 21:50:30 -040011797 * Stuff needs to be attented to when this function is invoked as an
11798 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011799 */
James Smart93996272008-08-24 21:50:30 -040011800 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011801 /* Check device state for handling interrupt */
11802 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011803 return IRQ_NONE;
11804 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011805 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011806 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11807 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011808 /* If somebody is waiting to handle an eratt don't process it
11809 * here. The brdkill function will do this.
11810 */
11811 if (phba->link_flag & LS_IGNORE_ERATT)
11812 ha_copy &= ~HA_ERATT;
11813 /* Check the need for handling ERATT in interrupt handler */
11814 if (ha_copy & HA_ERATT) {
11815 if (phba->hba_flag & HBA_ERATT_HANDLED)
11816 /* ERATT polling has handled ERATT */
11817 ha_copy &= ~HA_ERATT;
11818 else
11819 /* Indicate interrupt handler handles ERATT */
11820 phba->hba_flag |= HBA_ERATT_HANDLED;
11821 }
James Smarta257bf92009-04-06 18:48:10 -040011822
11823 /*
11824 * If there is deferred error attention, do not check for any
11825 * interrupt.
11826 */
11827 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011828 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011829 return IRQ_NONE;
11830 }
11831
James Smart93996272008-08-24 21:50:30 -040011832 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011833 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11834 goto unplug_error;
11835
James Smarta747c9c2009-11-18 15:41:10 -050011836 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11837 HC_LAINT_ENA | HC_ERINT_ENA),
11838 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011839 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11840 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011841 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011842 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011843 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011844 } else
11845 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011846
dea31012005-04-17 16:05:31 -050011847 work_ha_copy = ha_copy & phba->work_ha_mask;
11848
James Smart93996272008-08-24 21:50:30 -040011849 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011850 if (work_ha_copy & HA_LATT) {
11851 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11852 /*
11853 * Turn off Link Attention interrupts
11854 * until CLEAR_LA done
11855 */
James Smart5b75da22008-12-04 22:39:35 -050011856 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011857 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011858 if (lpfc_readl(phba->HCregaddr, &control))
11859 goto unplug_error;
dea31012005-04-17 16:05:31 -050011860 control &= ~HC_LAINT_ENA;
11861 writel(control, phba->HCregaddr);
11862 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011863 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011864 }
11865 else
11866 work_ha_copy &= ~HA_LATT;
11867 }
11868
James Smart93996272008-08-24 21:50:30 -040011869 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011870 /*
11871 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11872 * the only slow ring.
11873 */
11874 status = (work_ha_copy &
11875 (HA_RXMASK << (4*LPFC_ELS_RING)));
11876 status >>= (4*LPFC_ELS_RING);
11877 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011878 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011879 if (lpfc_readl(phba->HCregaddr, &control))
11880 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011881
11882 lpfc_debugfs_slow_ring_trc(phba,
11883 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11884 control, status,
11885 (uint32_t)phba->sli.slistat.sli_intr);
11886
James Smart858c9f62007-06-17 19:56:39 -050011887 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011888 lpfc_debugfs_slow_ring_trc(phba,
11889 "ISR Disable ring:"
11890 "pwork:x%x hawork:x%x wait:x%x",
11891 phba->work_ha, work_ha_copy,
11892 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011893 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011894
James Smart858c9f62007-06-17 19:56:39 -050011895 control &=
11896 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011897 writel(control, phba->HCregaddr);
11898 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011899 }
James Smarta58cbd52007-08-02 11:09:43 -040011900 else {
11901 lpfc_debugfs_slow_ring_trc(phba,
11902 "ISR slow ring: pwork:"
11903 "x%x hawork:x%x wait:x%x",
11904 phba->work_ha, work_ha_copy,
11905 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011906 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011907 }
James Smart5b75da22008-12-04 22:39:35 -050011908 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011909 }
11910 }
James Smart5b75da22008-12-04 22:39:35 -050011911 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011912 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011913 if (lpfc_sli_read_hs(phba))
11914 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011915 /*
11916 * Check if there is a deferred error condition
11917 * is active
11918 */
11919 if ((HS_FFER1 & phba->work_hs) &&
11920 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011921 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11922 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011923 phba->hba_flag |= DEFER_ERATT;
11924 /* Clear all interrupt enable conditions */
11925 writel(0, phba->HCregaddr);
11926 readl(phba->HCregaddr);
11927 }
11928 }
11929
James Smart93996272008-08-24 21:50:30 -040011930 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011931 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011932 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011933 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011934 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011935
11936 /* First check out the status word */
11937 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11938 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011939 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011940 /*
11941 * Stray Mailbox Interrupt, mbxCommand <cmd>
11942 * mbxStatus <status>
11943 */
James Smart09372822008-01-11 01:52:54 -050011944 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011945 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011946 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011947 "Interrupt mbxCommand x%x "
11948 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011949 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011950 pmbox->mbxCommand,
11951 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011952 /* clear mailbox attention bit */
11953 work_ha_copy &= ~HA_MBATT;
11954 } else {
James Smart97eab632008-04-07 10:16:05 -040011955 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011956 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011957 phba->last_completion_time = jiffies;
11958 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011959 if (pmb->mbox_cmpl) {
11960 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11961 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011962 if (pmb->out_ext_byte_len &&
11963 pmb->context2)
11964 lpfc_sli_pcimem_bcopy(
11965 phba->mbox_ext,
11966 pmb->context2,
11967 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011968 }
James Smart09372822008-01-11 01:52:54 -050011969 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11970 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11971
11972 lpfc_debugfs_disc_trc(vport,
11973 LPFC_DISC_TRC_MBOX_VPORT,
11974 "MBOX dflt rpi: : "
11975 "status:x%x rpi:x%x",
11976 (uint32_t)pmbox->mbxStatus,
11977 pmbox->un.varWords[0], 0);
11978
11979 if (!pmbox->mbxStatus) {
11980 mp = (struct lpfc_dmabuf *)
11981 (pmb->context1);
11982 ndlp = (struct lpfc_nodelist *)
11983 pmb->context2;
11984
11985 /* Reg_LOGIN of dflt RPI was
11986 * successful. new lets get
11987 * rid of the RPI using the
11988 * same mbox buffer.
11989 */
11990 lpfc_unreg_login(phba,
11991 vport->vpi,
11992 pmbox->un.varWords[0],
11993 pmb);
11994 pmb->mbox_cmpl =
11995 lpfc_mbx_cmpl_dflt_rpi;
11996 pmb->context1 = mp;
11997 pmb->context2 = ndlp;
11998 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011999 rc = lpfc_sli_issue_mbox(phba,
12000 pmb,
12001 MBX_NOWAIT);
12002 if (rc != MBX_BUSY)
12003 lpfc_printf_log(phba,
12004 KERN_ERR,
12005 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012006 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012007 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012008 if (rc != MBX_NOT_FINISHED)
12009 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012010 }
12011 }
James Smart5b75da22008-12-04 22:39:35 -050012012 spin_lock_irqsave(
12013 &phba->pport->work_port_lock,
12014 iflag);
James Smart09372822008-01-11 01:52:54 -050012015 phba->pport->work_port_events &=
12016 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012017 spin_unlock_irqrestore(
12018 &phba->pport->work_port_lock,
12019 iflag);
James Smart09372822008-01-11 01:52:54 -050012020 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012021 }
James Smart97eab632008-04-07 10:16:05 -040012022 } else
James Smart5b75da22008-12-04 22:39:35 -050012023 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012024
James Smart92d7f7b2007-06-17 19:56:38 -050012025 if ((work_ha_copy & HA_MBATT) &&
12026 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012027send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012028 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012029 do {
12030 rc = lpfc_sli_issue_mbox(phba, NULL,
12031 MBX_NOWAIT);
12032 } while (rc == MBX_NOT_FINISHED);
12033 if (rc != MBX_SUCCESS)
12034 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12035 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012036 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012037 }
12038
James Smart5b75da22008-12-04 22:39:35 -050012039 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012040 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012041 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012042 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012043 }
James Smart93996272008-08-24 21:50:30 -040012044 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012045unplug_error:
12046 spin_unlock_irqrestore(&phba->hbalock, iflag);
12047 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012048
James Smart3772a992009-05-22 14:50:54 -040012049} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012050
12051/**
James Smart3772a992009-05-22 14:50:54 -040012052 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012053 * @irq: Interrupt number.
12054 * @dev_id: The device context pointer.
12055 *
12056 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012057 * service routine when device with SLI-3 interface spec is enabled with
12058 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12059 * ring event in the HBA. However, when the device is enabled with either
12060 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12061 * device-level interrupt handler. When the PCI slot is in error recovery
12062 * or the HBA is undergoing initialization, the interrupt handler will not
12063 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12064 * the intrrupt context. This function is called without any lock held.
12065 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012066 *
12067 * This function returns IRQ_HANDLED when interrupt is handled else it
12068 * returns IRQ_NONE.
12069 **/
12070irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012071lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012072{
12073 struct lpfc_hba *phba;
12074 uint32_t ha_copy;
12075 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012076 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012077 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012078
12079 /* Get the driver's phba structure from the dev_id and
12080 * assume the HBA is not interrupting.
12081 */
12082 phba = (struct lpfc_hba *) dev_id;
12083
12084 if (unlikely(!phba))
12085 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012086
12087 /*
James Smart93996272008-08-24 21:50:30 -040012088 * Stuff needs to be attented to when this function is invoked as an
12089 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012090 */
James Smart93996272008-08-24 21:50:30 -040012091 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012092 /* Check device state for handling interrupt */
12093 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012094 return IRQ_NONE;
12095 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012096 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12097 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012098 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012099 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012100 /*
12101 * If there is deferred error attention, do not check for
12102 * any interrupt.
12103 */
12104 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012105 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012106 return IRQ_NONE;
12107 }
James Smart93996272008-08-24 21:50:30 -040012108 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12109 phba->HAregaddr);
12110 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012111 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012112 } else
12113 ha_copy = phba->ha_copy;
12114
12115 /*
12116 * Process all events on FCP ring. Take the optimized path for FCP IO.
12117 */
12118 ha_copy &= ~(phba->work_ha_mask);
12119
12120 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012121 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012122 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012123 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012124 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012125
12126 if (phba->cfg_multi_ring_support == 2) {
12127 /*
James Smart93996272008-08-24 21:50:30 -040012128 * Process all events on extra ring. Take the optimized path
12129 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012130 */
James Smart93996272008-08-24 21:50:30 -040012131 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012132 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012133 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012134 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012135 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012136 status);
12137 }
12138 }
dea31012005-04-17 16:05:31 -050012139 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012140} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012141
James Smart93996272008-08-24 21:50:30 -040012142/**
James Smart3772a992009-05-22 14:50:54 -040012143 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012144 * @irq: Interrupt number.
12145 * @dev_id: The device context pointer.
12146 *
James Smart3772a992009-05-22 14:50:54 -040012147 * This function is the HBA device-level interrupt handler to device with
12148 * SLI-3 interface spec, called from the PCI layer when either MSI or
12149 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12150 * requires driver attention. This function invokes the slow-path interrupt
12151 * attention handling function and fast-path interrupt attention handling
12152 * function in turn to process the relevant HBA attention events. This
12153 * function is called without any lock held. It gets the hbalock to access
12154 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012155 *
12156 * This function returns IRQ_HANDLED when interrupt is handled, else it
12157 * returns IRQ_NONE.
12158 **/
12159irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012160lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012161{
12162 struct lpfc_hba *phba;
12163 irqreturn_t sp_irq_rc, fp_irq_rc;
12164 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012165 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012166
12167 /*
12168 * Get the driver's phba structure from the dev_id and
12169 * assume the HBA is not interrupting.
12170 */
12171 phba = (struct lpfc_hba *) dev_id;
12172
12173 if (unlikely(!phba))
12174 return IRQ_NONE;
12175
James Smart3772a992009-05-22 14:50:54 -040012176 /* Check device state for handling interrupt */
12177 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012178 return IRQ_NONE;
12179
12180 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012181 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12182 spin_unlock(&phba->hbalock);
12183 return IRQ_HANDLED;
12184 }
12185
James Smart93996272008-08-24 21:50:30 -040012186 if (unlikely(!phba->ha_copy)) {
12187 spin_unlock(&phba->hbalock);
12188 return IRQ_NONE;
12189 } else if (phba->ha_copy & HA_ERATT) {
12190 if (phba->hba_flag & HBA_ERATT_HANDLED)
12191 /* ERATT polling has handled ERATT */
12192 phba->ha_copy &= ~HA_ERATT;
12193 else
12194 /* Indicate interrupt handler handles ERATT */
12195 phba->hba_flag |= HBA_ERATT_HANDLED;
12196 }
12197
James Smarta257bf92009-04-06 18:48:10 -040012198 /*
12199 * If there is deferred error attention, do not check for any interrupt.
12200 */
12201 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012202 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012203 return IRQ_NONE;
12204 }
12205
James Smart93996272008-08-24 21:50:30 -040012206 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012207 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12208 spin_unlock(&phba->hbalock);
12209 return IRQ_HANDLED;
12210 }
James Smarta747c9c2009-11-18 15:41:10 -050012211 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12212 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12213 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012214 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012215 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012216 readl(phba->HAregaddr); /* flush */
12217 spin_unlock(&phba->hbalock);
12218
12219 /*
12220 * Invokes slow-path host attention interrupt handling as appropriate.
12221 */
12222
12223 /* status of events with mailbox and link attention */
12224 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12225
12226 /* status of events with ELS ring */
12227 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12228 status2 >>= (4*LPFC_ELS_RING);
12229
12230 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012231 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012232 else
12233 sp_irq_rc = IRQ_NONE;
12234
12235 /*
12236 * Invoke fast-path host attention interrupt handling as appropriate.
12237 */
12238
12239 /* status of events with FCP ring */
12240 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12241 status1 >>= (4*LPFC_FCP_RING);
12242
12243 /* status of events with extra ring */
12244 if (phba->cfg_multi_ring_support == 2) {
12245 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12246 status2 >>= (4*LPFC_EXTRA_RING);
12247 } else
12248 status2 = 0;
12249
12250 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012251 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012252 else
12253 fp_irq_rc = IRQ_NONE;
12254
12255 /* Return device-level interrupt handling status */
12256 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012257} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012258
12259/**
12260 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12261 * @phba: pointer to lpfc hba data structure.
12262 *
12263 * This routine is invoked by the worker thread to process all the pending
12264 * SLI4 FCP abort XRI events.
12265 **/
12266void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12267{
12268 struct lpfc_cq_event *cq_event;
12269
12270 /* First, declare the fcp xri abort event has been handled */
12271 spin_lock_irq(&phba->hbalock);
12272 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12273 spin_unlock_irq(&phba->hbalock);
12274 /* Now, handle all the fcp xri abort events */
12275 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12276 /* Get the first event from the head of the event queue */
12277 spin_lock_irq(&phba->hbalock);
12278 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12279 cq_event, struct lpfc_cq_event, list);
12280 spin_unlock_irq(&phba->hbalock);
12281 /* Notify aborted XRI for FCP work queue */
12282 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12283 /* Free the event processed back to the free pool */
12284 lpfc_sli4_cq_event_release(phba, cq_event);
12285 }
12286}
12287
12288/**
James Smart318083a2017-03-04 09:30:30 -080012289 * lpfc_sli4_nvme_xri_abort_event_proc - Process nvme xri abort event
12290 * @phba: pointer to lpfc hba data structure.
12291 *
12292 * This routine is invoked by the worker thread to process all the pending
12293 * SLI4 NVME abort XRI events.
12294 **/
12295void lpfc_sli4_nvme_xri_abort_event_proc(struct lpfc_hba *phba)
12296{
12297 struct lpfc_cq_event *cq_event;
12298
12299 /* First, declare the fcp xri abort event has been handled */
12300 spin_lock_irq(&phba->hbalock);
12301 phba->hba_flag &= ~NVME_XRI_ABORT_EVENT;
12302 spin_unlock_irq(&phba->hbalock);
12303 /* Now, handle all the fcp xri abort events */
12304 while (!list_empty(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue)) {
12305 /* Get the first event from the head of the event queue */
12306 spin_lock_irq(&phba->hbalock);
12307 list_remove_head(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
12308 cq_event, struct lpfc_cq_event, list);
12309 spin_unlock_irq(&phba->hbalock);
12310 /* Notify aborted XRI for NVME work queue */
12311 if (phba->nvmet_support) {
12312 lpfc_sli4_nvmet_xri_aborted(phba,
12313 &cq_event->cqe.wcqe_axri);
12314 } else {
12315 lpfc_sli4_nvme_xri_aborted(phba,
12316 &cq_event->cqe.wcqe_axri);
12317 }
12318 /* Free the event processed back to the free pool */
12319 lpfc_sli4_cq_event_release(phba, cq_event);
12320 }
12321}
12322
12323/**
James Smart4f774512009-05-22 14:52:35 -040012324 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12325 * @phba: pointer to lpfc hba data structure.
12326 *
12327 * This routine is invoked by the worker thread to process all the pending
12328 * SLI4 els abort xri events.
12329 **/
12330void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12331{
12332 struct lpfc_cq_event *cq_event;
12333
12334 /* First, declare the els xri abort event has been handled */
12335 spin_lock_irq(&phba->hbalock);
12336 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12337 spin_unlock_irq(&phba->hbalock);
12338 /* Now, handle all the els xri abort events */
12339 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12340 /* Get the first event from the head of the event queue */
12341 spin_lock_irq(&phba->hbalock);
12342 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12343 cq_event, struct lpfc_cq_event, list);
12344 spin_unlock_irq(&phba->hbalock);
12345 /* Notify aborted XRI for ELS work queue */
12346 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12347 /* Free the event processed back to the free pool */
12348 lpfc_sli4_cq_event_release(phba, cq_event);
12349 }
12350}
12351
James Smart341af102010-01-26 23:07:37 -050012352/**
12353 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12354 * @phba: pointer to lpfc hba data structure
12355 * @pIocbIn: pointer to the rspiocbq
12356 * @pIocbOut: pointer to the cmdiocbq
12357 * @wcqe: pointer to the complete wcqe
12358 *
12359 * This routine transfers the fields of a command iocbq to a response iocbq
12360 * by copying all the IOCB fields from command iocbq and transferring the
12361 * completion status information from the complete wcqe.
12362 **/
James Smart4f774512009-05-22 14:52:35 -040012363static void
James Smart341af102010-01-26 23:07:37 -050012364lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12365 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012366 struct lpfc_iocbq *pIocbOut,
12367 struct lpfc_wcqe_complete *wcqe)
12368{
James Smartaf227412013-10-10 12:23:10 -040012369 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012370 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012371 uint32_t status, max_response;
12372 struct lpfc_dmabuf *dmabuf;
12373 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012374 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12375
12376 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12377 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012378 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012379 status = bf_get(lpfc_wcqe_c_status, wcqe);
12380 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012381 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12382 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12383 pIocbIn->iocb.un.fcpi.fcpi_parm =
12384 pIocbOut->iocb.un.fcpi.fcpi_parm -
12385 wcqe->total_data_placed;
12386 else
12387 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012388 else {
James Smart4f774512009-05-22 14:52:35 -040012389 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012390 switch (pIocbOut->iocb.ulpCommand) {
12391 case CMD_ELS_REQUEST64_CR:
12392 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12393 bpl = (struct ulp_bde64 *)dmabuf->virt;
12394 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12395 max_response = bde.tus.f.bdeSize;
12396 break;
12397 case CMD_GEN_REQUEST64_CR:
12398 max_response = 0;
12399 if (!pIocbOut->context3)
12400 break;
12401 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12402 sizeof(struct ulp_bde64);
12403 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12404 bpl = (struct ulp_bde64 *)dmabuf->virt;
12405 for (i = 0; i < numBdes; i++) {
12406 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12407 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12408 max_response += bde.tus.f.bdeSize;
12409 }
12410 break;
12411 default:
12412 max_response = wcqe->total_data_placed;
12413 break;
12414 }
12415 if (max_response < wcqe->total_data_placed)
12416 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12417 else
12418 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12419 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012420 }
James Smart341af102010-01-26 23:07:37 -050012421
James Smartacd68592012-01-18 16:25:09 -050012422 /* Convert BG errors for completion status */
12423 if (status == CQE_STATUS_DI_ERROR) {
12424 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12425
12426 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12427 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12428 else
12429 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12430
12431 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12432 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12433 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12434 BGS_GUARD_ERR_MASK;
12435 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12436 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12437 BGS_APPTAG_ERR_MASK;
12438 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12439 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12440 BGS_REFTAG_ERR_MASK;
12441
12442 /* Check to see if there was any good data before the error */
12443 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12444 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12445 BGS_HI_WATER_MARK_PRESENT_MASK;
12446 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12447 wcqe->total_data_placed;
12448 }
12449
12450 /*
12451 * Set ALL the error bits to indicate we don't know what
12452 * type of error it is.
12453 */
12454 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12455 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12456 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12457 BGS_GUARD_ERR_MASK);
12458 }
12459
James Smart341af102010-01-26 23:07:37 -050012460 /* Pick up HBA exchange busy condition */
12461 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12462 spin_lock_irqsave(&phba->hbalock, iflags);
12463 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12464 spin_unlock_irqrestore(&phba->hbalock, iflags);
12465 }
James Smart4f774512009-05-22 14:52:35 -040012466}
12467
12468/**
James Smart45ed1192009-10-02 15:17:02 -040012469 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12470 * @phba: Pointer to HBA context object.
12471 * @wcqe: Pointer to work-queue completion queue entry.
12472 *
12473 * This routine handles an ELS work-queue completion event and construct
12474 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12475 * discovery engine to handle.
12476 *
12477 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12478 **/
12479static struct lpfc_iocbq *
12480lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12481 struct lpfc_iocbq *irspiocbq)
12482{
James Smart895427b2017-02-12 13:52:30 -080012483 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012484 struct lpfc_iocbq *cmdiocbq;
12485 struct lpfc_wcqe_complete *wcqe;
12486 unsigned long iflags;
12487
James Smart895427b2017-02-12 13:52:30 -080012488 pring = lpfc_phba_elsring(phba);
12489
James Smart45ed1192009-10-02 15:17:02 -040012490 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012491 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012492 pring->stats.iocb_event++;
12493 /* Look up the ELS command IOCB and create pseudo response IOCB */
12494 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12495 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart89533e92016-10-13 15:06:15 -070012496 /* Put the iocb back on the txcmplq */
12497 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
James Smart7e56aa22012-08-03 12:35:34 -040012498 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012499
12500 if (unlikely(!cmdiocbq)) {
12501 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12502 "0386 ELS complete with no corresponding "
12503 "cmdiocb: iotag (%d)\n",
12504 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12505 lpfc_sli_release_iocbq(phba, irspiocbq);
12506 return NULL;
12507 }
12508
12509 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012510 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012511
12512 return irspiocbq;
12513}
12514
12515/**
James Smart04c68492009-05-22 14:52:52 -040012516 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12517 * @phba: Pointer to HBA context object.
12518 * @cqe: Pointer to mailbox completion queue entry.
12519 *
12520 * This routine process a mailbox completion queue entry with asynchrous
12521 * event.
12522 *
12523 * Return: true if work posted to worker thread, otherwise false.
12524 **/
12525static bool
12526lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12527{
12528 struct lpfc_cq_event *cq_event;
12529 unsigned long iflags;
12530
12531 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12532 "0392 Async Event: word0:x%x, word1:x%x, "
12533 "word2:x%x, word3:x%x\n", mcqe->word0,
12534 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12535
12536 /* Allocate a new internal CQ_EVENT entry */
12537 cq_event = lpfc_sli4_cq_event_alloc(phba);
12538 if (!cq_event) {
12539 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12540 "0394 Failed to allocate CQ_EVENT entry\n");
12541 return false;
12542 }
12543
12544 /* Move the CQE into an asynchronous event entry */
12545 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12546 spin_lock_irqsave(&phba->hbalock, iflags);
12547 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12548 /* Set the async event flag */
12549 phba->hba_flag |= ASYNC_EVENT;
12550 spin_unlock_irqrestore(&phba->hbalock, iflags);
12551
12552 return true;
12553}
12554
12555/**
12556 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12557 * @phba: Pointer to HBA context object.
12558 * @cqe: Pointer to mailbox completion queue entry.
12559 *
12560 * This routine process a mailbox completion queue entry with mailbox
12561 * completion event.
12562 *
12563 * Return: true if work posted to worker thread, otherwise false.
12564 **/
12565static bool
12566lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12567{
12568 uint32_t mcqe_status;
12569 MAILBOX_t *mbox, *pmbox;
12570 struct lpfc_mqe *mqe;
12571 struct lpfc_vport *vport;
12572 struct lpfc_nodelist *ndlp;
12573 struct lpfc_dmabuf *mp;
12574 unsigned long iflags;
12575 LPFC_MBOXQ_t *pmb;
12576 bool workposted = false;
12577 int rc;
12578
12579 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12580 if (!bf_get(lpfc_trailer_completed, mcqe))
12581 goto out_no_mqe_complete;
12582
12583 /* Get the reference to the active mbox command */
12584 spin_lock_irqsave(&phba->hbalock, iflags);
12585 pmb = phba->sli.mbox_active;
12586 if (unlikely(!pmb)) {
12587 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12588 "1832 No pending MBOX command to handle\n");
12589 spin_unlock_irqrestore(&phba->hbalock, iflags);
12590 goto out_no_mqe_complete;
12591 }
12592 spin_unlock_irqrestore(&phba->hbalock, iflags);
12593 mqe = &pmb->u.mqe;
12594 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12595 mbox = phba->mbox;
12596 vport = pmb->vport;
12597
12598 /* Reset heartbeat timer */
12599 phba->last_completion_time = jiffies;
12600 del_timer(&phba->sli.mbox_tmo);
12601
12602 /* Move mbox data to caller's mailbox region, do endian swapping */
12603 if (pmb->mbox_cmpl && mbox)
12604 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012605
James Smart73d91e52011-10-10 21:32:10 -040012606 /*
12607 * For mcqe errors, conditionally move a modified error code to
12608 * the mbox so that the error will not be missed.
12609 */
12610 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12611 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12612 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12613 bf_set(lpfc_mqe_status, mqe,
12614 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12615 }
James Smart04c68492009-05-22 14:52:52 -040012616 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12617 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12618 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12619 "MBOX dflt rpi: status:x%x rpi:x%x",
12620 mcqe_status,
12621 pmbox->un.varWords[0], 0);
12622 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12623 mp = (struct lpfc_dmabuf *)(pmb->context1);
12624 ndlp = (struct lpfc_nodelist *)pmb->context2;
12625 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12626 * RID of the PPI using the same mbox buffer.
12627 */
12628 lpfc_unreg_login(phba, vport->vpi,
12629 pmbox->un.varWords[0], pmb);
12630 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12631 pmb->context1 = mp;
12632 pmb->context2 = ndlp;
12633 pmb->vport = vport;
12634 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12635 if (rc != MBX_BUSY)
12636 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12637 LOG_SLI, "0385 rc should "
12638 "have been MBX_BUSY\n");
12639 if (rc != MBX_NOT_FINISHED)
12640 goto send_current_mbox;
12641 }
12642 }
12643 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12644 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12645 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12646
12647 /* There is mailbox completion work to do */
12648 spin_lock_irqsave(&phba->hbalock, iflags);
12649 __lpfc_mbox_cmpl_put(phba, pmb);
12650 phba->work_ha |= HA_MBATT;
12651 spin_unlock_irqrestore(&phba->hbalock, iflags);
12652 workposted = true;
12653
12654send_current_mbox:
12655 spin_lock_irqsave(&phba->hbalock, iflags);
12656 /* Release the mailbox command posting token */
12657 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12658 /* Setting active mailbox pointer need to be in sync to flag clear */
12659 phba->sli.mbox_active = NULL;
12660 spin_unlock_irqrestore(&phba->hbalock, iflags);
12661 /* Wake up worker thread to post the next pending mailbox command */
12662 lpfc_worker_wake_up(phba);
12663out_no_mqe_complete:
12664 if (bf_get(lpfc_trailer_consumed, mcqe))
12665 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12666 return workposted;
12667}
12668
12669/**
12670 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12671 * @phba: Pointer to HBA context object.
12672 * @cqe: Pointer to mailbox completion queue entry.
12673 *
12674 * This routine process a mailbox completion queue entry, it invokes the
12675 * proper mailbox complete handling or asynchrous event handling routine
12676 * according to the MCQE's async bit.
12677 *
12678 * Return: true if work posted to worker thread, otherwise false.
12679 **/
12680static bool
12681lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12682{
12683 struct lpfc_mcqe mcqe;
12684 bool workposted;
12685
12686 /* Copy the mailbox MCQE and convert endian order as needed */
12687 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12688
12689 /* Invoke the proper event handling routine */
12690 if (!bf_get(lpfc_trailer_async, &mcqe))
12691 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12692 else
12693 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12694 return workposted;
12695}
12696
12697/**
James Smart4f774512009-05-22 14:52:35 -040012698 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12699 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012700 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012701 * @wcqe: Pointer to work-queue completion queue entry.
12702 *
12703 * This routine handles an ELS work-queue completion event.
12704 *
12705 * Return: true if work posted to worker thread, otherwise false.
12706 **/
12707static bool
James Smart2a76a282012-08-03 12:35:54 -040012708lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012709 struct lpfc_wcqe_complete *wcqe)
12710{
James Smart4f774512009-05-22 14:52:35 -040012711 struct lpfc_iocbq *irspiocbq;
12712 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012713 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012714 int txq_cnt = 0;
12715 int txcmplq_cnt = 0;
12716 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012717
James Smart45ed1192009-10-02 15:17:02 -040012718 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012719 irspiocbq = lpfc_sli_get_iocbq(phba);
12720 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012721 if (!list_empty(&pring->txq))
12722 txq_cnt++;
12723 if (!list_empty(&pring->txcmplq))
12724 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012726 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12727 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012728 txq_cnt, phba->iocb_cnt,
12729 fcp_txcmplq_cnt,
12730 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012731 return false;
James Smart4f774512009-05-22 14:52:35 -040012732 }
James Smart4f774512009-05-22 14:52:35 -040012733
James Smart45ed1192009-10-02 15:17:02 -040012734 /* Save off the slow-path queue event for work thread to process */
12735 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012736 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012737 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012738 &phba->sli4_hba.sp_queue_event);
12739 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012740 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012741
James Smart45ed1192009-10-02 15:17:02 -040012742 return true;
James Smart4f774512009-05-22 14:52:35 -040012743}
12744
12745/**
12746 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12747 * @phba: Pointer to HBA context object.
12748 * @wcqe: Pointer to work-queue completion queue entry.
12749 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012750 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012751 * proper WQ release routine to the slow-path WQ.
12752 **/
12753static void
12754lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12755 struct lpfc_wcqe_release *wcqe)
12756{
James Smart2e90f4b2011-12-13 13:22:37 -050012757 /* sanity check on queue memory */
12758 if (unlikely(!phba->sli4_hba.els_wq))
12759 return;
James Smart4f774512009-05-22 14:52:35 -040012760 /* Check for the slow-path ELS work queue */
12761 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12762 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12763 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12764 else
12765 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12766 "2579 Slow-path wqe consume event carries "
12767 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12768 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12769 phba->sli4_hba.els_wq->queue_id);
12770}
12771
12772/**
12773 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12774 * @phba: Pointer to HBA context object.
12775 * @cq: Pointer to a WQ completion queue.
12776 * @wcqe: Pointer to work-queue completion queue entry.
12777 *
12778 * This routine handles an XRI abort event.
12779 *
12780 * Return: true if work posted to worker thread, otherwise false.
12781 **/
12782static bool
12783lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12784 struct lpfc_queue *cq,
12785 struct sli4_wcqe_xri_aborted *wcqe)
12786{
12787 bool workposted = false;
12788 struct lpfc_cq_event *cq_event;
12789 unsigned long iflags;
12790
12791 /* Allocate a new internal CQ_EVENT entry */
12792 cq_event = lpfc_sli4_cq_event_alloc(phba);
12793 if (!cq_event) {
12794 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12795 "0602 Failed to allocate CQ_EVENT entry\n");
12796 return false;
12797 }
12798
12799 /* Move the CQE into the proper xri abort event list */
12800 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12801 switch (cq->subtype) {
12802 case LPFC_FCP:
12803 spin_lock_irqsave(&phba->hbalock, iflags);
12804 list_add_tail(&cq_event->list,
12805 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12806 /* Set the fcp xri abort event flag */
12807 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12808 spin_unlock_irqrestore(&phba->hbalock, iflags);
12809 workposted = true;
12810 break;
12811 case LPFC_ELS:
12812 spin_lock_irqsave(&phba->hbalock, iflags);
12813 list_add_tail(&cq_event->list,
12814 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12815 /* Set the els xri abort event flag */
12816 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12817 spin_unlock_irqrestore(&phba->hbalock, iflags);
12818 workposted = true;
12819 break;
James Smart318083a2017-03-04 09:30:30 -080012820 case LPFC_NVME:
12821 spin_lock_irqsave(&phba->hbalock, iflags);
12822 list_add_tail(&cq_event->list,
12823 &phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
12824 /* Set the nvme xri abort event flag */
12825 phba->hba_flag |= NVME_XRI_ABORT_EVENT;
12826 spin_unlock_irqrestore(&phba->hbalock, iflags);
12827 workposted = true;
12828 break;
James Smart4f774512009-05-22 14:52:35 -040012829 default:
12830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080012831 "0603 Invalid CQ subtype %d: "
12832 "%08x %08x %08x %08x\n",
12833 cq->subtype, wcqe->word0, wcqe->parameter,
12834 wcqe->word2, wcqe->word3);
12835 lpfc_sli4_cq_event_release(phba, cq_event);
James Smart4f774512009-05-22 14:52:35 -040012836 workposted = false;
12837 break;
12838 }
12839 return workposted;
12840}
12841
12842/**
James Smart4d9ab992009-10-02 15:16:39 -040012843 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012844 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012845 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012846 *
James Smart4d9ab992009-10-02 15:16:39 -040012847 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012848 *
12849 * Return: true if work posted to worker thread, otherwise false.
12850 **/
12851static bool
James Smart4d9ab992009-10-02 15:16:39 -040012852lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12853{
12854 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012855 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012856 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12857 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070012858 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040012859 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012860 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012861 unsigned long iflags;
12862
James Smart2e90f4b2011-12-13 13:22:37 -050012863 /* sanity check on queue memory */
12864 if (unlikely(!hrq) || unlikely(!drq))
12865 return workposted;
12866
James Smart7851fe22011-07-22 18:36:52 -040012867 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12868 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12869 else
12870 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12871 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012872 goto out;
12873
12874 status = bf_get(lpfc_rcqe_status, rcqe);
12875 switch (status) {
12876 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12878 "2537 Receive Frame Truncated!!\n");
12879 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012880 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012881 spin_lock_irqsave(&phba->hbalock, iflags);
12882 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12883 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012884 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012885 spin_unlock_irqrestore(&phba->hbalock, iflags);
12886 goto out;
12887 }
James Smartb84daac2012-08-03 12:35:13 -040012888 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070012889 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040012890 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012891
12892 /* If a NVME LS event (type 0x28), treat it as Fast path */
12893 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12894
James Smart4d9ab992009-10-02 15:16:39 -040012895 /* save off the frame for the word thread to process */
12896 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012897 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012898 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012899 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012900 spin_unlock_irqrestore(&phba->hbalock, iflags);
12901 workposted = true;
12902 break;
James Smart4d9ab992009-10-02 15:16:39 -040012903 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070012904 if (phba->nvmet_support) {
12905 tgtp = phba->targetport->private;
12906 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
12907 "6402 RQE Error x%x, posted %d err_cnt "
12908 "%d: %x %x %x\n",
12909 status, hrq->RQ_buf_posted,
12910 hrq->RQ_no_posted_buf,
12911 atomic_read(&tgtp->rcv_fcp_cmd_in),
12912 atomic_read(&tgtp->rcv_fcp_cmd_out),
12913 atomic_read(&tgtp->xmt_fcp_release));
12914 }
12915 /* fallthrough */
12916
12917 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040012918 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012919 /* Post more buffers if possible */
12920 spin_lock_irqsave(&phba->hbalock, iflags);
12921 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12922 spin_unlock_irqrestore(&phba->hbalock, iflags);
12923 workposted = true;
12924 break;
12925 }
12926out:
12927 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012928}
12929
12930/**
12931 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12932 * @phba: Pointer to HBA context object.
12933 * @cq: Pointer to the completion queue.
12934 * @wcqe: Pointer to a completion queue entry.
12935 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012936 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012937 * entry.
12938 *
12939 * Return: true if work posted to worker thread, otherwise false.
12940 **/
12941static bool
12942lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012943 struct lpfc_cqe *cqe)
12944{
James Smart45ed1192009-10-02 15:17:02 -040012945 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012946 bool workposted = false;
12947
12948 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012949 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012950
12951 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012952 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012953 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012954 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012955 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012956 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012957 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012958 break;
12959 case CQE_CODE_RELEASE_WQE:
12960 /* Process the WQ release event */
12961 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012962 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012963 break;
12964 case CQE_CODE_XRI_ABORTED:
12965 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012966 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012967 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012968 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012969 break;
James Smart4d9ab992009-10-02 15:16:39 -040012970 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012971 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012972 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012973 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012974 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012975 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012976 break;
James Smart4f774512009-05-22 14:52:35 -040012977 default:
12978 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12979 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012980 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012981 break;
12982 }
12983 return workposted;
12984}
12985
12986/**
James Smart4f774512009-05-22 14:52:35 -040012987 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12988 * @phba: Pointer to HBA context object.
12989 * @eqe: Pointer to fast-path event queue entry.
12990 *
12991 * This routine process a event queue entry from the slow-path event queue.
12992 * It will check the MajorCode and MinorCode to determine this is for a
12993 * completion event on a completion queue, if not, an error shall be logged
12994 * and just return. Otherwise, it will get to the corresponding completion
12995 * queue and process all the entries on that completion queue, rearm the
12996 * completion queue, and then return.
12997 *
12998 **/
12999static void
James Smart67d12732012-08-03 12:36:13 -040013000lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13001 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013002{
James Smart67d12732012-08-03 12:36:13 -040013003 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013004 struct lpfc_cqe *cqe;
13005 bool workposted = false;
13006 int ecount = 0;
13007 uint16_t cqid;
13008
James Smart4f774512009-05-22 14:52:35 -040013009 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013010 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013011
James Smart4f774512009-05-22 14:52:35 -040013012 list_for_each_entry(childq, &speq->child_list, list) {
13013 if (childq->queue_id == cqid) {
13014 cq = childq;
13015 break;
13016 }
13017 }
13018 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013019 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13020 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13021 "0365 Slow-path CQ identifier "
13022 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040013023 return;
13024 }
13025
James Smart895427b2017-02-12 13:52:30 -080013026 /* Save EQ associated with this CQ */
13027 cq->assoc_qp = speq;
13028
James Smart4f774512009-05-22 14:52:35 -040013029 /* Process all the entries to the CQ */
13030 switch (cq->type) {
13031 case LPFC_MCQ:
13032 while ((cqe = lpfc_sli4_cq_get(cq))) {
13033 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013034 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013035 break;
James Smartb84daac2012-08-03 12:35:13 -040013036 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013037 }
13038 break;
13039 case LPFC_WCQ:
13040 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013041 if ((cq->subtype == LPFC_FCP) ||
13042 (cq->subtype == LPFC_NVME))
13043 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013044 cqe);
13045 else
13046 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13047 cqe);
James Smart73d91e52011-10-10 21:32:10 -040013048 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013049 break;
James Smart4f774512009-05-22 14:52:35 -040013050 }
James Smartb84daac2012-08-03 12:35:13 -040013051
13052 /* Track the max number of CQEs processed in 1 EQ */
13053 if (ecount > cq->CQ_max_cqe)
13054 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040013055 break;
13056 default:
13057 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13058 "0370 Invalid completion queue type (%d)\n",
13059 cq->type);
13060 return;
13061 }
13062
13063 /* Catch the no cq entry condition, log an error */
13064 if (unlikely(ecount == 0))
13065 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13066 "0371 No entry from the CQ: identifier "
13067 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13068
13069 /* In any case, flash and re-arm the RCQ */
13070 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13071
13072 /* wake up worker thread if there are works to be done */
13073 if (workposted)
13074 lpfc_worker_wake_up(phba);
13075}
13076
13077/**
13078 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013079 * @phba: Pointer to HBA context object.
13080 * @cq: Pointer to associated CQ
13081 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013082 *
13083 * This routine process a fast-path work queue completion entry from fast-path
13084 * event queue for FCP command response completion.
13085 **/
13086static void
James Smart2a76a282012-08-03 12:35:54 -040013087lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013088 struct lpfc_wcqe_complete *wcqe)
13089{
James Smart2a76a282012-08-03 12:35:54 -040013090 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013091 struct lpfc_iocbq *cmdiocbq;
13092 struct lpfc_iocbq irspiocbq;
13093 unsigned long iflags;
13094
James Smart4f774512009-05-22 14:52:35 -040013095 /* Check for response status */
13096 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13097 /* If resource errors reported from HBA, reduce queue
13098 * depth of the SCSI device.
13099 */
James Smarte3d2b802012-08-14 14:25:43 -040013100 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13101 IOSTAT_LOCAL_REJECT)) &&
13102 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13103 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013104 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013105
James Smart4f774512009-05-22 14:52:35 -040013106 /* Log the error status */
13107 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13108 "0373 FCP complete error: status=x%x, "
13109 "hw_status=x%x, total_data_specified=%d, "
13110 "parameter=x%x, word3=x%x\n",
13111 bf_get(lpfc_wcqe_c_status, wcqe),
13112 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13113 wcqe->total_data_placed, wcqe->parameter,
13114 wcqe->word3);
13115 }
13116
13117 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013118 spin_lock_irqsave(&pring->ring_lock, iflags);
13119 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013120 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13121 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013122 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013123 if (unlikely(!cmdiocbq)) {
13124 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13125 "0374 FCP complete with no corresponding "
13126 "cmdiocb: iotag (%d)\n",
13127 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13128 return;
13129 }
James Smart895427b2017-02-12 13:52:30 -080013130
13131 if (cq->assoc_qp)
13132 cmdiocbq->isr_timestamp =
13133 cq->assoc_qp->isr_timestamp;
13134
13135 if (cmdiocbq->iocb_cmpl == NULL) {
13136 if (cmdiocbq->wqe_cmpl) {
13137 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13138 spin_lock_irqsave(&phba->hbalock, iflags);
13139 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13140 spin_unlock_irqrestore(&phba->hbalock, iflags);
13141 }
13142
13143 /* Pass the cmd_iocb and the wcqe to the upper layer */
13144 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13145 return;
13146 }
James Smart4f774512009-05-22 14:52:35 -040013147 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13148 "0375 FCP cmdiocb not callback function "
13149 "iotag: (%d)\n",
13150 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13151 return;
13152 }
13153
13154 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013155 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013156
James Smart0f65ff62010-02-26 14:14:23 -050013157 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13158 spin_lock_irqsave(&phba->hbalock, iflags);
13159 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13160 spin_unlock_irqrestore(&phba->hbalock, iflags);
13161 }
13162
James Smart4f774512009-05-22 14:52:35 -040013163 /* Pass the cmd_iocb and the rsp state to the upper layer */
13164 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13165}
13166
13167/**
13168 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13169 * @phba: Pointer to HBA context object.
13170 * @cq: Pointer to completion queue.
13171 * @wcqe: Pointer to work-queue completion queue entry.
13172 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013173 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013174 * proper WQ release routine to the slow-path WQ.
13175 **/
13176static void
13177lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13178 struct lpfc_wcqe_release *wcqe)
13179{
13180 struct lpfc_queue *childwq;
13181 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013182 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013183
13184 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013185 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013186 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013187 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013188 lpfc_sli4_wq_release(childwq,
13189 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13190 wqid_matched = true;
13191 break;
13192 }
13193 }
13194 /* Report warning log message if no match found */
13195 if (wqid_matched != true)
13196 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13197 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013198 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013199}
13200
13201/**
James Smart2d7dbc42017-02-12 13:52:35 -080013202 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13203 * @phba: Pointer to HBA context object.
13204 * @rcqe: Pointer to receive-queue completion queue entry.
13205 *
13206 * This routine process a receive-queue completion queue entry.
13207 *
13208 * Return: true if work posted to worker thread, otherwise false.
13209 **/
13210static bool
13211lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13212 struct lpfc_rcqe *rcqe)
13213{
13214 bool workposted = false;
13215 struct lpfc_queue *hrq;
13216 struct lpfc_queue *drq;
13217 struct rqb_dmabuf *dma_buf;
13218 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013219 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013220 uint32_t status, rq_id;
13221 unsigned long iflags;
13222 uint32_t fctl, idx;
13223
13224 if ((phba->nvmet_support == 0) ||
13225 (phba->sli4_hba.nvmet_cqset == NULL))
13226 return workposted;
13227
13228 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13229 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13230 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13231
13232 /* sanity check on queue memory */
13233 if (unlikely(!hrq) || unlikely(!drq))
13234 return workposted;
13235
13236 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13237 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13238 else
13239 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13240
13241 if ((phba->nvmet_support == 0) ||
13242 (rq_id != hrq->queue_id))
13243 return workposted;
13244
13245 status = bf_get(lpfc_rcqe_status, rcqe);
13246 switch (status) {
13247 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13249 "6126 Receive Frame Truncated!!\n");
James Smart2d7dbc42017-02-12 13:52:35 -080013250 case FC_STATUS_RQ_SUCCESS:
13251 lpfc_sli4_rq_release(hrq, drq);
13252 spin_lock_irqsave(&phba->hbalock, iflags);
13253 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13254 if (!dma_buf) {
13255 hrq->RQ_no_buf_found++;
13256 spin_unlock_irqrestore(&phba->hbalock, iflags);
13257 goto out;
13258 }
13259 spin_unlock_irqrestore(&phba->hbalock, iflags);
13260 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013261 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013262 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13263
13264 /* Just some basic sanity checks on FCP Command frame */
13265 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13266 fc_hdr->fh_f_ctl[1] << 8 |
13267 fc_hdr->fh_f_ctl[2]);
13268 if (((fctl &
13269 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13270 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13271 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13272 goto drop;
13273
13274 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13275 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013276 lpfc_nvmet_unsol_fcp_event(
13277 phba, phba->sli4_hba.els_wq->pring, dma_buf,
13278 cq->assoc_qp->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013279 return false;
13280 }
13281drop:
13282 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13283 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013284 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013285 if (phba->nvmet_support) {
13286 tgtp = phba->targetport->private;
13287 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13288 "6401 RQE Error x%x, posted %d err_cnt "
13289 "%d: %x %x %x\n",
13290 status, hrq->RQ_buf_posted,
13291 hrq->RQ_no_posted_buf,
13292 atomic_read(&tgtp->rcv_fcp_cmd_in),
13293 atomic_read(&tgtp->rcv_fcp_cmd_out),
13294 atomic_read(&tgtp->xmt_fcp_release));
13295 }
13296 /* fallthrough */
13297
13298 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013299 hrq->RQ_no_posted_buf++;
13300 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013301 break;
13302 }
13303out:
13304 return workposted;
13305}
13306
13307/**
James Smart895427b2017-02-12 13:52:30 -080013308 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013309 * @cq: Pointer to the completion queue.
13310 * @eqe: Pointer to fast-path completion queue entry.
13311 *
13312 * This routine process a fast-path work queue completion entry from fast-path
13313 * event queue for FCP command response completion.
13314 **/
13315static int
James Smart895427b2017-02-12 13:52:30 -080013316lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013317 struct lpfc_cqe *cqe)
13318{
13319 struct lpfc_wcqe_release wcqe;
13320 bool workposted = false;
13321
13322 /* Copy the work queue CQE and convert endian order if needed */
13323 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13324
13325 /* Check and process for different type of WCQE and dispatch */
13326 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13327 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013328 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013329 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013330 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013331 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013332 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13333 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13334 (struct lpfc_wcqe_complete *)&wcqe);
13335 if (cq->subtype == LPFC_NVME_LS)
13336 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013337 (struct lpfc_wcqe_complete *)&wcqe);
13338 break;
13339 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013340 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013341 /* Process the WQ release event */
13342 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13343 (struct lpfc_wcqe_release *)&wcqe);
13344 break;
13345 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013346 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013347 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013348 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013349 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13350 (struct sli4_wcqe_xri_aborted *)&wcqe);
13351 break;
James Smart895427b2017-02-12 13:52:30 -080013352 case CQE_CODE_RECEIVE_V1:
13353 case CQE_CODE_RECEIVE:
13354 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013355 if (cq->subtype == LPFC_NVMET) {
13356 workposted = lpfc_sli4_nvmet_handle_rcqe(
13357 phba, cq, (struct lpfc_rcqe *)&wcqe);
13358 }
James Smart895427b2017-02-12 13:52:30 -080013359 break;
James Smart4f774512009-05-22 14:52:35 -040013360 default:
13361 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013362 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013363 bf_get(lpfc_wcqe_c_code, &wcqe));
13364 break;
13365 }
13366 return workposted;
13367}
13368
13369/**
James Smart67d12732012-08-03 12:36:13 -040013370 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013371 * @phba: Pointer to HBA context object.
13372 * @eqe: Pointer to fast-path event queue entry.
13373 *
13374 * This routine process a event queue entry from the fast-path event queue.
13375 * It will check the MajorCode and MinorCode to determine this is for a
13376 * completion event on a completion queue, if not, an error shall be logged
13377 * and just return. Otherwise, it will get to the corresponding completion
13378 * queue and process all the entries on the completion queue, rearm the
13379 * completion queue, and then return.
13380 **/
13381static void
James Smart67d12732012-08-03 12:36:13 -040013382lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13383 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013384{
James Smart895427b2017-02-12 13:52:30 -080013385 struct lpfc_queue *cq = NULL;
James Smart4f774512009-05-22 14:52:35 -040013386 struct lpfc_cqe *cqe;
13387 bool workposted = false;
James Smart2d7dbc42017-02-12 13:52:35 -080013388 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013389 int ecount = 0;
13390
James Smartcb5172e2010-03-15 11:25:07 -040013391 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013392 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013393 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013394 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013395 bf_get_le32(lpfc_eqe_major_code, eqe),
13396 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040013397 return;
13398 }
13399
James Smart67d12732012-08-03 12:36:13 -040013400 /* Get the reference to the corresponding CQ */
13401 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13402
James Smart2d7dbc42017-02-12 13:52:35 -080013403 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13404 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13405 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13406 /* Process NVMET unsol rcv */
13407 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13408 goto process_cq;
13409 }
13410 }
13411
James Smart895427b2017-02-12 13:52:30 -080013412 if (phba->sli4_hba.nvme_cq_map &&
13413 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013414 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013415 cq = phba->sli4_hba.nvme_cq[qidx];
13416 goto process_cq;
13417 }
13418
13419 if (phba->sli4_hba.fcp_cq_map &&
13420 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13421 /* Process FCP command completion */
13422 cq = phba->sli4_hba.fcp_cq[qidx];
13423 goto process_cq;
13424 }
13425
13426 if (phba->sli4_hba.nvmels_cq &&
13427 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13428 /* Process NVME unsol rcv */
13429 cq = phba->sli4_hba.nvmels_cq;
13430 }
13431
13432 /* Otherwise this is a Slow path event */
13433 if (cq == NULL) {
13434 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013435 return;
13436 }
13437
James Smart895427b2017-02-12 13:52:30 -080013438process_cq:
James Smart4f774512009-05-22 14:52:35 -040013439 if (unlikely(cqid != cq->queue_id)) {
13440 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13441 "0368 Miss-matched fast-path completion "
13442 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13443 cqid, cq->queue_id);
13444 return;
13445 }
13446
James Smart895427b2017-02-12 13:52:30 -080013447 /* Save EQ associated with this CQ */
13448 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13449
James Smart4f774512009-05-22 14:52:35 -040013450 /* Process all the entries to the CQ */
13451 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013452 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013453 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013454 break;
James Smart4f774512009-05-22 14:52:35 -040013455 }
13456
James Smartb84daac2012-08-03 12:35:13 -040013457 /* Track the max number of CQEs processed in 1 EQ */
13458 if (ecount > cq->CQ_max_cqe)
13459 cq->CQ_max_cqe = ecount;
13460
James Smart4f774512009-05-22 14:52:35 -040013461 /* Catch the no cq entry condition */
13462 if (unlikely(ecount == 0))
13463 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13464 "0369 No entry from fast-path completion "
13465 "queue fcpcqid=%d\n", cq->queue_id);
13466
13467 /* In any case, flash and re-arm the CQ */
13468 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13469
13470 /* wake up worker thread if there are works to be done */
13471 if (workposted)
13472 lpfc_worker_wake_up(phba);
13473}
13474
13475static void
13476lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13477{
13478 struct lpfc_eqe *eqe;
13479
13480 /* walk all the EQ entries and drop on the floor */
13481 while ((eqe = lpfc_sli4_eq_get(eq)))
13482 ;
13483
13484 /* Clear and re-arm the EQ */
13485 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13486}
13487
James Smart1ba981f2014-02-20 09:56:45 -050013488
13489/**
13490 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13491 * entry
13492 * @phba: Pointer to HBA context object.
13493 * @eqe: Pointer to fast-path event queue entry.
13494 *
13495 * This routine process a event queue entry from the Flash Optimized Fabric
13496 * event queue. It will check the MajorCode and MinorCode to determine this
13497 * is for a completion event on a completion queue, if not, an error shall be
13498 * logged and just return. Otherwise, it will get to the corresponding
13499 * completion queue and process all the entries on the completion queue, rearm
13500 * the completion queue, and then return.
13501 **/
13502static void
13503lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13504{
13505 struct lpfc_queue *cq;
13506 struct lpfc_cqe *cqe;
13507 bool workposted = false;
13508 uint16_t cqid;
13509 int ecount = 0;
13510
13511 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13512 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13513 "9147 Not a valid completion "
13514 "event: majorcode=x%x, minorcode=x%x\n",
13515 bf_get_le32(lpfc_eqe_major_code, eqe),
13516 bf_get_le32(lpfc_eqe_minor_code, eqe));
13517 return;
13518 }
13519
13520 /* Get the reference to the corresponding CQ */
13521 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13522
13523 /* Next check for OAS */
13524 cq = phba->sli4_hba.oas_cq;
13525 if (unlikely(!cq)) {
13526 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13527 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13528 "9148 OAS completion queue "
13529 "does not exist\n");
13530 return;
13531 }
13532
13533 if (unlikely(cqid != cq->queue_id)) {
13534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13535 "9149 Miss-matched fast-path compl "
13536 "queue id: eqcqid=%d, fcpcqid=%d\n",
13537 cqid, cq->queue_id);
13538 return;
13539 }
13540
13541 /* Process all the entries to the OAS CQ */
13542 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013543 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart1ba981f2014-02-20 09:56:45 -050013544 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013545 break;
James Smart1ba981f2014-02-20 09:56:45 -050013546 }
13547
13548 /* Track the max number of CQEs processed in 1 EQ */
13549 if (ecount > cq->CQ_max_cqe)
13550 cq->CQ_max_cqe = ecount;
13551
13552 /* Catch the no cq entry condition */
13553 if (unlikely(ecount == 0))
13554 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13555 "9153 No entry from fast-path completion "
13556 "queue fcpcqid=%d\n", cq->queue_id);
13557
13558 /* In any case, flash and re-arm the CQ */
13559 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13560
13561 /* wake up worker thread if there are works to be done */
13562 if (workposted)
13563 lpfc_worker_wake_up(phba);
13564}
13565
13566/**
13567 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13568 * @irq: Interrupt number.
13569 * @dev_id: The device context pointer.
13570 *
13571 * This function is directly called from the PCI layer as an interrupt
13572 * service routine when device with SLI-4 interface spec is enabled with
13573 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13574 * IOCB ring event in the HBA. However, when the device is enabled with either
13575 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13576 * device-level interrupt handler. When the PCI slot is in error recovery
13577 * or the HBA is undergoing initialization, the interrupt handler will not
13578 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13579 * the intrrupt context. This function is called without any lock held.
13580 * It gets the hbalock to access and update SLI data structures. Note that,
13581 * the EQ to CQ are one-to-one map such that the EQ index is
13582 * equal to that of CQ index.
13583 *
13584 * This function returns IRQ_HANDLED when interrupt is handled else it
13585 * returns IRQ_NONE.
13586 **/
13587irqreturn_t
13588lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13589{
13590 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013591 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013592 struct lpfc_queue *eq;
13593 struct lpfc_eqe *eqe;
13594 unsigned long iflag;
13595 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013596
13597 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013598 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13599 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013600
13601 if (unlikely(!phba))
13602 return IRQ_NONE;
13603
13604 /* Get to the EQ struct associated with this vector */
13605 eq = phba->sli4_hba.fof_eq;
13606 if (unlikely(!eq))
13607 return IRQ_NONE;
13608
13609 /* Check device state for handling interrupt */
13610 if (unlikely(lpfc_intr_state_check(phba))) {
13611 eq->EQ_badstate++;
13612 /* Check again for link_state with lock held */
13613 spin_lock_irqsave(&phba->hbalock, iflag);
13614 if (phba->link_state < LPFC_LINK_DOWN)
13615 /* Flush, clear interrupt, and rearm the EQ */
13616 lpfc_sli4_eq_flush(phba, eq);
13617 spin_unlock_irqrestore(&phba->hbalock, iflag);
13618 return IRQ_NONE;
13619 }
13620
13621 /*
13622 * Process all the event on FCP fast-path EQ
13623 */
13624 while ((eqe = lpfc_sli4_eq_get(eq))) {
13625 lpfc_sli4_fof_handle_eqe(phba, eqe);
13626 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013627 break;
James Smart1ba981f2014-02-20 09:56:45 -050013628 eq->EQ_processed++;
13629 }
13630
13631 /* Track the max number of EQEs processed in 1 intr */
13632 if (ecount > eq->EQ_max_eqe)
13633 eq->EQ_max_eqe = ecount;
13634
13635
13636 if (unlikely(ecount == 0)) {
13637 eq->EQ_no_entry++;
13638
13639 if (phba->intr_type == MSIX)
13640 /* MSI-X treated interrupt served as no EQ share INT */
13641 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13642 "9145 MSI-X interrupt with no EQE\n");
13643 else {
13644 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13645 "9146 ISR interrupt with no EQE\n");
13646 /* Non MSI-X treated on interrupt as EQ share INT */
13647 return IRQ_NONE;
13648 }
13649 }
13650 /* Always clear and re-arm the fast-path EQ */
13651 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13652 return IRQ_HANDLED;
13653}
13654
James Smart4f774512009-05-22 14:52:35 -040013655/**
James Smart67d12732012-08-03 12:36:13 -040013656 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013657 * @irq: Interrupt number.
13658 * @dev_id: The device context pointer.
13659 *
13660 * This function is directly called from the PCI layer as an interrupt
13661 * service routine when device with SLI-4 interface spec is enabled with
13662 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13663 * ring event in the HBA. However, when the device is enabled with either
13664 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13665 * device-level interrupt handler. When the PCI slot is in error recovery
13666 * or the HBA is undergoing initialization, the interrupt handler will not
13667 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13668 * the intrrupt context. This function is called without any lock held.
13669 * It gets the hbalock to access and update SLI data structures. Note that,
13670 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13671 * equal to that of FCP CQ index.
13672 *
James Smart67d12732012-08-03 12:36:13 -040013673 * The link attention and ELS ring attention events are handled
13674 * by the worker thread. The interrupt handler signals the worker thread
13675 * and returns for these events. This function is called without any lock
13676 * held. It gets the hbalock to access and update SLI data structures.
13677 *
James Smart4f774512009-05-22 14:52:35 -040013678 * This function returns IRQ_HANDLED when interrupt is handled else it
13679 * returns IRQ_NONE.
13680 **/
13681irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013682lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013683{
13684 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013685 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013686 struct lpfc_queue *fpeq;
13687 struct lpfc_eqe *eqe;
13688 unsigned long iflag;
13689 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013690 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013691
13692 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013693 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13694 phba = hba_eq_hdl->phba;
13695 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013696
13697 if (unlikely(!phba))
13698 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013699 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013700 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013701
13702 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013703 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013704 if (unlikely(!fpeq))
13705 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013706
James Smartbd2cdd52017-02-12 13:52:33 -080013707#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13708 if (phba->ktime_on)
13709 fpeq->isr_timestamp = ktime_get_ns();
13710#endif
13711
James Smartba20c852012-08-03 12:36:52 -040013712 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013713 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013714 lpfc_sli4_eq_clr_intr(fpeq);
13715 else {
James Smart895427b2017-02-12 13:52:30 -080013716 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013717 return IRQ_NONE;
13718 }
13719 }
13720
James Smart4f774512009-05-22 14:52:35 -040013721 /* Check device state for handling interrupt */
13722 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040013723 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040013724 /* Check again for link_state with lock held */
13725 spin_lock_irqsave(&phba->hbalock, iflag);
13726 if (phba->link_state < LPFC_LINK_DOWN)
13727 /* Flush, clear interrupt, and rearm the EQ */
13728 lpfc_sli4_eq_flush(phba, fpeq);
13729 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013730 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013731 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013732 return IRQ_NONE;
13733 }
13734
13735 /*
13736 * Process all the event on FCP fast-path EQ
13737 */
13738 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040013739 if (eqe == NULL)
13740 break;
13741
James Smart895427b2017-02-12 13:52:30 -080013742 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040013743 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013744 break;
James Smartb84daac2012-08-03 12:35:13 -040013745 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013746 }
13747
James Smartb84daac2012-08-03 12:35:13 -040013748 /* Track the max number of EQEs processed in 1 intr */
13749 if (ecount > fpeq->EQ_max_eqe)
13750 fpeq->EQ_max_eqe = ecount;
13751
James Smart4f774512009-05-22 14:52:35 -040013752 /* Always clear and re-arm the fast-path EQ */
13753 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13754
13755 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013756 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013757
13758 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013759 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013760 return IRQ_NONE;
13761 }
13762
James Smart4f774512009-05-22 14:52:35 -040013763 if (phba->intr_type == MSIX)
13764 /* MSI-X treated interrupt served as no EQ share INT */
13765 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13766 "0358 MSI-X interrupt with no EQE\n");
13767 else
13768 /* Non MSI-X treated on interrupt as EQ share INT */
13769 return IRQ_NONE;
13770 }
13771
James Smartba20c852012-08-03 12:36:52 -040013772 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013773 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13774
James Smart4f774512009-05-22 14:52:35 -040013775 return IRQ_HANDLED;
13776} /* lpfc_sli4_fp_intr_handler */
13777
13778/**
13779 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13780 * @irq: Interrupt number.
13781 * @dev_id: The device context pointer.
13782 *
13783 * This function is the device-level interrupt handler to device with SLI-4
13784 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13785 * interrupt mode is enabled and there is an event in the HBA which requires
13786 * driver attention. This function invokes the slow-path interrupt attention
13787 * handling function and fast-path interrupt attention handling function in
13788 * turn to process the relevant HBA attention events. This function is called
13789 * without any lock held. It gets the hbalock to access and update SLI data
13790 * structures.
13791 *
13792 * This function returns IRQ_HANDLED when interrupt is handled, else it
13793 * returns IRQ_NONE.
13794 **/
13795irqreturn_t
13796lpfc_sli4_intr_handler(int irq, void *dev_id)
13797{
13798 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013799 irqreturn_t hba_irq_rc;
13800 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013801 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013802
13803 /* Get the driver's phba structure from the dev_id */
13804 phba = (struct lpfc_hba *)dev_id;
13805
13806 if (unlikely(!phba))
13807 return IRQ_NONE;
13808
13809 /*
James Smart4f774512009-05-22 14:52:35 -040013810 * Invoke fast-path host attention interrupt handling as appropriate.
13811 */
James Smart895427b2017-02-12 13:52:30 -080013812 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013813 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013814 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013815 if (hba_irq_rc == IRQ_HANDLED)
13816 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013817 }
13818
James Smart1ba981f2014-02-20 09:56:45 -050013819 if (phba->cfg_fof) {
13820 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013821 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013822 if (hba_irq_rc == IRQ_HANDLED)
13823 hba_handled |= true;
13824 }
13825
James Smart67d12732012-08-03 12:36:13 -040013826 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013827} /* lpfc_sli4_intr_handler */
13828
13829/**
13830 * lpfc_sli4_queue_free - free a queue structure and associated memory
13831 * @queue: The queue structure to free.
13832 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013833 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013834 * the host resident queue. This function must be called after destroying the
13835 * queue on the HBA.
13836 **/
13837void
13838lpfc_sli4_queue_free(struct lpfc_queue *queue)
13839{
13840 struct lpfc_dmabuf *dmabuf;
13841
13842 if (!queue)
13843 return;
13844
13845 while (!list_empty(&queue->page_list)) {
13846 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13847 list);
James Smart49198b32010-04-06 15:04:33 -040013848 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040013849 dmabuf->virt, dmabuf->phys);
13850 kfree(dmabuf);
13851 }
James Smart895427b2017-02-12 13:52:30 -080013852 if (queue->rqbp) {
13853 lpfc_free_rq_buffer(queue->phba, queue);
13854 kfree(queue->rqbp);
13855 }
James Smartd1f525a2017-04-21 16:04:55 -070013856
13857 if (!list_empty(&queue->wq_list))
13858 list_del(&queue->wq_list);
13859
James Smart4f774512009-05-22 14:52:35 -040013860 kfree(queue);
13861 return;
13862}
13863
13864/**
13865 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13866 * @phba: The HBA that this queue is being created on.
13867 * @entry_size: The size of each queue entry for this queue.
13868 * @entry count: The number of entries that this queue will handle.
13869 *
13870 * This function allocates a queue structure and the DMAable memory used for
13871 * the host resident queue. This function must be called before creating the
13872 * queue on the HBA.
13873 **/
13874struct lpfc_queue *
13875lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13876 uint32_t entry_count)
13877{
13878 struct lpfc_queue *queue;
13879 struct lpfc_dmabuf *dmabuf;
13880 int x, total_qe_count;
13881 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013882 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013883
James Smartcb5172e2010-03-15 11:25:07 -040013884 if (!phba->sli4_hba.pc_sli4_params.supported)
13885 hw_page_size = SLI4_PAGE_SIZE;
13886
James Smart4f774512009-05-22 14:52:35 -040013887 queue = kzalloc(sizeof(struct lpfc_queue) +
13888 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13889 if (!queue)
13890 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013891 queue->page_count = (ALIGN(entry_size * entry_count,
13892 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013893
13894 /* If needed, Adjust page count to match the max the adapter supports */
13895 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13896 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13897
James Smart4f774512009-05-22 14:52:35 -040013898 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013899 INIT_LIST_HEAD(&queue->wq_list);
James Smart4f774512009-05-22 14:52:35 -040013900 INIT_LIST_HEAD(&queue->page_list);
13901 INIT_LIST_HEAD(&queue->child_list);
13902 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13903 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13904 if (!dmabuf)
13905 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013906 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13907 hw_page_size, &dmabuf->phys,
13908 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013909 if (!dmabuf->virt) {
13910 kfree(dmabuf);
13911 goto out_fail;
13912 }
13913 dmabuf->buffer_tag = x;
13914 list_add_tail(&dmabuf->list, &queue->page_list);
13915 /* initialize queue's entry array */
13916 dma_pointer = dmabuf->virt;
13917 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013918 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013919 total_qe_count++, dma_pointer += entry_size) {
13920 queue->qe[total_qe_count].address = dma_pointer;
13921 }
13922 }
13923 queue->entry_size = entry_size;
13924 queue->entry_count = entry_count;
13925 queue->phba = phba;
13926
James Smart64eb4dc2017-05-15 15:20:49 -070013927 /* entry_repost will be set during q creation */
13928
James Smart4f774512009-05-22 14:52:35 -040013929 return queue;
13930out_fail:
13931 lpfc_sli4_queue_free(queue);
13932 return NULL;
13933}
13934
13935/**
James Smart962bc512013-01-03 15:44:00 -050013936 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13937 * @phba: HBA structure that indicates port to create a queue on.
13938 * @pci_barset: PCI BAR set flag.
13939 *
13940 * This function shall perform iomap of the specified PCI BAR address to host
13941 * memory address if not already done so and return it. The returned host
13942 * memory address can be NULL.
13943 */
13944static void __iomem *
13945lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13946{
James Smart962bc512013-01-03 15:44:00 -050013947 if (!phba->pcidev)
13948 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013949
13950 switch (pci_barset) {
13951 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013952 return phba->pci_bar0_memmap_p;
13953 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013954 return phba->pci_bar2_memmap_p;
13955 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013956 return phba->pci_bar4_memmap_p;
13957 default:
13958 break;
13959 }
13960 return NULL;
13961}
13962
13963/**
James Smart895427b2017-02-12 13:52:30 -080013964 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040013965 * @phba: HBA structure that indicates port to create a queue on.
13966 * @startq: The starting FCP EQ to modify
13967 *
13968 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080013969 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
13970 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040013971 *
13972 * The @phba struct is used to send mailbox command to HBA. The @startq
13973 * is used to get the starting FCP EQ to change.
13974 * This function is asynchronous and will wait for the mailbox
13975 * command to finish before continuing.
13976 *
13977 * On success this function will return a zero. If unable to allocate enough
13978 * memory this function will return -ENOMEM. If the queue create mailbox command
13979 * fails this function will return -ENXIO.
13980 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013981int
James Smart895427b2017-02-12 13:52:30 -080013982lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013983{
13984 struct lpfc_mbx_modify_eq_delay *eq_delay;
13985 LPFC_MBOXQ_t *mbox;
13986 struct lpfc_queue *eq;
13987 int cnt, rc, length, status = 0;
13988 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013989 uint32_t result;
James Smart895427b2017-02-12 13:52:30 -080013990 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040013991 union lpfc_sli4_cfg_shdr *shdr;
13992 uint16_t dmult;
13993
James Smart895427b2017-02-12 13:52:30 -080013994 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040013995 return 0;
13996
13997 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13998 if (!mbox)
13999 return -ENOMEM;
14000 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14001 sizeof(struct lpfc_sli4_cfg_mhdr));
14002 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14003 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14004 length, LPFC_SLI4_MBX_EMBED);
14005 eq_delay = &mbox->u.mqe.un.eq_delay;
14006
14007 /* Calculate delay multiper from maximum interrupt per second */
James Smart895427b2017-02-12 13:52:30 -080014008 result = phba->cfg_fcp_imax / phba->io_channel_irqs;
14009 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014010 dmult = 0;
14011 else
14012 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040014013
14014 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014015 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14016 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014017 if (!eq)
14018 continue;
14019 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14020 eq_delay->u.request.eq[cnt].phase = 0;
14021 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14022 cnt++;
James Smart43140ca2017-03-04 09:30:34 -080014023 if (cnt >= LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart173edbb2012-06-12 13:54:50 -040014024 break;
14025 }
14026 eq_delay->u.request.num_eq = cnt;
14027
14028 mbox->vport = phba->pport;
14029 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14030 mbox->context1 = NULL;
14031 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14032 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14033 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14034 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14035 if (shdr_status || shdr_add_status || rc) {
14036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14037 "2512 MODIFY_EQ_DELAY mailbox failed with "
14038 "status x%x add_status x%x, mbx status x%x\n",
14039 shdr_status, shdr_add_status, rc);
14040 status = -ENXIO;
14041 }
14042 mempool_free(mbox, phba->mbox_mem_pool);
14043 return status;
14044}
14045
14046/**
James Smart4f774512009-05-22 14:52:35 -040014047 * lpfc_eq_create - Create an Event Queue on the HBA
14048 * @phba: HBA structure that indicates port to create a queue on.
14049 * @eq: The queue structure to use to create the event queue.
14050 * @imax: The maximum interrupt per second limit.
14051 *
14052 * This function creates an event queue, as detailed in @eq, on a port,
14053 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14054 *
14055 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14056 * is used to get the entry count and entry size that are necessary to
14057 * determine the number of pages to allocate and use for this queue. This
14058 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14059 * event queue. This function is asynchronous and will wait for the mailbox
14060 * command to finish before continuing.
14061 *
14062 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014063 * memory this function will return -ENOMEM. If the queue create mailbox command
14064 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014065 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014066int
James Smartee020062012-09-29 11:28:52 -040014067lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014068{
14069 struct lpfc_mbx_eq_create *eq_create;
14070 LPFC_MBOXQ_t *mbox;
14071 int rc, length, status = 0;
14072 struct lpfc_dmabuf *dmabuf;
14073 uint32_t shdr_status, shdr_add_status;
14074 union lpfc_sli4_cfg_shdr *shdr;
14075 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014076 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14077
James Smart2e90f4b2011-12-13 13:22:37 -050014078 /* sanity check on queue memory */
14079 if (!eq)
14080 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014081 if (!phba->sli4_hba.pc_sli4_params.supported)
14082 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014083
14084 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14085 if (!mbox)
14086 return -ENOMEM;
14087 length = (sizeof(struct lpfc_mbx_eq_create) -
14088 sizeof(struct lpfc_sli4_cfg_mhdr));
14089 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14090 LPFC_MBOX_OPCODE_EQ_CREATE,
14091 length, LPFC_SLI4_MBX_EMBED);
14092 eq_create = &mbox->u.mqe.un.eq_create;
14093 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14094 eq->page_count);
14095 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14096 LPFC_EQE_SIZE);
14097 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040014098 /* don't setup delay multiplier using EQ_CREATE */
14099 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014100 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14101 dmult);
14102 switch (eq->entry_count) {
14103 default:
14104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14105 "0360 Unsupported EQ count. (%d)\n",
14106 eq->entry_count);
14107 if (eq->entry_count < 256)
14108 return -EINVAL;
14109 /* otherwise default to smallest count (drop through) */
14110 case 256:
14111 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14112 LPFC_EQ_CNT_256);
14113 break;
14114 case 512:
14115 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14116 LPFC_EQ_CNT_512);
14117 break;
14118 case 1024:
14119 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14120 LPFC_EQ_CNT_1024);
14121 break;
14122 case 2048:
14123 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14124 LPFC_EQ_CNT_2048);
14125 break;
14126 case 4096:
14127 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14128 LPFC_EQ_CNT_4096);
14129 break;
14130 }
14131 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014132 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014133 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14134 putPaddrLow(dmabuf->phys);
14135 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14136 putPaddrHigh(dmabuf->phys);
14137 }
14138 mbox->vport = phba->pport;
14139 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14140 mbox->context1 = NULL;
14141 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14142 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14143 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14144 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14145 if (shdr_status || shdr_add_status || rc) {
14146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14147 "2500 EQ_CREATE mailbox failed with "
14148 "status x%x add_status x%x, mbx status x%x\n",
14149 shdr_status, shdr_add_status, rc);
14150 status = -ENXIO;
14151 }
14152 eq->type = LPFC_EQ;
14153 eq->subtype = LPFC_NONE;
14154 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14155 if (eq->queue_id == 0xFFFF)
14156 status = -ENXIO;
14157 eq->host_index = 0;
14158 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014159 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014160
James Smart8fa38512009-07-19 10:01:03 -040014161 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014162 return status;
14163}
14164
14165/**
14166 * lpfc_cq_create - Create a Completion Queue on the HBA
14167 * @phba: HBA structure that indicates port to create a queue on.
14168 * @cq: The queue structure to use to create the completion queue.
14169 * @eq: The event queue to bind this completion queue to.
14170 *
14171 * This function creates a completion queue, as detailed in @wq, on a port,
14172 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14173 *
14174 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14175 * is used to get the entry count and entry size that are necessary to
14176 * determine the number of pages to allocate and use for this queue. The @eq
14177 * is used to indicate which event queue to bind this completion queue to. This
14178 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14179 * completion queue. This function is asynchronous and will wait for the mailbox
14180 * command to finish before continuing.
14181 *
14182 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014183 * memory this function will return -ENOMEM. If the queue create mailbox command
14184 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014185 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014186int
James Smart4f774512009-05-22 14:52:35 -040014187lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14188 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14189{
14190 struct lpfc_mbx_cq_create *cq_create;
14191 struct lpfc_dmabuf *dmabuf;
14192 LPFC_MBOXQ_t *mbox;
14193 int rc, length, status = 0;
14194 uint32_t shdr_status, shdr_add_status;
14195 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014196 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14197
James Smart2e90f4b2011-12-13 13:22:37 -050014198 /* sanity check on queue memory */
14199 if (!cq || !eq)
14200 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014201 if (!phba->sli4_hba.pc_sli4_params.supported)
14202 hw_page_size = SLI4_PAGE_SIZE;
14203
James Smart4f774512009-05-22 14:52:35 -040014204 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14205 if (!mbox)
14206 return -ENOMEM;
14207 length = (sizeof(struct lpfc_mbx_cq_create) -
14208 sizeof(struct lpfc_sli4_cfg_mhdr));
14209 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14210 LPFC_MBOX_OPCODE_CQ_CREATE,
14211 length, LPFC_SLI4_MBX_EMBED);
14212 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014213 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014214 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14215 cq->page_count);
14216 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14217 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014218 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14219 phba->sli4_hba.pc_sli4_params.cqv);
14220 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040014221 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
14222 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014223 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14224 eq->queue_id);
14225 } else {
14226 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14227 eq->queue_id);
14228 }
James Smart4f774512009-05-22 14:52:35 -040014229 switch (cq->entry_count) {
14230 default:
14231 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014232 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014233 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014234 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014235 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014236 if (cq->entry_count < 256) {
14237 status = -EINVAL;
14238 goto out;
14239 }
James Smart4f774512009-05-22 14:52:35 -040014240 /* otherwise default to smallest count (drop through) */
14241 case 256:
14242 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14243 LPFC_CQ_CNT_256);
14244 break;
14245 case 512:
14246 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14247 LPFC_CQ_CNT_512);
14248 break;
14249 case 1024:
14250 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14251 LPFC_CQ_CNT_1024);
14252 break;
14253 }
14254 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014255 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014256 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14257 putPaddrLow(dmabuf->phys);
14258 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14259 putPaddrHigh(dmabuf->phys);
14260 }
14261 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14262
14263 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014264 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14265 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14266 if (shdr_status || shdr_add_status || rc) {
14267 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14268 "2501 CQ_CREATE mailbox failed with "
14269 "status x%x add_status x%x, mbx status x%x\n",
14270 shdr_status, shdr_add_status, rc);
14271 status = -ENXIO;
14272 goto out;
14273 }
14274 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14275 if (cq->queue_id == 0xFFFF) {
14276 status = -ENXIO;
14277 goto out;
14278 }
14279 /* link the cq onto the parent eq child list */
14280 list_add_tail(&cq->list, &eq->child_list);
14281 /* Set up completion queue's type and subtype */
14282 cq->type = type;
14283 cq->subtype = subtype;
14284 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014285 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014286 cq->host_index = 0;
14287 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014288 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014289
James Smart8fa38512009-07-19 10:01:03 -040014290out:
14291 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014292 return status;
14293}
14294
14295/**
James Smart2d7dbc42017-02-12 13:52:35 -080014296 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14297 * @phba: HBA structure that indicates port to create a queue on.
14298 * @cqp: The queue structure array to use to create the completion queues.
14299 * @eqp: The event queue array to bind these completion queues to.
14300 *
14301 * This function creates a set of completion queue, s to support MRQ
14302 * as detailed in @cqp, on a port,
14303 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14304 *
14305 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14306 * is used to get the entry count and entry size that are necessary to
14307 * determine the number of pages to allocate and use for this queue. The @eq
14308 * is used to indicate which event queue to bind this completion queue to. This
14309 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14310 * completion queue. This function is asynchronous and will wait for the mailbox
14311 * command to finish before continuing.
14312 *
14313 * On success this function will return a zero. If unable to allocate enough
14314 * memory this function will return -ENOMEM. If the queue create mailbox command
14315 * fails this function will return -ENXIO.
14316 **/
14317int
14318lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14319 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14320{
14321 struct lpfc_queue *cq;
14322 struct lpfc_queue *eq;
14323 struct lpfc_mbx_cq_create_set *cq_set;
14324 struct lpfc_dmabuf *dmabuf;
14325 LPFC_MBOXQ_t *mbox;
14326 int rc, length, alloclen, status = 0;
14327 int cnt, idx, numcq, page_idx = 0;
14328 uint32_t shdr_status, shdr_add_status;
14329 union lpfc_sli4_cfg_shdr *shdr;
14330 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14331
14332 /* sanity check on queue memory */
14333 numcq = phba->cfg_nvmet_mrq;
14334 if (!cqp || !eqp || !numcq)
14335 return -ENODEV;
14336 if (!phba->sli4_hba.pc_sli4_params.supported)
14337 hw_page_size = SLI4_PAGE_SIZE;
14338
14339 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14340 if (!mbox)
14341 return -ENOMEM;
14342
14343 length = sizeof(struct lpfc_mbx_cq_create_set);
14344 length += ((numcq * cqp[0]->page_count) *
14345 sizeof(struct dma_address));
14346 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14347 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14348 LPFC_SLI4_MBX_NEMBED);
14349 if (alloclen < length) {
14350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14351 "3098 Allocated DMA memory size (%d) is "
14352 "less than the requested DMA memory size "
14353 "(%d)\n", alloclen, length);
14354 status = -ENOMEM;
14355 goto out;
14356 }
14357 cq_set = mbox->sge_array->addr[0];
14358 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14359 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14360
14361 for (idx = 0; idx < numcq; idx++) {
14362 cq = cqp[idx];
14363 eq = eqp[idx];
14364 if (!cq || !eq) {
14365 status = -ENOMEM;
14366 goto out;
14367 }
14368
14369 switch (idx) {
14370 case 0:
14371 bf_set(lpfc_mbx_cq_create_set_page_size,
14372 &cq_set->u.request,
14373 (hw_page_size / SLI4_PAGE_SIZE));
14374 bf_set(lpfc_mbx_cq_create_set_num_pages,
14375 &cq_set->u.request, cq->page_count);
14376 bf_set(lpfc_mbx_cq_create_set_evt,
14377 &cq_set->u.request, 1);
14378 bf_set(lpfc_mbx_cq_create_set_valid,
14379 &cq_set->u.request, 1);
14380 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14381 &cq_set->u.request, 0);
14382 bf_set(lpfc_mbx_cq_create_set_num_cq,
14383 &cq_set->u.request, numcq);
14384 switch (cq->entry_count) {
14385 default:
14386 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14387 "3118 Bad CQ count. (%d)\n",
14388 cq->entry_count);
14389 if (cq->entry_count < 256) {
14390 status = -EINVAL;
14391 goto out;
14392 }
14393 /* otherwise default to smallest (drop thru) */
14394 case 256:
14395 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14396 &cq_set->u.request, LPFC_CQ_CNT_256);
14397 break;
14398 case 512:
14399 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14400 &cq_set->u.request, LPFC_CQ_CNT_512);
14401 break;
14402 case 1024:
14403 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14404 &cq_set->u.request, LPFC_CQ_CNT_1024);
14405 break;
14406 }
14407 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14408 &cq_set->u.request, eq->queue_id);
14409 break;
14410 case 1:
14411 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14412 &cq_set->u.request, eq->queue_id);
14413 break;
14414 case 2:
14415 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14416 &cq_set->u.request, eq->queue_id);
14417 break;
14418 case 3:
14419 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14420 &cq_set->u.request, eq->queue_id);
14421 break;
14422 case 4:
14423 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14424 &cq_set->u.request, eq->queue_id);
14425 break;
14426 case 5:
14427 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14428 &cq_set->u.request, eq->queue_id);
14429 break;
14430 case 6:
14431 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14432 &cq_set->u.request, eq->queue_id);
14433 break;
14434 case 7:
14435 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14436 &cq_set->u.request, eq->queue_id);
14437 break;
14438 case 8:
14439 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14440 &cq_set->u.request, eq->queue_id);
14441 break;
14442 case 9:
14443 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14444 &cq_set->u.request, eq->queue_id);
14445 break;
14446 case 10:
14447 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14448 &cq_set->u.request, eq->queue_id);
14449 break;
14450 case 11:
14451 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14452 &cq_set->u.request, eq->queue_id);
14453 break;
14454 case 12:
14455 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14456 &cq_set->u.request, eq->queue_id);
14457 break;
14458 case 13:
14459 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14460 &cq_set->u.request, eq->queue_id);
14461 break;
14462 case 14:
14463 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14464 &cq_set->u.request, eq->queue_id);
14465 break;
14466 case 15:
14467 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14468 &cq_set->u.request, eq->queue_id);
14469 break;
14470 }
14471
14472 /* link the cq onto the parent eq child list */
14473 list_add_tail(&cq->list, &eq->child_list);
14474 /* Set up completion queue's type and subtype */
14475 cq->type = type;
14476 cq->subtype = subtype;
14477 cq->assoc_qid = eq->queue_id;
14478 cq->host_index = 0;
14479 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014480 cq->entry_repost = LPFC_CQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080014481
14482 rc = 0;
14483 list_for_each_entry(dmabuf, &cq->page_list, list) {
14484 memset(dmabuf->virt, 0, hw_page_size);
14485 cnt = page_idx + dmabuf->buffer_tag;
14486 cq_set->u.request.page[cnt].addr_lo =
14487 putPaddrLow(dmabuf->phys);
14488 cq_set->u.request.page[cnt].addr_hi =
14489 putPaddrHigh(dmabuf->phys);
14490 rc++;
14491 }
14492 page_idx += rc;
14493 }
14494
14495 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14496
14497 /* The IOCTL status is embedded in the mailbox subheader. */
14498 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14499 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14500 if (shdr_status || shdr_add_status || rc) {
14501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14502 "3119 CQ_CREATE_SET mailbox failed with "
14503 "status x%x add_status x%x, mbx status x%x\n",
14504 shdr_status, shdr_add_status, rc);
14505 status = -ENXIO;
14506 goto out;
14507 }
14508 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14509 if (rc == 0xFFFF) {
14510 status = -ENXIO;
14511 goto out;
14512 }
14513
14514 for (idx = 0; idx < numcq; idx++) {
14515 cq = cqp[idx];
14516 cq->queue_id = rc + idx;
14517 }
14518
14519out:
14520 lpfc_sli4_mbox_cmd_free(phba, mbox);
14521 return status;
14522}
14523
14524/**
James Smartb19a0612010-04-06 14:48:51 -040014525 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014526 * @phba: HBA structure that indicates port to create a queue on.
14527 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014528 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14529 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014530 *
James Smartb19a0612010-04-06 14:48:51 -040014531 * This function provides failback (fb) functionality when the
14532 * mq_create_ext fails on older FW generations. It's purpose is identical
14533 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014534 *
James Smartb19a0612010-04-06 14:48:51 -040014535 * This routine cannot fail as all attributes were previously accessed and
14536 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014537 **/
James Smartb19a0612010-04-06 14:48:51 -040014538static void
14539lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14540 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014541{
14542 struct lpfc_mbx_mq_create *mq_create;
14543 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014544 int length;
James Smart04c68492009-05-22 14:52:52 -040014545
James Smart04c68492009-05-22 14:52:52 -040014546 length = (sizeof(struct lpfc_mbx_mq_create) -
14547 sizeof(struct lpfc_sli4_cfg_mhdr));
14548 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14549 LPFC_MBOX_OPCODE_MQ_CREATE,
14550 length, LPFC_SLI4_MBX_EMBED);
14551 mq_create = &mbox->u.mqe.un.mq_create;
14552 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014553 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014554 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014555 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014556 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14557 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014558 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014559 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14560 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014561 break;
14562 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014563 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14564 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014565 break;
14566 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014567 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14568 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014569 break;
14570 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014571 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14572 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014573 break;
14574 }
14575 list_for_each_entry(dmabuf, &mq->page_list, list) {
14576 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014577 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014578 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014579 putPaddrHigh(dmabuf->phys);
14580 }
14581}
14582
14583/**
14584 * lpfc_mq_create - Create a mailbox Queue on the HBA
14585 * @phba: HBA structure that indicates port to create a queue on.
14586 * @mq: The queue structure to use to create the mailbox queue.
14587 * @cq: The completion queue to associate with this cq.
14588 * @subtype: The queue's subtype.
14589 *
14590 * This function creates a mailbox queue, as detailed in @mq, on a port,
14591 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14592 *
14593 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14594 * is used to get the entry count and entry size that are necessary to
14595 * determine the number of pages to allocate and use for this queue. This
14596 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14597 * mailbox queue. This function is asynchronous and will wait for the mailbox
14598 * command to finish before continuing.
14599 *
14600 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014601 * memory this function will return -ENOMEM. If the queue create mailbox command
14602 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014603 **/
14604int32_t
14605lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14606 struct lpfc_queue *cq, uint32_t subtype)
14607{
14608 struct lpfc_mbx_mq_create *mq_create;
14609 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14610 struct lpfc_dmabuf *dmabuf;
14611 LPFC_MBOXQ_t *mbox;
14612 int rc, length, status = 0;
14613 uint32_t shdr_status, shdr_add_status;
14614 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014615 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014616
James Smart2e90f4b2011-12-13 13:22:37 -050014617 /* sanity check on queue memory */
14618 if (!mq || !cq)
14619 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014620 if (!phba->sli4_hba.pc_sli4_params.supported)
14621 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014622
14623 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14624 if (!mbox)
14625 return -ENOMEM;
14626 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14627 sizeof(struct lpfc_sli4_cfg_mhdr));
14628 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14629 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14630 length, LPFC_SLI4_MBX_EMBED);
14631
14632 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014633 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014634 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14635 &mq_create_ext->u.request, mq->page_count);
14636 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14637 &mq_create_ext->u.request, 1);
14638 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014639 &mq_create_ext->u.request, 1);
14640 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14641 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014642 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14643 &mq_create_ext->u.request, 1);
14644 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14645 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014646 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014647 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14648 phba->sli4_hba.pc_sli4_params.mqv);
14649 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14650 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14651 cq->queue_id);
14652 else
14653 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14654 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014655 switch (mq->entry_count) {
14656 default:
14657 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14658 "0362 Unsupported MQ count. (%d)\n",
14659 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014660 if (mq->entry_count < 16) {
14661 status = -EINVAL;
14662 goto out;
14663 }
James Smartb19a0612010-04-06 14:48:51 -040014664 /* otherwise default to smallest count (drop through) */
14665 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014666 bf_set(lpfc_mq_context_ring_size,
14667 &mq_create_ext->u.request.context,
14668 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014669 break;
14670 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014671 bf_set(lpfc_mq_context_ring_size,
14672 &mq_create_ext->u.request.context,
14673 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014674 break;
14675 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014676 bf_set(lpfc_mq_context_ring_size,
14677 &mq_create_ext->u.request.context,
14678 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014679 break;
14680 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014681 bf_set(lpfc_mq_context_ring_size,
14682 &mq_create_ext->u.request.context,
14683 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014684 break;
14685 }
14686 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014687 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014688 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14689 putPaddrLow(dmabuf->phys);
14690 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014691 putPaddrHigh(dmabuf->phys);
14692 }
14693 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014694 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14695 &mq_create_ext->u.response);
14696 if (rc != MBX_SUCCESS) {
14697 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14698 "2795 MQ_CREATE_EXT failed with "
14699 "status x%x. Failback to MQ_CREATE.\n",
14700 rc);
14701 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14702 mq_create = &mbox->u.mqe.un.mq_create;
14703 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14704 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14705 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14706 &mq_create->u.response);
14707 }
14708
James Smart04c68492009-05-22 14:52:52 -040014709 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014710 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14711 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14712 if (shdr_status || shdr_add_status || rc) {
14713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14714 "2502 MQ_CREATE mailbox failed with "
14715 "status x%x add_status x%x, mbx status x%x\n",
14716 shdr_status, shdr_add_status, rc);
14717 status = -ENXIO;
14718 goto out;
14719 }
James Smart04c68492009-05-22 14:52:52 -040014720 if (mq->queue_id == 0xFFFF) {
14721 status = -ENXIO;
14722 goto out;
14723 }
14724 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014725 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014726 mq->subtype = subtype;
14727 mq->host_index = 0;
14728 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014729 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040014730
14731 /* link the mq onto the parent cq child list */
14732 list_add_tail(&mq->list, &cq->child_list);
14733out:
James Smart8fa38512009-07-19 10:01:03 -040014734 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014735 return status;
14736}
14737
14738/**
James Smart4f774512009-05-22 14:52:35 -040014739 * lpfc_wq_create - Create a Work Queue on the HBA
14740 * @phba: HBA structure that indicates port to create a queue on.
14741 * @wq: The queue structure to use to create the work queue.
14742 * @cq: The completion queue to bind this work queue to.
14743 * @subtype: The subtype of the work queue indicating its functionality.
14744 *
14745 * This function creates a work queue, as detailed in @wq, on a port, described
14746 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14747 *
14748 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14749 * is used to get the entry count and entry size that are necessary to
14750 * determine the number of pages to allocate and use for this queue. The @cq
14751 * is used to indicate which completion queue to bind this work queue to. This
14752 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14753 * work queue. This function is asynchronous and will wait for the mailbox
14754 * command to finish before continuing.
14755 *
14756 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014757 * memory this function will return -ENOMEM. If the queue create mailbox command
14758 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014759 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014760int
James Smart4f774512009-05-22 14:52:35 -040014761lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14762 struct lpfc_queue *cq, uint32_t subtype)
14763{
14764 struct lpfc_mbx_wq_create *wq_create;
14765 struct lpfc_dmabuf *dmabuf;
14766 LPFC_MBOXQ_t *mbox;
14767 int rc, length, status = 0;
14768 uint32_t shdr_status, shdr_add_status;
14769 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014770 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014771 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014772 void __iomem *bar_memmap_p;
14773 uint32_t db_offset;
14774 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014775
James Smart2e90f4b2011-12-13 13:22:37 -050014776 /* sanity check on queue memory */
14777 if (!wq || !cq)
14778 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014779 if (!phba->sli4_hba.pc_sli4_params.supported)
14780 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014781
14782 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14783 if (!mbox)
14784 return -ENOMEM;
14785 length = (sizeof(struct lpfc_mbx_wq_create) -
14786 sizeof(struct lpfc_sli4_cfg_mhdr));
14787 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14788 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14789 length, LPFC_SLI4_MBX_EMBED);
14790 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014791 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014792 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14793 wq->page_count);
14794 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14795 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014796
14797 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014798 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14799 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014800
James Smart0c651872013-07-15 18:33:23 -040014801 switch (phba->sli4_hba.pc_sli4_params.wqv) {
14802 case LPFC_Q_CREATE_VERSION_0:
14803 switch (wq->entry_size) {
14804 default:
14805 case 64:
14806 /* Nothing to do, version 0 ONLY supports 64 byte */
14807 page = wq_create->u.request.page;
14808 break;
14809 case 128:
14810 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14811 LPFC_WQ_SZ128_SUPPORT)) {
14812 status = -ERANGE;
14813 goto out;
14814 }
14815 /* If we get here the HBA MUST also support V1 and
14816 * we MUST use it
14817 */
14818 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14819 LPFC_Q_CREATE_VERSION_1);
14820
14821 bf_set(lpfc_mbx_wq_create_wqe_count,
14822 &wq_create->u.request_1, wq->entry_count);
14823 bf_set(lpfc_mbx_wq_create_wqe_size,
14824 &wq_create->u.request_1,
14825 LPFC_WQ_WQE_SIZE_128);
14826 bf_set(lpfc_mbx_wq_create_page_size,
14827 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014828 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014829 page = wq_create->u.request_1.page;
14830 break;
14831 }
14832 break;
14833 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014834 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14835 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070014836 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14837 LPFC_Q_CREATE_VERSION_1);
14838
James Smart5a6f1332011-03-11 16:05:35 -050014839 switch (wq->entry_size) {
14840 default:
14841 case 64:
14842 bf_set(lpfc_mbx_wq_create_wqe_size,
14843 &wq_create->u.request_1,
14844 LPFC_WQ_WQE_SIZE_64);
14845 break;
14846 case 128:
James Smart0c651872013-07-15 18:33:23 -040014847 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14848 LPFC_WQ_SZ128_SUPPORT)) {
14849 status = -ERANGE;
14850 goto out;
14851 }
James Smart5a6f1332011-03-11 16:05:35 -050014852 bf_set(lpfc_mbx_wq_create_wqe_size,
14853 &wq_create->u.request_1,
14854 LPFC_WQ_WQE_SIZE_128);
14855 break;
14856 }
James Smart8ea73db2017-02-12 13:52:25 -080014857 bf_set(lpfc_mbx_wq_create_page_size,
14858 &wq_create->u.request_1,
14859 LPFC_WQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014860 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040014861 break;
14862 default:
14863 status = -ERANGE;
14864 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050014865 }
James Smart0c651872013-07-15 18:33:23 -040014866
James Smart4f774512009-05-22 14:52:35 -040014867 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014868 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050014869 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14870 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040014871 }
James Smart962bc512013-01-03 15:44:00 -050014872
14873 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14874 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14875
James Smart4f774512009-05-22 14:52:35 -040014876 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14877 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014878 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14879 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14880 if (shdr_status || shdr_add_status || rc) {
14881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14882 "2503 WQ_CREATE mailbox failed with "
14883 "status x%x add_status x%x, mbx status x%x\n",
14884 shdr_status, shdr_add_status, rc);
14885 status = -ENXIO;
14886 goto out;
14887 }
14888 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14889 if (wq->queue_id == 0xFFFF) {
14890 status = -ENXIO;
14891 goto out;
14892 }
James Smart962bc512013-01-03 15:44:00 -050014893 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14894 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14895 &wq_create->u.response);
14896 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14897 (wq->db_format != LPFC_DB_RING_FORMAT)) {
14898 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14899 "3265 WQ[%d] doorbell format not "
14900 "supported: x%x\n", wq->queue_id,
14901 wq->db_format);
14902 status = -EINVAL;
14903 goto out;
14904 }
14905 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14906 &wq_create->u.response);
14907 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14908 if (!bar_memmap_p) {
14909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14910 "3263 WQ[%d] failed to memmap pci "
14911 "barset:x%x\n", wq->queue_id,
14912 pci_barset);
14913 status = -ENOMEM;
14914 goto out;
14915 }
14916 db_offset = wq_create->u.response.doorbell_offset;
14917 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14918 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14920 "3252 WQ[%d] doorbell offset not "
14921 "supported: x%x\n", wq->queue_id,
14922 db_offset);
14923 status = -EINVAL;
14924 goto out;
14925 }
14926 wq->db_regaddr = bar_memmap_p + db_offset;
14927 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014928 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14929 "format:x%x\n", wq->queue_id, pci_barset,
14930 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014931 } else {
14932 wq->db_format = LPFC_DB_LIST_FORMAT;
14933 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14934 }
James Smart895427b2017-02-12 13:52:30 -080014935 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14936 if (wq->pring == NULL) {
14937 status = -ENOMEM;
14938 goto out;
14939 }
James Smart4f774512009-05-22 14:52:35 -040014940 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050014941 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014942 wq->subtype = subtype;
14943 wq->host_index = 0;
14944 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050014945 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040014946
14947 /* link the wq onto the parent cq child list */
14948 list_add_tail(&wq->list, &cq->child_list);
14949out:
James Smart8fa38512009-07-19 10:01:03 -040014950 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014951 return status;
14952}
14953
14954/**
14955 * lpfc_rq_create - Create a Receive Queue on the HBA
14956 * @phba: HBA structure that indicates port to create a queue on.
14957 * @hrq: The queue structure to use to create the header receive queue.
14958 * @drq: The queue structure to use to create the data receive queue.
14959 * @cq: The completion queue to bind this work queue to.
14960 *
14961 * This function creates a receive buffer queue pair , as detailed in @hrq and
14962 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14963 * to the HBA.
14964 *
14965 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14966 * struct is used to get the entry count that is necessary to determine the
14967 * number of pages to use for this queue. The @cq is used to indicate which
14968 * completion queue to bind received buffers that are posted to these queues to.
14969 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14970 * receive queue pair. This function is asynchronous and will wait for the
14971 * mailbox command to finish before continuing.
14972 *
14973 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014974 * memory this function will return -ENOMEM. If the queue create mailbox command
14975 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014976 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014977int
James Smart4f774512009-05-22 14:52:35 -040014978lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14979 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14980{
14981 struct lpfc_mbx_rq_create *rq_create;
14982 struct lpfc_dmabuf *dmabuf;
14983 LPFC_MBOXQ_t *mbox;
14984 int rc, length, status = 0;
14985 uint32_t shdr_status, shdr_add_status;
14986 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014987 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050014988 void __iomem *bar_memmap_p;
14989 uint32_t db_offset;
14990 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014991
James Smart2e90f4b2011-12-13 13:22:37 -050014992 /* sanity check on queue memory */
14993 if (!hrq || !drq || !cq)
14994 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014995 if (!phba->sli4_hba.pc_sli4_params.supported)
14996 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014997
14998 if (hrq->entry_count != drq->entry_count)
14999 return -EINVAL;
15000 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15001 if (!mbox)
15002 return -ENOMEM;
15003 length = (sizeof(struct lpfc_mbx_rq_create) -
15004 sizeof(struct lpfc_sli4_cfg_mhdr));
15005 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15006 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15007 length, LPFC_SLI4_MBX_EMBED);
15008 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015009 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15010 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15011 phba->sli4_hba.pc_sli4_params.rqv);
15012 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15013 bf_set(lpfc_rq_context_rqe_count_1,
15014 &rq_create->u.request.context,
15015 hrq->entry_count);
15016 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015017 bf_set(lpfc_rq_context_rqe_size,
15018 &rq_create->u.request.context,
15019 LPFC_RQE_SIZE_8);
15020 bf_set(lpfc_rq_context_page_size,
15021 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015022 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015023 } else {
15024 switch (hrq->entry_count) {
15025 default:
15026 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15027 "2535 Unsupported RQ count. (%d)\n",
15028 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015029 if (hrq->entry_count < 512) {
15030 status = -EINVAL;
15031 goto out;
15032 }
James Smart5a6f1332011-03-11 16:05:35 -050015033 /* otherwise default to smallest count (drop through) */
15034 case 512:
15035 bf_set(lpfc_rq_context_rqe_count,
15036 &rq_create->u.request.context,
15037 LPFC_RQ_RING_SIZE_512);
15038 break;
15039 case 1024:
15040 bf_set(lpfc_rq_context_rqe_count,
15041 &rq_create->u.request.context,
15042 LPFC_RQ_RING_SIZE_1024);
15043 break;
15044 case 2048:
15045 bf_set(lpfc_rq_context_rqe_count,
15046 &rq_create->u.request.context,
15047 LPFC_RQ_RING_SIZE_2048);
15048 break;
15049 case 4096:
15050 bf_set(lpfc_rq_context_rqe_count,
15051 &rq_create->u.request.context,
15052 LPFC_RQ_RING_SIZE_4096);
15053 break;
15054 }
15055 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15056 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015057 }
15058 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15059 cq->queue_id);
15060 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15061 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015062 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015063 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015064 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15065 putPaddrLow(dmabuf->phys);
15066 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15067 putPaddrHigh(dmabuf->phys);
15068 }
James Smart962bc512013-01-03 15:44:00 -050015069 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15070 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15071
James Smart4f774512009-05-22 14:52:35 -040015072 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15073 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015074 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15075 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15076 if (shdr_status || shdr_add_status || rc) {
15077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15078 "2504 RQ_CREATE mailbox failed with "
15079 "status x%x add_status x%x, mbx status x%x\n",
15080 shdr_status, shdr_add_status, rc);
15081 status = -ENXIO;
15082 goto out;
15083 }
15084 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15085 if (hrq->queue_id == 0xFFFF) {
15086 status = -ENXIO;
15087 goto out;
15088 }
James Smart962bc512013-01-03 15:44:00 -050015089
15090 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15091 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15092 &rq_create->u.response);
15093 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15094 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15096 "3262 RQ [%d] doorbell format not "
15097 "supported: x%x\n", hrq->queue_id,
15098 hrq->db_format);
15099 status = -EINVAL;
15100 goto out;
15101 }
15102
15103 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15104 &rq_create->u.response);
15105 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15106 if (!bar_memmap_p) {
15107 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15108 "3269 RQ[%d] failed to memmap pci "
15109 "barset:x%x\n", hrq->queue_id,
15110 pci_barset);
15111 status = -ENOMEM;
15112 goto out;
15113 }
15114
15115 db_offset = rq_create->u.response.doorbell_offset;
15116 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15117 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15118 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15119 "3270 RQ[%d] doorbell offset not "
15120 "supported: x%x\n", hrq->queue_id,
15121 db_offset);
15122 status = -EINVAL;
15123 goto out;
15124 }
15125 hrq->db_regaddr = bar_memmap_p + db_offset;
15126 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015127 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15128 "format:x%x\n", hrq->queue_id, pci_barset,
15129 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015130 } else {
15131 hrq->db_format = LPFC_DB_RING_FORMAT;
15132 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15133 }
James Smart4f774512009-05-22 14:52:35 -040015134 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015135 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015136 hrq->subtype = subtype;
15137 hrq->host_index = 0;
15138 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015139 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015140
15141 /* now create the data queue */
15142 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15143 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15144 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015145 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15146 phba->sli4_hba.pc_sli4_params.rqv);
15147 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15148 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015149 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015150 if (subtype == LPFC_NVMET)
15151 rq_create->u.request.context.buffer_size =
15152 LPFC_NVMET_DATA_BUF_SIZE;
15153 else
15154 rq_create->u.request.context.buffer_size =
15155 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015156 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15157 LPFC_RQE_SIZE_8);
15158 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15159 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015160 } else {
15161 switch (drq->entry_count) {
15162 default:
15163 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15164 "2536 Unsupported RQ count. (%d)\n",
15165 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015166 if (drq->entry_count < 512) {
15167 status = -EINVAL;
15168 goto out;
15169 }
James Smart5a6f1332011-03-11 16:05:35 -050015170 /* otherwise default to smallest count (drop through) */
15171 case 512:
15172 bf_set(lpfc_rq_context_rqe_count,
15173 &rq_create->u.request.context,
15174 LPFC_RQ_RING_SIZE_512);
15175 break;
15176 case 1024:
15177 bf_set(lpfc_rq_context_rqe_count,
15178 &rq_create->u.request.context,
15179 LPFC_RQ_RING_SIZE_1024);
15180 break;
15181 case 2048:
15182 bf_set(lpfc_rq_context_rqe_count,
15183 &rq_create->u.request.context,
15184 LPFC_RQ_RING_SIZE_2048);
15185 break;
15186 case 4096:
15187 bf_set(lpfc_rq_context_rqe_count,
15188 &rq_create->u.request.context,
15189 LPFC_RQ_RING_SIZE_4096);
15190 break;
15191 }
James Smart3c603be2017-05-15 15:20:44 -070015192 if (subtype == LPFC_NVMET)
15193 bf_set(lpfc_rq_context_buf_size,
15194 &rq_create->u.request.context,
15195 LPFC_NVMET_DATA_BUF_SIZE);
15196 else
15197 bf_set(lpfc_rq_context_buf_size,
15198 &rq_create->u.request.context,
15199 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015200 }
15201 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15202 cq->queue_id);
15203 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15204 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015205 list_for_each_entry(dmabuf, &drq->page_list, list) {
15206 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15207 putPaddrLow(dmabuf->phys);
15208 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15209 putPaddrHigh(dmabuf->phys);
15210 }
James Smart962bc512013-01-03 15:44:00 -050015211 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15212 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015213 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15214 /* The IOCTL status is embedded in the mailbox subheader. */
15215 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15216 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15217 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15218 if (shdr_status || shdr_add_status || rc) {
15219 status = -ENXIO;
15220 goto out;
15221 }
15222 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15223 if (drq->queue_id == 0xFFFF) {
15224 status = -ENXIO;
15225 goto out;
15226 }
15227 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015228 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015229 drq->subtype = subtype;
15230 drq->host_index = 0;
15231 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015232 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015233
15234 /* link the header and data RQs onto the parent cq child list */
15235 list_add_tail(&hrq->list, &cq->child_list);
15236 list_add_tail(&drq->list, &cq->child_list);
15237
15238out:
James Smart8fa38512009-07-19 10:01:03 -040015239 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015240 return status;
15241}
15242
15243/**
James Smart2d7dbc42017-02-12 13:52:35 -080015244 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15245 * @phba: HBA structure that indicates port to create a queue on.
15246 * @hrqp: The queue structure array to use to create the header receive queues.
15247 * @drqp: The queue structure array to use to create the data receive queues.
15248 * @cqp: The completion queue array to bind these receive queues to.
15249 *
15250 * This function creates a receive buffer queue pair , as detailed in @hrq and
15251 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15252 * to the HBA.
15253 *
15254 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15255 * struct is used to get the entry count that is necessary to determine the
15256 * number of pages to use for this queue. The @cq is used to indicate which
15257 * completion queue to bind received buffers that are posted to these queues to.
15258 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15259 * receive queue pair. This function is asynchronous and will wait for the
15260 * mailbox command to finish before continuing.
15261 *
15262 * On success this function will return a zero. If unable to allocate enough
15263 * memory this function will return -ENOMEM. If the queue create mailbox command
15264 * fails this function will return -ENXIO.
15265 **/
15266int
15267lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15268 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15269 uint32_t subtype)
15270{
15271 struct lpfc_queue *hrq, *drq, *cq;
15272 struct lpfc_mbx_rq_create_v2 *rq_create;
15273 struct lpfc_dmabuf *dmabuf;
15274 LPFC_MBOXQ_t *mbox;
15275 int rc, length, alloclen, status = 0;
15276 int cnt, idx, numrq, page_idx = 0;
15277 uint32_t shdr_status, shdr_add_status;
15278 union lpfc_sli4_cfg_shdr *shdr;
15279 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15280
15281 numrq = phba->cfg_nvmet_mrq;
15282 /* sanity check on array memory */
15283 if (!hrqp || !drqp || !cqp || !numrq)
15284 return -ENODEV;
15285 if (!phba->sli4_hba.pc_sli4_params.supported)
15286 hw_page_size = SLI4_PAGE_SIZE;
15287
15288 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15289 if (!mbox)
15290 return -ENOMEM;
15291
15292 length = sizeof(struct lpfc_mbx_rq_create_v2);
15293 length += ((2 * numrq * hrqp[0]->page_count) *
15294 sizeof(struct dma_address));
15295
15296 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15297 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15298 LPFC_SLI4_MBX_NEMBED);
15299 if (alloclen < length) {
15300 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15301 "3099 Allocated DMA memory size (%d) is "
15302 "less than the requested DMA memory size "
15303 "(%d)\n", alloclen, length);
15304 status = -ENOMEM;
15305 goto out;
15306 }
15307
15308
15309
15310 rq_create = mbox->sge_array->addr[0];
15311 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15312
15313 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15314 cnt = 0;
15315
15316 for (idx = 0; idx < numrq; idx++) {
15317 hrq = hrqp[idx];
15318 drq = drqp[idx];
15319 cq = cqp[idx];
15320
James Smart2d7dbc42017-02-12 13:52:35 -080015321 /* sanity check on queue memory */
15322 if (!hrq || !drq || !cq) {
15323 status = -ENODEV;
15324 goto out;
15325 }
15326
James Smart7aabe842017-03-04 09:30:22 -080015327 if (hrq->entry_count != drq->entry_count) {
15328 status = -EINVAL;
15329 goto out;
15330 }
15331
James Smart2d7dbc42017-02-12 13:52:35 -080015332 if (idx == 0) {
15333 bf_set(lpfc_mbx_rq_create_num_pages,
15334 &rq_create->u.request,
15335 hrq->page_count);
15336 bf_set(lpfc_mbx_rq_create_rq_cnt,
15337 &rq_create->u.request, (numrq * 2));
15338 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15339 1);
15340 bf_set(lpfc_rq_context_base_cq,
15341 &rq_create->u.request.context,
15342 cq->queue_id);
15343 bf_set(lpfc_rq_context_data_size,
15344 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015345 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015346 bf_set(lpfc_rq_context_hdr_size,
15347 &rq_create->u.request.context,
15348 LPFC_HDR_BUF_SIZE);
15349 bf_set(lpfc_rq_context_rqe_count_1,
15350 &rq_create->u.request.context,
15351 hrq->entry_count);
15352 bf_set(lpfc_rq_context_rqe_size,
15353 &rq_create->u.request.context,
15354 LPFC_RQE_SIZE_8);
15355 bf_set(lpfc_rq_context_page_size,
15356 &rq_create->u.request.context,
15357 (PAGE_SIZE/SLI4_PAGE_SIZE));
15358 }
15359 rc = 0;
15360 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15361 memset(dmabuf->virt, 0, hw_page_size);
15362 cnt = page_idx + dmabuf->buffer_tag;
15363 rq_create->u.request.page[cnt].addr_lo =
15364 putPaddrLow(dmabuf->phys);
15365 rq_create->u.request.page[cnt].addr_hi =
15366 putPaddrHigh(dmabuf->phys);
15367 rc++;
15368 }
15369 page_idx += rc;
15370
15371 rc = 0;
15372 list_for_each_entry(dmabuf, &drq->page_list, list) {
15373 memset(dmabuf->virt, 0, hw_page_size);
15374 cnt = page_idx + dmabuf->buffer_tag;
15375 rq_create->u.request.page[cnt].addr_lo =
15376 putPaddrLow(dmabuf->phys);
15377 rq_create->u.request.page[cnt].addr_hi =
15378 putPaddrHigh(dmabuf->phys);
15379 rc++;
15380 }
15381 page_idx += rc;
15382
15383 hrq->db_format = LPFC_DB_RING_FORMAT;
15384 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15385 hrq->type = LPFC_HRQ;
15386 hrq->assoc_qid = cq->queue_id;
15387 hrq->subtype = subtype;
15388 hrq->host_index = 0;
15389 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015390 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015391
15392 drq->db_format = LPFC_DB_RING_FORMAT;
15393 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15394 drq->type = LPFC_DRQ;
15395 drq->assoc_qid = cq->queue_id;
15396 drq->subtype = subtype;
15397 drq->host_index = 0;
15398 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015399 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015400
15401 list_add_tail(&hrq->list, &cq->child_list);
15402 list_add_tail(&drq->list, &cq->child_list);
15403 }
15404
15405 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15406 /* The IOCTL status is embedded in the mailbox subheader. */
15407 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15408 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15409 if (shdr_status || shdr_add_status || rc) {
15410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15411 "3120 RQ_CREATE mailbox failed with "
15412 "status x%x add_status x%x, mbx status x%x\n",
15413 shdr_status, shdr_add_status, rc);
15414 status = -ENXIO;
15415 goto out;
15416 }
15417 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15418 if (rc == 0xFFFF) {
15419 status = -ENXIO;
15420 goto out;
15421 }
15422
15423 /* Initialize all RQs with associated queue id */
15424 for (idx = 0; idx < numrq; idx++) {
15425 hrq = hrqp[idx];
15426 hrq->queue_id = rc + (2 * idx);
15427 drq = drqp[idx];
15428 drq->queue_id = rc + (2 * idx) + 1;
15429 }
15430
15431out:
15432 lpfc_sli4_mbox_cmd_free(phba, mbox);
15433 return status;
15434}
15435
15436/**
James Smart4f774512009-05-22 14:52:35 -040015437 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15438 * @eq: The queue structure associated with the queue to destroy.
15439 *
15440 * This function destroys a queue, as detailed in @eq by sending an mailbox
15441 * command, specific to the type of queue, to the HBA.
15442 *
15443 * The @eq struct is used to get the queue ID of the queue to destroy.
15444 *
15445 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015446 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015447 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015448int
James Smart4f774512009-05-22 14:52:35 -040015449lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15450{
15451 LPFC_MBOXQ_t *mbox;
15452 int rc, length, status = 0;
15453 uint32_t shdr_status, shdr_add_status;
15454 union lpfc_sli4_cfg_shdr *shdr;
15455
James Smart2e90f4b2011-12-13 13:22:37 -050015456 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015457 if (!eq)
15458 return -ENODEV;
15459 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15460 if (!mbox)
15461 return -ENOMEM;
15462 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15463 sizeof(struct lpfc_sli4_cfg_mhdr));
15464 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15465 LPFC_MBOX_OPCODE_EQ_DESTROY,
15466 length, LPFC_SLI4_MBX_EMBED);
15467 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15468 eq->queue_id);
15469 mbox->vport = eq->phba->pport;
15470 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15471
15472 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15473 /* The IOCTL status is embedded in the mailbox subheader. */
15474 shdr = (union lpfc_sli4_cfg_shdr *)
15475 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15476 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15477 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15478 if (shdr_status || shdr_add_status || rc) {
15479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15480 "2505 EQ_DESTROY mailbox failed with "
15481 "status x%x add_status x%x, mbx status x%x\n",
15482 shdr_status, shdr_add_status, rc);
15483 status = -ENXIO;
15484 }
15485
15486 /* Remove eq from any list */
15487 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015488 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015489 return status;
15490}
15491
15492/**
15493 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15494 * @cq: The queue structure associated with the queue to destroy.
15495 *
15496 * This function destroys a queue, as detailed in @cq by sending an mailbox
15497 * command, specific to the type of queue, to the HBA.
15498 *
15499 * The @cq struct is used to get the queue ID of the queue to destroy.
15500 *
15501 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015502 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015503 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015504int
James Smart4f774512009-05-22 14:52:35 -040015505lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15506{
15507 LPFC_MBOXQ_t *mbox;
15508 int rc, length, status = 0;
15509 uint32_t shdr_status, shdr_add_status;
15510 union lpfc_sli4_cfg_shdr *shdr;
15511
James Smart2e90f4b2011-12-13 13:22:37 -050015512 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015513 if (!cq)
15514 return -ENODEV;
15515 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15516 if (!mbox)
15517 return -ENOMEM;
15518 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15519 sizeof(struct lpfc_sli4_cfg_mhdr));
15520 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15521 LPFC_MBOX_OPCODE_CQ_DESTROY,
15522 length, LPFC_SLI4_MBX_EMBED);
15523 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15524 cq->queue_id);
15525 mbox->vport = cq->phba->pport;
15526 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15527 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15528 /* The IOCTL status is embedded in the mailbox subheader. */
15529 shdr = (union lpfc_sli4_cfg_shdr *)
15530 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15531 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15532 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15533 if (shdr_status || shdr_add_status || rc) {
15534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15535 "2506 CQ_DESTROY mailbox failed with "
15536 "status x%x add_status x%x, mbx status x%x\n",
15537 shdr_status, shdr_add_status, rc);
15538 status = -ENXIO;
15539 }
15540 /* Remove cq from any list */
15541 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015542 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015543 return status;
15544}
15545
15546/**
James Smart04c68492009-05-22 14:52:52 -040015547 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15548 * @qm: The queue structure associated with the queue to destroy.
15549 *
15550 * This function destroys a queue, as detailed in @mq by sending an mailbox
15551 * command, specific to the type of queue, to the HBA.
15552 *
15553 * The @mq struct is used to get the queue ID of the queue to destroy.
15554 *
15555 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015556 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015557 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015558int
James Smart04c68492009-05-22 14:52:52 -040015559lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15560{
15561 LPFC_MBOXQ_t *mbox;
15562 int rc, length, status = 0;
15563 uint32_t shdr_status, shdr_add_status;
15564 union lpfc_sli4_cfg_shdr *shdr;
15565
James Smart2e90f4b2011-12-13 13:22:37 -050015566 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015567 if (!mq)
15568 return -ENODEV;
15569 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15570 if (!mbox)
15571 return -ENOMEM;
15572 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15573 sizeof(struct lpfc_sli4_cfg_mhdr));
15574 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15575 LPFC_MBOX_OPCODE_MQ_DESTROY,
15576 length, LPFC_SLI4_MBX_EMBED);
15577 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15578 mq->queue_id);
15579 mbox->vport = mq->phba->pport;
15580 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15581 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15582 /* The IOCTL status is embedded in the mailbox subheader. */
15583 shdr = (union lpfc_sli4_cfg_shdr *)
15584 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15585 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15586 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15587 if (shdr_status || shdr_add_status || rc) {
15588 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15589 "2507 MQ_DESTROY mailbox failed with "
15590 "status x%x add_status x%x, mbx status x%x\n",
15591 shdr_status, shdr_add_status, rc);
15592 status = -ENXIO;
15593 }
15594 /* Remove mq from any list */
15595 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015596 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015597 return status;
15598}
15599
15600/**
James Smart4f774512009-05-22 14:52:35 -040015601 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15602 * @wq: The queue structure associated with the queue to destroy.
15603 *
15604 * This function destroys a queue, as detailed in @wq by sending an mailbox
15605 * command, specific to the type of queue, to the HBA.
15606 *
15607 * The @wq struct is used to get the queue ID of the queue to destroy.
15608 *
15609 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015610 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015611 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015612int
James Smart4f774512009-05-22 14:52:35 -040015613lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15614{
15615 LPFC_MBOXQ_t *mbox;
15616 int rc, length, status = 0;
15617 uint32_t shdr_status, shdr_add_status;
15618 union lpfc_sli4_cfg_shdr *shdr;
15619
James Smart2e90f4b2011-12-13 13:22:37 -050015620 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015621 if (!wq)
15622 return -ENODEV;
15623 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15624 if (!mbox)
15625 return -ENOMEM;
15626 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15627 sizeof(struct lpfc_sli4_cfg_mhdr));
15628 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15629 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15630 length, LPFC_SLI4_MBX_EMBED);
15631 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15632 wq->queue_id);
15633 mbox->vport = wq->phba->pport;
15634 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15635 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15636 shdr = (union lpfc_sli4_cfg_shdr *)
15637 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15638 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15639 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15640 if (shdr_status || shdr_add_status || rc) {
15641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15642 "2508 WQ_DESTROY mailbox failed with "
15643 "status x%x add_status x%x, mbx status x%x\n",
15644 shdr_status, shdr_add_status, rc);
15645 status = -ENXIO;
15646 }
15647 /* Remove wq from any list */
15648 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070015649 kfree(wq->pring);
15650 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040015651 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015652 return status;
15653}
15654
15655/**
15656 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15657 * @rq: The queue structure associated with the queue to destroy.
15658 *
15659 * This function destroys a queue, as detailed in @rq by sending an mailbox
15660 * command, specific to the type of queue, to the HBA.
15661 *
15662 * The @rq struct is used to get the queue ID of the queue to destroy.
15663 *
15664 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015665 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015666 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015667int
James Smart4f774512009-05-22 14:52:35 -040015668lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15669 struct lpfc_queue *drq)
15670{
15671 LPFC_MBOXQ_t *mbox;
15672 int rc, length, status = 0;
15673 uint32_t shdr_status, shdr_add_status;
15674 union lpfc_sli4_cfg_shdr *shdr;
15675
James Smart2e90f4b2011-12-13 13:22:37 -050015676 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015677 if (!hrq || !drq)
15678 return -ENODEV;
15679 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15680 if (!mbox)
15681 return -ENOMEM;
15682 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050015683 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040015684 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15685 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15686 length, LPFC_SLI4_MBX_EMBED);
15687 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15688 hrq->queue_id);
15689 mbox->vport = hrq->phba->pport;
15690 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15691 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15692 /* The IOCTL status is embedded in the mailbox subheader. */
15693 shdr = (union lpfc_sli4_cfg_shdr *)
15694 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15695 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15696 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15697 if (shdr_status || shdr_add_status || rc) {
15698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15699 "2509 RQ_DESTROY mailbox failed with "
15700 "status x%x add_status x%x, mbx status x%x\n",
15701 shdr_status, shdr_add_status, rc);
15702 if (rc != MBX_TIMEOUT)
15703 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15704 return -ENXIO;
15705 }
15706 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15707 drq->queue_id);
15708 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15709 shdr = (union lpfc_sli4_cfg_shdr *)
15710 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15711 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15712 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15713 if (shdr_status || shdr_add_status || rc) {
15714 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15715 "2510 RQ_DESTROY mailbox failed with "
15716 "status x%x add_status x%x, mbx status x%x\n",
15717 shdr_status, shdr_add_status, rc);
15718 status = -ENXIO;
15719 }
15720 list_del_init(&hrq->list);
15721 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040015722 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015723 return status;
15724}
15725
15726/**
15727 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15728 * @phba: The virtual port for which this call being executed.
15729 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15730 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15731 * @xritag: the xritag that ties this io to the SGL pages.
15732 *
15733 * This routine will post the sgl pages for the IO that has the xritag
15734 * that is in the iocbq structure. The xritag is assigned during iocbq
15735 * creation and persists for as long as the driver is loaded.
15736 * if the caller has fewer than 256 scatter gather segments to map then
15737 * pdma_phys_addr1 should be 0.
15738 * If the caller needs to map more than 256 scatter gather segment then
15739 * pdma_phys_addr1 should be a valid physical address.
15740 * physical address for SGLs must be 64 byte aligned.
15741 * If you are going to map 2 SGL's then the first one must have 256 entries
15742 * the second sgl can have between 1 and 256 entries.
15743 *
15744 * Return codes:
15745 * 0 - Success
15746 * -ENXIO, -ENOMEM - Failure
15747 **/
15748int
15749lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15750 dma_addr_t pdma_phys_addr0,
15751 dma_addr_t pdma_phys_addr1,
15752 uint16_t xritag)
15753{
15754 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15755 LPFC_MBOXQ_t *mbox;
15756 int rc;
15757 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040015758 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040015759 union lpfc_sli4_cfg_shdr *shdr;
15760
15761 if (xritag == NO_XRI) {
15762 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15763 "0364 Invalid param:\n");
15764 return -EINVAL;
15765 }
15766
15767 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15768 if (!mbox)
15769 return -ENOMEM;
15770
15771 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15772 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15773 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050015774 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040015775
15776 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15777 &mbox->u.mqe.un.post_sgl_pages;
15778 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15779 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15780
15781 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15782 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15783 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15784 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15785
15786 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15787 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15788 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15789 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15790 if (!phba->sli4_hba.intr_enable)
15791 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040015792 else {
James Smarta183a152011-10-10 21:32:43 -040015793 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040015794 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15795 }
James Smart4f774512009-05-22 14:52:35 -040015796 /* The IOCTL status is embedded in the mailbox subheader. */
15797 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15798 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15799 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15800 if (rc != MBX_TIMEOUT)
15801 mempool_free(mbox, phba->mbox_mem_pool);
15802 if (shdr_status || shdr_add_status || rc) {
15803 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15804 "2511 POST_SGL mailbox failed with "
15805 "status x%x add_status x%x, mbx status x%x\n",
15806 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040015807 }
15808 return 0;
15809}
James Smart4f774512009-05-22 14:52:35 -040015810
15811/**
James Smart88a2cfb2011-07-22 18:36:33 -040015812 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040015813 * @phba: pointer to lpfc hba data structure.
15814 *
15815 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040015816 * HBA consistent with the SLI-4 interface spec. This routine
15817 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15818 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040015819 *
James Smart88a2cfb2011-07-22 18:36:33 -040015820 * Returns
15821 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15822 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15823 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015824static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015825lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15826{
15827 unsigned long xri;
15828
15829 /*
15830 * Fetch the next logical xri. Because this index is logical,
15831 * the driver starts at 0 each time.
15832 */
15833 spin_lock_irq(&phba->hbalock);
15834 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15835 phba->sli4_hba.max_cfg_param.max_xri, 0);
15836 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15837 spin_unlock_irq(&phba->hbalock);
15838 return NO_XRI;
15839 } else {
15840 set_bit(xri, phba->sli4_hba.xri_bmask);
15841 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015842 }
James Smart6d368e52011-05-24 11:44:12 -040015843 spin_unlock_irq(&phba->hbalock);
15844 return xri;
15845}
15846
15847/**
15848 * lpfc_sli4_free_xri - Release an xri for reuse.
15849 * @phba: pointer to lpfc hba data structure.
15850 *
15851 * This routine is invoked to release an xri to the pool of
15852 * available rpis maintained by the driver.
15853 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015854static void
James Smart6d368e52011-05-24 11:44:12 -040015855__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15856{
15857 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040015858 phba->sli4_hba.max_cfg_param.xri_used--;
15859 }
15860}
15861
15862/**
15863 * lpfc_sli4_free_xri - Release an xri for reuse.
15864 * @phba: pointer to lpfc hba data structure.
15865 *
15866 * This routine is invoked to release an xri to the pool of
15867 * available rpis maintained by the driver.
15868 **/
15869void
15870lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15871{
15872 spin_lock_irq(&phba->hbalock);
15873 __lpfc_sli4_free_xri(phba, xri);
15874 spin_unlock_irq(&phba->hbalock);
15875}
15876
15877/**
James Smart4f774512009-05-22 14:52:35 -040015878 * lpfc_sli4_next_xritag - Get an xritag for the io
15879 * @phba: Pointer to HBA context object.
15880 *
15881 * This function gets an xritag for the iocb. If there is no unused xritag
15882 * it will return 0xffff.
15883 * The function returns the allocated xritag if successful, else returns zero.
15884 * Zero is not a valid xritag.
15885 * The caller is not required to hold any lock.
15886 **/
15887uint16_t
15888lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15889{
James Smart6d368e52011-05-24 11:44:12 -040015890 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040015891
James Smart6d368e52011-05-24 11:44:12 -040015892 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040015893 if (xri_index == NO_XRI)
15894 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15895 "2004 Failed to allocate XRI.last XRITAG is %d"
15896 " Max XRI is %d, Used XRI is %d\n",
15897 xri_index,
15898 phba->sli4_hba.max_cfg_param.max_xri,
15899 phba->sli4_hba.max_cfg_param.xri_used);
15900 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040015901}
15902
15903/**
James Smart895427b2017-02-12 13:52:30 -080015904 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040015905 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040015906 * @post_sgl_list: pointer to els sgl entry list.
15907 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040015908 *
15909 * This routine is invoked to post a block of driver's sgl pages to the
15910 * HBA using non-embedded mailbox command. No Lock is held. This routine
15911 * is only called when the driver is loading and after all IO has been
15912 * stopped.
15913 **/
James Smart8a9d2e82012-05-09 21:16:12 -040015914static int
James Smart895427b2017-02-12 13:52:30 -080015915lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040015916 struct list_head *post_sgl_list,
15917 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040015918{
James Smart8a9d2e82012-05-09 21:16:12 -040015919 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040015920 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15921 struct sgl_page_pairs *sgl_pg_pairs;
15922 void *viraddr;
15923 LPFC_MBOXQ_t *mbox;
15924 uint32_t reqlen, alloclen, pg_pairs;
15925 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040015926 uint16_t xritag_start = 0;
15927 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040015928 uint32_t shdr_status, shdr_add_status;
15929 union lpfc_sli4_cfg_shdr *shdr;
15930
James Smart895427b2017-02-12 13:52:30 -080015931 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015932 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015933 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080015934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040015935 "2559 Block sgl registration required DMA "
15936 "size (%d) great than a page\n", reqlen);
15937 return -ENOMEM;
15938 }
James Smart895427b2017-02-12 13:52:30 -080015939
James Smart4f774512009-05-22 14:52:35 -040015940 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040015941 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040015942 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040015943
15944 /* Allocate DMA memory and set up the non-embedded mailbox command */
15945 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15946 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15947 LPFC_SLI4_MBX_NEMBED);
15948
15949 if (alloclen < reqlen) {
15950 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15951 "0285 Allocated DMA memory size (%d) is "
15952 "less than the requested DMA memory "
15953 "size (%d)\n", alloclen, reqlen);
15954 lpfc_sli4_mbox_cmd_free(phba, mbox);
15955 return -ENOMEM;
15956 }
James Smart4f774512009-05-22 14:52:35 -040015957 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040015958 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040015959 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15960 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15961
James Smart8a9d2e82012-05-09 21:16:12 -040015962 pg_pairs = 0;
15963 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040015964 /* Set up the sge entry */
15965 sgl_pg_pairs->sgl_pg0_addr_lo =
15966 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15967 sgl_pg_pairs->sgl_pg0_addr_hi =
15968 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15969 sgl_pg_pairs->sgl_pg1_addr_lo =
15970 cpu_to_le32(putPaddrLow(0));
15971 sgl_pg_pairs->sgl_pg1_addr_hi =
15972 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040015973
James Smart4f774512009-05-22 14:52:35 -040015974 /* Keep the first xritag on the list */
15975 if (pg_pairs == 0)
15976 xritag_start = sglq_entry->sli4_xritag;
15977 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040015978 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040015979 }
James Smart6d368e52011-05-24 11:44:12 -040015980
15981 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040015982 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080015983 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040015984 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080015985
James Smart4f774512009-05-22 14:52:35 -040015986 if (!phba->sli4_hba.intr_enable)
15987 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15988 else {
James Smarta183a152011-10-10 21:32:43 -040015989 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040015990 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15991 }
15992 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15993 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15994 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15995 if (rc != MBX_TIMEOUT)
15996 lpfc_sli4_mbox_cmd_free(phba, mbox);
15997 if (shdr_status || shdr_add_status || rc) {
15998 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15999 "2513 POST_SGL_BLOCK mailbox command failed "
16000 "status x%x add_status x%x mbx status x%x\n",
16001 shdr_status, shdr_add_status, rc);
16002 rc = -ENXIO;
16003 }
16004 return rc;
16005}
16006
16007/**
16008 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16009 * @phba: pointer to lpfc hba data structure.
16010 * @sblist: pointer to scsi buffer list.
16011 * @count: number of scsi buffers on the list.
16012 *
16013 * This routine is invoked to post a block of @count scsi sgl pages from a
16014 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16015 * No Lock is held.
16016 *
16017 **/
16018int
James Smart8a9d2e82012-05-09 21:16:12 -040016019lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16020 struct list_head *sblist,
16021 int count)
James Smart4f774512009-05-22 14:52:35 -040016022{
16023 struct lpfc_scsi_buf *psb;
16024 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16025 struct sgl_page_pairs *sgl_pg_pairs;
16026 void *viraddr;
16027 LPFC_MBOXQ_t *mbox;
16028 uint32_t reqlen, alloclen, pg_pairs;
16029 uint32_t mbox_tmo;
16030 uint16_t xritag_start = 0;
16031 int rc = 0;
16032 uint32_t shdr_status, shdr_add_status;
16033 dma_addr_t pdma_phys_bpl1;
16034 union lpfc_sli4_cfg_shdr *shdr;
16035
16036 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016037 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016038 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016039 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016040 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16041 "0217 Block sgl registration required DMA "
16042 "size (%d) great than a page\n", reqlen);
16043 return -ENOMEM;
16044 }
16045 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16046 if (!mbox) {
16047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16048 "0283 Failed to allocate mbox cmd memory\n");
16049 return -ENOMEM;
16050 }
16051
16052 /* Allocate DMA memory and set up the non-embedded mailbox command */
16053 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16054 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16055 LPFC_SLI4_MBX_NEMBED);
16056
16057 if (alloclen < reqlen) {
16058 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16059 "2561 Allocated DMA memory size (%d) is "
16060 "less than the requested DMA memory "
16061 "size (%d)\n", alloclen, reqlen);
16062 lpfc_sli4_mbox_cmd_free(phba, mbox);
16063 return -ENOMEM;
16064 }
James Smart6d368e52011-05-24 11:44:12 -040016065
James Smart4f774512009-05-22 14:52:35 -040016066 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016067 viraddr = mbox->sge_array->addr[0];
16068
16069 /* Set up the SGL pages in the non-embedded DMA pages */
16070 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16071 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16072
16073 pg_pairs = 0;
16074 list_for_each_entry(psb, sblist, list) {
16075 /* Set up the sge entry */
16076 sgl_pg_pairs->sgl_pg0_addr_lo =
16077 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16078 sgl_pg_pairs->sgl_pg0_addr_hi =
16079 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16080 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16081 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16082 else
16083 pdma_phys_bpl1 = 0;
16084 sgl_pg_pairs->sgl_pg1_addr_lo =
16085 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16086 sgl_pg_pairs->sgl_pg1_addr_hi =
16087 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16088 /* Keep the first xritag on the list */
16089 if (pg_pairs == 0)
16090 xritag_start = psb->cur_iocbq.sli4_xritag;
16091 sgl_pg_pairs++;
16092 pg_pairs++;
16093 }
16094 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16095 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16096 /* Perform endian conversion if necessary */
16097 sgl->word0 = cpu_to_le32(sgl->word0);
16098
16099 if (!phba->sli4_hba.intr_enable)
16100 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16101 else {
James Smarta183a152011-10-10 21:32:43 -040016102 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016103 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16104 }
16105 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16106 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16107 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16108 if (rc != MBX_TIMEOUT)
16109 lpfc_sli4_mbox_cmd_free(phba, mbox);
16110 if (shdr_status || shdr_add_status || rc) {
16111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16112 "2564 POST_SGL_BLOCK mailbox command failed "
16113 "status x%x add_status x%x mbx status x%x\n",
16114 shdr_status, shdr_add_status, rc);
16115 rc = -ENXIO;
16116 }
16117 return rc;
16118}
16119
James Smart2ea259e2017-02-12 13:52:27 -080016120static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
16121static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
16122
James Smart4f774512009-05-22 14:52:35 -040016123/**
16124 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16125 * @phba: pointer to lpfc_hba struct that the frame was received on
16126 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16127 *
16128 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16129 * valid type of frame that the LPFC driver will handle. This function will
16130 * return a zero if the frame is a valid frame or a non zero value when the
16131 * frame does not pass the check.
16132 **/
16133static int
16134lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16135{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016136 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016137 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016138 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016139
16140 switch (fc_hdr->fh_r_ctl) {
16141 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16142 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16143 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16144 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16145 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16146 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16147 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16148 case FC_RCTL_DD_CMD_STATUS: /* command status */
16149 case FC_RCTL_ELS_REQ: /* extended link services request */
16150 case FC_RCTL_ELS_REP: /* extended link services reply */
16151 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16152 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16153 case FC_RCTL_BA_NOP: /* basic link service NOP */
16154 case FC_RCTL_BA_ABTS: /* basic link service abort */
16155 case FC_RCTL_BA_RMC: /* remove connection */
16156 case FC_RCTL_BA_ACC: /* basic accept */
16157 case FC_RCTL_BA_RJT: /* basic reject */
16158 case FC_RCTL_BA_PRMT:
16159 case FC_RCTL_ACK_1: /* acknowledge_1 */
16160 case FC_RCTL_ACK_0: /* acknowledge_0 */
16161 case FC_RCTL_P_RJT: /* port reject */
16162 case FC_RCTL_F_RJT: /* fabric reject */
16163 case FC_RCTL_P_BSY: /* port busy */
16164 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16165 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16166 case FC_RCTL_LCR: /* link credit reset */
16167 case FC_RCTL_END: /* end */
16168 break;
16169 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16170 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16171 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16172 return lpfc_fc_frame_check(phba, fc_hdr);
16173 default:
16174 goto drop;
16175 }
16176 switch (fc_hdr->fh_type) {
16177 case FC_TYPE_BLS:
16178 case FC_TYPE_ELS:
16179 case FC_TYPE_FCP:
16180 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016181 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040016182 break;
16183 case FC_TYPE_IP:
16184 case FC_TYPE_ILS:
16185 default:
16186 goto drop;
16187 }
James Smart546fc852011-03-11 16:06:29 -050016188
James Smart4f774512009-05-22 14:52:35 -040016189 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040016190 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
16191 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart2ea259e2017-02-12 13:52:27 -080016192 lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
16193 lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050016194 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16195 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040016196 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16197 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016198 return 0;
16199drop:
16200 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16201 "2539 Dropped frame rctl:%s type:%s\n",
James Smart2ea259e2017-02-12 13:52:27 -080016202 lpfc_rctl_names[fc_hdr->fh_r_ctl],
16203 lpfc_type_names[fc_hdr->fh_type]);
James Smart4f774512009-05-22 14:52:35 -040016204 return 1;
16205}
16206
16207/**
16208 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16209 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16210 *
16211 * This function processes the FC header to retrieve the VFI from the VF
16212 * header, if one exists. This function will return the VFI if one exists
16213 * or 0 if no VSAN Header exists.
16214 **/
16215static uint32_t
16216lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16217{
16218 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16219
16220 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16221 return 0;
16222 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16223}
16224
16225/**
16226 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16227 * @phba: Pointer to the HBA structure to search for the vport on
16228 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16229 * @fcfi: The FC Fabric ID that the frame came from
16230 *
16231 * This function searches the @phba for a vport that matches the content of the
16232 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16233 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16234 * returns the matching vport pointer or NULL if unable to match frame to a
16235 * vport.
16236 **/
16237static struct lpfc_vport *
16238lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016239 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016240{
16241 struct lpfc_vport **vports;
16242 struct lpfc_vport *vport = NULL;
16243 int i;
James Smart939723a2012-05-09 21:19:03 -040016244
James Smartbf086112011-08-21 21:48:13 -040016245 if (did == Fabric_DID)
16246 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016247 if ((phba->pport->fc_flag & FC_PT2PT) &&
16248 !(phba->link_state == LPFC_HBA_READY))
16249 return phba->pport;
16250
James Smart4f774512009-05-22 14:52:35 -040016251 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016252 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016253 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16254 if (phba->fcf.fcfi == fcfi &&
16255 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16256 vports[i]->fc_myDID == did) {
16257 vport = vports[i];
16258 break;
16259 }
16260 }
James Smart895427b2017-02-12 13:52:30 -080016261 }
James Smart4f774512009-05-22 14:52:35 -040016262 lpfc_destroy_vport_work_array(phba, vports);
16263 return vport;
16264}
16265
16266/**
James Smart45ed1192009-10-02 15:17:02 -040016267 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16268 * @vport: The vport to work on.
16269 *
16270 * This function updates the receive sequence time stamp for this vport. The
16271 * receive sequence time stamp indicates the time that the last frame of the
16272 * the sequence that has been idle for the longest amount of time was received.
16273 * the driver uses this time stamp to indicate if any received sequences have
16274 * timed out.
16275 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016276static void
James Smart45ed1192009-10-02 15:17:02 -040016277lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16278{
16279 struct lpfc_dmabuf *h_buf;
16280 struct hbq_dmabuf *dmabuf = NULL;
16281
16282 /* get the oldest sequence on the rcv list */
16283 h_buf = list_get_first(&vport->rcv_buffer_list,
16284 struct lpfc_dmabuf, list);
16285 if (!h_buf)
16286 return;
16287 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16288 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16289}
16290
16291/**
16292 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16293 * @vport: The vport that the received sequences were sent to.
16294 *
16295 * This function cleans up all outstanding received sequences. This is called
16296 * by the driver when a link event or user action invalidates all the received
16297 * sequences.
16298 **/
16299void
16300lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16301{
16302 struct lpfc_dmabuf *h_buf, *hnext;
16303 struct lpfc_dmabuf *d_buf, *dnext;
16304 struct hbq_dmabuf *dmabuf = NULL;
16305
16306 /* start with the oldest sequence on the rcv list */
16307 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16308 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16309 list_del_init(&dmabuf->hbuf.list);
16310 list_for_each_entry_safe(d_buf, dnext,
16311 &dmabuf->dbuf.list, list) {
16312 list_del_init(&d_buf->list);
16313 lpfc_in_buf_free(vport->phba, d_buf);
16314 }
16315 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16316 }
16317}
16318
16319/**
16320 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16321 * @vport: The vport that the received sequences were sent to.
16322 *
16323 * This function determines whether any received sequences have timed out by
16324 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16325 * indicates that there is at least one timed out sequence this routine will
16326 * go through the received sequences one at a time from most inactive to most
16327 * active to determine which ones need to be cleaned up. Once it has determined
16328 * that a sequence needs to be cleaned up it will simply free up the resources
16329 * without sending an abort.
16330 **/
16331void
16332lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16333{
16334 struct lpfc_dmabuf *h_buf, *hnext;
16335 struct lpfc_dmabuf *d_buf, *dnext;
16336 struct hbq_dmabuf *dmabuf = NULL;
16337 unsigned long timeout;
16338 int abort_count = 0;
16339
16340 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16341 vport->rcv_buffer_time_stamp);
16342 if (list_empty(&vport->rcv_buffer_list) ||
16343 time_before(jiffies, timeout))
16344 return;
16345 /* start with the oldest sequence on the rcv list */
16346 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16347 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16348 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16349 dmabuf->time_stamp);
16350 if (time_before(jiffies, timeout))
16351 break;
16352 abort_count++;
16353 list_del_init(&dmabuf->hbuf.list);
16354 list_for_each_entry_safe(d_buf, dnext,
16355 &dmabuf->dbuf.list, list) {
16356 list_del_init(&d_buf->list);
16357 lpfc_in_buf_free(vport->phba, d_buf);
16358 }
16359 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16360 }
16361 if (abort_count)
16362 lpfc_update_rcv_time_stamp(vport);
16363}
16364
16365/**
James Smart4f774512009-05-22 14:52:35 -040016366 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16367 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16368 *
16369 * This function searches through the existing incomplete sequences that have
16370 * been sent to this @vport. If the frame matches one of the incomplete
16371 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16372 * make up that sequence. If no sequence is found that matches this frame then
16373 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16374 * This function returns a pointer to the first dmabuf in the sequence list that
16375 * the frame was linked to.
16376 **/
16377static struct hbq_dmabuf *
16378lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16379{
16380 struct fc_frame_header *new_hdr;
16381 struct fc_frame_header *temp_hdr;
16382 struct lpfc_dmabuf *d_buf;
16383 struct lpfc_dmabuf *h_buf;
16384 struct hbq_dmabuf *seq_dmabuf = NULL;
16385 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016386 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016387
James Smart4d9ab992009-10-02 15:16:39 -040016388 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016389 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016390 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016391
James Smart4f774512009-05-22 14:52:35 -040016392 /* Use the hdr_buf to find the sequence that this frame belongs to */
16393 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16394 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16395 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16396 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16397 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16398 continue;
16399 /* found a pending sequence that matches this frame */
16400 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16401 break;
16402 }
16403 if (!seq_dmabuf) {
16404 /*
16405 * This indicates first frame received for this sequence.
16406 * Queue the buffer on the vport's rcv_buffer_list.
16407 */
16408 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016409 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016410 return dmabuf;
16411 }
16412 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016413 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16414 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016415 list_del_init(&seq_dmabuf->hbuf.list);
16416 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16417 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016418 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016419 return dmabuf;
16420 }
James Smart45ed1192009-10-02 15:17:02 -040016421 /* move this sequence to the tail to indicate a young sequence */
16422 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16423 seq_dmabuf->time_stamp = jiffies;
16424 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016425 if (list_empty(&seq_dmabuf->dbuf.list)) {
16426 temp_hdr = dmabuf->hbuf.virt;
16427 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16428 return seq_dmabuf;
16429 }
James Smart4f774512009-05-22 14:52:35 -040016430 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016431 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16432 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016433 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16434 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16435 /*
16436 * If the frame's sequence count is greater than the frame on
16437 * the list then insert the frame right after this frame
16438 */
James Smarteeead812009-12-21 17:01:23 -050016439 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16440 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016441 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016442 found = 1;
16443 break;
James Smart4f774512009-05-22 14:52:35 -040016444 }
James Smart4360ca92015-12-16 18:12:04 -050016445
16446 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16447 break;
16448 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016449 }
James Smart4360ca92015-12-16 18:12:04 -050016450
16451 if (found)
16452 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016453 return NULL;
16454}
16455
16456/**
James Smart6669f9b2009-10-02 15:16:45 -040016457 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16458 * @vport: pointer to a vitural port
16459 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16460 *
16461 * This function tries to abort from the partially assembed sequence, described
16462 * by the information from basic abbort @dmabuf. It checks to see whether such
16463 * partially assembled sequence held by the driver. If so, it shall free up all
16464 * the frames from the partially assembled sequence.
16465 *
16466 * Return
16467 * true -- if there is matching partially assembled sequence present and all
16468 * the frames freed with the sequence;
16469 * false -- if there is no matching partially assembled sequence present so
16470 * nothing got aborted in the lower layer driver
16471 **/
16472static bool
16473lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16474 struct hbq_dmabuf *dmabuf)
16475{
16476 struct fc_frame_header *new_hdr;
16477 struct fc_frame_header *temp_hdr;
16478 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16479 struct hbq_dmabuf *seq_dmabuf = NULL;
16480
16481 /* Use the hdr_buf to find the sequence that matches this frame */
16482 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16483 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16484 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16485 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16486 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16487 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16488 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16489 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16490 continue;
16491 /* found a pending sequence that matches this frame */
16492 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16493 break;
16494 }
16495
16496 /* Free up all the frames from the partially assembled sequence */
16497 if (seq_dmabuf) {
16498 list_for_each_entry_safe(d_buf, n_buf,
16499 &seq_dmabuf->dbuf.list, list) {
16500 list_del_init(&d_buf->list);
16501 lpfc_in_buf_free(vport->phba, d_buf);
16502 }
16503 return true;
16504 }
16505 return false;
16506}
16507
16508/**
James Smart6dd9e312013-01-03 15:43:37 -050016509 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16510 * @vport: pointer to a vitural port
16511 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16512 *
16513 * This function tries to abort from the assembed sequence from upper level
16514 * protocol, described by the information from basic abbort @dmabuf. It
16515 * checks to see whether such pending context exists at upper level protocol.
16516 * If so, it shall clean up the pending context.
16517 *
16518 * Return
16519 * true -- if there is matching pending context of the sequence cleaned
16520 * at ulp;
16521 * false -- if there is no matching pending context of the sequence present
16522 * at ulp.
16523 **/
16524static bool
16525lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16526{
16527 struct lpfc_hba *phba = vport->phba;
16528 int handled;
16529
16530 /* Accepting abort at ulp with SLI4 only */
16531 if (phba->sli_rev < LPFC_SLI_REV4)
16532 return false;
16533
16534 /* Register all caring upper level protocols to attend abort */
16535 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16536 if (handled)
16537 return true;
16538
16539 return false;
16540}
16541
16542/**
James Smart546fc852011-03-11 16:06:29 -050016543 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016544 * @phba: Pointer to HBA context object.
16545 * @cmd_iocbq: pointer to the command iocbq structure.
16546 * @rsp_iocbq: pointer to the response iocbq structure.
16547 *
James Smart546fc852011-03-11 16:06:29 -050016548 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016549 * event. It properly releases the memory allocated to the sequence abort
16550 * accept iocb.
16551 **/
16552static void
James Smart546fc852011-03-11 16:06:29 -050016553lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016554 struct lpfc_iocbq *cmd_iocbq,
16555 struct lpfc_iocbq *rsp_iocbq)
16556{
James Smart6dd9e312013-01-03 15:43:37 -050016557 struct lpfc_nodelist *ndlp;
16558
16559 if (cmd_iocbq) {
16560 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16561 lpfc_nlp_put(ndlp);
16562 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016563 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016564 }
James Smart6b5151f2012-01-18 16:24:06 -050016565
16566 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16567 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16568 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16569 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16570 rsp_iocbq->iocb.ulpStatus,
16571 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016572}
16573
16574/**
James Smart6d368e52011-05-24 11:44:12 -040016575 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16576 * @phba: Pointer to HBA context object.
16577 * @xri: xri id in transaction.
16578 *
16579 * This function validates the xri maps to the known range of XRIs allocated an
16580 * used by the driver.
16581 **/
James Smart7851fe22011-07-22 18:36:52 -040016582uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016583lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16584 uint16_t xri)
16585{
James Smarta2fc4aef2014-09-03 12:57:55 -040016586 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016587
16588 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16589 if (xri == phba->sli4_hba.xri_ids[i])
16590 return i;
16591 }
16592 return NO_XRI;
16593}
16594
James Smart6d368e52011-05-24 11:44:12 -040016595/**
James Smart546fc852011-03-11 16:06:29 -050016596 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016597 * @phba: Pointer to HBA context object.
16598 * @fc_hdr: pointer to a FC frame header.
16599 *
James Smart546fc852011-03-11 16:06:29 -050016600 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016601 * event after aborting the sequence handling.
16602 **/
James Smart86c67372017-04-21 16:05:04 -070016603void
James Smart6dd9e312013-01-03 15:43:37 -050016604lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16605 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016606{
James Smart6dd9e312013-01-03 15:43:37 -050016607 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040016608 struct lpfc_iocbq *ctiocb = NULL;
16609 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040016610 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050016611 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040016612 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050016613 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040016614
16615 if (!lpfc_is_link_up(phba))
16616 return;
16617
16618 sid = sli4_sid_from_fc_hdr(fc_hdr);
16619 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050016620 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040016621
James Smart6dd9e312013-01-03 15:43:37 -050016622 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040016623 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070016624 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050016625 if (!ndlp) {
16626 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16627 "1268 Failed to allocate ndlp for "
16628 "oxid:x%x SID:x%x\n", oxid, sid);
16629 return;
16630 }
James Smart6dd9e312013-01-03 15:43:37 -050016631 /* Put ndlp onto pport node list */
16632 lpfc_enqueue_node(vport, ndlp);
16633 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16634 /* re-setup ndlp without removing from node list */
16635 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16636 if (!ndlp) {
16637 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16638 "3275 Failed to active ndlp found "
16639 "for oxid:x%x SID:x%x\n", oxid, sid);
16640 return;
16641 }
James Smart6669f9b2009-10-02 15:16:45 -040016642 }
16643
James Smart546fc852011-03-11 16:06:29 -050016644 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040016645 ctiocb = lpfc_sli_get_iocbq(phba);
16646 if (!ctiocb)
16647 return;
16648
James Smart5ffc2662009-11-18 15:39:44 -050016649 /* Extract the F_CTL field from FC_HDR */
16650 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16651
James Smart6669f9b2009-10-02 15:16:45 -040016652 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040016653 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050016654 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040016655 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16656 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16657 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16658
16659 /* Fill in the rest of iocb fields */
16660 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16661 icmd->ulpBdeCount = 0;
16662 icmd->ulpLe = 1;
16663 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040016664 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050016665 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016666
James Smart6669f9b2009-10-02 15:16:45 -040016667 ctiocb->iocb_cmpl = NULL;
16668 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050016669 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040016670 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050016671 ctiocb->sli4_xritag = NO_XRI;
16672
James Smartee0f4fe2012-05-09 21:19:14 -040016673 if (fctl & FC_FC_EX_CTX)
16674 /* Exchange responder sent the abort so we
16675 * own the oxid.
16676 */
16677 xri = oxid;
16678 else
16679 xri = rxid;
16680 lxri = lpfc_sli4_xri_inrange(phba, xri);
16681 if (lxri != NO_XRI)
16682 lpfc_set_rrq_active(phba, ndlp, lxri,
16683 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050016684 /* For BA_ABTS from exchange responder, if the logical xri with
16685 * the oxid maps to the FCP XRI range, the port no longer has
16686 * that exchange context, send a BLS_RJT. Override the IOCB for
16687 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050016688 */
James Smart6dd9e312013-01-03 15:43:37 -050016689 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080016690 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050016691 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16692 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16693 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16694 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16695 }
16696
16697 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16698 * the driver no longer has that exchange, send a BLS_RJT. Override
16699 * the IOCB for a BA_RJT.
16700 */
16701 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050016702 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16703 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16704 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16705 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16706 }
James Smart6669f9b2009-10-02 15:16:45 -040016707
James Smart5ffc2662009-11-18 15:39:44 -050016708 if (fctl & FC_FC_EX_CTX) {
16709 /* ABTS sent by responder to CT exchange, construction
16710 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16711 * field and RX_ID from ABTS for RX_ID field.
16712 */
James Smart546fc852011-03-11 16:06:29 -050016713 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050016714 } else {
16715 /* ABTS sent by initiator to CT exchange, construction
16716 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050016717 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050016718 */
James Smart546fc852011-03-11 16:06:29 -050016719 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050016720 }
James Smartf09c3ac2012-03-01 22:33:29 -050016721 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050016722 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050016723
James Smart546fc852011-03-11 16:06:29 -050016724 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050016725 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16726 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16727 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050016728
16729 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16730 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050016731 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16732 "2925 Failed to issue CT ABTS RSP x%x on "
16733 "xri x%x, Data x%x\n",
16734 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16735 phba->link_state);
16736 lpfc_nlp_put(ndlp);
16737 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050016738 lpfc_sli_release_iocbq(phba, ctiocb);
16739 }
James Smart6669f9b2009-10-02 15:16:45 -040016740}
16741
16742/**
16743 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16744 * @vport: Pointer to the vport on which this sequence was received
16745 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16746 *
16747 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16748 * receive sequence is only partially assembed by the driver, it shall abort
16749 * the partially assembled frames for the sequence. Otherwise, if the
16750 * unsolicited receive sequence has been completely assembled and passed to
16751 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16752 * unsolicited sequence has been aborted. After that, it will issue a basic
16753 * accept to accept the abort.
16754 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016755static void
James Smart6669f9b2009-10-02 15:16:45 -040016756lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16757 struct hbq_dmabuf *dmabuf)
16758{
16759 struct lpfc_hba *phba = vport->phba;
16760 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050016761 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050016762 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040016763
James Smart6669f9b2009-10-02 15:16:45 -040016764 /* Make a copy of fc_hdr before the dmabuf being released */
16765 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050016766 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040016767
James Smart5ffc2662009-11-18 15:39:44 -050016768 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050016769 /* ABTS by responder to exchange, no cleanup needed */
16770 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050016771 } else {
James Smart6dd9e312013-01-03 15:43:37 -050016772 /* ABTS by initiator to exchange, need to do cleanup */
16773 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16774 if (aborted == false)
16775 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050016776 }
James Smart6dd9e312013-01-03 15:43:37 -050016777 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16778
James Smart86c67372017-04-21 16:05:04 -070016779 if (phba->nvmet_support) {
16780 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16781 return;
16782 }
16783
James Smart6dd9e312013-01-03 15:43:37 -050016784 /* Respond with BA_ACC or BA_RJT accordingly */
16785 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040016786}
16787
16788/**
James Smart4f774512009-05-22 14:52:35 -040016789 * lpfc_seq_complete - Indicates if a sequence is complete
16790 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16791 *
16792 * This function checks the sequence, starting with the frame described by
16793 * @dmabuf, to see if all the frames associated with this sequence are present.
16794 * the frames associated with this sequence are linked to the @dmabuf using the
16795 * dbuf list. This function looks for two major things. 1) That the first frame
16796 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16797 * set. 3) That there are no holes in the sequence count. The function will
16798 * return 1 when the sequence is complete, otherwise it will return 0.
16799 **/
16800static int
16801lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16802{
16803 struct fc_frame_header *hdr;
16804 struct lpfc_dmabuf *d_buf;
16805 struct hbq_dmabuf *seq_dmabuf;
16806 uint32_t fctl;
16807 int seq_count = 0;
16808
16809 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16810 /* make sure first fame of sequence has a sequence count of zero */
16811 if (hdr->fh_seq_cnt != seq_count)
16812 return 0;
16813 fctl = (hdr->fh_f_ctl[0] << 16 |
16814 hdr->fh_f_ctl[1] << 8 |
16815 hdr->fh_f_ctl[2]);
16816 /* If last frame of sequence we can return success. */
16817 if (fctl & FC_FC_END_SEQ)
16818 return 1;
16819 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16820 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16821 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16822 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050016823 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040016824 return 0;
16825 fctl = (hdr->fh_f_ctl[0] << 16 |
16826 hdr->fh_f_ctl[1] << 8 |
16827 hdr->fh_f_ctl[2]);
16828 /* If last frame of sequence we can return success. */
16829 if (fctl & FC_FC_END_SEQ)
16830 return 1;
16831 }
16832 return 0;
16833}
16834
16835/**
16836 * lpfc_prep_seq - Prep sequence for ULP processing
16837 * @vport: Pointer to the vport on which this sequence was received
16838 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16839 *
16840 * This function takes a sequence, described by a list of frames, and creates
16841 * a list of iocbq structures to describe the sequence. This iocbq list will be
16842 * used to issue to the generic unsolicited sequence handler. This routine
16843 * returns a pointer to the first iocbq in the list. If the function is unable
16844 * to allocate an iocbq then it throw out the received frames that were not
16845 * able to be described and return a pointer to the first iocbq. If unable to
16846 * allocate any iocbqs (including the first) this function will return NULL.
16847 **/
16848static struct lpfc_iocbq *
16849lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16850{
James Smart7851fe22011-07-22 18:36:52 -040016851 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016852 struct lpfc_dmabuf *d_buf, *n_buf;
16853 struct lpfc_iocbq *first_iocbq, *iocbq;
16854 struct fc_frame_header *fc_hdr;
16855 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040016856 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050016857 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040016858
16859 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16860 /* remove from receive buffer list */
16861 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016862 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016863 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040016864 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040016865 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016866 /* Get an iocbq struct to fill in. */
16867 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16868 if (first_iocbq) {
16869 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040016870 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016871 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080016872 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040016873
16874 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16875 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16876 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16877 first_iocbq->iocb.un.rcvels.parmRo =
16878 sli4_did_from_fc_hdr(fc_hdr);
16879 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16880 } else
16881 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040016882 first_iocbq->iocb.ulpContext = NO_XRI;
16883 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16884 be16_to_cpu(fc_hdr->fh_ox_id);
16885 /* iocbq is prepped for internal consumption. Physical vpi. */
16886 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16887 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040016888 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040016889 tot_len = bf_get(lpfc_rcqe_length,
16890 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16891
James Smart4f774512009-05-22 14:52:35 -040016892 first_iocbq->context2 = &seq_dmabuf->dbuf;
16893 first_iocbq->context3 = NULL;
16894 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040016895 if (tot_len > LPFC_DATA_BUF_SIZE)
16896 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040016897 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040016898 else
16899 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16900
James Smart4f774512009-05-22 14:52:35 -040016901 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040016902
James Smart7851fe22011-07-22 18:36:52 -040016903 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040016904 }
16905 iocbq = first_iocbq;
16906 /*
16907 * Each IOCBq can have two Buffers assigned, so go through the list
16908 * of buffers for this sequence and save two buffers in each IOCBq
16909 */
16910 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16911 if (!iocbq) {
16912 lpfc_in_buf_free(vport->phba, d_buf);
16913 continue;
16914 }
16915 if (!iocbq->context3) {
16916 iocbq->context3 = d_buf;
16917 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040016918 /* We need to get the size out of the right CQE */
16919 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16920 len = bf_get(lpfc_rcqe_length,
16921 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016922 pbde = (struct ulp_bde64 *)
16923 &iocbq->iocb.unsli3.sli3Words[4];
16924 if (len > LPFC_DATA_BUF_SIZE)
16925 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16926 else
16927 pbde->tus.f.bdeSize = len;
16928
James Smart7851fe22011-07-22 18:36:52 -040016929 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16930 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040016931 } else {
16932 iocbq = lpfc_sli_get_iocbq(vport->phba);
16933 if (!iocbq) {
16934 if (first_iocbq) {
16935 first_iocbq->iocb.ulpStatus =
16936 IOSTAT_FCP_RSP_ERROR;
16937 first_iocbq->iocb.un.ulpWord[4] =
16938 IOERR_NO_RESOURCES;
16939 }
16940 lpfc_in_buf_free(vport->phba, d_buf);
16941 continue;
16942 }
James Smart7851fe22011-07-22 18:36:52 -040016943 /* We need to get the size out of the right CQE */
16944 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16945 len = bf_get(lpfc_rcqe_length,
16946 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016947 iocbq->context2 = d_buf;
16948 iocbq->context3 = NULL;
16949 iocbq->iocb.ulpBdeCount = 1;
16950 if (len > LPFC_DATA_BUF_SIZE)
16951 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16952 LPFC_DATA_BUF_SIZE;
16953 else
16954 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
16955
James Smart7851fe22011-07-22 18:36:52 -040016956 tot_len += len;
16957 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16958
James Smart4f774512009-05-22 14:52:35 -040016959 iocbq->iocb.un.rcvels.remoteID = sid;
16960 list_add_tail(&iocbq->list, &first_iocbq->list);
16961 }
16962 }
16963 return first_iocbq;
16964}
16965
James Smart6669f9b2009-10-02 15:16:45 -040016966static void
16967lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16968 struct hbq_dmabuf *seq_dmabuf)
16969{
16970 struct fc_frame_header *fc_hdr;
16971 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16972 struct lpfc_hba *phba = vport->phba;
16973
16974 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16975 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16976 if (!iocbq) {
16977 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16978 "2707 Ring %d handler: Failed to allocate "
16979 "iocb Rctl x%x Type x%x received\n",
16980 LPFC_ELS_RING,
16981 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16982 return;
16983 }
16984 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080016985 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040016986 iocbq, fc_hdr->fh_r_ctl,
16987 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040016988 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040016989 "2540 Ring %d handler: unexpected Rctl "
16990 "x%x Type x%x received\n",
16991 LPFC_ELS_RING,
16992 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16993
16994 /* Free iocb created in lpfc_prep_seq */
16995 list_for_each_entry_safe(curr_iocb, next_iocb,
16996 &iocbq->list, list) {
16997 list_del_init(&curr_iocb->list);
16998 lpfc_sli_release_iocbq(phba, curr_iocb);
16999 }
17000 lpfc_sli_release_iocbq(phba, iocbq);
17001}
17002
James Smart4f774512009-05-22 14:52:35 -040017003/**
17004 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17005 * @phba: Pointer to HBA context object.
17006 *
17007 * This function is called with no lock held. This function processes all
17008 * the received buffers and gives it to upper layers when a received buffer
17009 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017010 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017011 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17012 * appropriate receive function when the final frame in a sequence is received.
17013 **/
James Smart4d9ab992009-10-02 15:16:39 -040017014void
17015lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17016 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017017{
James Smart4d9ab992009-10-02 15:16:39 -040017018 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017019 struct fc_frame_header *fc_hdr;
17020 struct lpfc_vport *vport;
17021 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017022 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017023
James Smart4f774512009-05-22 14:52:35 -040017024 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017025 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017026
James Smart4d9ab992009-10-02 15:16:39 -040017027 /* check to see if this a valid type of frame */
17028 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17029 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17030 return;
17031 }
James Smart2ea259e2017-02-12 13:52:27 -080017032
James Smart7851fe22011-07-22 18:36:52 -040017033 if ((bf_get(lpfc_cqe_code,
17034 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17035 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17036 &dmabuf->cq_event.cqe.rcqe_cmpl);
17037 else
17038 fcfi = bf_get(lpfc_rcqe_fcf_id,
17039 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017040
James Smart895427b2017-02-12 13:52:30 -080017041 /* d_id this frame is directed to */
17042 did = sli4_did_from_fc_hdr(fc_hdr);
17043
17044 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017045 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017046 /* throw out the frame */
17047 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17048 return;
17049 }
James Smart939723a2012-05-09 21:19:03 -040017050
James Smart939723a2012-05-09 21:19:03 -040017051 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17052 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17053 (did != Fabric_DID)) {
17054 /*
17055 * Throw out the frame if we are not pt2pt.
17056 * The pt2pt protocol allows for discovery frames
17057 * to be received without a registered VPI.
17058 */
17059 if (!(vport->fc_flag & FC_PT2PT) ||
17060 (phba->link_state == LPFC_HBA_READY)) {
17061 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17062 return;
17063 }
17064 }
17065
James Smart6669f9b2009-10-02 15:16:45 -040017066 /* Handle the basic abort sequence (BA_ABTS) event */
17067 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17068 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17069 return;
17070 }
17071
James Smart4d9ab992009-10-02 15:16:39 -040017072 /* Link this frame */
17073 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17074 if (!seq_dmabuf) {
17075 /* unable to add frame to vport - throw it out */
17076 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17077 return;
17078 }
17079 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017080 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017081 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017082
James Smart6669f9b2009-10-02 15:16:45 -040017083 /* Send the complete sequence to the upper layer protocol */
17084 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017085}
James Smart6fb120a2009-05-22 14:52:59 -040017086
17087/**
17088 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17089 * @phba: pointer to lpfc hba data structure.
17090 *
17091 * This routine is invoked to post rpi header templates to the
17092 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017093 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17094 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017095 *
17096 * This routine does not require any locks. It's usage is expected
17097 * to be driver load or reset recovery when the driver is
17098 * sequential.
17099 *
17100 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017101 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017102 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017103 * When this error occurs, the driver is not guaranteed
17104 * to have any rpi regions posted to the device and
17105 * must either attempt to repost the regions or take a
17106 * fatal error.
17107 **/
17108int
17109lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17110{
17111 struct lpfc_rpi_hdr *rpi_page;
17112 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017113 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017114
James Smart6d368e52011-05-24 11:44:12 -040017115 /* SLI4 ports that support extents do not require RPI headers. */
17116 if (!phba->sli4_hba.rpi_hdrs_in_use)
17117 goto exit;
17118 if (phba->sli4_hba.extents_in_use)
17119 return -EIO;
17120
James Smart6fb120a2009-05-22 14:52:59 -040017121 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017122 /*
17123 * Assign the rpi headers a physical rpi only if the driver
17124 * has not initialized those resources. A port reset only
17125 * needs the headers posted.
17126 */
17127 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17128 LPFC_RPI_RSRC_RDY)
17129 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17130
James Smart6fb120a2009-05-22 14:52:59 -040017131 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17132 if (rc != MBX_SUCCESS) {
17133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17134 "2008 Error %d posting all rpi "
17135 "headers\n", rc);
17136 rc = -EIO;
17137 break;
17138 }
17139 }
17140
James Smart6d368e52011-05-24 11:44:12 -040017141 exit:
17142 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17143 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017144 return rc;
17145}
17146
17147/**
17148 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17149 * @phba: pointer to lpfc hba data structure.
17150 * @rpi_page: pointer to the rpi memory region.
17151 *
17152 * This routine is invoked to post a single rpi header to the
17153 * HBA consistent with the SLI-4 interface spec. This memory region
17154 * maps up to 64 rpi context regions.
17155 *
17156 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017157 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017158 * -ENOMEM - No available memory
17159 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017160 **/
17161int
17162lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17163{
17164 LPFC_MBOXQ_t *mboxq;
17165 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17166 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017167 uint32_t shdr_status, shdr_add_status;
17168 union lpfc_sli4_cfg_shdr *shdr;
17169
James Smart6d368e52011-05-24 11:44:12 -040017170 /* SLI4 ports that support extents do not require RPI headers. */
17171 if (!phba->sli4_hba.rpi_hdrs_in_use)
17172 return rc;
17173 if (phba->sli4_hba.extents_in_use)
17174 return -EIO;
17175
James Smart6fb120a2009-05-22 14:52:59 -040017176 /* The port is notified of the header region via a mailbox command. */
17177 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17178 if (!mboxq) {
17179 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17180 "2001 Unable to allocate memory for issuing "
17181 "SLI_CONFIG_SPECIAL mailbox command\n");
17182 return -ENOMEM;
17183 }
17184
17185 /* Post all rpi memory regions to the port. */
17186 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017187 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17188 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17189 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017190 sizeof(struct lpfc_sli4_cfg_mhdr),
17191 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017192
17193
17194 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017195 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17196 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017197 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17198 hdr_tmpl, rpi_page->page_count);
17199
James Smart6fb120a2009-05-22 14:52:59 -040017200 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17201 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017202 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017203 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17204 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17205 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17206 if (rc != MBX_TIMEOUT)
17207 mempool_free(mboxq, phba->mbox_mem_pool);
17208 if (shdr_status || shdr_add_status || rc) {
17209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17210 "2514 POST_RPI_HDR mailbox failed with "
17211 "status x%x add_status x%x, mbx status x%x\n",
17212 shdr_status, shdr_add_status, rc);
17213 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017214 } else {
17215 /*
17216 * The next_rpi stores the next logical module-64 rpi value used
17217 * to post physical rpis in subsequent rpi postings.
17218 */
17219 spin_lock_irq(&phba->hbalock);
17220 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17221 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017222 }
17223 return rc;
17224}
17225
17226/**
17227 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17228 * @phba: pointer to lpfc hba data structure.
17229 *
17230 * This routine is invoked to post rpi header templates to the
17231 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017232 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17233 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017234 *
17235 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017236 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017237 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17238 **/
17239int
17240lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17241{
James Smart6d368e52011-05-24 11:44:12 -040017242 unsigned long rpi;
17243 uint16_t max_rpi, rpi_limit;
17244 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017245 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017246 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017247
James Smart6fb120a2009-05-22 14:52:59 -040017248 /*
James Smart6d368e52011-05-24 11:44:12 -040017249 * Fetch the next logical rpi. Because this index is logical,
17250 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017251 */
James Smart4902b382013-10-10 12:20:35 -040017252 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017253 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17254 rpi_limit = phba->sli4_hba.next_rpi;
17255
James Smart6d368e52011-05-24 11:44:12 -040017256 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17257 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017258 rpi = LPFC_RPI_ALLOC_ERROR;
17259 else {
17260 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17261 phba->sli4_hba.max_cfg_param.rpi_used++;
17262 phba->sli4_hba.rpi_count++;
17263 }
James Smartbe6bb942015-04-07 15:07:22 -040017264 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17265 "0001 rpi:%x max:%x lim:%x\n",
17266 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017267
17268 /*
17269 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017270 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017271 */
17272 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17273 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017274 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017275 return rpi;
17276 }
17277
17278 /*
James Smart6d368e52011-05-24 11:44:12 -040017279 * RPI header postings are not required for SLI4 ports capable of
17280 * extents.
17281 */
17282 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017283 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017284 return rpi;
17285 }
17286
17287 /*
James Smart6fb120a2009-05-22 14:52:59 -040017288 * If the driver is running low on rpi resources, allocate another
17289 * page now. Note that the next_rpi value is used because
17290 * it represents how many are actually in use whereas max_rpi notes
17291 * how many are supported max by the device.
17292 */
James Smart6d368e52011-05-24 11:44:12 -040017293 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017294 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017295 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17296 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17297 if (!rpi_hdr) {
17298 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17299 "2002 Error Could not grow rpi "
17300 "count\n");
17301 } else {
James Smart6d368e52011-05-24 11:44:12 -040017302 lrpi = rpi_hdr->start_rpi;
17303 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017304 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17305 }
17306 }
17307
17308 return rpi;
17309}
17310
17311/**
17312 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17313 * @phba: pointer to lpfc hba data structure.
17314 *
17315 * This routine is invoked to release an rpi to the pool of
17316 * available rpis maintained by the driver.
17317 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017318static void
James Smartd7c47992010-06-08 18:31:54 -040017319__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17320{
17321 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17322 phba->sli4_hba.rpi_count--;
17323 phba->sli4_hba.max_cfg_param.rpi_used--;
17324 }
17325}
17326
17327/**
17328 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17329 * @phba: pointer to lpfc hba data structure.
17330 *
17331 * This routine is invoked to release an rpi to the pool of
17332 * available rpis maintained by the driver.
17333 **/
17334void
James Smart6fb120a2009-05-22 14:52:59 -040017335lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17336{
17337 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017338 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017339 spin_unlock_irq(&phba->hbalock);
17340}
17341
17342/**
17343 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17344 * @phba: pointer to lpfc hba data structure.
17345 *
17346 * This routine is invoked to remove the memory region that
17347 * provided rpi via a bitmask.
17348 **/
17349void
17350lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17351{
17352 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017353 kfree(phba->sli4_hba.rpi_ids);
17354 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017355}
17356
17357/**
17358 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17359 * @phba: pointer to lpfc hba data structure.
17360 *
17361 * This routine is invoked to remove the memory region that
17362 * provided rpi via a bitmask.
17363 **/
17364int
James Smart6b5151f2012-01-18 16:24:06 -050017365lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17366 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017367{
17368 LPFC_MBOXQ_t *mboxq;
17369 struct lpfc_hba *phba = ndlp->phba;
17370 int rc;
17371
17372 /* The port is notified of the header region via a mailbox command. */
17373 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17374 if (!mboxq)
17375 return -ENOMEM;
17376
17377 /* Post all rpi memory regions to the port. */
17378 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017379 if (cmpl) {
17380 mboxq->mbox_cmpl = cmpl;
17381 mboxq->context1 = arg;
17382 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017383 } else
17384 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017385 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017386 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17387 if (rc == MBX_NOT_FINISHED) {
17388 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17389 "2010 Resume RPI Mailbox failed "
17390 "status %d, mbxStatus x%x\n", rc,
17391 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17392 mempool_free(mboxq, phba->mbox_mem_pool);
17393 return -EIO;
17394 }
17395 return 0;
17396}
17397
17398/**
17399 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017400 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017401 *
James Smart76a95d72010-11-20 23:11:48 -050017402 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017403 *
17404 * Returns:
17405 * 0 success
17406 * -Evalue otherwise
17407 **/
17408int
James Smart76a95d72010-11-20 23:11:48 -050017409lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017410{
17411 LPFC_MBOXQ_t *mboxq;
17412 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017413 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017414 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017415 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017416 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17417 if (!mboxq)
17418 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017419 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017420 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017421 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017422 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017423 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017424 "2022 INIT VPI Mailbox failed "
17425 "status %d, mbxStatus x%x\n", rc,
17426 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017427 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017428 }
James Smart6a9c52c2009-10-02 15:16:51 -040017429 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017430 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017431
17432 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017433}
17434
17435/**
17436 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17437 * @phba: pointer to lpfc hba data structure.
17438 * @mboxq: Pointer to mailbox object.
17439 *
17440 * This routine is invoked to manually add a single FCF record. The caller
17441 * must pass a completely initialized FCF_Record. This routine takes
17442 * care of the nonembedded mailbox operations.
17443 **/
17444static void
17445lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17446{
17447 void *virt_addr;
17448 union lpfc_sli4_cfg_shdr *shdr;
17449 uint32_t shdr_status, shdr_add_status;
17450
17451 virt_addr = mboxq->sge_array->addr[0];
17452 /* The IOCTL status is embedded in the mailbox subheader. */
17453 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17454 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17455 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17456
17457 if ((shdr_status || shdr_add_status) &&
17458 (shdr_status != STATUS_FCF_IN_USE))
17459 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17460 "2558 ADD_FCF_RECORD mailbox failed with "
17461 "status x%x add_status x%x\n",
17462 shdr_status, shdr_add_status);
17463
17464 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17465}
17466
17467/**
17468 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17469 * @phba: pointer to lpfc hba data structure.
17470 * @fcf_record: pointer to the initialized fcf record to add.
17471 *
17472 * This routine is invoked to manually add a single FCF record. The caller
17473 * must pass a completely initialized FCF_Record. This routine takes
17474 * care of the nonembedded mailbox operations.
17475 **/
17476int
17477lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17478{
17479 int rc = 0;
17480 LPFC_MBOXQ_t *mboxq;
17481 uint8_t *bytep;
17482 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017483 struct lpfc_mbx_sge sge;
17484 uint32_t alloc_len, req_len;
17485 uint32_t fcfindex;
17486
17487 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17488 if (!mboxq) {
17489 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17490 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17491 return -ENOMEM;
17492 }
17493
17494 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17495 sizeof(uint32_t);
17496
17497 /* Allocate DMA memory and set up the non-embedded mailbox command */
17498 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17499 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17500 req_len, LPFC_SLI4_MBX_NEMBED);
17501 if (alloc_len < req_len) {
17502 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17503 "2523 Allocated DMA memory size (x%x) is "
17504 "less than the requested DMA memory "
17505 "size (x%x)\n", alloc_len, req_len);
17506 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17507 return -ENOMEM;
17508 }
17509
17510 /*
17511 * Get the first SGE entry from the non-embedded DMA memory. This
17512 * routine only uses a single SGE.
17513 */
17514 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040017515 virt_addr = mboxq->sge_array->addr[0];
17516 /*
17517 * Configure the FCF record for FCFI 0. This is the driver's
17518 * hardcoded default and gets used in nonFIP mode.
17519 */
17520 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17521 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17522 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17523
17524 /*
17525 * Copy the fcf_index and the FCF Record Data. The data starts after
17526 * the FCoE header plus word10. The data copy needs to be endian
17527 * correct.
17528 */
17529 bytep += sizeof(uint32_t);
17530 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17531 mboxq->vport = phba->pport;
17532 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17533 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17534 if (rc == MBX_NOT_FINISHED) {
17535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17536 "2515 ADD_FCF_RECORD mailbox failed with "
17537 "status 0x%x\n", rc);
17538 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17539 rc = -EIO;
17540 } else
17541 rc = 0;
17542
17543 return rc;
17544}
17545
17546/**
17547 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17548 * @phba: pointer to lpfc hba data structure.
17549 * @fcf_record: pointer to the fcf record to write the default data.
17550 * @fcf_index: FCF table entry index.
17551 *
17552 * This routine is invoked to build the driver's default FCF record. The
17553 * values used are hardcoded. This routine handles memory initialization.
17554 *
17555 **/
17556void
17557lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17558 struct fcf_record *fcf_record,
17559 uint16_t fcf_index)
17560{
17561 memset(fcf_record, 0, sizeof(struct fcf_record));
17562 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17563 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17564 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17565 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17566 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17567 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17568 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17569 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17570 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17571 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17572 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17573 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17574 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040017575 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040017576 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17577 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17578 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17579 /* Set the VLAN bit map */
17580 if (phba->valid_vlan) {
17581 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17582 = 1 << (phba->vlan_id % 8);
17583 }
17584}
17585
17586/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017587 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040017588 * @phba: pointer to lpfc hba data structure.
17589 * @fcf_index: FCF table entry offset.
17590 *
James Smart0c9ab6f2010-02-26 14:15:57 -050017591 * This routine is invoked to scan the entire FCF table by reading FCF
17592 * record and processing it one at a time starting from the @fcf_index
17593 * for initial FCF discovery or fast FCF failover rediscovery.
17594 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017595 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017596 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040017597 **/
17598int
James Smart0c9ab6f2010-02-26 14:15:57 -050017599lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040017600{
17601 int rc = 0, error;
17602 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040017603
James Smart32b97932009-07-19 10:01:21 -040017604 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050017605 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040017606 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17607 if (!mboxq) {
17608 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17609 "2000 Failed to allocate mbox for "
17610 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040017611 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050017612 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017613 }
James Smartecfd03c2010-02-12 14:41:27 -050017614 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050017615 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050017616 if (rc) {
17617 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050017618 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017619 }
James Smartecfd03c2010-02-12 14:41:27 -050017620 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040017621 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050017622 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040017623
17624 spin_lock_irq(&phba->hbalock);
17625 phba->hba_flag |= FCF_TS_INPROG;
17626 spin_unlock_irq(&phba->hbalock);
17627
James Smart6fb120a2009-05-22 14:52:59 -040017628 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050017629 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040017630 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050017631 else {
James Smart38b92ef2010-08-04 16:11:39 -040017632 /* Reset eligible FCF count for new scan */
17633 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040017634 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017635 error = 0;
James Smart32b97932009-07-19 10:01:21 -040017636 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017637fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040017638 if (error) {
17639 if (mboxq)
17640 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040017641 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040017642 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040017643 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040017644 spin_unlock_irq(&phba->hbalock);
17645 }
James Smart6fb120a2009-05-22 14:52:59 -040017646 return error;
17647}
James Smarta0c87cb2009-07-19 10:01:10 -040017648
17649/**
James Smarta93ff372010-10-22 11:06:08 -040017650 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050017651 * @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
James Smarta93ff372010-10-22 11:06:08 -040017655 * and to use it for FLOGI roundrobin FCF failover.
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_fcf_rr_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 "2763 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_fcf_rr_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/**
17696 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17697 * @phba: pointer to lpfc hba data structure.
17698 * @fcf_index: FCF table entry offset.
17699 *
17700 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040017701 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050017702 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017703 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017704 * otherwise.
17705 **/
17706int
17707lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17708{
17709 int rc = 0, error;
17710 LPFC_MBOXQ_t *mboxq;
17711
17712 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17713 if (!mboxq) {
17714 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17715 "2758 Failed to allocate mbox for "
17716 "READ_FCF cmd\n");
17717 error = -ENOMEM;
17718 goto fail_fcf_read;
17719 }
17720 /* Construct the read FCF record mailbox command */
17721 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17722 if (rc) {
17723 error = -EINVAL;
17724 goto fail_fcf_read;
17725 }
17726 /* Issue the mailbox command asynchronously */
17727 mboxq->vport = phba->pport;
17728 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17729 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17730 if (rc == MBX_NOT_FINISHED)
17731 error = -EIO;
17732 else
17733 error = 0;
17734
17735fail_fcf_read:
17736 if (error && mboxq)
17737 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17738 return error;
17739}
17740
17741/**
James Smartf5cb5302015-12-16 18:11:52 -050017742 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040017743 * phba pointer to the lpfc_hba struct for this port.
17744 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17745 * routine when the rr_bmask is empty. The FCF indecies are put into the
17746 * rr_bmask based on their priority level. Starting from the highest priority
17747 * to the lowest. The most likely FCF candidate will be in the highest
17748 * priority group. When this routine is called it searches the fcf_pri list for
17749 * next lowest priority group and repopulates the rr_bmask with only those
17750 * fcf_indexes.
17751 * returns:
17752 * 1=success 0=failure
17753 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017754static int
James Smart7d791df2011-07-22 18:37:52 -040017755lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17756{
17757 uint16_t next_fcf_pri;
17758 uint16_t last_index;
17759 struct lpfc_fcf_pri *fcf_pri;
17760 int rc;
17761 int ret = 0;
17762
17763 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17764 LPFC_SLI4_FCF_TBL_INDX_MAX);
17765 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17766 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050017767
17768 /* Verify the priority list has 2 or more entries */
17769 spin_lock_irq(&phba->hbalock);
17770 if (list_empty(&phba->fcf.fcf_pri_list) ||
17771 list_is_singular(&phba->fcf.fcf_pri_list)) {
17772 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040017773 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17774 "3061 Last IDX %d\n", last_index);
17775 return 0; /* Empty rr list */
17776 }
James Smart25626692013-03-01 16:36:54 -050017777 spin_unlock_irq(&phba->hbalock);
17778
James Smart7d791df2011-07-22 18:37:52 -040017779 next_fcf_pri = 0;
17780 /*
17781 * Clear the rr_bmask and set all of the bits that are at this
17782 * priority.
17783 */
17784 memset(phba->fcf.fcf_rr_bmask, 0,
17785 sizeof(*phba->fcf.fcf_rr_bmask));
17786 spin_lock_irq(&phba->hbalock);
17787 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17788 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
17789 continue;
17790 /*
17791 * the 1st priority that has not FLOGI failed
17792 * will be the highest.
17793 */
17794 if (!next_fcf_pri)
17795 next_fcf_pri = fcf_pri->fcf_rec.priority;
17796 spin_unlock_irq(&phba->hbalock);
17797 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17798 rc = lpfc_sli4_fcf_rr_index_set(phba,
17799 fcf_pri->fcf_rec.fcf_index);
17800 if (rc)
17801 return 0;
17802 }
17803 spin_lock_irq(&phba->hbalock);
17804 }
17805 /*
17806 * if next_fcf_pri was not set above and the list is not empty then
17807 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070017808 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040017809 */
17810 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
17811 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17812 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
17813 /*
17814 * the 1st priority that has not FLOGI failed
17815 * will be the highest.
17816 */
17817 if (!next_fcf_pri)
17818 next_fcf_pri = fcf_pri->fcf_rec.priority;
17819 spin_unlock_irq(&phba->hbalock);
17820 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17821 rc = lpfc_sli4_fcf_rr_index_set(phba,
17822 fcf_pri->fcf_rec.fcf_index);
17823 if (rc)
17824 return 0;
17825 }
17826 spin_lock_irq(&phba->hbalock);
17827 }
17828 } else
17829 ret = 1;
17830 spin_unlock_irq(&phba->hbalock);
17831
17832 return ret;
17833}
17834/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017835 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17836 * @phba: pointer to lpfc hba data structure.
17837 *
17838 * This routine is to get the next eligible FCF record index in a round
17839 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040017840 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050017841 * shall be returned, otherwise, the next eligible FCF record's index
17842 * shall be returned.
17843 **/
17844uint16_t
17845lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17846{
17847 uint16_t next_fcf_index;
17848
James Smart421c6622013-01-03 15:44:16 -050017849initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040017850 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050017851 next_fcf_index = phba->fcf.current_rec.fcf_indx;
17852
James Smart7d791df2011-07-22 18:37:52 -040017853next_priority:
James Smart421c6622013-01-03 15:44:16 -050017854 /* Determine the next fcf index to check */
17855 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050017856 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17857 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040017858 next_fcf_index);
17859
James Smart0c9ab6f2010-02-26 14:15:57 -050017860 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040017861 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17862 /*
17863 * If we have wrapped then we need to clear the bits that
17864 * have been tested so that we can detect when we should
17865 * change the priority level.
17866 */
James Smart0c9ab6f2010-02-26 14:15:57 -050017867 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17868 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040017869 }
17870
James Smart0c9ab6f2010-02-26 14:15:57 -050017871
James Smart3804dc82010-07-14 15:31:37 -040017872 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040017873 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17874 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17875 /*
17876 * If next fcf index is not found check if there are lower
17877 * Priority level fcf's in the fcf_priority list.
17878 * Set up the rr_bmask with all of the avaiable fcf bits
17879 * at that level and continue the selection process.
17880 */
17881 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050017882 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040017883 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17884 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040017885 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
17886 return LPFC_FCOE_FCF_NEXT_NONE;
17887 else {
17888 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17889 "3063 Only FCF available idx %d, flag %x\n",
17890 next_fcf_index,
17891 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
17892 return next_fcf_index;
17893 }
James Smart3804dc82010-07-14 15:31:37 -040017894 }
17895
James Smart7d791df2011-07-22 18:37:52 -040017896 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
17897 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050017898 LPFC_FCF_FLOGI_FAILED) {
17899 if (list_is_singular(&phba->fcf.fcf_pri_list))
17900 return LPFC_FCOE_FCF_NEXT_NONE;
17901
James Smart7d791df2011-07-22 18:37:52 -040017902 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050017903 }
James Smart7d791df2011-07-22 18:37:52 -040017904
James Smart3804dc82010-07-14 15:31:37 -040017905 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017906 "2845 Get next roundrobin failover FCF (x%x)\n",
17907 next_fcf_index);
17908
James Smart0c9ab6f2010-02-26 14:15:57 -050017909 return next_fcf_index;
17910}
17911
17912/**
17913 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
17914 * @phba: pointer to lpfc hba data structure.
17915 *
17916 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017917 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017918 * does not go beyond the range of the driver allocated bmask dimension
17919 * before setting the bit.
17920 *
17921 * Returns 0 if the index bit successfully set, otherwise, it returns
17922 * -EINVAL.
17923 **/
17924int
17925lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
17926{
17927 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17928 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017929 "2610 FCF (x%x) reached driver's book "
17930 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017931 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17932 return -EINVAL;
17933 }
17934 /* Set the eligible FCF record index bmask */
17935 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17936
James Smart3804dc82010-07-14 15:31:37 -040017937 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017938 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040017939 "bmask\n", fcf_index);
17940
James Smart0c9ab6f2010-02-26 14:15:57 -050017941 return 0;
17942}
17943
17944/**
James Smart3804dc82010-07-14 15:31:37 -040017945 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050017946 * @phba: pointer to lpfc hba data structure.
17947 *
17948 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017949 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017950 * does not go beyond the range of the driver allocated bmask dimension
17951 * before clearing the bit.
17952 **/
17953void
17954lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
17955{
James Smart9a803a72013-09-06 12:17:56 -040017956 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050017957 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17958 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017959 "2762 FCF (x%x) reached driver's book "
17960 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017961 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17962 return;
17963 }
17964 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040017965 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040017966 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
17967 list) {
James Smart7d791df2011-07-22 18:37:52 -040017968 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
17969 list_del_init(&fcf_pri->list);
17970 break;
17971 }
17972 }
17973 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017974 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040017975
17976 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017977 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040017978 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050017979}
17980
17981/**
James Smartecfd03c2010-02-12 14:41:27 -050017982 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
17983 * @phba: pointer to lpfc hba data structure.
17984 *
17985 * This routine is the completion routine for the rediscover FCF table mailbox
17986 * command. If the mailbox command returned failure, it will try to stop the
17987 * FCF rediscover wait timer.
17988 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017989static void
James Smartecfd03c2010-02-12 14:41:27 -050017990lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
17991{
17992 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17993 uint32_t shdr_status, shdr_add_status;
17994
17995 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17996
17997 shdr_status = bf_get(lpfc_mbox_hdr_status,
17998 &redisc_fcf->header.cfg_shdr.response);
17999 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18000 &redisc_fcf->header.cfg_shdr.response);
18001 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018002 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018003 "2746 Requesting for FCF rediscovery failed "
18004 "status x%x add_status x%x\n",
18005 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018006 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018007 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018008 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018009 spin_unlock_irq(&phba->hbalock);
18010 /*
18011 * CVL event triggered FCF rediscover request failed,
18012 * last resort to re-try current registered FCF entry.
18013 */
18014 lpfc_retry_pport_discovery(phba);
18015 } else {
18016 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018017 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018018 spin_unlock_irq(&phba->hbalock);
18019 /*
18020 * DEAD FCF event triggered FCF rediscover request
18021 * failed, last resort to fail over as a link down
18022 * to FCF registration.
18023 */
18024 lpfc_sli4_fcf_dead_failthrough(phba);
18025 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018026 } else {
18027 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018028 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018029 /*
18030 * Start FCF rediscovery wait timer for pending FCF
18031 * before rescan FCF record table.
18032 */
18033 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018034 }
James Smartecfd03c2010-02-12 14:41:27 -050018035
18036 mempool_free(mbox, phba->mbox_mem_pool);
18037}
18038
18039/**
James Smart3804dc82010-07-14 15:31:37 -040018040 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018041 * @phba: pointer to lpfc hba data structure.
18042 *
18043 * This routine is invoked to request for rediscovery of the entire FCF table
18044 * by the port.
18045 **/
18046int
18047lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18048{
18049 LPFC_MBOXQ_t *mbox;
18050 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18051 int rc, length;
18052
James Smart0c9ab6f2010-02-26 14:15:57 -050018053 /* Cancel retry delay timers to all vports before FCF rediscover */
18054 lpfc_cancel_all_vport_retry_delay_timer(phba);
18055
James Smartecfd03c2010-02-12 14:41:27 -050018056 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18057 if (!mbox) {
18058 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18059 "2745 Failed to allocate mbox for "
18060 "requesting FCF rediscover.\n");
18061 return -ENOMEM;
18062 }
18063
18064 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18065 sizeof(struct lpfc_sli4_cfg_mhdr));
18066 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18067 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18068 length, LPFC_SLI4_MBX_EMBED);
18069
18070 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18071 /* Set count to 0 for invalidating the entire FCF database */
18072 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18073
18074 /* Issue the mailbox command asynchronously */
18075 mbox->vport = phba->pport;
18076 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18077 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18078
18079 if (rc == MBX_NOT_FINISHED) {
18080 mempool_free(mbox, phba->mbox_mem_pool);
18081 return -EIO;
18082 }
18083 return 0;
18084}
18085
18086/**
James Smartfc2b9892010-02-26 14:15:29 -050018087 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18088 * @phba: pointer to lpfc hba data structure.
18089 *
18090 * This function is the failover routine as a last resort to the FCF DEAD
18091 * event when driver failed to perform fast FCF failover.
18092 **/
18093void
18094lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18095{
18096 uint32_t link_state;
18097
18098 /*
18099 * Last resort as FCF DEAD event failover will treat this as
18100 * a link down, but save the link state because we don't want
18101 * it to be changed to Link Down unless it is already down.
18102 */
18103 link_state = phba->link_state;
18104 lpfc_linkdown(phba);
18105 phba->link_state = link_state;
18106
18107 /* Unregister FCF if no devices connected to it */
18108 lpfc_unregister_unused_fcf(phba);
18109}
18110
18111/**
James Smart026abb82011-12-13 13:20:45 -050018112 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018113 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018114 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018115 *
James Smart026abb82011-12-13 13:20:45 -050018116 * This function gets SLI3 port configure region 23 data through memory dump
18117 * mailbox command. When it successfully retrieves data, the size of the data
18118 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018119 **/
James Smart026abb82011-12-13 13:20:45 -050018120static uint32_t
18121lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018122{
18123 LPFC_MBOXQ_t *pmb = NULL;
18124 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018125 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018126 int rc;
18127
James Smart026abb82011-12-13 13:20:45 -050018128 if (!rgn23_data)
18129 return 0;
18130
James Smarta0c87cb2009-07-19 10:01:10 -040018131 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18132 if (!pmb) {
18133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018134 "2600 failed to allocate mailbox memory\n");
18135 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018136 }
18137 mb = &pmb->u.mb;
18138
James Smarta0c87cb2009-07-19 10:01:10 -040018139 do {
18140 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18141 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18142
18143 if (rc != MBX_SUCCESS) {
18144 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018145 "2601 failed to read config "
18146 "region 23, rc 0x%x Status 0x%x\n",
18147 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018148 mb->un.varDmp.word_cnt = 0;
18149 }
18150 /*
18151 * dump mem may return a zero when finished or we got a
18152 * mailbox error, either way we are done.
18153 */
18154 if (mb->un.varDmp.word_cnt == 0)
18155 break;
18156 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18157 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18158
18159 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018160 rgn23_data + offset,
18161 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018162 offset += mb->un.varDmp.word_cnt;
18163 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18164
James Smart026abb82011-12-13 13:20:45 -050018165 mempool_free(pmb, phba->mbox_mem_pool);
18166 return offset;
18167}
18168
18169/**
18170 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18171 * @phba: pointer to lpfc hba data structure.
18172 * @rgn23_data: pointer to configure region 23 data.
18173 *
18174 * This function gets SLI4 port configure region 23 data through memory dump
18175 * mailbox command. When it successfully retrieves data, the size of the data
18176 * will be returned, otherwise, 0 will be returned.
18177 **/
18178static uint32_t
18179lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18180{
18181 LPFC_MBOXQ_t *mboxq = NULL;
18182 struct lpfc_dmabuf *mp = NULL;
18183 struct lpfc_mqe *mqe;
18184 uint32_t data_length = 0;
18185 int rc;
18186
18187 if (!rgn23_data)
18188 return 0;
18189
18190 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18191 if (!mboxq) {
18192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18193 "3105 failed to allocate mailbox memory\n");
18194 return 0;
18195 }
18196
18197 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18198 goto out;
18199 mqe = &mboxq->u.mqe;
18200 mp = (struct lpfc_dmabuf *) mboxq->context1;
18201 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18202 if (rc)
18203 goto out;
18204 data_length = mqe->un.mb_words[5];
18205 if (data_length == 0)
18206 goto out;
18207 if (data_length > DMP_RGN23_SIZE) {
18208 data_length = 0;
18209 goto out;
18210 }
18211 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18212out:
18213 mempool_free(mboxq, phba->mbox_mem_pool);
18214 if (mp) {
18215 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18216 kfree(mp);
18217 }
18218 return data_length;
18219}
18220
18221/**
18222 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18223 * @phba: pointer to lpfc hba data structure.
18224 *
18225 * This function read region 23 and parse TLV for port status to
18226 * decide if the user disaled the port. If the TLV indicates the
18227 * port is disabled, the hba_flag is set accordingly.
18228 **/
18229void
18230lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18231{
18232 uint8_t *rgn23_data = NULL;
18233 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18234 uint32_t offset = 0;
18235
18236 /* Get adapter Region 23 data */
18237 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18238 if (!rgn23_data)
18239 goto out;
18240
18241 if (phba->sli_rev < LPFC_SLI_REV4)
18242 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18243 else {
18244 if_type = bf_get(lpfc_sli_intf_if_type,
18245 &phba->sli4_hba.sli_intf);
18246 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18247 goto out;
18248 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18249 }
James Smarta0c87cb2009-07-19 10:01:10 -040018250
18251 if (!data_size)
18252 goto out;
18253
18254 /* Check the region signature first */
18255 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18257 "2619 Config region 23 has bad signature\n");
18258 goto out;
18259 }
18260 offset += 4;
18261
18262 /* Check the data structure version */
18263 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18265 "2620 Config region 23 has bad version\n");
18266 goto out;
18267 }
18268 offset += 4;
18269
18270 /* Parse TLV entries in the region */
18271 while (offset < data_size) {
18272 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18273 break;
18274 /*
18275 * If the TLV is not driver specific TLV or driver id is
18276 * not linux driver id, skip the record.
18277 */
18278 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18279 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18280 (rgn23_data[offset + 3] != 0)) {
18281 offset += rgn23_data[offset + 1] * 4 + 4;
18282 continue;
18283 }
18284
18285 /* Driver found a driver specific TLV in the config region */
18286 sub_tlv_len = rgn23_data[offset + 1] * 4;
18287 offset += 4;
18288 tlv_offset = 0;
18289
18290 /*
18291 * Search for configured port state sub-TLV.
18292 */
18293 while ((offset < data_size) &&
18294 (tlv_offset < sub_tlv_len)) {
18295 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18296 offset += 4;
18297 tlv_offset += 4;
18298 break;
18299 }
18300 if (rgn23_data[offset] != PORT_STE_TYPE) {
18301 offset += rgn23_data[offset + 1] * 4 + 4;
18302 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18303 continue;
18304 }
18305
18306 /* This HBA contains PORT_STE configured */
18307 if (!rgn23_data[offset + 2])
18308 phba->hba_flag |= LINK_DISABLED;
18309
18310 goto out;
18311 }
18312 }
James Smart026abb82011-12-13 13:20:45 -050018313
James Smarta0c87cb2009-07-19 10:01:10 -040018314out:
James Smarta0c87cb2009-07-19 10:01:10 -040018315 kfree(rgn23_data);
18316 return;
18317}
James Smart695a8142010-01-26 23:08:03 -050018318
18319/**
James Smart52d52442011-05-24 11:42:45 -040018320 * lpfc_wr_object - write an object to the firmware
18321 * @phba: HBA structure that indicates port to create a queue on.
18322 * @dmabuf_list: list of dmabufs to write to the port.
18323 * @size: the total byte value of the objects to write to the port.
18324 * @offset: the current offset to be used to start the transfer.
18325 *
18326 * This routine will create a wr_object mailbox command to send to the port.
18327 * the mailbox command will be constructed using the dma buffers described in
18328 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18329 * BDEs that the imbedded mailbox can support. The @offset variable will be
18330 * used to indicate the starting offset of the transfer and will also return
18331 * the offset after the write object mailbox has completed. @size is used to
18332 * determine the end of the object and whether the eof bit should be set.
18333 *
18334 * Return 0 is successful and offset will contain the the new offset to use
18335 * for the next write.
18336 * Return negative value for error cases.
18337 **/
18338int
18339lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18340 uint32_t size, uint32_t *offset)
18341{
18342 struct lpfc_mbx_wr_object *wr_object;
18343 LPFC_MBOXQ_t *mbox;
18344 int rc = 0, i = 0;
18345 uint32_t shdr_status, shdr_add_status;
18346 uint32_t mbox_tmo;
18347 union lpfc_sli4_cfg_shdr *shdr;
18348 struct lpfc_dmabuf *dmabuf;
18349 uint32_t written = 0;
18350
18351 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18352 if (!mbox)
18353 return -ENOMEM;
18354
18355 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18356 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18357 sizeof(struct lpfc_mbx_wr_object) -
18358 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18359
18360 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18361 wr_object->u.request.write_offset = *offset;
18362 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18363 wr_object->u.request.object_name[0] =
18364 cpu_to_le32(wr_object->u.request.object_name[0]);
18365 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18366 list_for_each_entry(dmabuf, dmabuf_list, list) {
18367 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18368 break;
18369 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18370 wr_object->u.request.bde[i].addrHigh =
18371 putPaddrHigh(dmabuf->phys);
18372 if (written + SLI4_PAGE_SIZE >= size) {
18373 wr_object->u.request.bde[i].tus.f.bdeSize =
18374 (size - written);
18375 written += (size - written);
18376 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18377 } else {
18378 wr_object->u.request.bde[i].tus.f.bdeSize =
18379 SLI4_PAGE_SIZE;
18380 written += SLI4_PAGE_SIZE;
18381 }
18382 i++;
18383 }
18384 wr_object->u.request.bde_count = i;
18385 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18386 if (!phba->sli4_hba.intr_enable)
18387 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18388 else {
James Smarta183a152011-10-10 21:32:43 -040018389 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018390 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18391 }
18392 /* The IOCTL status is embedded in the mailbox subheader. */
18393 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18394 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18395 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18396 if (rc != MBX_TIMEOUT)
18397 mempool_free(mbox, phba->mbox_mem_pool);
18398 if (shdr_status || shdr_add_status || rc) {
18399 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18400 "3025 Write Object mailbox failed with "
18401 "status x%x add_status x%x, mbx status x%x\n",
18402 shdr_status, shdr_add_status, rc);
18403 rc = -ENXIO;
18404 } else
18405 *offset += wr_object->u.response.actual_write_length;
18406 return rc;
18407}
18408
18409/**
James Smart695a8142010-01-26 23:08:03 -050018410 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18411 * @vport: pointer to vport data structure.
18412 *
18413 * This function iterate through the mailboxq and clean up all REG_LOGIN
18414 * and REG_VPI mailbox commands associated with the vport. This function
18415 * is called when driver want to restart discovery of the vport due to
18416 * a Clear Virtual Link event.
18417 **/
18418void
18419lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18420{
18421 struct lpfc_hba *phba = vport->phba;
18422 LPFC_MBOXQ_t *mb, *nextmb;
18423 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018424 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018425 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018426 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018427 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018428 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018429
James Smartd439d282010-09-29 11:18:45 -040018430 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018431 spin_lock_irq(&phba->hbalock);
18432 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18433 if (mb->vport != vport)
18434 continue;
18435
18436 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18437 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18438 continue;
18439
James Smartd439d282010-09-29 11:18:45 -040018440 list_del(&mb->list);
18441 list_add_tail(&mb->list, &mbox_cmd_list);
18442 }
18443 /* Clean up active mailbox command with the vport */
18444 mb = phba->sli.mbox_active;
18445 if (mb && (mb->vport == vport)) {
18446 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18447 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18448 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18449 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18450 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18451 /* Put reference count for delayed processing */
18452 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18453 /* Unregister the RPI when mailbox complete */
18454 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18455 }
18456 }
James Smart63e801c2010-11-20 23:14:19 -050018457 /* Cleanup any mailbox completions which are not yet processed */
18458 do {
18459 restart_loop = 0;
18460 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18461 /*
18462 * If this mailox is already processed or it is
18463 * for another vport ignore it.
18464 */
18465 if ((mb->vport != vport) ||
18466 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18467 continue;
18468
18469 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18470 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18471 continue;
18472
18473 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18474 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18475 ndlp = (struct lpfc_nodelist *)mb->context2;
18476 /* Unregister the RPI when mailbox complete */
18477 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18478 restart_loop = 1;
18479 spin_unlock_irq(&phba->hbalock);
18480 spin_lock(shost->host_lock);
18481 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18482 spin_unlock(shost->host_lock);
18483 spin_lock_irq(&phba->hbalock);
18484 break;
18485 }
18486 }
18487 } while (restart_loop);
18488
James Smartd439d282010-09-29 11:18:45 -040018489 spin_unlock_irq(&phba->hbalock);
18490
18491 /* Release the cleaned-up mailbox commands */
18492 while (!list_empty(&mbox_cmd_list)) {
18493 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018494 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18495 mp = (struct lpfc_dmabuf *) (mb->context1);
18496 if (mp) {
18497 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18498 kfree(mp);
18499 }
James Smart78730cf2010-04-06 15:06:30 -040018500 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018501 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018502 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018503 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018504 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018505 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018506 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018507 }
James Smart695a8142010-01-26 23:08:03 -050018508 }
James Smart695a8142010-01-26 23:08:03 -050018509 mempool_free(mb, phba->mbox_mem_pool);
18510 }
James Smartd439d282010-09-29 11:18:45 -040018511
18512 /* Release the ndlp with the cleaned-up active mailbox command */
18513 if (act_mbx_ndlp) {
18514 spin_lock(shost->host_lock);
18515 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18516 spin_unlock(shost->host_lock);
18517 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050018518 }
James Smart695a8142010-01-26 23:08:03 -050018519}
18520
James Smart2a9bf3d2010-06-07 15:24:45 -040018521/**
18522 * lpfc_drain_txq - Drain the txq
18523 * @phba: Pointer to HBA context object.
18524 *
18525 * This function attempt to submit IOCBs on the txq
18526 * to the adapter. For SLI4 adapters, the txq contains
18527 * ELS IOCBs that have been deferred because the there
18528 * are no SGLs. This congestion can occur with large
18529 * vport counts during node discovery.
18530 **/
18531
18532uint32_t
18533lpfc_drain_txq(struct lpfc_hba *phba)
18534{
18535 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080018536 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090018537 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040018538 unsigned long iflags = 0;
18539 char *fail_msg = NULL;
18540 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080018541 union lpfc_wqe128 wqe128;
18542 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040018543 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040018544
James Smart895427b2017-02-12 13:52:30 -080018545 pring = lpfc_phba_elsring(phba);
18546
James Smart398d81c2013-05-31 17:04:19 -040018547 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050018548 list_for_each_entry(piocbq, &pring->txq, list) {
18549 txq_cnt++;
18550 }
18551
18552 if (txq_cnt > pring->txq_max)
18553 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018554
James Smart398d81c2013-05-31 17:04:19 -040018555 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018556
James Smart0e9bb8d2013-03-01 16:35:12 -050018557 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040018558 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018559
James Smart19ca7602010-11-20 23:11:55 -050018560 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040018561 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040018562 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040018563 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18564 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050018565 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040018566 break;
18567 }
James Smart895427b2017-02-12 13:52:30 -080018568 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040018569 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050018570 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040018571 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018572 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040018573 }
James Smart0e9bb8d2013-03-01 16:35:12 -050018574 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040018575
18576 /* The xri and iocb resources secured,
18577 * attempt to issue request
18578 */
James Smart6d368e52011-05-24 11:44:12 -040018579 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040018580 piocbq->sli4_xritag = sglq->sli4_xritag;
18581 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18582 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080018583 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018584 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080018585 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018586 fail_msg = " - Wq is full";
18587 else
18588 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18589
18590 if (fail_msg) {
18591 /* Failed means we can't issue and need to cancel */
18592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18593 "2822 IOCB failed %s iotag 0x%x "
18594 "xri 0x%x\n",
18595 fail_msg,
18596 piocbq->iotag, piocbq->sli4_xritag);
18597 list_add_tail(&piocbq->list, &completions);
18598 }
James Smart398d81c2013-05-31 17:04:19 -040018599 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018600 }
18601
James Smart2a9bf3d2010-06-07 15:24:45 -040018602 /* Cancel all the IOCBs that cannot be issued */
18603 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18604 IOERR_SLI_ABORTED);
18605
James Smart0e9bb8d2013-03-01 16:35:12 -050018606 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018607}
James Smart895427b2017-02-12 13:52:30 -080018608
18609/**
18610 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18611 * @phba: Pointer to HBA context object.
18612 * @pwqe: Pointer to command WQE.
18613 * @sglq: Pointer to the scatter gather queue object.
18614 *
18615 * This routine converts the bpl or bde that is in the WQE
18616 * to a sgl list for the sli4 hardware. The physical address
18617 * of the bpl/bde is converted back to a virtual address.
18618 * If the WQE contains a BPL then the list of BDE's is
18619 * converted to sli4_sge's. If the WQE contains a single
18620 * BDE then it is converted to a single sli_sge.
18621 * The WQE is still in cpu endianness so the contents of
18622 * the bpl can be used without byte swapping.
18623 *
18624 * Returns valid XRI = Success, NO_XRI = Failure.
18625 */
18626static uint16_t
18627lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18628 struct lpfc_sglq *sglq)
18629{
18630 uint16_t xritag = NO_XRI;
18631 struct ulp_bde64 *bpl = NULL;
18632 struct ulp_bde64 bde;
18633 struct sli4_sge *sgl = NULL;
18634 struct lpfc_dmabuf *dmabuf;
18635 union lpfc_wqe *wqe;
18636 int numBdes = 0;
18637 int i = 0;
18638 uint32_t offset = 0; /* accumulated offset in the sg request list */
18639 int inbound = 0; /* number of sg reply entries inbound from firmware */
18640 uint32_t cmd;
18641
18642 if (!pwqeq || !sglq)
18643 return xritag;
18644
18645 sgl = (struct sli4_sge *)sglq->sgl;
18646 wqe = &pwqeq->wqe;
18647 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18648
18649 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18650 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18651 return sglq->sli4_xritag;
18652 numBdes = pwqeq->rsvd2;
18653 if (numBdes) {
18654 /* The addrHigh and addrLow fields within the WQE
18655 * have not been byteswapped yet so there is no
18656 * need to swap them back.
18657 */
18658 if (pwqeq->context3)
18659 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18660 else
18661 return xritag;
18662
18663 bpl = (struct ulp_bde64 *)dmabuf->virt;
18664 if (!bpl)
18665 return xritag;
18666
18667 for (i = 0; i < numBdes; i++) {
18668 /* Should already be byte swapped. */
18669 sgl->addr_hi = bpl->addrHigh;
18670 sgl->addr_lo = bpl->addrLow;
18671
18672 sgl->word2 = le32_to_cpu(sgl->word2);
18673 if ((i+1) == numBdes)
18674 bf_set(lpfc_sli4_sge_last, sgl, 1);
18675 else
18676 bf_set(lpfc_sli4_sge_last, sgl, 0);
18677 /* swap the size field back to the cpu so we
18678 * can assign it to the sgl.
18679 */
18680 bde.tus.w = le32_to_cpu(bpl->tus.w);
18681 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18682 /* The offsets in the sgl need to be accumulated
18683 * separately for the request and reply lists.
18684 * The request is always first, the reply follows.
18685 */
18686 switch (cmd) {
18687 case CMD_GEN_REQUEST64_WQE:
18688 /* add up the reply sg entries */
18689 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18690 inbound++;
18691 /* first inbound? reset the offset */
18692 if (inbound == 1)
18693 offset = 0;
18694 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18695 bf_set(lpfc_sli4_sge_type, sgl,
18696 LPFC_SGE_TYPE_DATA);
18697 offset += bde.tus.f.bdeSize;
18698 break;
18699 case CMD_FCP_TRSP64_WQE:
18700 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18701 bf_set(lpfc_sli4_sge_type, sgl,
18702 LPFC_SGE_TYPE_DATA);
18703 break;
18704 case CMD_FCP_TSEND64_WQE:
18705 case CMD_FCP_TRECEIVE64_WQE:
18706 bf_set(lpfc_sli4_sge_type, sgl,
18707 bpl->tus.f.bdeFlags);
18708 if (i < 3)
18709 offset = 0;
18710 else
18711 offset += bde.tus.f.bdeSize;
18712 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18713 break;
18714 }
18715 sgl->word2 = cpu_to_le32(sgl->word2);
18716 bpl++;
18717 sgl++;
18718 }
18719 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18720 /* The addrHigh and addrLow fields of the BDE have not
18721 * been byteswapped yet so they need to be swapped
18722 * before putting them in the sgl.
18723 */
18724 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18725 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18726 sgl->word2 = le32_to_cpu(sgl->word2);
18727 bf_set(lpfc_sli4_sge_last, sgl, 1);
18728 sgl->word2 = cpu_to_le32(sgl->word2);
18729 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18730 }
18731 return sglq->sli4_xritag;
18732}
18733
18734/**
18735 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18736 * @phba: Pointer to HBA context object.
18737 * @ring_number: Base sli ring number
18738 * @pwqe: Pointer to command WQE.
18739 **/
18740int
18741lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18742 struct lpfc_iocbq *pwqe)
18743{
18744 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080018745 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080018746 struct lpfc_queue *wq;
18747 struct lpfc_sglq *sglq;
18748 struct lpfc_sli_ring *pring;
18749 unsigned long iflags;
18750
18751 /* NVME_LS and NVME_LS ABTS requests. */
18752 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18753 pring = phba->sli4_hba.nvmels_wq->pring;
18754 spin_lock_irqsave(&pring->ring_lock, iflags);
18755 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18756 if (!sglq) {
18757 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18758 return WQE_BUSY;
18759 }
18760 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18761 pwqe->sli4_xritag = sglq->sli4_xritag;
18762 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18763 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18764 return WQE_ERROR;
18765 }
18766 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18767 pwqe->sli4_xritag);
18768 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
18769 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18770 return WQE_ERROR;
18771 }
18772 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18773 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18774 return 0;
18775 }
18776
18777 /* NVME_FCREQ and NVME_ABTS requests */
18778 if (pwqe->iocb_flag & LPFC_IO_NVME) {
18779 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18780 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18781
18782 spin_lock_irqsave(&pring->ring_lock, iflags);
18783 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18784 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18785 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18786 if (lpfc_sli4_wq_put(wq, wqe)) {
18787 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18788 return WQE_ERROR;
18789 }
18790 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18791 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18792 return 0;
18793 }
18794
James Smartf358dd02017-02-12 13:52:34 -080018795 /* NVMET requests */
18796 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
18797 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18798 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18799
18800 spin_lock_irqsave(&pring->ring_lock, iflags);
18801 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070018802 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080018803 if (pwqe->sli4_xritag == NO_XRI) {
18804 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18805 pwqe->sli4_xritag = sglq->sli4_xritag;
18806 }
18807 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18808 pwqe->sli4_xritag);
18809 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18810 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18811 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18812 if (lpfc_sli4_wq_put(wq, wqe)) {
18813 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18814 return WQE_ERROR;
18815 }
18816 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18817 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18818 return 0;
18819 }
James Smart895427b2017-02-12 13:52:30 -080018820 return WQE_ERROR;
18821}