blob: 8a606d0d2c795da02a179cd5c407a6d1efdf3376 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd080abe2017-02-12 13:52:39 -08004 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010029#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050030
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040031#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050032#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040035#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040036#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040037#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050038
James Smart895427b2017-02-12 13:52:30 -080039#include <linux/nvme-fc-driver.h>
40
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_hw.h"
43#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040045#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080048#include "lpfc_scsi.h"
49#include "lpfc_nvme.h"
James Smartf358dd02017-02-12 13:52:34 -080050#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050051#include "lpfc_crtn.h"
52#include "lpfc_logmsg.h"
53#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050054#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040055#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070056#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050057
58/* There are only four IOCB completion types. */
59typedef enum _lpfc_iocb_type {
60 LPFC_UNKNOWN_IOCB,
61 LPFC_UNSOL_IOCB,
62 LPFC_SOL_IOCB,
63 LPFC_ABORT_IOCB
64} lpfc_iocb_type;
65
James Smart4f774512009-05-22 14:52:35 -040066
67/* Provide function prototypes local to this module. */
68static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
69 uint32_t);
70static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040071 uint8_t *, uint32_t *);
72static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
73 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040074static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
75 struct hbq_dmabuf *);
James Smart895427b2017-02-12 13:52:30 -080076static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
James Smart05580562011-05-24 11:40:48 -040077 struct lpfc_cqe *);
James Smart895427b2017-02-12 13:52:30 -080078static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040079 int);
James Smartba20c852012-08-03 12:36:52 -040080static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
81 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040082static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
83static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart895427b2017-02-12 13:52:30 -080084static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
85 struct lpfc_sli_ring *pring,
86 struct lpfc_iocbq *cmdiocb);
James Smart05580562011-05-24 11:40:48 -040087
James Smart4f774512009-05-22 14:52:35 -040088static IOCB_t *
89lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
90{
91 return &iocbq->iocb;
92}
93
94/**
95 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
96 * @q: The Work Queue to operate on.
97 * @wqe: The work Queue Entry to put on the Work queue.
98 *
99 * This routine will copy the contents of @wqe to the next available entry on
100 * the @q. This function will then ring the Work Queue Doorbell to signal the
101 * HBA to start processing the Work Queue Entry. This function returns 0 if
102 * successful. If no entries are available on @q then this function will return
103 * -ENOMEM.
104 * The caller is expected to hold the hbalock when calling this routine.
105 **/
106static uint32_t
107lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
108{
James Smart2e90f4b2011-12-13 13:22:37 -0500109 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400110 struct lpfc_register doorbell;
111 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400112 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400113
James Smart2e90f4b2011-12-13 13:22:37 -0500114 /* sanity check on queue memory */
115 if (unlikely(!q))
116 return -ENOMEM;
117 temp_wqe = q->qe[q->host_index].wqe;
118
James Smart4f774512009-05-22 14:52:35 -0400119 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400120 idx = ((q->host_index + 1) % q->entry_count);
121 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400122 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400123 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400124 }
125 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400126 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500127 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400128 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500129 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
130 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400131 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart6b3b3bd2016-12-19 15:07:30 -0800132 /* ensure WQE bcopy flushed before doorbell write */
133 wmb();
James Smart4f774512009-05-22 14:52:35 -0400134
135 /* Update the host index before invoking device */
136 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400137
138 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400139
140 /* Ring Doorbell */
141 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500142 if (q->db_format == LPFC_DB_LIST_FORMAT) {
143 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
144 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
145 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
146 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
147 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
148 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
149 } else {
150 return -EINVAL;
151 }
152 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400153
154 return 0;
155}
156
157/**
158 * lpfc_sli4_wq_release - Updates internal hba index for WQ
159 * @q: The Work Queue to operate on.
160 * @index: The index to advance the hba index to.
161 *
162 * This routine will update the HBA index of a queue to reflect consumption of
163 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
164 * an entry the host calls this function to update the queue's internal
165 * pointers. This routine returns the number of entries that were consumed by
166 * the HBA.
167 **/
168static uint32_t
169lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
170{
171 uint32_t released = 0;
172
James Smart2e90f4b2011-12-13 13:22:37 -0500173 /* sanity check on queue memory */
174 if (unlikely(!q))
175 return 0;
176
James Smart4f774512009-05-22 14:52:35 -0400177 if (q->hba_index == index)
178 return 0;
179 do {
180 q->hba_index = ((q->hba_index + 1) % q->entry_count);
181 released++;
182 } while (q->hba_index != index);
183 return released;
184}
185
186/**
187 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
188 * @q: The Mailbox Queue to operate on.
189 * @wqe: The Mailbox Queue Entry to put on the Work queue.
190 *
191 * This routine will copy the contents of @mqe to the next available entry on
192 * the @q. This function will then ring the Work Queue Doorbell to signal the
193 * HBA to start processing the Work Queue Entry. This function returns 0 if
194 * successful. If no entries are available on @q then this function will return
195 * -ENOMEM.
196 * The caller is expected to hold the hbalock when calling this routine.
197 **/
198static uint32_t
199lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
200{
James Smart2e90f4b2011-12-13 13:22:37 -0500201 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400202 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400203
James Smart2e90f4b2011-12-13 13:22:37 -0500204 /* sanity check on queue memory */
205 if (unlikely(!q))
206 return -ENOMEM;
207 temp_mqe = q->qe[q->host_index].mqe;
208
James Smart4f774512009-05-22 14:52:35 -0400209 /* If the host has not yet processed the next entry then we are done */
210 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
211 return -ENOMEM;
212 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
213 /* Save off the mailbox pointer for completion */
214 q->phba->mbox = (MAILBOX_t *)temp_mqe;
215
216 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400217 q->host_index = ((q->host_index + 1) % q->entry_count);
218
219 /* Ring Doorbell */
220 doorbell.word0 = 0;
221 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
222 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
223 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400224 return 0;
225}
226
227/**
228 * lpfc_sli4_mq_release - Updates internal hba index for MQ
229 * @q: The Mailbox Queue to operate on.
230 *
231 * This routine will update the HBA index of a queue to reflect consumption of
232 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
233 * an entry the host calls this function to update the queue's internal
234 * pointers. This routine returns the number of entries that were consumed by
235 * the HBA.
236 **/
237static uint32_t
238lpfc_sli4_mq_release(struct lpfc_queue *q)
239{
James Smart2e90f4b2011-12-13 13:22:37 -0500240 /* sanity check on queue memory */
241 if (unlikely(!q))
242 return 0;
243
James Smart4f774512009-05-22 14:52:35 -0400244 /* Clear the mailbox pointer for completion */
245 q->phba->mbox = NULL;
246 q->hba_index = ((q->hba_index + 1) % q->entry_count);
247 return 1;
248}
249
250/**
251 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
252 * @q: The Event Queue to get the first valid EQE from
253 *
254 * This routine will get the first valid Event Queue Entry from @q, update
255 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
256 * the Queue (no more work to do), or the Queue is full of EQEs that have been
257 * processed, but not popped back to the HBA then this routine will return NULL.
258 **/
259static struct lpfc_eqe *
260lpfc_sli4_eq_get(struct lpfc_queue *q)
261{
James Smart2e90f4b2011-12-13 13:22:37 -0500262 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400263 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500264
265 /* sanity check on queue memory */
266 if (unlikely(!q))
267 return NULL;
268 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400269
270 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400271 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400272 return NULL;
273 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400274 idx = ((q->hba_index + 1) % q->entry_count);
275 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400276 return NULL;
277
James Smart027140e2012-08-03 12:35:44 -0400278 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400279
280 /*
281 * insert barrier for instruction interlock : data from the hardware
282 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800283 * upon. Speculative instructions were allowing a bcopy at the start
284 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
285 * after our return, to copy data before the valid bit check above
286 * was done. As such, some of the copied data was stale. The barrier
287 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400288 */
289 mb();
James Smart4f774512009-05-22 14:52:35 -0400290 return eqe;
291}
292
293/**
James Smartba20c852012-08-03 12:36:52 -0400294 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
295 * @q: The Event Queue to disable interrupts
296 *
297 **/
298static inline void
299lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
300{
301 struct lpfc_register doorbell;
302
303 doorbell.word0 = 0;
304 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
305 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
306 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
307 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
308 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
309 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
310}
311
312/**
James Smart4f774512009-05-22 14:52:35 -0400313 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
314 * @q: The Event Queue that the host has completed processing for.
315 * @arm: Indicates whether the host wants to arms this CQ.
316 *
317 * This routine will mark all Event Queue Entries on @q, from the last
318 * known completed entry to the last entry that was processed, as completed
319 * by clearing the valid bit for each completion queue entry. Then it will
320 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
321 * The internal host index in the @q will be updated by this routine to indicate
322 * that the host has finished processing the entries. The @arm parameter
323 * indicates that the queue should be rearmed when ringing the doorbell.
324 *
325 * This function will return the number of EQEs that were popped.
326 **/
327uint32_t
328lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
329{
330 uint32_t released = 0;
331 struct lpfc_eqe *temp_eqe;
332 struct lpfc_register doorbell;
333
James Smart2e90f4b2011-12-13 13:22:37 -0500334 /* sanity check on queue memory */
335 if (unlikely(!q))
336 return 0;
337
James Smart4f774512009-05-22 14:52:35 -0400338 /* while there are valid entries */
339 while (q->hba_index != q->host_index) {
340 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400341 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400342 released++;
343 q->host_index = ((q->host_index + 1) % q->entry_count);
344 }
345 if (unlikely(released == 0 && !arm))
346 return 0;
347
348 /* ring doorbell for number popped */
349 doorbell.word0 = 0;
350 if (arm) {
351 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
352 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
353 }
354 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
355 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500356 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
357 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
358 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400359 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500360 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
361 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
362 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400363 return released;
364}
365
366/**
367 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
368 * @q: The Completion Queue to get the first valid CQE from
369 *
370 * This routine will get the first valid Completion Queue Entry from @q, update
371 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
372 * the Queue (no more work to do), or the Queue is full of CQEs that have been
373 * processed, but not popped back to the HBA then this routine will return NULL.
374 **/
375static struct lpfc_cqe *
376lpfc_sli4_cq_get(struct lpfc_queue *q)
377{
378 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400379 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400380
James Smart2e90f4b2011-12-13 13:22:37 -0500381 /* sanity check on queue memory */
382 if (unlikely(!q))
383 return NULL;
384
James Smart4f774512009-05-22 14:52:35 -0400385 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400386 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400387 return NULL;
388 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400389 idx = ((q->hba_index + 1) % q->entry_count);
390 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400391 return NULL;
392
393 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400394 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400395
396 /*
397 * insert barrier for instruction interlock : data from the hardware
398 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800399 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
400 * instructions allowing action on content before valid bit checked,
401 * add barrier here as well. May not be needed as "content" is a
402 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400403 */
404 mb();
James Smart4f774512009-05-22 14:52:35 -0400405 return cqe;
406}
407
408/**
409 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
410 * @q: The Completion Queue that the host has completed processing for.
411 * @arm: Indicates whether the host wants to arms this CQ.
412 *
413 * This routine will mark all Completion queue entries on @q, from the last
414 * known completed entry to the last entry that was processed, as completed
415 * by clearing the valid bit for each completion queue entry. Then it will
416 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
417 * The internal host index in the @q will be updated by this routine to indicate
418 * that the host has finished processing the entries. The @arm parameter
419 * indicates that the queue should be rearmed when ringing the doorbell.
420 *
421 * This function will return the number of CQEs that were released.
422 **/
423uint32_t
424lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
425{
426 uint32_t released = 0;
427 struct lpfc_cqe *temp_qe;
428 struct lpfc_register doorbell;
429
James Smart2e90f4b2011-12-13 13:22:37 -0500430 /* sanity check on queue memory */
431 if (unlikely(!q))
432 return 0;
James Smart4f774512009-05-22 14:52:35 -0400433 /* while there are valid entries */
434 while (q->hba_index != q->host_index) {
435 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400436 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400437 released++;
438 q->host_index = ((q->host_index + 1) % q->entry_count);
439 }
440 if (unlikely(released == 0 && !arm))
441 return 0;
442
443 /* ring doorbell for number popped */
444 doorbell.word0 = 0;
445 if (arm)
446 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
447 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
448 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500449 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
450 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
451 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400452 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
453 return released;
454}
455
456/**
457 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
458 * @q: The Header Receive Queue to operate on.
459 * @wqe: The Receive Queue Entry to put on the Receive queue.
460 *
461 * This routine will copy the contents of @wqe to the next available entry on
462 * the @q. This function will then ring the Receive Queue Doorbell to signal the
463 * HBA to start processing the Receive Queue Entry. This function returns the
464 * index that the rqe was copied to if successful. If no entries are available
465 * on @q then this function will return -ENOMEM.
466 * The caller is expected to hold the hbalock when calling this routine.
467 **/
James Smart895427b2017-02-12 13:52:30 -0800468int
James Smart4f774512009-05-22 14:52:35 -0400469lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
470 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
471{
James Smart2e90f4b2011-12-13 13:22:37 -0500472 struct lpfc_rqe *temp_hrqe;
473 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400474 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500475 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400476
James Smart2e90f4b2011-12-13 13:22:37 -0500477 /* sanity check on queue memory */
478 if (unlikely(!hq) || unlikely(!dq))
479 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500480 put_index = hq->host_index;
James Smart2e90f4b2011-12-13 13:22:37 -0500481 temp_hrqe = hq->qe[hq->host_index].rqe;
482 temp_drqe = dq->qe[dq->host_index].rqe;
483
James Smart4f774512009-05-22 14:52:35 -0400484 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
485 return -EINVAL;
486 if (hq->host_index != dq->host_index)
487 return -EINVAL;
488 /* If the host has not yet processed the next entry then we are done */
489 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
490 return -EBUSY;
491 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
492 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
493
494 /* Update the host index to point to the next slot */
495 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
496 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
497
498 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400499 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400500 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500501 if (hq->db_format == LPFC_DB_RING_FORMAT) {
502 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
503 hq->entry_repost);
504 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
505 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
506 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
507 hq->entry_repost);
508 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
509 hq->host_index);
510 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
511 } else {
512 return -EINVAL;
513 }
514 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400515 }
516 return put_index;
517}
518
519/**
520 * lpfc_sli4_rq_release - Updates internal hba index for RQ
521 * @q: The Header Receive Queue to operate on.
522 *
523 * This routine will update the HBA index of a queue to reflect consumption of
524 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
525 * consumed an entry the host calls this function to update the queue's
526 * internal pointers. This routine returns the number of entries that were
527 * consumed by the HBA.
528 **/
529static uint32_t
530lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
531{
James Smart2e90f4b2011-12-13 13:22:37 -0500532 /* sanity check on queue memory */
533 if (unlikely(!hq) || unlikely(!dq))
534 return 0;
535
James Smart4f774512009-05-22 14:52:35 -0400536 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
537 return 0;
538 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
539 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
540 return 1;
541}
542
James Smarte59058c2008-08-24 21:49:00 -0400543/**
James Smart3621a712009-04-06 18:47:14 -0400544 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400545 * @phba: Pointer to HBA context object.
546 * @pring: Pointer to driver SLI ring object.
547 *
548 * This function returns pointer to next command iocb entry
549 * in the command ring. The caller must hold hbalock to prevent
550 * other threads consume the next command iocb.
551 * SLI-2/SLI-3 provide different sized iocbs.
552 **/
James Smarted957682007-06-17 19:56:37 -0500553static inline IOCB_t *
554lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
555{
James Smart7e56aa22012-08-03 12:35:34 -0400556 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
557 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500558}
559
James Smarte59058c2008-08-24 21:49:00 -0400560/**
James Smart3621a712009-04-06 18:47:14 -0400561 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400562 * @phba: Pointer to HBA context object.
563 * @pring: Pointer to driver SLI ring object.
564 *
565 * This function returns pointer to next response iocb entry
566 * in the response ring. The caller must hold hbalock to make sure
567 * that no other thread consume the next response iocb.
568 * SLI-2/SLI-3 provide different sized iocbs.
569 **/
James Smarted957682007-06-17 19:56:37 -0500570static inline IOCB_t *
571lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
572{
James Smart7e56aa22012-08-03 12:35:34 -0400573 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
574 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500575}
576
James Smarte59058c2008-08-24 21:49:00 -0400577/**
James Smart3621a712009-04-06 18:47:14 -0400578 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400579 * @phba: Pointer to HBA context object.
580 *
581 * This function is called with hbalock held. This function
582 * allocates a new driver iocb object from the iocb pool. If the
583 * allocation is successful, it returns pointer to the newly
584 * allocated iocb object else it returns NULL.
585 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400586struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500587__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400588{
589 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
590 struct lpfc_iocbq * iocbq = NULL;
591
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100592 lockdep_assert_held(&phba->hbalock);
593
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400594 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400595 if (iocbq)
596 phba->iocb_cnt++;
597 if (phba->iocb_cnt > phba->iocb_max)
598 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400599 return iocbq;
600}
601
James Smarte59058c2008-08-24 21:49:00 -0400602/**
James Smartda0436e2009-05-22 14:51:39 -0400603 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
604 * @phba: Pointer to HBA context object.
605 * @xritag: XRI value.
606 *
607 * This function clears the sglq pointer from the array of acive
608 * sglq's. The xritag that is passed in is used to index into the
609 * array. Before the xritag can be used it needs to be adjusted
610 * by subtracting the xribase.
611 *
612 * Returns sglq ponter = success, NULL = Failure.
613 **/
James Smart895427b2017-02-12 13:52:30 -0800614struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400615__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
616{
James Smartda0436e2009-05-22 14:51:39 -0400617 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400618
619 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
620 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400621 return sglq;
622}
623
624/**
625 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
626 * @phba: Pointer to HBA context object.
627 * @xritag: XRI value.
628 *
629 * This function returns the sglq pointer from the array of acive
630 * sglq's. The xritag that is passed in is used to index into the
631 * array. Before the xritag can be used it needs to be adjusted
632 * by subtracting the xribase.
633 *
634 * Returns sglq ponter = success, NULL = Failure.
635 **/
James Smart0f65ff62010-02-26 14:14:23 -0500636struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400637__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
638{
James Smartda0436e2009-05-22 14:51:39 -0400639 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400640
641 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400642 return sglq;
643}
644
645/**
James Smart1151e3e2011-02-16 12:39:35 -0500646 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500647 * @phba: Pointer to HBA context object.
648 * @xritag: xri used in this exchange.
649 * @rrq: The RRQ to be cleared.
650 *
James Smart19ca7602010-11-20 23:11:55 -0500651 **/
James Smart1151e3e2011-02-16 12:39:35 -0500652void
653lpfc_clr_rrq_active(struct lpfc_hba *phba,
654 uint16_t xritag,
655 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500656{
James Smart1151e3e2011-02-16 12:39:35 -0500657 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500658
James Smart1151e3e2011-02-16 12:39:35 -0500659 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
660 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500661
662 /* The target DID could have been swapped (cable swap)
663 * we should use the ndlp from the findnode if it is
664 * available.
665 */
James Smart1151e3e2011-02-16 12:39:35 -0500666 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500667 ndlp = rrq->ndlp;
668
James Smart1151e3e2011-02-16 12:39:35 -0500669 if (!ndlp)
670 goto out;
671
James Smartcff261f2013-12-17 20:29:47 -0500672 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500673 rrq->send_rrq = 0;
674 rrq->xritag = 0;
675 rrq->rrq_stop_time = 0;
676 }
James Smart1151e3e2011-02-16 12:39:35 -0500677out:
James Smart19ca7602010-11-20 23:11:55 -0500678 mempool_free(rrq, phba->rrq_pool);
679}
680
681/**
682 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
683 * @phba: Pointer to HBA context object.
684 *
685 * This function is called with hbalock held. This function
686 * Checks if stop_time (ratov from setting rrq active) has
687 * been reached, if it has and the send_rrq flag is set then
688 * it will call lpfc_send_rrq. If the send_rrq flag is not set
689 * then it will just call the routine to clear the rrq and
690 * free the rrq resource.
691 * The timer is set to the next rrq that is going to expire before
692 * leaving the routine.
693 *
694 **/
695void
696lpfc_handle_rrq_active(struct lpfc_hba *phba)
697{
698 struct lpfc_node_rrq *rrq;
699 struct lpfc_node_rrq *nextrrq;
700 unsigned long next_time;
701 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500702 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500703
704 spin_lock_irqsave(&phba->hbalock, iflags);
705 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400706 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500707 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500708 &phba->active_rrq_list, list) {
709 if (time_after(jiffies, rrq->rrq_stop_time))
710 list_move(&rrq->list, &send_rrq);
711 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500712 next_time = rrq->rrq_stop_time;
713 }
714 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500715 if ((!list_empty(&phba->active_rrq_list)) &&
716 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500717 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500718 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
719 list_del(&rrq->list);
720 if (!rrq->send_rrq)
721 /* this call will free the rrq */
722 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
723 else if (lpfc_send_rrq(phba, rrq)) {
724 /* if we send the rrq then the completion handler
725 * will clear the bit in the xribitmap.
726 */
727 lpfc_clr_rrq_active(phba, rrq->xritag,
728 rrq);
729 }
730 }
James Smart19ca7602010-11-20 23:11:55 -0500731}
732
733/**
734 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
735 * @vport: Pointer to vport context object.
736 * @xri: The xri used in the exchange.
737 * @did: The targets DID for this exchange.
738 *
739 * returns NULL = rrq not found in the phba->active_rrq_list.
740 * rrq = rrq for this xri and target.
741 **/
742struct lpfc_node_rrq *
743lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
744{
745 struct lpfc_hba *phba = vport->phba;
746 struct lpfc_node_rrq *rrq;
747 struct lpfc_node_rrq *nextrrq;
748 unsigned long iflags;
749
750 if (phba->sli_rev != LPFC_SLI_REV4)
751 return NULL;
752 spin_lock_irqsave(&phba->hbalock, iflags);
753 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
754 if (rrq->vport == vport && rrq->xritag == xri &&
755 rrq->nlp_DID == did){
756 list_del(&rrq->list);
757 spin_unlock_irqrestore(&phba->hbalock, iflags);
758 return rrq;
759 }
760 }
761 spin_unlock_irqrestore(&phba->hbalock, iflags);
762 return NULL;
763}
764
765/**
766 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
767 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500768 * @ndlp: Pointer to the lpfc_node_list structure.
769 * If ndlp is NULL Remove all active RRQs for this vport from the
770 * phba->active_rrq_list and clear the rrq.
771 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500772 **/
773void
James Smart1151e3e2011-02-16 12:39:35 -0500774lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500775
776{
777 struct lpfc_hba *phba = vport->phba;
778 struct lpfc_node_rrq *rrq;
779 struct lpfc_node_rrq *nextrrq;
780 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500781 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500782
783 if (phba->sli_rev != LPFC_SLI_REV4)
784 return;
James Smart1151e3e2011-02-16 12:39:35 -0500785 if (!ndlp) {
786 lpfc_sli4_vport_delete_els_xri_aborted(vport);
787 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500788 }
James Smart1151e3e2011-02-16 12:39:35 -0500789 spin_lock_irqsave(&phba->hbalock, iflags);
790 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
791 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
792 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500793 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500794
795 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
796 list_del(&rrq->list);
797 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
798 }
James Smart19ca7602010-11-20 23:11:55 -0500799}
800
801/**
James Smart1151e3e2011-02-16 12:39:35 -0500802 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500803 * @phba: Pointer to HBA context object.
804 * @ndlp: Targets nodelist pointer for this exchange.
805 * @xritag the xri in the bitmap to test.
806 *
807 * This function is called with hbalock held. This function
808 * returns 0 = rrq not active for this xri
809 * 1 = rrq is valid for this xri.
810 **/
James Smart1151e3e2011-02-16 12:39:35 -0500811int
812lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500813 uint16_t xritag)
814{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100815 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500816 if (!ndlp)
817 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500818 if (!ndlp->active_rrqs_xri_bitmap)
819 return 0;
820 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500821 return 1;
822 else
823 return 0;
824}
825
826/**
827 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
828 * @phba: Pointer to HBA context object.
829 * @ndlp: nodelist pointer for this target.
830 * @xritag: xri used in this exchange.
831 * @rxid: Remote Exchange ID.
832 * @send_rrq: Flag used to determine if we should send rrq els cmd.
833 *
834 * This function takes the hbalock.
835 * The active bit is always set in the active rrq xri_bitmap even
836 * if there is no slot avaiable for the other rrq information.
837 *
838 * returns 0 rrq actived for this xri
839 * < 0 No memory or invalid ndlp.
840 **/
841int
842lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500843 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500844{
James Smart19ca7602010-11-20 23:11:55 -0500845 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500846 struct lpfc_node_rrq *rrq;
847 int empty;
848
849 if (!ndlp)
850 return -EINVAL;
851
852 if (!phba->cfg_enable_rrq)
853 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500854
855 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500856 if (phba->pport->load_flag & FC_UNLOADING) {
857 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
858 goto out;
859 }
860
861 /*
862 * set the active bit even if there is no mem available.
863 */
864 if (NLP_CHK_FREE_REQ(ndlp))
865 goto out;
866
867 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
868 goto out;
869
James Smartcff261f2013-12-17 20:29:47 -0500870 if (!ndlp->active_rrqs_xri_bitmap)
871 goto out;
872
873 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500874 goto out;
875
James Smart19ca7602010-11-20 23:11:55 -0500876 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500877 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
878 if (!rrq) {
879 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
880 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
881 " DID:0x%x Send:%d\n",
882 xritag, rxid, ndlp->nlp_DID, send_rrq);
883 return -EINVAL;
884 }
James Smarte5771b42013-03-01 16:37:14 -0500885 if (phba->cfg_enable_rrq == 1)
886 rrq->send_rrq = send_rrq;
887 else
888 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500889 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400890 rrq->rrq_stop_time = jiffies +
891 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500892 rrq->ndlp = ndlp;
893 rrq->nlp_DID = ndlp->nlp_DID;
894 rrq->vport = ndlp->vport;
895 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500896 spin_lock_irqsave(&phba->hbalock, iflags);
897 empty = list_empty(&phba->active_rrq_list);
898 list_add_tail(&rrq->list, &phba->active_rrq_list);
899 phba->hba_flag |= HBA_RRQ_ACTIVE;
900 if (empty)
901 lpfc_worker_wake_up(phba);
902 spin_unlock_irqrestore(&phba->hbalock, iflags);
903 return 0;
904out:
905 spin_unlock_irqrestore(&phba->hbalock, iflags);
906 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
907 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
908 " DID:0x%x Send:%d\n",
909 xritag, rxid, ndlp->nlp_DID, send_rrq);
910 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500911}
912
913/**
James Smart895427b2017-02-12 13:52:30 -0800914 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -0400915 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500916 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400917 *
James Smartdafe8ce2014-09-03 12:56:40 -0400918 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400919 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400920 * list is not empty then it is successful, it returns pointer to the newly
921 * allocated sglq object else it returns NULL.
922 **/
923static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -0800924__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400925{
James Smart895427b2017-02-12 13:52:30 -0800926 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -0400927 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500928 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500929 struct lpfc_scsi_buf *lpfc_cmd;
930 struct lpfc_nodelist *ndlp;
931 int found = 0;
932
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100933 lockdep_assert_held(&phba->hbalock);
934
James Smart19ca7602010-11-20 23:11:55 -0500935 if (piocbq->iocb_flag & LPFC_IO_FCP) {
936 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
937 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500938 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400939 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500940 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400941 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
942 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
943 ndlp = NULL;
944 else
945 ndlp = piocbq->context_un.ndlp;
946 } else {
James Smart19ca7602010-11-20 23:11:55 -0500947 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400948 }
James Smart19ca7602010-11-20 23:11:55 -0500949
James Smart895427b2017-02-12 13:52:30 -0800950 spin_lock(&phba->sli4_hba.sgl_list_lock);
951 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500952 start_sglq = sglq;
953 while (!found) {
954 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -0800955 break;
James Smart895427b2017-02-12 13:52:30 -0800956 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
957 test_bit(sglq->sli4_lxritag,
958 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500959 /* This xri has an rrq outstanding for this DID.
960 * put it back in the list and get another xri.
961 */
James Smart895427b2017-02-12 13:52:30 -0800962 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -0500963 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -0800964 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -0500965 struct lpfc_sglq, list);
966 if (sglq == start_sglq) {
967 sglq = NULL;
968 break;
969 } else
970 continue;
971 }
972 sglq->ndlp = ndlp;
973 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400974 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500975 sglq->state = SGL_ALLOCATED;
976 }
James Smart895427b2017-02-12 13:52:30 -0800977 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400978 return sglq;
979}
980
981/**
James Smartf358dd02017-02-12 13:52:34 -0800982 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
983 * @phba: Pointer to HBA context object.
984 * @piocb: Pointer to the iocbq.
985 *
986 * This function is called with the sgl_list lock held. This function
987 * gets a new driver sglq object from the sglq list. If the
988 * list is not empty then it is successful, it returns pointer to the newly
989 * allocated sglq object else it returns NULL.
990 **/
991struct lpfc_sglq *
992__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
993{
994 struct list_head *lpfc_nvmet_sgl_list;
995 struct lpfc_sglq *sglq = NULL;
996
997 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
998
999 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1000
1001 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1002 if (!sglq)
1003 return NULL;
1004 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1005 sglq->state = SGL_ALLOCATED;
1006 return sglq;
1007}
1008
1009/**
James Smart3621a712009-04-06 18:47:14 -04001010 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001011 * @phba: Pointer to HBA context object.
1012 *
1013 * This function is called with no lock held. This function
1014 * allocates a new driver iocb object from the iocb pool. If the
1015 * allocation is successful, it returns pointer to the newly
1016 * allocated iocb object else it returns NULL.
1017 **/
James Smart2e0fef82007-06-17 19:56:36 -05001018struct lpfc_iocbq *
1019lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001020{
James Smart2e0fef82007-06-17 19:56:36 -05001021 struct lpfc_iocbq * iocbq = NULL;
1022 unsigned long iflags;
1023
1024 spin_lock_irqsave(&phba->hbalock, iflags);
1025 iocbq = __lpfc_sli_get_iocbq(phba);
1026 spin_unlock_irqrestore(&phba->hbalock, iflags);
1027 return iocbq;
1028}
1029
James Smarte59058c2008-08-24 21:49:00 -04001030/**
James Smart4f774512009-05-22 14:52:35 -04001031 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1032 * @phba: Pointer to HBA context object.
1033 * @iocbq: Pointer to driver iocb object.
1034 *
1035 * This function is called with hbalock held to release driver
1036 * iocb object to the iocb pool. The iotag in the iocb object
1037 * does not change for each use of the iocb object. This function
1038 * clears all other fields of the iocb object when it is freed.
1039 * The sqlq structure that holds the xritag and phys and virtual
1040 * mappings for the scatter gather list is retrieved from the
1041 * active array of sglq. The get of the sglq pointer also clears
1042 * the entry in the array. If the status of the IO indiactes that
1043 * this IO was aborted then the sglq entry it put on the
1044 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1045 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001046 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001047 **/
1048static void
1049__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1050{
1051 struct lpfc_sglq *sglq;
1052 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001053 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001054 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001055
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001056 lockdep_assert_held(&phba->hbalock);
1057
James Smart4f774512009-05-22 14:52:35 -04001058 if (iocbq->sli4_xritag == NO_XRI)
1059 sglq = NULL;
1060 else
James Smart6d368e52011-05-24 11:44:12 -04001061 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1062
James Smart0e9bb8d2013-03-01 16:35:12 -05001063
James Smart4f774512009-05-22 14:52:35 -04001064 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001065 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1066 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1067 iflag);
1068 sglq->state = SGL_FREED;
1069 sglq->ndlp = NULL;
1070 list_add_tail(&sglq->list,
1071 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1072 spin_unlock_irqrestore(
1073 &phba->sli4_hba.sgl_list_lock, iflag);
1074 goto out;
1075 }
1076
James Smart895427b2017-02-12 13:52:30 -08001077 pring = phba->sli4_hba.els_wq->pring;
James Smart0f65ff62010-02-26 14:14:23 -05001078 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1079 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001080 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1081 iflag);
James Smart4f774512009-05-22 14:52:35 -04001082 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001083 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001084 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001085 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001086 } else {
James Smart895427b2017-02-12 13:52:30 -08001087 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1088 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001089 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001090 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001091 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001092 &phba->sli4_hba.lpfc_els_sgl_list);
1093 spin_unlock_irqrestore(
1094 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001095
1096 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001097 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001098 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001099 }
James Smart4f774512009-05-22 14:52:35 -04001100 }
1101
James Smartf358dd02017-02-12 13:52:34 -08001102out:
James Smart4f774512009-05-22 14:52:35 -04001103 /*
1104 * Clean all volatile data fields, preserve iotag and node struct.
1105 */
1106 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001107 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001108 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001109 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1110 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001111 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1112}
1113
James Smart2a9bf3d2010-06-07 15:24:45 -04001114
James Smart4f774512009-05-22 14:52:35 -04001115/**
James Smart3772a992009-05-22 14:50:54 -04001116 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1117 * @phba: Pointer to HBA context object.
1118 * @iocbq: Pointer to driver iocb object.
1119 *
1120 * This function is called with hbalock held to release driver
1121 * iocb object to the iocb pool. The iotag in the iocb object
1122 * does not change for each use of the iocb object. This function
1123 * clears all other fields of the iocb object when it is freed.
1124 **/
1125static void
1126__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1127{
1128 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1129
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001130 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001131
1132 /*
James Smart3772a992009-05-22 14:50:54 -04001133 * Clean all volatile data fields, preserve iotag and node struct.
1134 */
1135 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1136 iocbq->sli4_xritag = NO_XRI;
1137 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1138}
1139
1140/**
James Smart3621a712009-04-06 18:47:14 -04001141 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001142 * @phba: Pointer to HBA context object.
1143 * @iocbq: Pointer to driver iocb object.
1144 *
1145 * This function is called with hbalock held to release driver
1146 * iocb object to the iocb pool. The iotag in the iocb object
1147 * does not change for each use of the iocb object. This function
1148 * clears all other fields of the iocb object when it is freed.
1149 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001150static void
James Smart2e0fef82007-06-17 19:56:36 -05001151__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1152{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001153 lockdep_assert_held(&phba->hbalock);
1154
James Smart3772a992009-05-22 14:50:54 -04001155 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001156 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001157}
1158
James Smarte59058c2008-08-24 21:49:00 -04001159/**
James Smart3621a712009-04-06 18:47:14 -04001160 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001161 * @phba: Pointer to HBA context object.
1162 * @iocbq: Pointer to driver iocb object.
1163 *
1164 * This function is called with no lock held to release the iocb to
1165 * iocb pool.
1166 **/
James Smart2e0fef82007-06-17 19:56:36 -05001167void
1168lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1169{
1170 unsigned long iflags;
1171
1172 /*
1173 * Clean all volatile data fields, preserve iotag and node struct.
1174 */
1175 spin_lock_irqsave(&phba->hbalock, iflags);
1176 __lpfc_sli_release_iocbq(phba, iocbq);
1177 spin_unlock_irqrestore(&phba->hbalock, iflags);
1178}
1179
James Smarte59058c2008-08-24 21:49:00 -04001180/**
James Smarta257bf92009-04-06 18:48:10 -04001181 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1182 * @phba: Pointer to HBA context object.
1183 * @iocblist: List of IOCBs.
1184 * @ulpstatus: ULP status in IOCB command field.
1185 * @ulpWord4: ULP word-4 in IOCB command field.
1186 *
1187 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1188 * on the list by invoking the complete callback function associated with the
1189 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1190 * fields.
1191 **/
1192void
1193lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1194 uint32_t ulpstatus, uint32_t ulpWord4)
1195{
1196 struct lpfc_iocbq *piocb;
1197
1198 while (!list_empty(iocblist)) {
1199 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001200 if (!piocb->iocb_cmpl)
1201 lpfc_sli_release_iocbq(phba, piocb);
1202 else {
1203 piocb->iocb.ulpStatus = ulpstatus;
1204 piocb->iocb.un.ulpWord[4] = ulpWord4;
1205 (piocb->iocb_cmpl) (phba, piocb, piocb);
1206 }
1207 }
1208 return;
1209}
1210
1211/**
James Smart3621a712009-04-06 18:47:14 -04001212 * lpfc_sli_iocb_cmd_type - Get the iocb type
1213 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001214 *
1215 * This function is called by ring event handler function to get the iocb type.
1216 * This function translates the iocb command to an iocb command type used to
1217 * decide the final disposition of each completed IOCB.
1218 * The function returns
1219 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1220 * LPFC_SOL_IOCB if it is a solicited iocb completion
1221 * LPFC_ABORT_IOCB if it is an abort iocb
1222 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1223 *
1224 * The caller is not required to hold any lock.
1225 **/
dea31012005-04-17 16:05:31 -05001226static lpfc_iocb_type
1227lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1228{
1229 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1230
1231 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1232 return 0;
1233
1234 switch (iocb_cmnd) {
1235 case CMD_XMIT_SEQUENCE_CR:
1236 case CMD_XMIT_SEQUENCE_CX:
1237 case CMD_XMIT_BCAST_CN:
1238 case CMD_XMIT_BCAST_CX:
1239 case CMD_ELS_REQUEST_CR:
1240 case CMD_ELS_REQUEST_CX:
1241 case CMD_CREATE_XRI_CR:
1242 case CMD_CREATE_XRI_CX:
1243 case CMD_GET_RPI_CN:
1244 case CMD_XMIT_ELS_RSP_CX:
1245 case CMD_GET_RPI_CR:
1246 case CMD_FCP_IWRITE_CR:
1247 case CMD_FCP_IWRITE_CX:
1248 case CMD_FCP_IREAD_CR:
1249 case CMD_FCP_IREAD_CX:
1250 case CMD_FCP_ICMND_CR:
1251 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001252 case CMD_FCP_TSEND_CX:
1253 case CMD_FCP_TRSP_CX:
1254 case CMD_FCP_TRECEIVE_CX:
1255 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001256 case CMD_ADAPTER_MSG:
1257 case CMD_ADAPTER_DUMP:
1258 case CMD_XMIT_SEQUENCE64_CR:
1259 case CMD_XMIT_SEQUENCE64_CX:
1260 case CMD_XMIT_BCAST64_CN:
1261 case CMD_XMIT_BCAST64_CX:
1262 case CMD_ELS_REQUEST64_CR:
1263 case CMD_ELS_REQUEST64_CX:
1264 case CMD_FCP_IWRITE64_CR:
1265 case CMD_FCP_IWRITE64_CX:
1266 case CMD_FCP_IREAD64_CR:
1267 case CMD_FCP_IREAD64_CX:
1268 case CMD_FCP_ICMND64_CR:
1269 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001270 case CMD_FCP_TSEND64_CX:
1271 case CMD_FCP_TRSP64_CX:
1272 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001273 case CMD_GEN_REQUEST64_CR:
1274 case CMD_GEN_REQUEST64_CX:
1275 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001276 case DSSCMD_IWRITE64_CR:
1277 case DSSCMD_IWRITE64_CX:
1278 case DSSCMD_IREAD64_CR:
1279 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001280 type = LPFC_SOL_IOCB;
1281 break;
1282 case CMD_ABORT_XRI_CN:
1283 case CMD_ABORT_XRI_CX:
1284 case CMD_CLOSE_XRI_CN:
1285 case CMD_CLOSE_XRI_CX:
1286 case CMD_XRI_ABORTED_CX:
1287 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001288 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001289 type = LPFC_ABORT_IOCB;
1290 break;
1291 case CMD_RCV_SEQUENCE_CX:
1292 case CMD_RCV_ELS_REQ_CX:
1293 case CMD_RCV_SEQUENCE64_CX:
1294 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001295 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001296 case CMD_IOCB_RCV_SEQ64_CX:
1297 case CMD_IOCB_RCV_ELS64_CX:
1298 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001299 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001300 type = LPFC_UNSOL_IOCB;
1301 break;
James Smart3163f722008-02-08 18:50:25 -05001302 case CMD_IOCB_XMIT_MSEQ64_CR:
1303 case CMD_IOCB_XMIT_MSEQ64_CX:
1304 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1305 case CMD_IOCB_RCV_ELS_LIST64_CX:
1306 case CMD_IOCB_CLOSE_EXTENDED_CN:
1307 case CMD_IOCB_ABORT_EXTENDED_CN:
1308 case CMD_IOCB_RET_HBQE64_CN:
1309 case CMD_IOCB_FCP_IBIDIR64_CR:
1310 case CMD_IOCB_FCP_IBIDIR64_CX:
1311 case CMD_IOCB_FCP_ITASKMGT64_CX:
1312 case CMD_IOCB_LOGENTRY_CN:
1313 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1314 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001315 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001316 type = LPFC_UNKNOWN_IOCB;
1317 break;
dea31012005-04-17 16:05:31 -05001318 default:
1319 type = LPFC_UNKNOWN_IOCB;
1320 break;
1321 }
1322
1323 return type;
1324}
1325
James Smarte59058c2008-08-24 21:49:00 -04001326/**
James Smart3621a712009-04-06 18:47:14 -04001327 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001328 * @phba: Pointer to HBA context object.
1329 *
1330 * This function is called from SLI initialization code
1331 * to configure every ring of the HBA's SLI interface. The
1332 * caller is not required to hold any lock. This function issues
1333 * a config_ring mailbox command for each ring.
1334 * This function returns zero if successful else returns a negative
1335 * error code.
1336 **/
dea31012005-04-17 16:05:31 -05001337static int
James Smarted957682007-06-17 19:56:37 -05001338lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001339{
1340 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001341 LPFC_MBOXQ_t *pmb;
1342 MAILBOX_t *pmbox;
1343 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001344
James Smarted957682007-06-17 19:56:37 -05001345 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1346 if (!pmb)
1347 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001348 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001349 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001350 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001351 lpfc_config_ring(phba, i, pmb);
1352 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1353 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001355 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001356 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1357 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001358 rc, pmbox->mbxCommand,
1359 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001360 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001361 ret = -ENXIO;
1362 break;
dea31012005-04-17 16:05:31 -05001363 }
1364 }
James Smarted957682007-06-17 19:56:37 -05001365 mempool_free(pmb, phba->mbox_mem_pool);
1366 return ret;
dea31012005-04-17 16:05:31 -05001367}
1368
James Smarte59058c2008-08-24 21:49:00 -04001369/**
James Smart3621a712009-04-06 18:47:14 -04001370 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001371 * @phba: Pointer to HBA context object.
1372 * @pring: Pointer to driver SLI ring object.
1373 * @piocb: Pointer to the driver iocb object.
1374 *
1375 * This function is called with hbalock held. The function adds the
1376 * new iocb to txcmplq of the given ring. This function always returns
1377 * 0. If this function is called for ELS ring, this function checks if
1378 * there is a vport associated with the ELS command. This function also
1379 * starts els_tmofunc timer if this is an ELS command.
1380 **/
dea31012005-04-17 16:05:31 -05001381static int
James Smart2e0fef82007-06-17 19:56:36 -05001382lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1383 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001384{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001385 lockdep_assert_held(&phba->hbalock);
1386
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001387 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001388
dea31012005-04-17 16:05:31 -05001389 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001390 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001391
James Smart92d7f7b2007-06-17 19:56:38 -05001392 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1393 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001394 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1395 BUG_ON(!piocb->vport);
1396 if (!(piocb->vport->load_flag & FC_UNLOADING))
1397 mod_timer(&piocb->vport->els_tmofunc,
1398 jiffies +
1399 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1400 }
dea31012005-04-17 16:05:31 -05001401
James Smart2e0fef82007-06-17 19:56:36 -05001402 return 0;
dea31012005-04-17 16:05:31 -05001403}
1404
James Smarte59058c2008-08-24 21:49:00 -04001405/**
James Smart3621a712009-04-06 18:47:14 -04001406 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001407 * @phba: Pointer to HBA context object.
1408 * @pring: Pointer to driver SLI ring object.
1409 *
1410 * This function is called with hbalock held to get next
1411 * iocb in txq of the given ring. If there is any iocb in
1412 * the txq, the function returns first iocb in the list after
1413 * removing the iocb from the list, else it returns NULL.
1414 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001415struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001416lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001417{
dea31012005-04-17 16:05:31 -05001418 struct lpfc_iocbq *cmd_iocb;
1419
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001420 lockdep_assert_held(&phba->hbalock);
1421
James Smart858c9f62007-06-17 19:56:39 -05001422 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001423 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001424}
1425
James Smarte59058c2008-08-24 21:49:00 -04001426/**
James Smart3621a712009-04-06 18:47:14 -04001427 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001428 * @phba: Pointer to HBA context object.
1429 * @pring: Pointer to driver SLI ring object.
1430 *
1431 * This function is called with hbalock held and the caller must post the
1432 * iocb without releasing the lock. If the caller releases the lock,
1433 * iocb slot returned by the function is not guaranteed to be available.
1434 * The function returns pointer to the next available iocb slot if there
1435 * is available slot in the ring, else it returns NULL.
1436 * If the get index of the ring is ahead of the put index, the function
1437 * will post an error attention event to the worker thread to take the
1438 * HBA to offline state.
1439 **/
dea31012005-04-17 16:05:31 -05001440static IOCB_t *
1441lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1442{
James Smart34b02dc2008-08-24 21:49:55 -04001443 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001444 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001445
1446 lockdep_assert_held(&phba->hbalock);
1447
James Smart7e56aa22012-08-03 12:35:34 -04001448 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1449 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1450 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001451
James Smart7e56aa22012-08-03 12:35:34 -04001452 if (unlikely(pring->sli.sli3.local_getidx ==
1453 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001454
James Smart7e56aa22012-08-03 12:35:34 -04001455 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001456
James Smart7e56aa22012-08-03 12:35:34 -04001457 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001458 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001459 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001460 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001461 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001462 pring->sli.sli3.local_getidx,
1463 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001464
James Smart2e0fef82007-06-17 19:56:36 -05001465 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001466 /*
1467 * All error attention handlers are posted to
1468 * worker thread
1469 */
1470 phba->work_ha |= HA_ERATT;
1471 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001472
James Smart5e9d9b82008-06-14 22:52:53 -04001473 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001474
1475 return NULL;
1476 }
1477
James Smart7e56aa22012-08-03 12:35:34 -04001478 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001479 return NULL;
1480 }
1481
James Smarted957682007-06-17 19:56:37 -05001482 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001483}
1484
James Smarte59058c2008-08-24 21:49:00 -04001485/**
James Smart3621a712009-04-06 18:47:14 -04001486 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001487 * @phba: Pointer to HBA context object.
1488 * @iocbq: Pointer to driver iocb object.
1489 *
1490 * This function gets an iotag for the iocb. If there is no unused iotag and
1491 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1492 * array and assigns a new iotag.
1493 * The function returns the allocated iotag if successful, else returns zero.
1494 * Zero is not a valid iotag.
1495 * The caller is not required to hold any lock.
1496 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001497uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001498lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001499{
James Smart2e0fef82007-06-17 19:56:36 -05001500 struct lpfc_iocbq **new_arr;
1501 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001502 size_t new_len;
1503 struct lpfc_sli *psli = &phba->sli;
1504 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001505
James Smart2e0fef82007-06-17 19:56:36 -05001506 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001507 iotag = psli->last_iotag;
1508 if(++iotag < psli->iocbq_lookup_len) {
1509 psli->last_iotag = iotag;
1510 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001511 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001512 iocbq->iotag = iotag;
1513 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001514 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001515 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1516 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001517 spin_unlock_irq(&phba->hbalock);
1518 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001519 GFP_KERNEL);
1520 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001521 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001522 old_arr = psli->iocbq_lookup;
1523 if (new_len <= psli->iocbq_lookup_len) {
1524 /* highly unprobable case */
1525 kfree(new_arr);
1526 iotag = psli->last_iotag;
1527 if(++iotag < psli->iocbq_lookup_len) {
1528 psli->last_iotag = iotag;
1529 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001530 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001531 iocbq->iotag = iotag;
1532 return iotag;
1533 }
James Smart2e0fef82007-06-17 19:56:36 -05001534 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001535 return 0;
1536 }
1537 if (psli->iocbq_lookup)
1538 memcpy(new_arr, old_arr,
1539 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001540 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001541 psli->iocbq_lookup = new_arr;
1542 psli->iocbq_lookup_len = new_len;
1543 psli->last_iotag = iotag;
1544 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001545 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001546 iocbq->iotag = iotag;
1547 kfree(old_arr);
1548 return iotag;
1549 }
James Smart8f6d98d2006-08-01 07:34:00 -04001550 } else
James Smart2e0fef82007-06-17 19:56:36 -05001551 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001552
James Smartbc739052010-08-04 16:11:18 -04001553 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001554 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1555 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001556
James Bottomley604a3e32005-10-29 10:28:33 -05001557 return 0;
dea31012005-04-17 16:05:31 -05001558}
1559
James Smarte59058c2008-08-24 21:49:00 -04001560/**
James Smart3621a712009-04-06 18:47:14 -04001561 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001562 * @phba: Pointer to HBA context object.
1563 * @pring: Pointer to driver SLI ring object.
1564 * @iocb: Pointer to iocb slot in the ring.
1565 * @nextiocb: Pointer to driver iocb object which need to be
1566 * posted to firmware.
1567 *
1568 * This function is called with hbalock held to post a new iocb to
1569 * the firmware. This function copies the new iocb to ring iocb slot and
1570 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1571 * a completion call back for this iocb else the function will free the
1572 * iocb object.
1573 **/
dea31012005-04-17 16:05:31 -05001574static void
1575lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1576 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1577{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001578 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001579 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001580 * Set up an iotag
dea31012005-04-17 16:05:31 -05001581 */
James Bottomley604a3e32005-10-29 10:28:33 -05001582 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001583
James Smarte2a0a9d2008-12-04 22:40:02 -05001584
James Smarta58cbd52007-08-02 11:09:43 -04001585 if (pring->ringno == LPFC_ELS_RING) {
1586 lpfc_debugfs_slow_ring_trc(phba,
1587 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1588 *(((uint32_t *) &nextiocb->iocb) + 4),
1589 *(((uint32_t *) &nextiocb->iocb) + 6),
1590 *(((uint32_t *) &nextiocb->iocb) + 7));
1591 }
1592
dea31012005-04-17 16:05:31 -05001593 /*
1594 * Issue iocb command to adapter
1595 */
James Smart92d7f7b2007-06-17 19:56:38 -05001596 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001597 wmb();
1598 pring->stats.iocb_cmd++;
1599
1600 /*
1601 * If there is no completion routine to call, we can release the
1602 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1603 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1604 */
1605 if (nextiocb->iocb_cmpl)
1606 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001607 else
James Smart2e0fef82007-06-17 19:56:36 -05001608 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001609
1610 /*
1611 * Let the HBA know what IOCB slot will be the next one the
1612 * driver will put a command into.
1613 */
James Smart7e56aa22012-08-03 12:35:34 -04001614 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1615 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001616}
1617
James Smarte59058c2008-08-24 21:49:00 -04001618/**
James Smart3621a712009-04-06 18:47:14 -04001619 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001620 * @phba: Pointer to HBA context object.
1621 * @pring: Pointer to driver SLI ring object.
1622 *
1623 * The caller is not required to hold any lock for calling this function.
1624 * This function updates the chip attention bits for the ring to inform firmware
1625 * that there are pending work to be done for this ring and requests an
1626 * interrupt when there is space available in the ring. This function is
1627 * called when the driver is unable to post more iocbs to the ring due
1628 * to unavailability of space in the ring.
1629 **/
dea31012005-04-17 16:05:31 -05001630static void
James Smart2e0fef82007-06-17 19:56:36 -05001631lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001632{
1633 int ringno = pring->ringno;
1634
1635 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1636
1637 wmb();
1638
1639 /*
1640 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1641 * The HBA will tell us when an IOCB entry is available.
1642 */
1643 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1644 readl(phba->CAregaddr); /* flush */
1645
1646 pring->stats.iocb_cmd_full++;
1647}
1648
James Smarte59058c2008-08-24 21:49:00 -04001649/**
James Smart3621a712009-04-06 18:47:14 -04001650 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001651 * @phba: Pointer to HBA context object.
1652 * @pring: Pointer to driver SLI ring object.
1653 *
1654 * This function updates the chip attention register bit for the
1655 * given ring to inform HBA that there is more work to be done
1656 * in this ring. The caller is not required to hold any lock.
1657 **/
dea31012005-04-17 16:05:31 -05001658static void
James Smart2e0fef82007-06-17 19:56:36 -05001659lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001660{
1661 int ringno = pring->ringno;
1662
1663 /*
1664 * Tell the HBA that there is work to do in this ring.
1665 */
James Smart34b02dc2008-08-24 21:49:55 -04001666 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1667 wmb();
1668 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1669 readl(phba->CAregaddr); /* flush */
1670 }
dea31012005-04-17 16:05:31 -05001671}
1672
James Smarte59058c2008-08-24 21:49:00 -04001673/**
James Smart3621a712009-04-06 18:47:14 -04001674 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001675 * @phba: Pointer to HBA context object.
1676 * @pring: Pointer to driver SLI ring object.
1677 *
1678 * This function is called with hbalock held to post pending iocbs
1679 * in the txq to the firmware. This function is called when driver
1680 * detects space available in the ring.
1681 **/
dea31012005-04-17 16:05:31 -05001682static void
James Smart2e0fef82007-06-17 19:56:36 -05001683lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001684{
1685 IOCB_t *iocb;
1686 struct lpfc_iocbq *nextiocb;
1687
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001688 lockdep_assert_held(&phba->hbalock);
1689
dea31012005-04-17 16:05:31 -05001690 /*
1691 * Check to see if:
1692 * (a) there is anything on the txq to send
1693 * (b) link is up
1694 * (c) link attention events can be processed (fcp ring only)
1695 * (d) IOCB processing is not blocked by the outstanding mbox command.
1696 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001697
1698 if (lpfc_is_link_up(phba) &&
1699 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001700 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001701 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001702
1703 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1704 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1705 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1706
1707 if (iocb)
1708 lpfc_sli_update_ring(phba, pring);
1709 else
1710 lpfc_sli_update_full_ring(phba, pring);
1711 }
1712
1713 return;
1714}
1715
James Smarte59058c2008-08-24 21:49:00 -04001716/**
James Smart3621a712009-04-06 18:47:14 -04001717 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001718 * @phba: Pointer to HBA context object.
1719 * @hbqno: HBQ number.
1720 *
1721 * This function is called with hbalock held to get the next
1722 * available slot for the given HBQ. If there is free slot
1723 * available for the HBQ it will return pointer to the next available
1724 * HBQ entry else it will return NULL.
1725 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001726static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001727lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1728{
1729 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1730
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001731 lockdep_assert_held(&phba->hbalock);
1732
James Smarted957682007-06-17 19:56:37 -05001733 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1734 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1735 hbqp->next_hbqPutIdx = 0;
1736
1737 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001738 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001739 uint32_t getidx = le32_to_cpu(raw_index);
1740
1741 hbqp->local_hbqGetIdx = getidx;
1742
1743 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1744 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001745 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001746 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001747 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001748 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001749 hbqp->entry_count);
1750
1751 phba->link_state = LPFC_HBA_ERROR;
1752 return NULL;
1753 }
1754
1755 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1756 return NULL;
1757 }
1758
James Smart51ef4c22007-08-02 11:10:31 -04001759 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1760 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001761}
1762
James Smarte59058c2008-08-24 21:49:00 -04001763/**
James Smart3621a712009-04-06 18:47:14 -04001764 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001765 * @phba: Pointer to HBA context object.
1766 *
1767 * This function is called with no lock held to free all the
1768 * hbq buffers while uninitializing the SLI interface. It also
1769 * frees the HBQ buffers returned by the firmware but not yet
1770 * processed by the upper layers.
1771 **/
James Smarted957682007-06-17 19:56:37 -05001772void
1773lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1774{
James Smart92d7f7b2007-06-17 19:56:38 -05001775 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1776 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001777 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001778 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001779
James Smart51ef4c22007-08-02 11:10:31 -04001780 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001781 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001782 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001783 for (i = 0; i < hbq_count; ++i) {
1784 list_for_each_entry_safe(dmabuf, next_dmabuf,
1785 &phba->hbqs[i].hbq_buffer_list, list) {
1786 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1787 list_del(&hbq_buf->dbuf.list);
1788 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1789 }
James Smarta8adb832007-10-27 13:37:53 -04001790 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001791 }
James Smart3163f722008-02-08 18:50:25 -05001792
1793 /* Mark the HBQs not in use */
1794 phba->hbq_in_use = 0;
1795 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001796}
1797
James Smarte59058c2008-08-24 21:49:00 -04001798/**
James Smart3621a712009-04-06 18:47:14 -04001799 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001800 * @phba: Pointer to HBA context object.
1801 * @hbqno: HBQ number.
1802 * @hbq_buf: Pointer to HBQ buffer.
1803 *
1804 * This function is called with the hbalock held to post a
1805 * hbq buffer to the firmware. If the function finds an empty
1806 * slot in the HBQ, it will post the buffer. The function will return
1807 * pointer to the hbq entry if it successfully post the buffer
1808 * else it will return NULL.
1809 **/
James Smart3772a992009-05-22 14:50:54 -04001810static int
James Smarted957682007-06-17 19:56:37 -05001811lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001812 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001813{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001814 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001815 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1816}
1817
1818/**
1819 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1820 * @phba: Pointer to HBA context object.
1821 * @hbqno: HBQ number.
1822 * @hbq_buf: Pointer to HBQ buffer.
1823 *
1824 * This function is called with the hbalock held to post a hbq buffer to the
1825 * firmware. If the function finds an empty slot in the HBQ, it will post the
1826 * buffer and place it on the hbq_buffer_list. The function will return zero if
1827 * it successfully post the buffer else it will return an error.
1828 **/
1829static int
1830lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1831 struct hbq_dmabuf *hbq_buf)
1832{
James Smarted957682007-06-17 19:56:37 -05001833 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001834 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001835
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001836 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001837 /* Get next HBQ entry slot to use */
1838 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1839 if (hbqe) {
1840 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1841
James Smart92d7f7b2007-06-17 19:56:38 -05001842 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1843 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08001844 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05001845 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001846 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1847 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1848 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001849 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1850 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001851 /* flush */
James Smarted957682007-06-17 19:56:37 -05001852 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001853 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001854 return 0;
1855 } else
1856 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001857}
1858
James Smart4f774512009-05-22 14:52:35 -04001859/**
1860 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1861 * @phba: Pointer to HBA context object.
1862 * @hbqno: HBQ number.
1863 * @hbq_buf: Pointer to HBQ buffer.
1864 *
1865 * This function is called with the hbalock held to post an RQE to the SLI4
1866 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1867 * the hbq_buffer_list and return zero, otherwise it will return an error.
1868 **/
1869static int
1870lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1871 struct hbq_dmabuf *hbq_buf)
1872{
1873 int rc;
1874 struct lpfc_rqe hrqe;
1875 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08001876 struct lpfc_queue *hrq;
1877 struct lpfc_queue *drq;
1878
1879 if (hbqno != LPFC_ELS_HBQ)
1880 return 1;
1881 hrq = phba->sli4_hba.hdr_rq;
1882 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04001883
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001884 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001885 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1886 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1887 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1888 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08001889 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04001890 if (rc < 0)
1891 return rc;
James Smart895427b2017-02-12 13:52:30 -08001892 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04001893 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1894 return 0;
1895}
1896
James Smarte59058c2008-08-24 21:49:00 -04001897/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001898static struct lpfc_hbq_init lpfc_els_hbq = {
1899 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001900 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001901 .mask_count = 0,
1902 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001903 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001904 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001905 .init_count = 40,
1906 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001907};
James Smarted957682007-06-17 19:56:37 -05001908
James Smarte59058c2008-08-24 21:49:00 -04001909/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001910struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001911 &lpfc_els_hbq,
1912};
1913
James Smarte59058c2008-08-24 21:49:00 -04001914/**
James Smart3621a712009-04-06 18:47:14 -04001915 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001916 * @phba: Pointer to HBA context object.
1917 * @hbqno: HBQ number.
1918 * @count: Number of HBQ buffers to be posted.
1919 *
James Smartd7c255b2008-08-24 21:50:00 -04001920 * This function is called with no lock held to post more hbq buffers to the
1921 * given HBQ. The function returns the number of HBQ buffers successfully
1922 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001923 **/
James Smart311464e2007-08-02 11:10:37 -04001924static int
James Smart92d7f7b2007-06-17 19:56:38 -05001925lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1926{
James Smartd7c255b2008-08-24 21:50:00 -04001927 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001928 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001929 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001930 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001931 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001932 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001933
James Smartd7c255b2008-08-24 21:50:00 -04001934 if ((phba->hbqs[hbqno].buffer_count + count) >
1935 lpfc_hbq_defs[hbqno]->entry_count)
1936 count = lpfc_hbq_defs[hbqno]->entry_count -
1937 phba->hbqs[hbqno].buffer_count;
1938 if (!count)
1939 return 0;
1940 /* Allocate HBQ entries */
1941 for (i = 0; i < count; i++) {
1942 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1943 if (!hbq_buffer)
1944 break;
1945 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1946 }
James Smart3163f722008-02-08 18:50:25 -05001947 /* Check whether HBQ is still in use */
1948 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001949 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001950 goto err;
1951 while (!list_empty(&hbq_buf_list)) {
1952 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1953 dbuf.list);
1954 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1955 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001956 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001957 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001958 posted++;
1959 } else
James Smart51ef4c22007-08-02 11:10:31 -04001960 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001961 }
James Smart3163f722008-02-08 18:50:25 -05001962 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001963 return posted;
1964err:
1965 spin_unlock_irqrestore(&phba->hbalock, flags);
1966 while (!list_empty(&hbq_buf_list)) {
1967 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1968 dbuf.list);
1969 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1970 }
James Smart92d7f7b2007-06-17 19:56:38 -05001971 return 0;
James Smarted957682007-06-17 19:56:37 -05001972}
1973
James Smarte59058c2008-08-24 21:49:00 -04001974/**
James Smart3621a712009-04-06 18:47:14 -04001975 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001976 * @phba: Pointer to HBA context object.
1977 * @qno: HBQ number.
1978 *
1979 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001980 * is called with no lock held. The function returns the number of HBQ entries
1981 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001982 **/
James Smarted957682007-06-17 19:56:37 -05001983int
James Smart92d7f7b2007-06-17 19:56:38 -05001984lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001985{
James Smartdef9c7a2009-12-21 17:02:28 -05001986 if (phba->sli_rev == LPFC_SLI_REV4)
1987 return 0;
1988 else
1989 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1990 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001991}
1992
James Smarte59058c2008-08-24 21:49:00 -04001993/**
James Smart3621a712009-04-06 18:47:14 -04001994 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001995 * @phba: Pointer to HBA context object.
1996 * @qno: HBQ queue number.
1997 *
1998 * This function is called from SLI initialization code path with
1999 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002000 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002001 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002002static int
James Smart92d7f7b2007-06-17 19:56:38 -05002003lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002004{
James Smartdef9c7a2009-12-21 17:02:28 -05002005 if (phba->sli_rev == LPFC_SLI_REV4)
2006 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002007 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002008 else
2009 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2010 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002011}
2012
James Smarte59058c2008-08-24 21:49:00 -04002013/**
James Smart3772a992009-05-22 14:50:54 -04002014 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2015 * @phba: Pointer to HBA context object.
2016 * @hbqno: HBQ number.
2017 *
2018 * This function removes the first hbq buffer on an hbq list and returns a
2019 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2020 **/
2021static struct hbq_dmabuf *
2022lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2023{
2024 struct lpfc_dmabuf *d_buf;
2025
2026 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2027 if (!d_buf)
2028 return NULL;
2029 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2030}
2031
2032/**
James Smart2d7dbc42017-02-12 13:52:35 -08002033 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2034 * @phba: Pointer to HBA context object.
2035 * @hbqno: HBQ number.
2036 *
2037 * This function removes the first RQ buffer on an RQ buffer list and returns a
2038 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2039 **/
2040static struct rqb_dmabuf *
2041lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2042{
2043 struct lpfc_dmabuf *h_buf;
2044 struct lpfc_rqb *rqbp;
2045
2046 rqbp = hrq->rqbp;
2047 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2048 struct lpfc_dmabuf, list);
2049 if (!h_buf)
2050 return NULL;
2051 rqbp->buffer_count--;
2052 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2053}
2054
2055/**
James Smart3621a712009-04-06 18:47:14 -04002056 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002057 * @phba: Pointer to HBA context object.
2058 * @tag: Tag of the hbq buffer.
2059 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002060 * This function searches for the hbq buffer associated with the given tag in
2061 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2062 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002063 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002064static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002065lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2066{
James Smart92d7f7b2007-06-17 19:56:38 -05002067 struct lpfc_dmabuf *d_buf;
2068 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002069 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002070
James Smart51ef4c22007-08-02 11:10:31 -04002071 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002072 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002073 return NULL;
2074
James Smart3772a992009-05-22 14:50:54 -04002075 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002076 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002077 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002078 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002079 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002080 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002081 }
2082 }
James Smart3772a992009-05-22 14:50:54 -04002083 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002085 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002086 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002087 return NULL;
James Smarted957682007-06-17 19:56:37 -05002088}
2089
James Smarte59058c2008-08-24 21:49:00 -04002090/**
James Smart3621a712009-04-06 18:47:14 -04002091 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002092 * @phba: Pointer to HBA context object.
2093 * @hbq_buffer: Pointer to HBQ buffer.
2094 *
2095 * This function is called with hbalock. This function gives back
2096 * the hbq buffer to firmware. If the HBQ does not have space to
2097 * post the buffer, it will free the buffer.
2098 **/
James Smarted957682007-06-17 19:56:37 -05002099void
James Smart51ef4c22007-08-02 11:10:31 -04002100lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002101{
2102 uint32_t hbqno;
2103
James Smart51ef4c22007-08-02 11:10:31 -04002104 if (hbq_buffer) {
2105 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002106 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002107 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002108 }
2109}
2110
James Smarte59058c2008-08-24 21:49:00 -04002111/**
James Smart3621a712009-04-06 18:47:14 -04002112 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002113 * @mbxCommand: mailbox command code.
2114 *
2115 * This function is called by the mailbox event handler function to verify
2116 * that the completed mailbox command is a legitimate mailbox command. If the
2117 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2118 * and the mailbox event handler will take the HBA offline.
2119 **/
dea31012005-04-17 16:05:31 -05002120static int
2121lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2122{
2123 uint8_t ret;
2124
2125 switch (mbxCommand) {
2126 case MBX_LOAD_SM:
2127 case MBX_READ_NV:
2128 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002129 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002130 case MBX_RUN_BIU_DIAG:
2131 case MBX_INIT_LINK:
2132 case MBX_DOWN_LINK:
2133 case MBX_CONFIG_LINK:
2134 case MBX_CONFIG_RING:
2135 case MBX_RESET_RING:
2136 case MBX_READ_CONFIG:
2137 case MBX_READ_RCONFIG:
2138 case MBX_READ_SPARM:
2139 case MBX_READ_STATUS:
2140 case MBX_READ_RPI:
2141 case MBX_READ_XRI:
2142 case MBX_READ_REV:
2143 case MBX_READ_LNK_STAT:
2144 case MBX_REG_LOGIN:
2145 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002146 case MBX_CLEAR_LA:
2147 case MBX_DUMP_MEMORY:
2148 case MBX_DUMP_CONTEXT:
2149 case MBX_RUN_DIAGS:
2150 case MBX_RESTART:
2151 case MBX_UPDATE_CFG:
2152 case MBX_DOWN_LOAD:
2153 case MBX_DEL_LD_ENTRY:
2154 case MBX_RUN_PROGRAM:
2155 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002156 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002157 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002158 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002159 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002160 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002161 case MBX_LOAD_AREA:
2162 case MBX_RUN_BIU_DIAG64:
2163 case MBX_CONFIG_PORT:
2164 case MBX_READ_SPARM64:
2165 case MBX_READ_RPI64:
2166 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002167 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002168 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002169 case MBX_SET_DEBUG:
2170 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002171 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002172 case MBX_REG_VPI:
2173 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002174 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002175 case MBX_PORT_CAPABILITIES:
2176 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002177 case MBX_SLI4_CONFIG:
2178 case MBX_SLI4_REQ_FTRS:
2179 case MBX_REG_FCFI:
2180 case MBX_UNREG_FCFI:
2181 case MBX_REG_VFI:
2182 case MBX_UNREG_VFI:
2183 case MBX_INIT_VPI:
2184 case MBX_INIT_VFI:
2185 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002186 case MBX_READ_EVENT_LOG_STATUS:
2187 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002188 case MBX_SECURITY_MGMT:
2189 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002190 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002191 ret = mbxCommand;
2192 break;
2193 default:
2194 ret = MBX_SHUTDOWN;
2195 break;
2196 }
James Smart2e0fef82007-06-17 19:56:36 -05002197 return ret;
dea31012005-04-17 16:05:31 -05002198}
James Smarte59058c2008-08-24 21:49:00 -04002199
2200/**
James Smart3621a712009-04-06 18:47:14 -04002201 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002202 * @phba: Pointer to HBA context object.
2203 * @pmboxq: Pointer to mailbox command.
2204 *
2205 * This is completion handler function for mailbox commands issued from
2206 * lpfc_sli_issue_mbox_wait function. This function is called by the
2207 * mailbox event handler function with no lock held. This function
2208 * will wake up thread waiting on the wait queue pointed by context1
2209 * of the mailbox.
2210 **/
James Smart04c68492009-05-22 14:52:52 -04002211void
James Smart2e0fef82007-06-17 19:56:36 -05002212lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002213{
2214 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002215 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002216
2217 /*
2218 * If pdone_q is empty, the driver thread gave up waiting and
2219 * continued running.
2220 */
James Smart7054a602007-04-25 09:52:34 -04002221 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002222 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002223 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2224 if (pdone_q)
2225 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002226 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002227 return;
2228}
2229
James Smarte59058c2008-08-24 21:49:00 -04002230
2231/**
James Smart3621a712009-04-06 18:47:14 -04002232 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002233 * @phba: Pointer to HBA context object.
2234 * @pmb: Pointer to mailbox object.
2235 *
2236 * This function is the default mailbox completion handler. It
2237 * frees the memory resources associated with the completed mailbox
2238 * command. If the completed command is a REG_LOGIN mailbox command,
2239 * this function will issue a UREG_LOGIN to re-claim the RPI.
2240 **/
dea31012005-04-17 16:05:31 -05002241void
James Smart2e0fef82007-06-17 19:56:36 -05002242lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002243{
James Smartd439d282010-09-29 11:18:45 -04002244 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002245 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002246 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002247 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002248 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002249 int rc;
2250
dea31012005-04-17 16:05:31 -05002251 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002252
dea31012005-04-17 16:05:31 -05002253 if (mp) {
2254 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2255 kfree(mp);
2256 }
James Smart7054a602007-04-25 09:52:34 -04002257
2258 /*
2259 * If a REG_LOGIN succeeded after node is destroyed or node
2260 * is in re-discovery driver need to cleanup the RPI.
2261 */
James Smart2e0fef82007-06-17 19:56:36 -05002262 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002263 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2264 !pmb->u.mb.mbxStatus) {
2265 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002266 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002267 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002268 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002269 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002270 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2271 if (rc != MBX_NOT_FINISHED)
2272 return;
2273 }
2274
James Smart695a8142010-01-26 23:08:03 -05002275 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2276 !(phba->pport->load_flag & FC_UNLOADING) &&
2277 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002278 shost = lpfc_shost_from_vport(vport);
2279 spin_lock_irq(shost->host_lock);
2280 vport->vpi_state |= LPFC_VPI_REGISTERED;
2281 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2282 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002283 }
2284
James Smartd439d282010-09-29 11:18:45 -04002285 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2286 ndlp = (struct lpfc_nodelist *)pmb->context2;
2287 lpfc_nlp_put(ndlp);
2288 pmb->context2 = NULL;
2289 }
2290
James Smartdcf2a4e2010-09-29 11:18:53 -04002291 /* Check security permission status on INIT_LINK mailbox command */
2292 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2293 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2294 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2295 "2860 SLI authentication is required "
2296 "for INIT_LINK but has not done yet\n");
2297
James Smart04c68492009-05-22 14:52:52 -04002298 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2299 lpfc_sli4_mbox_cmd_free(phba, pmb);
2300 else
2301 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002302}
James Smartbe6bb942015-04-07 15:07:22 -04002303 /**
2304 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2305 * @phba: Pointer to HBA context object.
2306 * @pmb: Pointer to mailbox object.
2307 *
2308 * This function is the unreg rpi mailbox completion handler. It
2309 * frees the memory resources associated with the completed mailbox
2310 * command. An additional refrenece is put on the ndlp to prevent
2311 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2312 * the unreg mailbox command completes, this routine puts the
2313 * reference back.
2314 *
2315 **/
2316void
2317lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2318{
2319 struct lpfc_vport *vport = pmb->vport;
2320 struct lpfc_nodelist *ndlp;
2321
2322 ndlp = pmb->context1;
2323 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2324 if (phba->sli_rev == LPFC_SLI_REV4 &&
2325 (bf_get(lpfc_sli_intf_if_type,
2326 &phba->sli4_hba.sli_intf) ==
2327 LPFC_SLI_INTF_IF_TYPE_2)) {
2328 if (ndlp) {
2329 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2330 "0010 UNREG_LOGIN vpi:%x "
2331 "rpi:%x DID:%x map:%x %p\n",
2332 vport->vpi, ndlp->nlp_rpi,
2333 ndlp->nlp_DID,
2334 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002335 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002336 lpfc_nlp_put(ndlp);
2337 }
2338 }
2339 }
2340
2341 mempool_free(pmb, phba->mbox_mem_pool);
2342}
dea31012005-04-17 16:05:31 -05002343
James Smarte59058c2008-08-24 21:49:00 -04002344/**
James Smart3621a712009-04-06 18:47:14 -04002345 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002346 * @phba: Pointer to HBA context object.
2347 *
2348 * This function is called with no lock held. This function processes all
2349 * the completed mailbox commands and gives it to upper layers. The interrupt
2350 * service routine processes mailbox completion interrupt and adds completed
2351 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2352 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2353 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2354 * function returns the mailbox commands to the upper layer by calling the
2355 * completion handler function of each mailbox.
2356 **/
dea31012005-04-17 16:05:31 -05002357int
James Smart2e0fef82007-06-17 19:56:36 -05002358lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002359{
James Smart92d7f7b2007-06-17 19:56:38 -05002360 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002361 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002362 int rc;
2363 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002364
2365 phba->sli.slistat.mbox_event++;
2366
James Smart92d7f7b2007-06-17 19:56:38 -05002367 /* Get all completed mailboxe buffers into the cmplq */
2368 spin_lock_irq(&phba->hbalock);
2369 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2370 spin_unlock_irq(&phba->hbalock);
2371
dea31012005-04-17 16:05:31 -05002372 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002373 do {
2374 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2375 if (pmb == NULL)
2376 break;
2377
James Smart04c68492009-05-22 14:52:52 -04002378 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002379
James Smart858c9f62007-06-17 19:56:39 -05002380 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2381 if (pmb->vport) {
2382 lpfc_debugfs_disc_trc(pmb->vport,
2383 LPFC_DISC_TRC_MBOX_VPORT,
2384 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2385 (uint32_t)pmbox->mbxCommand,
2386 pmbox->un.varWords[0],
2387 pmbox->un.varWords[1]);
2388 }
2389 else {
2390 lpfc_debugfs_disc_trc(phba->pport,
2391 LPFC_DISC_TRC_MBOX,
2392 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2393 (uint32_t)pmbox->mbxCommand,
2394 pmbox->un.varWords[0],
2395 pmbox->un.varWords[1]);
2396 }
2397 }
2398
dea31012005-04-17 16:05:31 -05002399 /*
2400 * It is a fatal error if unknown mbox command completion.
2401 */
2402 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2403 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002404 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002405 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002406 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002407 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002408 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002409 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002410 lpfc_sli_config_mbox_subsys_get(phba,
2411 pmb),
2412 lpfc_sli_config_mbox_opcode_get(phba,
2413 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002414 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002415 phba->work_hs = HS_FFER3;
2416 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002417 continue;
dea31012005-04-17 16:05:31 -05002418 }
2419
dea31012005-04-17 16:05:31 -05002420 if (pmbox->mbxStatus) {
2421 phba->sli.slistat.mbox_stat_err++;
2422 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2423 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002424 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002425 LOG_MBOX | LOG_SLI,
2426 "(%d):0305 Mbox cmd cmpl "
2427 "error - RETRYing Data: x%x "
2428 "(x%x/x%x) x%x x%x x%x\n",
2429 pmb->vport ? pmb->vport->vpi : 0,
2430 pmbox->mbxCommand,
2431 lpfc_sli_config_mbox_subsys_get(phba,
2432 pmb),
2433 lpfc_sli_config_mbox_opcode_get(phba,
2434 pmb),
2435 pmbox->mbxStatus,
2436 pmbox->un.varWords[0],
2437 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002438 pmbox->mbxStatus = 0;
2439 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002440 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002441 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002442 continue;
dea31012005-04-17 16:05:31 -05002443 }
2444 }
2445
2446 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002447 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002448 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002449 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2450 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002451 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002452 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002453 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2454 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002455 pmb->mbox_cmpl,
2456 *((uint32_t *) pmbox),
2457 pmbox->un.varWords[0],
2458 pmbox->un.varWords[1],
2459 pmbox->un.varWords[2],
2460 pmbox->un.varWords[3],
2461 pmbox->un.varWords[4],
2462 pmbox->un.varWords[5],
2463 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002464 pmbox->un.varWords[7],
2465 pmbox->un.varWords[8],
2466 pmbox->un.varWords[9],
2467 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002468
James Smart92d7f7b2007-06-17 19:56:38 -05002469 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002470 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002471 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002472 return 0;
dea31012005-04-17 16:05:31 -05002473}
James Smart92d7f7b2007-06-17 19:56:38 -05002474
James Smarte59058c2008-08-24 21:49:00 -04002475/**
James Smart3621a712009-04-06 18:47:14 -04002476 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002477 * @phba: Pointer to HBA context object.
2478 * @pring: Pointer to driver SLI ring object.
2479 * @tag: buffer tag.
2480 *
2481 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2482 * is set in the tag the buffer is posted for a particular exchange,
2483 * the function will return the buffer without replacing the buffer.
2484 * If the buffer is for unsolicited ELS or CT traffic, this function
2485 * returns the buffer and also posts another buffer to the firmware.
2486 **/
James Smart76bb24e2007-10-27 13:38:00 -04002487static struct lpfc_dmabuf *
2488lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002489 struct lpfc_sli_ring *pring,
2490 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002491{
James Smart9f1e1b52008-12-04 22:39:40 -05002492 struct hbq_dmabuf *hbq_entry;
2493
James Smart76bb24e2007-10-27 13:38:00 -04002494 if (tag & QUE_BUFTAG_BIT)
2495 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002496 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2497 if (!hbq_entry)
2498 return NULL;
2499 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002500}
James Smart57127f12007-10-27 13:37:05 -04002501
James Smart3772a992009-05-22 14:50:54 -04002502/**
2503 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2504 * @phba: Pointer to HBA context object.
2505 * @pring: Pointer to driver SLI ring object.
2506 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2507 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2508 * @fch_type: the type for the first frame of the sequence.
2509 *
2510 * This function is called with no lock held. This function uses the r_ctl and
2511 * type of the received sequence to find the correct callback function to call
2512 * to process the sequence.
2513 **/
2514static int
2515lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2516 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2517 uint32_t fch_type)
2518{
2519 int i;
2520
James Smartf358dd02017-02-12 13:52:34 -08002521 switch (fch_type) {
2522 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002523 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002524 return 1;
2525 default:
2526 break;
2527 }
2528
James Smart3772a992009-05-22 14:50:54 -04002529 /* unSolicited Responses */
2530 if (pring->prt[0].profile) {
2531 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2532 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2533 saveq);
2534 return 1;
2535 }
2536 /* We must search, based on rctl / type
2537 for the right routine */
2538 for (i = 0; i < pring->num_mask; i++) {
2539 if ((pring->prt[i].rctl == fch_r_ctl) &&
2540 (pring->prt[i].type == fch_type)) {
2541 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2542 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2543 (phba, pring, saveq);
2544 return 1;
2545 }
2546 }
2547 return 0;
2548}
James Smarte59058c2008-08-24 21:49:00 -04002549
2550/**
James Smart3621a712009-04-06 18:47:14 -04002551 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002552 * @phba: Pointer to HBA context object.
2553 * @pring: Pointer to driver SLI ring object.
2554 * @saveq: Pointer to the unsolicited iocb.
2555 *
2556 * This function is called with no lock held by the ring event handler
2557 * when there is an unsolicited iocb posted to the response ring by the
2558 * firmware. This function gets the buffer associated with the iocbs
2559 * and calls the event handler for the ring. This function handles both
2560 * qring buffers and hbq buffers.
2561 * When the function returns 1 the caller can free the iocb object otherwise
2562 * upper layer functions will free the iocb objects.
2563 **/
dea31012005-04-17 16:05:31 -05002564static int
2565lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2566 struct lpfc_iocbq *saveq)
2567{
2568 IOCB_t * irsp;
2569 WORD5 * w5p;
2570 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002571 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002572 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002573
dea31012005-04-17 16:05:31 -05002574 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002575
2576 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2577 if (pring->lpfc_sli_rcv_async_status)
2578 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2579 else
2580 lpfc_printf_log(phba,
2581 KERN_WARNING,
2582 LOG_SLI,
2583 "0316 Ring %d handler: unexpected "
2584 "ASYNC_STATUS iocb received evt_code "
2585 "0x%x\n",
2586 pring->ringno,
2587 irsp->un.asyncstat.evt_code);
2588 return 1;
2589 }
2590
James Smart3163f722008-02-08 18:50:25 -05002591 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2592 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2593 if (irsp->ulpBdeCount > 0) {
2594 dmzbuf = lpfc_sli_get_buff(phba, pring,
2595 irsp->un.ulpWord[3]);
2596 lpfc_in_buf_free(phba, dmzbuf);
2597 }
2598
2599 if (irsp->ulpBdeCount > 1) {
2600 dmzbuf = lpfc_sli_get_buff(phba, pring,
2601 irsp->unsli3.sli3Words[3]);
2602 lpfc_in_buf_free(phba, dmzbuf);
2603 }
2604
2605 if (irsp->ulpBdeCount > 2) {
2606 dmzbuf = lpfc_sli_get_buff(phba, pring,
2607 irsp->unsli3.sli3Words[7]);
2608 lpfc_in_buf_free(phba, dmzbuf);
2609 }
2610
2611 return 1;
2612 }
2613
James Smart92d7f7b2007-06-17 19:56:38 -05002614 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002615 if (irsp->ulpBdeCount != 0) {
2616 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002617 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002618 if (!saveq->context2)
2619 lpfc_printf_log(phba,
2620 KERN_ERR,
2621 LOG_SLI,
2622 "0341 Ring %d Cannot find buffer for "
2623 "an unsolicited iocb. tag 0x%x\n",
2624 pring->ringno,
2625 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002626 }
2627 if (irsp->ulpBdeCount == 2) {
2628 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002629 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002630 if (!saveq->context3)
2631 lpfc_printf_log(phba,
2632 KERN_ERR,
2633 LOG_SLI,
2634 "0342 Ring %d Cannot find buffer for an"
2635 " unsolicited iocb. tag 0x%x\n",
2636 pring->ringno,
2637 irsp->unsli3.sli3Words[7]);
2638 }
2639 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002640 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002641 if (irsp->ulpBdeCount != 0) {
2642 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2643 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002644 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002645 lpfc_printf_log(phba,
2646 KERN_ERR,
2647 LOG_SLI,
2648 "0343 Ring %d Cannot find "
2649 "buffer for an unsolicited iocb"
2650 ". tag 0x%x\n", pring->ringno,
2651 irsp->un.ulpWord[3]);
2652 }
2653 if (irsp->ulpBdeCount == 2) {
2654 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2655 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002656 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002657 lpfc_printf_log(phba,
2658 KERN_ERR,
2659 LOG_SLI,
2660 "0344 Ring %d Cannot find "
2661 "buffer for an unsolicited "
2662 "iocb. tag 0x%x\n",
2663 pring->ringno,
2664 irsp->unsli3.sli3Words[7]);
2665 }
2666 }
James Smart92d7f7b2007-06-17 19:56:38 -05002667 }
James Smart9c2face2008-01-11 01:53:18 -05002668 if (irsp->ulpBdeCount != 0 &&
2669 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2670 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2671 int found = 0;
2672
2673 /* search continue save q for same XRI */
2674 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002675 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2676 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002677 list_add_tail(&saveq->list, &iocbq->list);
2678 found = 1;
2679 break;
2680 }
2681 }
2682 if (!found)
2683 list_add_tail(&saveq->clist,
2684 &pring->iocb_continue_saveq);
2685 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2686 list_del_init(&iocbq->clist);
2687 saveq = iocbq;
2688 irsp = &(saveq->iocb);
2689 } else
2690 return 0;
2691 }
2692 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2693 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2694 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002695 Rctl = FC_RCTL_ELS_REQ;
2696 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002697 } else {
2698 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2699 Rctl = w5p->hcsw.Rctl;
2700 Type = w5p->hcsw.Type;
2701
2702 /* Firmware Workaround */
2703 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2704 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2705 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002706 Rctl = FC_RCTL_ELS_REQ;
2707 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002708 w5p->hcsw.Rctl = Rctl;
2709 w5p->hcsw.Type = Type;
2710 }
2711 }
James Smart92d7f7b2007-06-17 19:56:38 -05002712
James Smart3772a992009-05-22 14:50:54 -04002713 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002714 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002715 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002716 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002717 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002718
James Smart92d7f7b2007-06-17 19:56:38 -05002719 return 1;
dea31012005-04-17 16:05:31 -05002720}
2721
James Smarte59058c2008-08-24 21:49:00 -04002722/**
James Smart3621a712009-04-06 18:47:14 -04002723 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002724 * @phba: Pointer to HBA context object.
2725 * @pring: Pointer to driver SLI ring object.
2726 * @prspiocb: Pointer to response iocb object.
2727 *
2728 * This function looks up the iocb_lookup table to get the command iocb
2729 * corresponding to the given response iocb using the iotag of the
2730 * response iocb. This function is called with the hbalock held.
2731 * This function returns the command iocb object if it finds the command
2732 * iocb else returns NULL.
2733 **/
dea31012005-04-17 16:05:31 -05002734static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002735lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2736 struct lpfc_sli_ring *pring,
2737 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002738{
dea31012005-04-17 16:05:31 -05002739 struct lpfc_iocbq *cmd_iocb = NULL;
2740 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002741 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002742
James Bottomley604a3e32005-10-29 10:28:33 -05002743 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002744
James Bottomley604a3e32005-10-29 10:28:33 -05002745 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2746 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002747 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002748 /* remove from txcmpl queue list */
2749 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002750 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002751 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002752 }
dea31012005-04-17 16:05:31 -05002753 }
2754
dea31012005-04-17 16:05:31 -05002755 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002756 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002757 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002758 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002759 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002760 return NULL;
2761}
2762
James Smarte59058c2008-08-24 21:49:00 -04002763/**
James Smart3772a992009-05-22 14:50:54 -04002764 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2765 * @phba: Pointer to HBA context object.
2766 * @pring: Pointer to driver SLI ring object.
2767 * @iotag: IOCB tag.
2768 *
2769 * This function looks up the iocb_lookup table to get the command iocb
2770 * corresponding to the given iotag. This function is called with the
2771 * hbalock held.
2772 * This function returns the command iocb object if it finds the command
2773 * iocb else returns NULL.
2774 **/
2775static struct lpfc_iocbq *
2776lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2777 struct lpfc_sli_ring *pring, uint16_t iotag)
2778{
James Smart895427b2017-02-12 13:52:30 -08002779 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002780
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002781 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002782 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2783 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002784 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2785 /* remove from txcmpl queue list */
2786 list_del_init(&cmd_iocb->list);
2787 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002788 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002789 }
James Smart3772a992009-05-22 14:50:54 -04002790 }
James Smart89533e92016-10-13 15:06:15 -07002791
James Smart3772a992009-05-22 14:50:54 -04002792 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002793 "0372 iotag x%x lookup error: max iotag (x%x) "
2794 "iocb_flag x%x\n",
2795 iotag, phba->sli.last_iotag,
2796 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002797 return NULL;
2798}
2799
2800/**
James Smart3621a712009-04-06 18:47:14 -04002801 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002802 * @phba: Pointer to HBA context object.
2803 * @pring: Pointer to driver SLI ring object.
2804 * @saveq: Pointer to the response iocb to be processed.
2805 *
2806 * This function is called by the ring event handler for non-fcp
2807 * rings when there is a new response iocb in the response ring.
2808 * The caller is not required to hold any locks. This function
2809 * gets the command iocb associated with the response iocb and
2810 * calls the completion handler for the command iocb. If there
2811 * is no completion handler, the function will free the resources
2812 * associated with command iocb. If the response iocb is for
2813 * an already aborted command iocb, the status of the completion
2814 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2815 * This function always returns 1.
2816 **/
dea31012005-04-17 16:05:31 -05002817static int
James Smart2e0fef82007-06-17 19:56:36 -05002818lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002819 struct lpfc_iocbq *saveq)
2820{
James Smart2e0fef82007-06-17 19:56:36 -05002821 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002822 int rc = 1;
2823 unsigned long iflag;
2824
2825 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002826 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002827 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002828 spin_unlock_irqrestore(&phba->hbalock, iflag);
2829
dea31012005-04-17 16:05:31 -05002830 if (cmdiocbp) {
2831 if (cmdiocbp->iocb_cmpl) {
2832 /*
James Smartea2151b2008-09-07 11:52:10 -04002833 * If an ELS command failed send an event to mgmt
2834 * application.
2835 */
2836 if (saveq->iocb.ulpStatus &&
2837 (pring->ringno == LPFC_ELS_RING) &&
2838 (cmdiocbp->iocb.ulpCommand ==
2839 CMD_ELS_REQUEST64_CR))
2840 lpfc_send_els_failure_event(phba,
2841 cmdiocbp, saveq);
2842
2843 /*
dea31012005-04-17 16:05:31 -05002844 * Post all ELS completions to the worker thread.
2845 * All other are passed to the completion callback.
2846 */
2847 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002848 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2849 (cmdiocbp->iocb_flag &
2850 LPFC_DRIVER_ABORTED)) {
2851 spin_lock_irqsave(&phba->hbalock,
2852 iflag);
James Smart07951072007-04-25 09:51:38 -04002853 cmdiocbp->iocb_flag &=
2854 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002855 spin_unlock_irqrestore(&phba->hbalock,
2856 iflag);
James Smart07951072007-04-25 09:51:38 -04002857 saveq->iocb.ulpStatus =
2858 IOSTAT_LOCAL_REJECT;
2859 saveq->iocb.un.ulpWord[4] =
2860 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002861
2862 /* Firmware could still be in progress
2863 * of DMAing payload, so don't free data
2864 * buffer till after a hbeat.
2865 */
James Smart341af102010-01-26 23:07:37 -05002866 spin_lock_irqsave(&phba->hbalock,
2867 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002868 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002869 spin_unlock_irqrestore(&phba->hbalock,
2870 iflag);
2871 }
James Smart0f65ff62010-02-26 14:14:23 -05002872 if (phba->sli_rev == LPFC_SLI_REV4) {
2873 if (saveq->iocb_flag &
2874 LPFC_EXCHANGE_BUSY) {
2875 /* Set cmdiocb flag for the
2876 * exchange busy so sgl (xri)
2877 * will not be released until
2878 * the abort xri is received
2879 * from hba.
2880 */
2881 spin_lock_irqsave(
2882 &phba->hbalock, iflag);
2883 cmdiocbp->iocb_flag |=
2884 LPFC_EXCHANGE_BUSY;
2885 spin_unlock_irqrestore(
2886 &phba->hbalock, iflag);
2887 }
2888 if (cmdiocbp->iocb_flag &
2889 LPFC_DRIVER_ABORTED) {
2890 /*
2891 * Clear LPFC_DRIVER_ABORTED
2892 * bit in case it was driver
2893 * initiated abort.
2894 */
2895 spin_lock_irqsave(
2896 &phba->hbalock, iflag);
2897 cmdiocbp->iocb_flag &=
2898 ~LPFC_DRIVER_ABORTED;
2899 spin_unlock_irqrestore(
2900 &phba->hbalock, iflag);
2901 cmdiocbp->iocb.ulpStatus =
2902 IOSTAT_LOCAL_REJECT;
2903 cmdiocbp->iocb.un.ulpWord[4] =
2904 IOERR_ABORT_REQUESTED;
2905 /*
2906 * For SLI4, irsiocb contains
2907 * NO_XRI in sli_xritag, it
2908 * shall not affect releasing
2909 * sgl (xri) process.
2910 */
2911 saveq->iocb.ulpStatus =
2912 IOSTAT_LOCAL_REJECT;
2913 saveq->iocb.un.ulpWord[4] =
2914 IOERR_SLI_ABORTED;
2915 spin_lock_irqsave(
2916 &phba->hbalock, iflag);
2917 saveq->iocb_flag |=
2918 LPFC_DELAY_MEM_FREE;
2919 spin_unlock_irqrestore(
2920 &phba->hbalock, iflag);
2921 }
James Smart07951072007-04-25 09:51:38 -04002922 }
dea31012005-04-17 16:05:31 -05002923 }
James Smart2e0fef82007-06-17 19:56:36 -05002924 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002925 } else
2926 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002927 } else {
2928 /*
2929 * Unknown initiating command based on the response iotag.
2930 * This could be the case on the ELS ring because of
2931 * lpfc_els_abort().
2932 */
2933 if (pring->ringno != LPFC_ELS_RING) {
2934 /*
2935 * Ring <ringno> handler: unexpected completion IoTag
2936 * <IoTag>
2937 */
James Smarta257bf92009-04-06 18:48:10 -04002938 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002939 "0322 Ring %d handler: "
2940 "unexpected completion IoTag x%x "
2941 "Data: x%x x%x x%x x%x\n",
2942 pring->ringno,
2943 saveq->iocb.ulpIoTag,
2944 saveq->iocb.ulpStatus,
2945 saveq->iocb.un.ulpWord[4],
2946 saveq->iocb.ulpCommand,
2947 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002948 }
2949 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002950
dea31012005-04-17 16:05:31 -05002951 return rc;
2952}
2953
James Smarte59058c2008-08-24 21:49:00 -04002954/**
James Smart3621a712009-04-06 18:47:14 -04002955 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002956 * @phba: Pointer to HBA context object.
2957 * @pring: Pointer to driver SLI ring object.
2958 *
2959 * This function is called from the iocb ring event handlers when
2960 * put pointer is ahead of the get pointer for a ring. This function signal
2961 * an error attention condition to the worker thread and the worker
2962 * thread will transition the HBA to offline state.
2963 **/
James Smart2e0fef82007-06-17 19:56:36 -05002964static void
2965lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002966{
James Smart34b02dc2008-08-24 21:49:55 -04002967 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002968 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002969 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002970 * rsp ring <portRspMax>
2971 */
2972 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002973 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002974 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002975 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002976 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002977
James Smart2e0fef82007-06-17 19:56:36 -05002978 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002979
2980 /*
2981 * All error attention handlers are posted to
2982 * worker thread
2983 */
2984 phba->work_ha |= HA_ERATT;
2985 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002986
James Smart5e9d9b82008-06-14 22:52:53 -04002987 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002988
2989 return;
2990}
2991
James Smarte59058c2008-08-24 21:49:00 -04002992/**
James Smart3621a712009-04-06 18:47:14 -04002993 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002994 * @ptr: Pointer to address of HBA context object.
2995 *
2996 * This function is invoked by the Error Attention polling timer when the
2997 * timer times out. It will check the SLI Error Attention register for
2998 * possible attention events. If so, it will post an Error Attention event
2999 * and wake up worker thread to process it. Otherwise, it will set up the
3000 * Error Attention polling timer for the next poll.
3001 **/
3002void lpfc_poll_eratt(unsigned long ptr)
3003{
3004 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003005 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003006 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003007
3008 phba = (struct lpfc_hba *)ptr;
3009
James Smartaa6fbb72012-08-03 12:36:03 -04003010 /* Here we will also keep track of interrupts per sec of the hba */
3011 sli_intr = phba->sli.slistat.sli_intr;
3012
3013 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3014 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3015 sli_intr);
3016 else
3017 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3018
James Smart65791f12016-07-06 12:35:56 -07003019 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3020 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003021 phba->sli.slistat.sli_ips = cnt;
3022
3023 phba->sli.slistat.sli_prev_intr = sli_intr;
3024
James Smart93996272008-08-24 21:50:30 -04003025 /* Check chip HA register for error event */
3026 eratt = lpfc_sli_check_eratt(phba);
3027
3028 if (eratt)
3029 /* Tell the worker thread there is work to do */
3030 lpfc_worker_wake_up(phba);
3031 else
3032 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003033 mod_timer(&phba->eratt_poll,
3034 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003035 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003036 return;
3037}
3038
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003039
James Smarte59058c2008-08-24 21:49:00 -04003040/**
James Smart3621a712009-04-06 18:47:14 -04003041 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003042 * @phba: Pointer to HBA context object.
3043 * @pring: Pointer to driver SLI ring object.
3044 * @mask: Host attention register mask for this ring.
3045 *
3046 * This function is called from the interrupt context when there is a ring
3047 * event for the fcp ring. The caller does not hold any lock.
3048 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003049 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003050 * LE bit set. The function will call the completion handler of the command iocb
3051 * if the response iocb indicates a completion for a command iocb or it is
3052 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3053 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003054 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003055 * to check it explicitly.
3056 */
3057int
James Smart2e0fef82007-06-17 19:56:36 -05003058lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3059 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003060{
James Smart34b02dc2008-08-24 21:49:55 -04003061 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003062 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003063 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003064 struct lpfc_iocbq *cmdiocbq = NULL;
3065 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003066 uint32_t status;
3067 uint32_t portRspPut, portRspMax;
3068 int rc = 1;
3069 lpfc_iocb_type type;
3070 unsigned long iflag;
3071 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003072
James Smart2e0fef82007-06-17 19:56:36 -05003073 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003074 pring->stats.iocb_event++;
3075
dea31012005-04-17 16:05:31 -05003076 /*
3077 * The next available response entry should never exceed the maximum
3078 * entries. If it does, treat it as an adapter hardware error.
3079 */
James Smart7e56aa22012-08-03 12:35:34 -04003080 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003081 portRspPut = le32_to_cpu(pgp->rspPutInx);
3082 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003083 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003084 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003085 return 1;
3086 }
James Smart45ed1192009-10-02 15:17:02 -04003087 if (phba->fcp_ring_in_use) {
3088 spin_unlock_irqrestore(&phba->hbalock, iflag);
3089 return 1;
3090 } else
3091 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003092
3093 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003094 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003095 /*
3096 * Fetch an entry off the ring and copy it into a local data
3097 * structure. The copy involves a byte-swap since the
3098 * network byte order and pci byte orders are different.
3099 */
James Smarted957682007-06-17 19:56:37 -05003100 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003101 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003102
James Smart7e56aa22012-08-03 12:35:34 -04003103 if (++pring->sli.sli3.rspidx >= portRspMax)
3104 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003105
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003106 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3107 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003108 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003109 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003110 irsp = &rspiocbq.iocb;
3111
dea31012005-04-17 16:05:31 -05003112 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3113 pring->stats.iocb_rsp++;
3114 rsp_cmpl++;
3115
3116 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003117 /*
3118 * If resource errors reported from HBA, reduce
3119 * queuedepths of the SCSI device.
3120 */
3121 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003122 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3123 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003124 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003125 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003126 spin_lock_irqsave(&phba->hbalock, iflag);
3127 }
3128
dea31012005-04-17 16:05:31 -05003129 /* Rsp ring <ringno> error: IOCB */
3130 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003131 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003132 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003133 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003134 irsp->un.ulpWord[0],
3135 irsp->un.ulpWord[1],
3136 irsp->un.ulpWord[2],
3137 irsp->un.ulpWord[3],
3138 irsp->un.ulpWord[4],
3139 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003140 *(uint32_t *)&irsp->un1,
3141 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003142 }
3143
3144 switch (type) {
3145 case LPFC_ABORT_IOCB:
3146 case LPFC_SOL_IOCB:
3147 /*
3148 * Idle exchange closed via ABTS from port. No iocb
3149 * resources need to be recovered.
3150 */
3151 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003152 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003153 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003154 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003155 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003156 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003157 break;
3158 }
3159
James Bottomley604a3e32005-10-29 10:28:33 -05003160 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3161 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003162 if (unlikely(!cmdiocbq))
3163 break;
3164 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3165 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3166 if (cmdiocbq->iocb_cmpl) {
3167 spin_unlock_irqrestore(&phba->hbalock, iflag);
3168 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3169 &rspiocbq);
3170 spin_lock_irqsave(&phba->hbalock, iflag);
3171 }
dea31012005-04-17 16:05:31 -05003172 break;
James Smarta4bc3372006-12-02 13:34:16 -05003173 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003174 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003175 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003176 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003177 break;
dea31012005-04-17 16:05:31 -05003178 default:
3179 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3180 char adaptermsg[LPFC_MAX_ADPTMSG];
3181 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3182 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3183 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003184 dev_warn(&((phba->pcidev)->dev),
3185 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003186 phba->brd_no, adaptermsg);
3187 } else {
3188 /* Unknown IOCB command */
3189 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003190 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003191 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003192 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003193 irsp->ulpStatus,
3194 irsp->ulpIoTag,
3195 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003196 }
3197 break;
3198 }
3199
3200 /*
3201 * The response IOCB has been processed. Update the ring
3202 * pointer in SLIM. If the port response put pointer has not
3203 * been updated, sync the pgp->rspPutInx and fetch the new port
3204 * response put pointer.
3205 */
James Smart7e56aa22012-08-03 12:35:34 -04003206 writel(pring->sli.sli3.rspidx,
3207 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003208
James Smart7e56aa22012-08-03 12:35:34 -04003209 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003210 portRspPut = le32_to_cpu(pgp->rspPutInx);
3211 }
3212
3213 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3214 pring->stats.iocb_rsp_full++;
3215 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3216 writel(status, phba->CAregaddr);
3217 readl(phba->CAregaddr);
3218 }
3219 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3220 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3221 pring->stats.iocb_cmd_empty++;
3222
3223 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003224 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003225 lpfc_sli_resume_iocb(phba, pring);
3226
3227 if ((pring->lpfc_sli_cmd_available))
3228 (pring->lpfc_sli_cmd_available) (phba, pring);
3229
3230 }
3231
James Smart45ed1192009-10-02 15:17:02 -04003232 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003233 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003234 return rc;
3235}
3236
James Smarte59058c2008-08-24 21:49:00 -04003237/**
James Smart3772a992009-05-22 14:50:54 -04003238 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3239 * @phba: Pointer to HBA context object.
3240 * @pring: Pointer to driver SLI ring object.
3241 * @rspiocbp: Pointer to driver response IOCB object.
3242 *
3243 * This function is called from the worker thread when there is a slow-path
3244 * response IOCB to process. This function chains all the response iocbs until
3245 * seeing the iocb with the LE bit set. The function will call
3246 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3247 * completion of a command iocb. The function will call the
3248 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3249 * The function frees the resources or calls the completion handler if this
3250 * iocb is an abort completion. The function returns NULL when the response
3251 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3252 * this function shall chain the iocb on to the iocb_continueq and return the
3253 * response iocb passed in.
3254 **/
3255static struct lpfc_iocbq *
3256lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3257 struct lpfc_iocbq *rspiocbp)
3258{
3259 struct lpfc_iocbq *saveq;
3260 struct lpfc_iocbq *cmdiocbp;
3261 struct lpfc_iocbq *next_iocb;
3262 IOCB_t *irsp = NULL;
3263 uint32_t free_saveq;
3264 uint8_t iocb_cmd_type;
3265 lpfc_iocb_type type;
3266 unsigned long iflag;
3267 int rc;
3268
3269 spin_lock_irqsave(&phba->hbalock, iflag);
3270 /* First add the response iocb to the countinueq list */
3271 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3272 pring->iocb_continueq_cnt++;
3273
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003274 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003275 irsp = &rspiocbp->iocb;
3276 if (irsp->ulpLe) {
3277 /*
3278 * By default, the driver expects to free all resources
3279 * associated with this iocb completion.
3280 */
3281 free_saveq = 1;
3282 saveq = list_get_first(&pring->iocb_continueq,
3283 struct lpfc_iocbq, list);
3284 irsp = &(saveq->iocb);
3285 list_del_init(&pring->iocb_continueq);
3286 pring->iocb_continueq_cnt = 0;
3287
3288 pring->stats.iocb_rsp++;
3289
3290 /*
3291 * If resource errors reported from HBA, reduce
3292 * queuedepths of the SCSI device.
3293 */
3294 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003295 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3296 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003297 spin_unlock_irqrestore(&phba->hbalock, iflag);
3298 phba->lpfc_rampdown_queue_depth(phba);
3299 spin_lock_irqsave(&phba->hbalock, iflag);
3300 }
3301
3302 if (irsp->ulpStatus) {
3303 /* Rsp ring <ringno> error: IOCB */
3304 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3305 "0328 Rsp Ring %d error: "
3306 "IOCB Data: "
3307 "x%x x%x x%x x%x "
3308 "x%x x%x x%x x%x "
3309 "x%x x%x x%x x%x "
3310 "x%x x%x x%x x%x\n",
3311 pring->ringno,
3312 irsp->un.ulpWord[0],
3313 irsp->un.ulpWord[1],
3314 irsp->un.ulpWord[2],
3315 irsp->un.ulpWord[3],
3316 irsp->un.ulpWord[4],
3317 irsp->un.ulpWord[5],
3318 *(((uint32_t *) irsp) + 6),
3319 *(((uint32_t *) irsp) + 7),
3320 *(((uint32_t *) irsp) + 8),
3321 *(((uint32_t *) irsp) + 9),
3322 *(((uint32_t *) irsp) + 10),
3323 *(((uint32_t *) irsp) + 11),
3324 *(((uint32_t *) irsp) + 12),
3325 *(((uint32_t *) irsp) + 13),
3326 *(((uint32_t *) irsp) + 14),
3327 *(((uint32_t *) irsp) + 15));
3328 }
3329
3330 /*
3331 * Fetch the IOCB command type and call the correct completion
3332 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3333 * get freed back to the lpfc_iocb_list by the discovery
3334 * kernel thread.
3335 */
3336 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3337 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3338 switch (type) {
3339 case LPFC_SOL_IOCB:
3340 spin_unlock_irqrestore(&phba->hbalock, iflag);
3341 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3342 spin_lock_irqsave(&phba->hbalock, iflag);
3343 break;
3344
3345 case LPFC_UNSOL_IOCB:
3346 spin_unlock_irqrestore(&phba->hbalock, iflag);
3347 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3348 spin_lock_irqsave(&phba->hbalock, iflag);
3349 if (!rc)
3350 free_saveq = 0;
3351 break;
3352
3353 case LPFC_ABORT_IOCB:
3354 cmdiocbp = NULL;
3355 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3356 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3357 saveq);
3358 if (cmdiocbp) {
3359 /* Call the specified completion routine */
3360 if (cmdiocbp->iocb_cmpl) {
3361 spin_unlock_irqrestore(&phba->hbalock,
3362 iflag);
3363 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3364 saveq);
3365 spin_lock_irqsave(&phba->hbalock,
3366 iflag);
3367 } else
3368 __lpfc_sli_release_iocbq(phba,
3369 cmdiocbp);
3370 }
3371 break;
3372
3373 case LPFC_UNKNOWN_IOCB:
3374 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3375 char adaptermsg[LPFC_MAX_ADPTMSG];
3376 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3377 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3378 MAX_MSG_DATA);
3379 dev_warn(&((phba->pcidev)->dev),
3380 "lpfc%d: %s\n",
3381 phba->brd_no, adaptermsg);
3382 } else {
3383 /* Unknown IOCB command */
3384 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3385 "0335 Unknown IOCB "
3386 "command Data: x%x "
3387 "x%x x%x x%x\n",
3388 irsp->ulpCommand,
3389 irsp->ulpStatus,
3390 irsp->ulpIoTag,
3391 irsp->ulpContext);
3392 }
3393 break;
3394 }
3395
3396 if (free_saveq) {
3397 list_for_each_entry_safe(rspiocbp, next_iocb,
3398 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003399 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003400 __lpfc_sli_release_iocbq(phba, rspiocbp);
3401 }
3402 __lpfc_sli_release_iocbq(phba, saveq);
3403 }
3404 rspiocbp = NULL;
3405 }
3406 spin_unlock_irqrestore(&phba->hbalock, iflag);
3407 return rspiocbp;
3408}
3409
3410/**
3411 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003412 * @phba: Pointer to HBA context object.
3413 * @pring: Pointer to driver SLI ring object.
3414 * @mask: Host attention register mask for this ring.
3415 *
James Smart3772a992009-05-22 14:50:54 -04003416 * This routine wraps the actual slow_ring event process routine from the
3417 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003418 **/
James Smart3772a992009-05-22 14:50:54 -04003419void
James Smart2e0fef82007-06-17 19:56:36 -05003420lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3421 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003422{
James Smart3772a992009-05-22 14:50:54 -04003423 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3424}
3425
3426/**
3427 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3428 * @phba: Pointer to HBA context object.
3429 * @pring: Pointer to driver SLI ring object.
3430 * @mask: Host attention register mask for this ring.
3431 *
3432 * This function is called from the worker thread when there is a ring event
3433 * for non-fcp rings. The caller does not hold any lock. The function will
3434 * remove each response iocb in the response ring and calls the handle
3435 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3436 **/
3437static void
3438lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3439 struct lpfc_sli_ring *pring, uint32_t mask)
3440{
James Smart34b02dc2008-08-24 21:49:55 -04003441 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003442 IOCB_t *entry;
3443 IOCB_t *irsp = NULL;
3444 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003445 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003446 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003447 uint32_t status;
dea31012005-04-17 16:05:31 -05003448
James Smart34b02dc2008-08-24 21:49:55 -04003449 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003450 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003451 pring->stats.iocb_event++;
3452
dea31012005-04-17 16:05:31 -05003453 /*
3454 * The next available response entry should never exceed the maximum
3455 * entries. If it does, treat it as an adapter hardware error.
3456 */
James Smart7e56aa22012-08-03 12:35:34 -04003457 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003458 portRspPut = le32_to_cpu(pgp->rspPutInx);
3459 if (portRspPut >= portRspMax) {
3460 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003461 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003462 * rsp ring <portRspMax>
3463 */
James Smarted957682007-06-17 19:56:37 -05003464 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003465 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003466 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003467 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003468
James Smart2e0fef82007-06-17 19:56:36 -05003469 phba->link_state = LPFC_HBA_ERROR;
3470 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003471
3472 phba->work_hs = HS_FFER3;
3473 lpfc_handle_eratt(phba);
3474
James Smart3772a992009-05-22 14:50:54 -04003475 return;
dea31012005-04-17 16:05:31 -05003476 }
3477
3478 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003479 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003480 /*
3481 * Build a completion list and call the appropriate handler.
3482 * The process is to get the next available response iocb, get
3483 * a free iocb from the list, copy the response data into the
3484 * free iocb, insert to the continuation list, and update the
3485 * next response index to slim. This process makes response
3486 * iocb's in the ring available to DMA as fast as possible but
3487 * pays a penalty for a copy operation. Since the iocb is
3488 * only 32 bytes, this penalty is considered small relative to
3489 * the PCI reads for register values and a slim write. When
3490 * the ulpLe field is set, the entire Command has been
3491 * received.
3492 */
James Smarted957682007-06-17 19:56:37 -05003493 entry = lpfc_resp_iocb(phba, pring);
3494
James Smart858c9f62007-06-17 19:56:39 -05003495 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003496 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003497 if (rspiocbp == NULL) {
3498 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003499 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003500 break;
3501 }
3502
James Smarted957682007-06-17 19:56:37 -05003503 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3504 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003505 irsp = &rspiocbp->iocb;
3506
James Smart7e56aa22012-08-03 12:35:34 -04003507 if (++pring->sli.sli3.rspidx >= portRspMax)
3508 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003509
James Smarta58cbd52007-08-02 11:09:43 -04003510 if (pring->ringno == LPFC_ELS_RING) {
3511 lpfc_debugfs_slow_ring_trc(phba,
3512 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3513 *(((uint32_t *) irsp) + 4),
3514 *(((uint32_t *) irsp) + 6),
3515 *(((uint32_t *) irsp) + 7));
3516 }
3517
James Smart7e56aa22012-08-03 12:35:34 -04003518 writel(pring->sli.sli3.rspidx,
3519 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003520
James Smart3772a992009-05-22 14:50:54 -04003521 spin_unlock_irqrestore(&phba->hbalock, iflag);
3522 /* Handle the response IOCB */
3523 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3524 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003525
3526 /*
3527 * If the port response put pointer has not been updated, sync
3528 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3529 * response put pointer.
3530 */
James Smart7e56aa22012-08-03 12:35:34 -04003531 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003532 portRspPut = le32_to_cpu(pgp->rspPutInx);
3533 }
James Smart7e56aa22012-08-03 12:35:34 -04003534 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003535
James Smart92d7f7b2007-06-17 19:56:38 -05003536 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003537 /* At least one response entry has been freed */
3538 pring->stats.iocb_rsp_full++;
3539 /* SET RxRE_RSP in Chip Att register */
3540 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3541 writel(status, phba->CAregaddr);
3542 readl(phba->CAregaddr); /* flush */
3543 }
3544 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3545 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3546 pring->stats.iocb_cmd_empty++;
3547
3548 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003549 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003550 lpfc_sli_resume_iocb(phba, pring);
3551
3552 if ((pring->lpfc_sli_cmd_available))
3553 (pring->lpfc_sli_cmd_available) (phba, pring);
3554
3555 }
3556
James Smart2e0fef82007-06-17 19:56:36 -05003557 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003558 return;
dea31012005-04-17 16:05:31 -05003559}
3560
James Smarte59058c2008-08-24 21:49:00 -04003561/**
James Smart4f774512009-05-22 14:52:35 -04003562 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3563 * @phba: Pointer to HBA context object.
3564 * @pring: Pointer to driver SLI ring object.
3565 * @mask: Host attention register mask for this ring.
3566 *
3567 * This function is called from the worker thread when there is a pending
3568 * ELS response iocb on the driver internal slow-path response iocb worker
3569 * queue. The caller does not hold any lock. The function will remove each
3570 * response iocb from the response worker queue and calls the handle
3571 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3572 **/
3573static void
3574lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3575 struct lpfc_sli_ring *pring, uint32_t mask)
3576{
3577 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003578 struct hbq_dmabuf *dmabuf;
3579 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003580 unsigned long iflag;
3581
James Smart45ed1192009-10-02 15:17:02 -04003582 spin_lock_irqsave(&phba->hbalock, iflag);
3583 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3584 spin_unlock_irqrestore(&phba->hbalock, iflag);
3585 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003586 /* Get the response iocb from the head of work queue */
3587 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003588 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003589 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003590 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003591
3592 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3593 case CQE_CODE_COMPL_WQE:
3594 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3595 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003596 /* Translate ELS WCQE to response IOCBQ */
3597 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3598 irspiocbq);
3599 if (irspiocbq)
3600 lpfc_sli_sp_handle_rspiocb(phba, pring,
3601 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003602 break;
3603 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003604 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003605 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3606 cq_event);
3607 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3608 break;
3609 default:
3610 break;
3611 }
James Smart4f774512009-05-22 14:52:35 -04003612 }
3613}
3614
3615/**
James Smart3621a712009-04-06 18:47:14 -04003616 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003617 * @phba: Pointer to HBA context object.
3618 * @pring: Pointer to driver SLI ring object.
3619 *
3620 * This function aborts all iocbs in the given ring and frees all the iocb
3621 * objects in txq. This function issues an abort iocb for all the iocb commands
3622 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3623 * the return of this function. The caller is not required to hold any locks.
3624 **/
James Smart2e0fef82007-06-17 19:56:36 -05003625void
dea31012005-04-17 16:05:31 -05003626lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3627{
James Smart2534ba72007-04-25 09:52:20 -04003628 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003629 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003630
James Smart92d7f7b2007-06-17 19:56:38 -05003631 if (pring->ringno == LPFC_ELS_RING) {
3632 lpfc_fabric_abort_hba(phba);
3633 }
3634
dea31012005-04-17 16:05:31 -05003635 /* Error everything on txq and txcmplq
3636 * First do the txq.
3637 */
James Smartdb55fba2014-04-04 13:52:02 -04003638 if (phba->sli_rev >= LPFC_SLI_REV4) {
3639 spin_lock_irq(&pring->ring_lock);
3640 list_splice_init(&pring->txq, &completions);
3641 pring->txq_cnt = 0;
3642 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003643
James Smartdb55fba2014-04-04 13:52:02 -04003644 spin_lock_irq(&phba->hbalock);
3645 /* Next issue ABTS for everything on the txcmplq */
3646 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3647 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3648 spin_unlock_irq(&phba->hbalock);
3649 } else {
3650 spin_lock_irq(&phba->hbalock);
3651 list_splice_init(&pring->txq, &completions);
3652 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003653
James Smartdb55fba2014-04-04 13:52:02 -04003654 /* Next issue ABTS for everything on the txcmplq */
3655 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3656 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3657 spin_unlock_irq(&phba->hbalock);
3658 }
James Smart2534ba72007-04-25 09:52:20 -04003659
James Smarta257bf92009-04-06 18:48:10 -04003660 /* Cancel all the IOCBs from the completions list */
3661 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3662 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003663}
3664
James Smarte59058c2008-08-24 21:49:00 -04003665/**
James Smart895427b2017-02-12 13:52:30 -08003666 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3667 * @phba: Pointer to HBA context object.
3668 * @pring: Pointer to driver SLI ring object.
3669 *
3670 * This function aborts all iocbs in the given ring and frees all the iocb
3671 * objects in txq. This function issues an abort iocb for all the iocb commands
3672 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3673 * the return of this function. The caller is not required to hold any locks.
3674 **/
3675void
3676lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3677{
3678 LIST_HEAD(completions);
3679 struct lpfc_iocbq *iocb, *next_iocb;
3680
3681 if (pring->ringno == LPFC_ELS_RING)
3682 lpfc_fabric_abort_hba(phba);
3683
3684 spin_lock_irq(&phba->hbalock);
3685 /* Next issue ABTS for everything on the txcmplq */
3686 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3687 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3688 spin_unlock_irq(&phba->hbalock);
3689}
3690
3691
3692/**
James Smartdb55fba2014-04-04 13:52:02 -04003693 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3694 * @phba: Pointer to HBA context object.
3695 * @pring: Pointer to driver SLI ring object.
3696 *
3697 * This function aborts all iocbs in FCP rings and frees all the iocb
3698 * objects in txq. This function issues an abort iocb for all the iocb commands
3699 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3700 * the return of this function. The caller is not required to hold any locks.
3701 **/
3702void
3703lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3704{
3705 struct lpfc_sli *psli = &phba->sli;
3706 struct lpfc_sli_ring *pring;
3707 uint32_t i;
3708
3709 /* Look on all the FCP Rings for the iotag */
3710 if (phba->sli_rev >= LPFC_SLI_REV4) {
3711 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003712 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003713 lpfc_sli_abort_iocb_ring(phba, pring);
3714 }
3715 } else {
James Smart895427b2017-02-12 13:52:30 -08003716 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003717 lpfc_sli_abort_iocb_ring(phba, pring);
3718 }
3719}
3720
James Smart895427b2017-02-12 13:52:30 -08003721/**
3722 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3723 * @phba: Pointer to HBA context object.
3724 *
3725 * This function aborts all wqes in NVME rings. This function issues an
3726 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3727 * the txcmplq is not guaranteed to complete before the return of this
3728 * function. The caller is not required to hold any locks.
3729 **/
3730void
3731lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3732{
3733 struct lpfc_sli_ring *pring;
3734 uint32_t i;
3735
3736 if (phba->sli_rev < LPFC_SLI_REV4)
3737 return;
3738
3739 /* Abort all IO on each NVME ring. */
3740 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3741 pring = phba->sli4_hba.nvme_wq[i]->pring;
3742 lpfc_sli_abort_wqe_ring(phba, pring);
3743 }
3744}
3745
James Smartdb55fba2014-04-04 13:52:02 -04003746
3747/**
James Smart3621a712009-04-06 18:47:14 -04003748 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003749 * @phba: Pointer to HBA context object.
3750 *
3751 * This function flushes all iocbs in the fcp ring and frees all the iocb
3752 * objects in txq and txcmplq. This function will not issue abort iocbs
3753 * for all the iocb commands in txcmplq, they will just be returned with
3754 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3755 * slot has been permanently disabled.
3756 **/
3757void
3758lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3759{
3760 LIST_HEAD(txq);
3761 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003762 struct lpfc_sli *psli = &phba->sli;
3763 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003764 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003765
3766 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003767 /* Indicate the I/O queues are flushed */
3768 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003769 spin_unlock_irq(&phba->hbalock);
3770
James Smartdb55fba2014-04-04 13:52:02 -04003771 /* Look on all the FCP Rings for the iotag */
3772 if (phba->sli_rev >= LPFC_SLI_REV4) {
3773 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003774 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003775
James Smartdb55fba2014-04-04 13:52:02 -04003776 spin_lock_irq(&pring->ring_lock);
3777 /* Retrieve everything on txq */
3778 list_splice_init(&pring->txq, &txq);
3779 /* Retrieve everything on the txcmplq */
3780 list_splice_init(&pring->txcmplq, &txcmplq);
3781 pring->txq_cnt = 0;
3782 pring->txcmplq_cnt = 0;
3783 spin_unlock_irq(&pring->ring_lock);
3784
3785 /* Flush the txq */
3786 lpfc_sli_cancel_iocbs(phba, &txq,
3787 IOSTAT_LOCAL_REJECT,
3788 IOERR_SLI_DOWN);
3789 /* Flush the txcmpq */
3790 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3791 IOSTAT_LOCAL_REJECT,
3792 IOERR_SLI_DOWN);
3793 }
3794 } else {
James Smart895427b2017-02-12 13:52:30 -08003795 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003796
3797 spin_lock_irq(&phba->hbalock);
3798 /* Retrieve everything on txq */
3799 list_splice_init(&pring->txq, &txq);
3800 /* Retrieve everything on the txcmplq */
3801 list_splice_init(&pring->txcmplq, &txcmplq);
3802 pring->txq_cnt = 0;
3803 pring->txcmplq_cnt = 0;
3804 spin_unlock_irq(&phba->hbalock);
3805
3806 /* Flush the txq */
3807 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3808 IOERR_SLI_DOWN);
3809 /* Flush the txcmpq */
3810 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3811 IOERR_SLI_DOWN);
3812 }
James Smarta8e497d2008-08-24 21:50:11 -04003813}
3814
3815/**
James Smart895427b2017-02-12 13:52:30 -08003816 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3817 * @phba: Pointer to HBA context object.
3818 *
3819 * This function flushes all wqes in the nvme rings and frees all resources
3820 * in the txcmplq. This function does not issue abort wqes for the IO
3821 * commands in txcmplq, they will just be returned with
3822 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3823 * slot has been permanently disabled.
3824 **/
3825void
3826lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3827{
3828 LIST_HEAD(txcmplq);
3829 struct lpfc_sli_ring *pring;
3830 uint32_t i;
3831
3832 if (phba->sli_rev < LPFC_SLI_REV4)
3833 return;
3834
3835 /* Hint to other driver operations that a flush is in progress. */
3836 spin_lock_irq(&phba->hbalock);
3837 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3838 spin_unlock_irq(&phba->hbalock);
3839
3840 /* Cycle through all NVME rings and complete each IO with
3841 * a local driver reason code. This is a flush so no
3842 * abort exchange to FW.
3843 */
3844 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3845 pring = phba->sli4_hba.nvme_wq[i]->pring;
3846
3847 /* Retrieve everything on the txcmplq */
3848 spin_lock_irq(&pring->ring_lock);
3849 list_splice_init(&pring->txcmplq, &txcmplq);
3850 pring->txcmplq_cnt = 0;
3851 spin_unlock_irq(&pring->ring_lock);
3852
3853 /* Flush the txcmpq &&&PAE */
3854 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3855 IOSTAT_LOCAL_REJECT,
3856 IOERR_SLI_DOWN);
3857 }
3858}
3859
3860/**
James Smart3772a992009-05-22 14:50:54 -04003861 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003862 * @phba: Pointer to HBA context object.
3863 * @mask: Bit mask to be checked.
3864 *
3865 * This function reads the host status register and compares
3866 * with the provided bit mask to check if HBA completed
3867 * the restart. This function will wait in a loop for the
3868 * HBA to complete restart. If the HBA does not restart within
3869 * 15 iterations, the function will reset the HBA again. The
3870 * function returns 1 when HBA fail to restart otherwise returns
3871 * zero.
3872 **/
James Smart3772a992009-05-22 14:50:54 -04003873static int
3874lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003875{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003876 uint32_t status;
3877 int i = 0;
3878 int retval = 0;
dea31012005-04-17 16:05:31 -05003879
Jamie Wellnitz41415862006-02-28 19:25:27 -05003880 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003881 if (lpfc_readl(phba->HSregaddr, &status))
3882 return 1;
dea31012005-04-17 16:05:31 -05003883
Jamie Wellnitz41415862006-02-28 19:25:27 -05003884 /*
3885 * Check status register every 100ms for 5 retries, then every
3886 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3887 * every 2.5 sec for 4.
3888 * Break our of the loop if errors occurred during init.
3889 */
3890 while (((status & mask) != mask) &&
3891 !(status & HS_FFERM) &&
3892 i++ < 20) {
dea31012005-04-17 16:05:31 -05003893
Jamie Wellnitz41415862006-02-28 19:25:27 -05003894 if (i <= 5)
3895 msleep(10);
3896 else if (i <= 10)
3897 msleep(500);
3898 else
3899 msleep(2500);
dea31012005-04-17 16:05:31 -05003900
Jamie Wellnitz41415862006-02-28 19:25:27 -05003901 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003902 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003903 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003904 lpfc_sli_brdrestart(phba);
3905 }
3906 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003907 if (lpfc_readl(phba->HSregaddr, &status)) {
3908 retval = 1;
3909 break;
3910 }
dea31012005-04-17 16:05:31 -05003911 }
dea31012005-04-17 16:05:31 -05003912
Jamie Wellnitz41415862006-02-28 19:25:27 -05003913 /* Check to see if any errors occurred during init */
3914 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3916 "2751 Adapter failed to restart, "
3917 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3918 status,
3919 readl(phba->MBslimaddr + 0xa8),
3920 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003921 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003922 retval = 1;
3923 }
dea31012005-04-17 16:05:31 -05003924
Jamie Wellnitz41415862006-02-28 19:25:27 -05003925 return retval;
dea31012005-04-17 16:05:31 -05003926}
3927
James Smartda0436e2009-05-22 14:51:39 -04003928/**
3929 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3930 * @phba: Pointer to HBA context object.
3931 * @mask: Bit mask to be checked.
3932 *
3933 * This function checks the host status register to check if HBA is
3934 * ready. This function will wait in a loop for the HBA to be ready
3935 * If the HBA is not ready , the function will will reset the HBA PCI
3936 * function again. The function returns 1 when HBA fail to be ready
3937 * otherwise returns zero.
3938 **/
3939static int
3940lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3941{
3942 uint32_t status;
3943 int retval = 0;
3944
3945 /* Read the HBA Host Status Register */
3946 status = lpfc_sli4_post_status_check(phba);
3947
3948 if (status) {
3949 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3950 lpfc_sli_brdrestart(phba);
3951 status = lpfc_sli4_post_status_check(phba);
3952 }
3953
3954 /* Check to see if any errors occurred during init */
3955 if (status) {
3956 phba->link_state = LPFC_HBA_ERROR;
3957 retval = 1;
3958 } else
3959 phba->sli4_hba.intr_enable = 0;
3960
3961 return retval;
3962}
3963
3964/**
3965 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3966 * @phba: Pointer to HBA context object.
3967 * @mask: Bit mask to be checked.
3968 *
3969 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3970 * from the API jump table function pointer from the lpfc_hba struct.
3971 **/
3972int
3973lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3974{
3975 return phba->lpfc_sli_brdready(phba, mask);
3976}
3977
James Smart92908312006-03-07 15:04:13 -05003978#define BARRIER_TEST_PATTERN (0xdeadbeef)
3979
James Smarte59058c2008-08-24 21:49:00 -04003980/**
James Smart3621a712009-04-06 18:47:14 -04003981 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003982 * @phba: Pointer to HBA context object.
3983 *
James Smart1b511972011-12-13 13:23:09 -05003984 * This function is called before resetting an HBA. This function is called
3985 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003986 **/
James Smart2e0fef82007-06-17 19:56:36 -05003987void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003988{
James Smart65a29c12006-07-06 15:50:50 -04003989 uint32_t __iomem *resp_buf;
3990 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003991 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003992 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003993 int i;
3994 uint8_t hdrtype;
3995
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003996 lockdep_assert_held(&phba->hbalock);
3997
James Smart92908312006-03-07 15:04:13 -05003998 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3999 if (hdrtype != 0x80 ||
4000 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4001 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4002 return;
4003
4004 /*
4005 * Tell the other part of the chip to suspend temporarily all
4006 * its DMA activity.
4007 */
James Smart65a29c12006-07-06 15:50:50 -04004008 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004009
4010 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004011 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4012 return;
James Smart92908312006-03-07 15:04:13 -05004013 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4014 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004015 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004016
James Smart9940b972011-03-11 16:06:12 -05004017 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4018 return;
4019 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004020 /* Clear Chip error bit */
4021 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004022 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004023 }
4024
4025 mbox = 0;
4026 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4027 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4028
4029 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004030 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004031 writel(mbox, mbox_buf);
4032
James Smart9940b972011-03-11 16:06:12 -05004033 for (i = 0; i < 50; i++) {
4034 if (lpfc_readl((resp_buf + 1), &resp_data))
4035 return;
4036 if (resp_data != ~(BARRIER_TEST_PATTERN))
4037 mdelay(1);
4038 else
4039 break;
4040 }
4041 resp_data = 0;
4042 if (lpfc_readl((resp_buf + 1), &resp_data))
4043 return;
4044 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004045 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004046 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004047 goto restore_hc;
4048 else
4049 goto clear_errat;
4050 }
4051
4052 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004053 resp_data = 0;
4054 for (i = 0; i < 500; i++) {
4055 if (lpfc_readl(resp_buf, &resp_data))
4056 return;
4057 if (resp_data != mbox)
4058 mdelay(1);
4059 else
4060 break;
4061 }
James Smart92908312006-03-07 15:04:13 -05004062
4063clear_errat:
4064
James Smart9940b972011-03-11 16:06:12 -05004065 while (++i < 500) {
4066 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4067 return;
4068 if (!(ha_copy & HA_ERATT))
4069 mdelay(1);
4070 else
4071 break;
4072 }
James Smart92908312006-03-07 15:04:13 -05004073
4074 if (readl(phba->HAregaddr) & HA_ERATT) {
4075 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004076 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004077 }
4078
4079restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004080 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004081 writel(hc_copy, phba->HCregaddr);
4082 readl(phba->HCregaddr); /* flush */
4083}
4084
James Smarte59058c2008-08-24 21:49:00 -04004085/**
James Smart3621a712009-04-06 18:47:14 -04004086 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004087 * @phba: Pointer to HBA context object.
4088 *
4089 * This function issues a kill_board mailbox command and waits for
4090 * the error attention interrupt. This function is called for stopping
4091 * the firmware processing. The caller is not required to hold any
4092 * locks. This function calls lpfc_hba_down_post function to free
4093 * any pending commands after the kill. The function will return 1 when it
4094 * fails to kill the board else will return 0.
4095 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004096int
James Smart2e0fef82007-06-17 19:56:36 -05004097lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004098{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004099 struct lpfc_sli *psli;
4100 LPFC_MBOXQ_t *pmb;
4101 uint32_t status;
4102 uint32_t ha_copy;
4103 int retval;
4104 int i = 0;
4105
4106 psli = &phba->sli;
4107
4108 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004109 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004110 "0329 Kill HBA Data: x%x x%x\n",
4111 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004112
James Smart98c9ea52007-10-27 13:37:33 -04004113 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4114 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004115 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004116
4117 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004118 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004119 if (lpfc_readl(phba->HCregaddr, &status)) {
4120 spin_unlock_irq(&phba->hbalock);
4121 mempool_free(pmb, phba->mbox_mem_pool);
4122 return 1;
4123 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004124 status &= ~HC_ERINT_ENA;
4125 writel(status, phba->HCregaddr);
4126 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004127 phba->link_flag |= LS_IGNORE_ERATT;
4128 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004129
4130 lpfc_kill_board(phba, pmb);
4131 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4132 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4133
4134 if (retval != MBX_SUCCESS) {
4135 if (retval != MBX_BUSY)
4136 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004137 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4138 "2752 KILL_BOARD command failed retval %d\n",
4139 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004140 spin_lock_irq(&phba->hbalock);
4141 phba->link_flag &= ~LS_IGNORE_ERATT;
4142 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004143 return 1;
4144 }
4145
James Smartf4b4c682009-05-22 14:53:12 -04004146 spin_lock_irq(&phba->hbalock);
4147 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4148 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004149
Jamie Wellnitz41415862006-02-28 19:25:27 -05004150 mempool_free(pmb, phba->mbox_mem_pool);
4151
4152 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4153 * attention every 100ms for 3 seconds. If we don't get ERATT after
4154 * 3 seconds we still set HBA_ERROR state because the status of the
4155 * board is now undefined.
4156 */
James Smart9940b972011-03-11 16:06:12 -05004157 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4158 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004159 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4160 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004161 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4162 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004163 }
4164
4165 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004166 if (ha_copy & HA_ERATT) {
4167 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004168 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004169 }
James Smart2e0fef82007-06-17 19:56:36 -05004170 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004171 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004172 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004173 phba->link_flag &= ~LS_IGNORE_ERATT;
4174 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004175
Jamie Wellnitz41415862006-02-28 19:25:27 -05004176 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004177 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004178
James Smart2e0fef82007-06-17 19:56:36 -05004179 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004180}
4181
James Smarte59058c2008-08-24 21:49:00 -04004182/**
James Smart3772a992009-05-22 14:50:54 -04004183 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004184 * @phba: Pointer to HBA context object.
4185 *
4186 * This function resets the HBA by writing HC_INITFF to the control
4187 * register. After the HBA resets, this function resets all the iocb ring
4188 * indices. This function disables PCI layer parity checking during
4189 * the reset.
4190 * This function returns 0 always.
4191 * The caller is not required to hold any locks.
4192 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004193int
James Smart2e0fef82007-06-17 19:56:36 -05004194lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004195{
4196 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004197 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004198 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004199 int i;
dea31012005-04-17 16:05:31 -05004200
Jamie Wellnitz41415862006-02-28 19:25:27 -05004201 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004202
Jamie Wellnitz41415862006-02-28 19:25:27 -05004203 /* Reset HBA */
4204 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004205 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004206 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05004207
4208 /* perform board reset */
4209 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004210 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004211 phba->pport->fc_myDID = 0;
4212 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05004213
Jamie Wellnitz41415862006-02-28 19:25:27 -05004214 /* Turn off parity checking and serr during the physical reset */
4215 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4216 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4217 (cfg_value &
4218 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4219
James Smart3772a992009-05-22 14:50:54 -04004220 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4221
Jamie Wellnitz41415862006-02-28 19:25:27 -05004222 /* Now toggle INITFF bit in the Host Control Register */
4223 writel(HC_INITFF, phba->HCregaddr);
4224 mdelay(1);
4225 readl(phba->HCregaddr); /* flush */
4226 writel(0, phba->HCregaddr);
4227 readl(phba->HCregaddr); /* flush */
4228
4229 /* Restore PCI cmd register */
4230 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004231
4232 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004233 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004234 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004235 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004236 pring->sli.sli3.rspidx = 0;
4237 pring->sli.sli3.next_cmdidx = 0;
4238 pring->sli.sli3.local_getidx = 0;
4239 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004240 pring->missbufcnt = 0;
4241 }
dea31012005-04-17 16:05:31 -05004242
James Smart2e0fef82007-06-17 19:56:36 -05004243 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004244 return 0;
4245}
4246
James Smarte59058c2008-08-24 21:49:00 -04004247/**
James Smartda0436e2009-05-22 14:51:39 -04004248 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4249 * @phba: Pointer to HBA context object.
4250 *
4251 * This function resets a SLI4 HBA. This function disables PCI layer parity
4252 * checking during resets the device. The caller is not required to hold
4253 * any locks.
4254 *
4255 * This function returns 0 always.
4256 **/
4257int
4258lpfc_sli4_brdreset(struct lpfc_hba *phba)
4259{
4260 struct lpfc_sli *psli = &phba->sli;
4261 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004262 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004263
4264 /* Reset HBA */
4265 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004266 "0295 Reset HBA Data: x%x x%x x%x\n",
4267 phba->pport->port_state, psli->sli_flag,
4268 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004269
4270 /* perform board reset */
4271 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004272 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004273 phba->pport->fc_myDID = 0;
4274 phba->pport->fc_prevDID = 0;
4275
James Smartda0436e2009-05-22 14:51:39 -04004276 spin_lock_irq(&phba->hbalock);
4277 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4278 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004279 spin_unlock_irq(&phba->hbalock);
4280
James Smart02936352014-04-04 13:52:12 -04004281 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4282 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4283 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4284 return rc;
4285 }
4286
James Smartda0436e2009-05-22 14:51:39 -04004287 /* Now physically reset the device */
4288 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4289 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004290
4291 /* Turn off parity checking and serr during the physical reset */
4292 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4293 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4294 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4295
James Smart88318812012-09-29 11:29:29 -04004296 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004297 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004298 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004299
James Smartbe858b62010-12-15 17:57:20 -05004300 /* Restore PCI cmd register */
4301 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4302
James Smart27b01b82012-05-09 21:19:44 -04004303 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004304}
4305
4306/**
4307 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004308 * @phba: Pointer to HBA context object.
4309 *
4310 * This function is called in the SLI initialization code path to
4311 * restart the HBA. The caller is not required to hold any lock.
4312 * This function writes MBX_RESTART mailbox command to the SLIM and
4313 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4314 * function to free any pending commands. The function enables
4315 * POST only during the first initialization. The function returns zero.
4316 * The function does not guarantee completion of MBX_RESTART mailbox
4317 * command before the return of this function.
4318 **/
James Smartda0436e2009-05-22 14:51:39 -04004319static int
4320lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004321{
4322 MAILBOX_t *mb;
4323 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004324 volatile uint32_t word0;
4325 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004326 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004327
James Smart2e0fef82007-06-17 19:56:36 -05004328 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004329
James Smart0d878412009-10-02 15:16:56 -04004330 /* Take PCIe device Advanced Error Reporting (AER) state */
4331 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4332
Jamie Wellnitz41415862006-02-28 19:25:27 -05004333 psli = &phba->sli;
4334
4335 /* Restart HBA */
4336 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004337 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004338 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004339
4340 word0 = 0;
4341 mb = (MAILBOX_t *) &word0;
4342 mb->mbxCommand = MBX_RESTART;
4343 mb->mbxHc = 1;
4344
James Smart92908312006-03-07 15:04:13 -05004345 lpfc_reset_barrier(phba);
4346
Jamie Wellnitz41415862006-02-28 19:25:27 -05004347 to_slim = phba->MBslimaddr;
4348 writel(*(uint32_t *) mb, to_slim);
4349 readl(to_slim); /* flush */
4350
4351 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004352 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004353 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004354 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004355 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004356 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004357 writel(*(uint32_t *) mb, to_slim);
4358 readl(to_slim); /* flush */
4359
4360 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004361 phba->pport->stopped = 0;
4362 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004363 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004364 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004365
James Smart64ba8812006-08-02 15:24:34 -04004366 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4367 psli->stats_start = get_seconds();
4368
James Smarteaf15d52008-12-04 22:39:29 -05004369 /* Give the INITFF and Post time to settle. */
4370 mdelay(100);
dea31012005-04-17 16:05:31 -05004371
James Smart0d878412009-10-02 15:16:56 -04004372 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4373 if (hba_aer_enabled)
4374 pci_disable_pcie_error_reporting(phba->pcidev);
4375
Jamie Wellnitz41415862006-02-28 19:25:27 -05004376 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004377
4378 return 0;
4379}
4380
James Smarte59058c2008-08-24 21:49:00 -04004381/**
James Smartda0436e2009-05-22 14:51:39 -04004382 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4383 * @phba: Pointer to HBA context object.
4384 *
4385 * This function is called in the SLI initialization code path to restart
4386 * a SLI4 HBA. The caller is not required to hold any lock.
4387 * At the end of the function, it calls lpfc_hba_down_post function to
4388 * free any pending commands.
4389 **/
4390static int
4391lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4392{
4393 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004394 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004395 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004396
4397 /* Restart HBA */
4398 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4399 "0296 Restart HBA Data: x%x x%x\n",
4400 phba->pport->port_state, psli->sli_flag);
4401
James Smart75baf692010-06-08 18:31:21 -04004402 /* Take PCIe device Advanced Error Reporting (AER) state */
4403 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4404
James Smart27b01b82012-05-09 21:19:44 -04004405 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004406
4407 spin_lock_irq(&phba->hbalock);
4408 phba->pport->stopped = 0;
4409 phba->link_state = LPFC_INIT_START;
4410 phba->hba_flag = 0;
4411 spin_unlock_irq(&phba->hbalock);
4412
4413 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4414 psli->stats_start = get_seconds();
4415
James Smart75baf692010-06-08 18:31:21 -04004416 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4417 if (hba_aer_enabled)
4418 pci_disable_pcie_error_reporting(phba->pcidev);
4419
James Smartda0436e2009-05-22 14:51:39 -04004420 lpfc_hba_down_post(phba);
4421
James Smart27b01b82012-05-09 21:19:44 -04004422 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004423}
4424
4425/**
4426 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4427 * @phba: Pointer to HBA context object.
4428 *
4429 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4430 * API jump table function pointer from the lpfc_hba struct.
4431**/
4432int
4433lpfc_sli_brdrestart(struct lpfc_hba *phba)
4434{
4435 return phba->lpfc_sli_brdrestart(phba);
4436}
4437
4438/**
James Smart3621a712009-04-06 18:47:14 -04004439 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004440 * @phba: Pointer to HBA context object.
4441 *
4442 * This function is called after a HBA restart to wait for successful
4443 * restart of the HBA. Successful restart of the HBA is indicated by
4444 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4445 * iteration, the function will restart the HBA again. The function returns
4446 * zero if HBA successfully restarted else returns negative error code.
4447 **/
dea31012005-04-17 16:05:31 -05004448static int
4449lpfc_sli_chipset_init(struct lpfc_hba *phba)
4450{
4451 uint32_t status, i = 0;
4452
4453 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004454 if (lpfc_readl(phba->HSregaddr, &status))
4455 return -EIO;
dea31012005-04-17 16:05:31 -05004456
4457 /* Check status register to see what current state is */
4458 i = 0;
4459 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4460
James Smartdcf2a4e2010-09-29 11:18:53 -04004461 /* Check every 10ms for 10 retries, then every 100ms for 90
4462 * retries, then every 1 sec for 50 retires for a total of
4463 * ~60 seconds before reset the board again and check every
4464 * 1 sec for 50 retries. The up to 60 seconds before the
4465 * board ready is required by the Falcon FIPS zeroization
4466 * complete, and any reset the board in between shall cause
4467 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004468 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004469 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004470 /* Adapter failed to init, timeout, status reg
4471 <status> */
James Smarted957682007-06-17 19:56:37 -05004472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004473 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004474 "timeout, status reg x%x, "
4475 "FW Data: A8 x%x AC x%x\n", status,
4476 readl(phba->MBslimaddr + 0xa8),
4477 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004478 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004479 return -ETIMEDOUT;
4480 }
4481
4482 /* Check to see if any errors occurred during init */
4483 if (status & HS_FFERM) {
4484 /* ERROR: During chipset initialization */
4485 /* Adapter failed to init, chipset, status reg
4486 <status> */
James Smarted957682007-06-17 19:56:37 -05004487 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004488 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004489 "chipset, status reg x%x, "
4490 "FW Data: A8 x%x AC x%x\n", status,
4491 readl(phba->MBslimaddr + 0xa8),
4492 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004493 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004494 return -EIO;
4495 }
4496
James Smartdcf2a4e2010-09-29 11:18:53 -04004497 if (i <= 10)
dea31012005-04-17 16:05:31 -05004498 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004499 else if (i <= 100)
4500 msleep(100);
4501 else
4502 msleep(1000);
dea31012005-04-17 16:05:31 -05004503
James Smartdcf2a4e2010-09-29 11:18:53 -04004504 if (i == 150) {
4505 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004506 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004507 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004508 }
4509 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004510 if (lpfc_readl(phba->HSregaddr, &status))
4511 return -EIO;
dea31012005-04-17 16:05:31 -05004512 }
4513
4514 /* Check to see if any errors occurred during init */
4515 if (status & HS_FFERM) {
4516 /* ERROR: During chipset initialization */
4517 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004519 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004520 "status reg x%x, "
4521 "FW Data: A8 x%x AC x%x\n", status,
4522 readl(phba->MBslimaddr + 0xa8),
4523 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004524 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004525 return -EIO;
4526 }
4527
4528 /* Clear all interrupt enable conditions */
4529 writel(0, phba->HCregaddr);
4530 readl(phba->HCregaddr); /* flush */
4531
4532 /* setup host attn register */
4533 writel(0xffffffff, phba->HAregaddr);
4534 readl(phba->HAregaddr); /* flush */
4535 return 0;
4536}
4537
James Smarte59058c2008-08-24 21:49:00 -04004538/**
James Smart3621a712009-04-06 18:47:14 -04004539 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004540 *
4541 * This function calculates and returns the number of HBQs required to be
4542 * configured.
4543 **/
James Smart78b2d852007-08-02 11:10:21 -04004544int
James Smarted957682007-06-17 19:56:37 -05004545lpfc_sli_hbq_count(void)
4546{
James Smart92d7f7b2007-06-17 19:56:38 -05004547 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004548}
4549
James Smarte59058c2008-08-24 21:49:00 -04004550/**
James Smart3621a712009-04-06 18:47:14 -04004551 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004552 *
4553 * This function adds the number of hbq entries in every HBQ to get
4554 * the total number of hbq entries required for the HBA and returns
4555 * the total count.
4556 **/
James Smarted957682007-06-17 19:56:37 -05004557static int
4558lpfc_sli_hbq_entry_count(void)
4559{
4560 int hbq_count = lpfc_sli_hbq_count();
4561 int count = 0;
4562 int i;
4563
4564 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004565 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004566 return count;
4567}
4568
James Smarte59058c2008-08-24 21:49:00 -04004569/**
James Smart3621a712009-04-06 18:47:14 -04004570 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004571 *
4572 * This function calculates amount of memory required for all hbq entries
4573 * to be configured and returns the total memory required.
4574 **/
dea31012005-04-17 16:05:31 -05004575int
James Smarted957682007-06-17 19:56:37 -05004576lpfc_sli_hbq_size(void)
4577{
4578 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4579}
4580
James Smarte59058c2008-08-24 21:49:00 -04004581/**
James Smart3621a712009-04-06 18:47:14 -04004582 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004583 * @phba: Pointer to HBA context object.
4584 *
4585 * This function is called during the SLI initialization to configure
4586 * all the HBQs and post buffers to the HBQ. The caller is not
4587 * required to hold any locks. This function will return zero if successful
4588 * else it will return negative error code.
4589 **/
James Smarted957682007-06-17 19:56:37 -05004590static int
4591lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4592{
4593 int hbq_count = lpfc_sli_hbq_count();
4594 LPFC_MBOXQ_t *pmb;
4595 MAILBOX_t *pmbox;
4596 uint32_t hbqno;
4597 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004598
James Smart92d7f7b2007-06-17 19:56:38 -05004599 /* Get a Mailbox buffer to setup mailbox
4600 * commands for HBA initialization
4601 */
James Smarted957682007-06-17 19:56:37 -05004602 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4603
4604 if (!pmb)
4605 return -ENOMEM;
4606
James Smart04c68492009-05-22 14:52:52 -04004607 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004608
4609 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4610 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004611 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004612
4613 hbq_entry_index = 0;
4614 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4615 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4616 phba->hbqs[hbqno].hbqPutIdx = 0;
4617 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4618 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004619 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004620 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4621 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004622 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4623
4624 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4625 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4626 mbxStatus <status>, ring <num> */
4627
4628 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004629 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004630 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004631 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004632 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004633 pmbox->mbxStatus, hbqno);
4634
4635 phba->link_state = LPFC_HBA_ERROR;
4636 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004637 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004638 }
4639 }
4640 phba->hbq_count = hbq_count;
4641
James Smarted957682007-06-17 19:56:37 -05004642 mempool_free(pmb, phba->mbox_mem_pool);
4643
James Smart92d7f7b2007-06-17 19:56:38 -05004644 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004645 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4646 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004647 return 0;
4648}
4649
James Smarte59058c2008-08-24 21:49:00 -04004650/**
James Smart4f774512009-05-22 14:52:35 -04004651 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4652 * @phba: Pointer to HBA context object.
4653 *
4654 * This function is called during the SLI initialization to configure
4655 * all the HBQs and post buffers to the HBQ. The caller is not
4656 * required to hold any locks. This function will return zero if successful
4657 * else it will return negative error code.
4658 **/
4659static int
4660lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4661{
4662 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004663 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4664 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004665 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004666 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004667 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004668 return 0;
4669}
4670
4671/**
James Smart3621a712009-04-06 18:47:14 -04004672 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004673 * @phba: Pointer to HBA context object.
4674 * @sli_mode: sli mode - 2/3
4675 *
4676 * This function is called by the sli intialization code path
4677 * to issue config_port mailbox command. This function restarts the
4678 * HBA firmware and issues a config_port mailbox command to configure
4679 * the SLI interface in the sli mode specified by sli_mode
4680 * variable. The caller is not required to hold any locks.
4681 * The function returns 0 if successful, else returns negative error
4682 * code.
4683 **/
James Smart93996272008-08-24 21:50:30 -04004684int
4685lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004686{
4687 LPFC_MBOXQ_t *pmb;
4688 uint32_t resetcount = 0, rc = 0, done = 0;
4689
4690 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4691 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004692 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004693 return -ENOMEM;
4694 }
4695
James Smarted957682007-06-17 19:56:37 -05004696 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004697 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004698 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004699 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004700 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004701 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004702 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004703 rc = lpfc_sli_chipset_init(phba);
4704 if (rc)
4705 break;
4706
James Smart2e0fef82007-06-17 19:56:36 -05004707 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004708 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004709 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004710 resetcount++;
4711
James Smarted957682007-06-17 19:56:37 -05004712 /* Call pre CONFIG_PORT mailbox command initialization. A
4713 * value of 0 means the call was successful. Any other
4714 * nonzero value is a failure, but if ERESTART is returned,
4715 * the driver may reset the HBA and try again.
4716 */
dea31012005-04-17 16:05:31 -05004717 rc = lpfc_config_port_prep(phba);
4718 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004719 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004720 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004721 } else if (rc)
dea31012005-04-17 16:05:31 -05004722 break;
James Smart6d368e52011-05-24 11:44:12 -04004723
James Smart2e0fef82007-06-17 19:56:36 -05004724 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004725 lpfc_config_port(phba, pmb);
4726 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004727 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4728 LPFC_SLI3_HBQ_ENABLED |
4729 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004730 LPFC_SLI3_BG_ENABLED |
4731 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004732 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004734 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004735 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004736 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004737 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004738 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004739 spin_unlock_irq(&phba->hbalock);
4740 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004741 } else {
4742 /* Allow asynchronous mailbox command to go through */
4743 spin_lock_irq(&phba->hbalock);
4744 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4745 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004746 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004747
4748 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4749 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4750 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4751 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004752 }
dea31012005-04-17 16:05:31 -05004753 }
James Smarted957682007-06-17 19:56:37 -05004754 if (!done) {
4755 rc = -EINVAL;
4756 goto do_prep_failed;
4757 }
James Smart04c68492009-05-22 14:52:52 -04004758 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4759 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004760 rc = -ENXIO;
4761 goto do_prep_failed;
4762 }
James Smart04c68492009-05-22 14:52:52 -04004763 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004764 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004765 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4766 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4767 phba->max_vpi : phba->max_vports;
4768
James Smart34b02dc2008-08-24 21:49:55 -04004769 } else
4770 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004771 phba->fips_level = 0;
4772 phba->fips_spec_rev = 0;
4773 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004774 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004775 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4776 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4777 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4778 "2850 Security Crypto Active. FIPS x%d "
4779 "(Spec Rev: x%d)",
4780 phba->fips_level, phba->fips_spec_rev);
4781 }
4782 if (pmb->u.mb.un.varCfgPort.sec_err) {
4783 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4784 "2856 Config Port Security Crypto "
4785 "Error: x%x ",
4786 pmb->u.mb.un.varCfgPort.sec_err);
4787 }
James Smart04c68492009-05-22 14:52:52 -04004788 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004789 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004790 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004791 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004792
4793 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4794 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004795
4796 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004797 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004798 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4799 else
4800 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4801 "0443 Adapter did not grant "
4802 "BlockGuard\n");
4803 }
James Smart34b02dc2008-08-24 21:49:55 -04004804 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004805 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004806 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004807 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004808 }
James Smart92d7f7b2007-06-17 19:56:38 -05004809do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004810 mempool_free(pmb, phba->mbox_mem_pool);
4811 return rc;
4812}
4813
James Smarte59058c2008-08-24 21:49:00 -04004814
4815/**
James Smart3621a712009-04-06 18:47:14 -04004816 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004817 * @phba: Pointer to HBA context object.
4818 *
4819 * This function is the main SLI intialization function. This function
4820 * is called by the HBA intialization code, HBA reset code and HBA
4821 * error attention handler code. Caller is not required to hold any
4822 * locks. This function issues config_port mailbox command to configure
4823 * the SLI, setup iocb rings and HBQ rings. In the end the function
4824 * calls the config_port_post function to issue init_link mailbox
4825 * command and to start the discovery. The function will return zero
4826 * if successful, else it will return negative error code.
4827 **/
James Smarted957682007-06-17 19:56:37 -05004828int
4829lpfc_sli_hba_setup(struct lpfc_hba *phba)
4830{
4831 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004832 int mode = 3, i;
4833 int longs;
James Smarted957682007-06-17 19:56:37 -05004834
James Smart12247e82016-07-06 12:36:09 -07004835 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004836 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004837 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004838 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004839 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004840 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004841 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004842 break;
4843 }
James Smarted957682007-06-17 19:56:37 -05004844 mode = 2;
4845 break;
4846 case 0:
4847 case 3:
4848 break;
4849 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004850 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004851 "1819 Unrecognized sli_mode parameter: %d.\n",
4852 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004853
4854 break;
4855 }
James Smartb5c53952016-03-31 14:12:30 -07004856 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004857
James Smart93996272008-08-24 21:50:30 -04004858 rc = lpfc_sli_config_port(phba, mode);
4859
James Smart12247e82016-07-06 12:36:09 -07004860 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004861 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004862 "1820 Unable to select SLI-3. "
4863 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004864 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004865 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004866 else if (rc && mode == 2)
4867 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004868 if (rc)
dea31012005-04-17 16:05:31 -05004869 goto lpfc_sli_hba_setup_error;
4870
James Smart0d878412009-10-02 15:16:56 -04004871 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4872 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4873 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4874 if (!rc) {
4875 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4876 "2709 This device supports "
4877 "Advanced Error Reporting (AER)\n");
4878 spin_lock_irq(&phba->hbalock);
4879 phba->hba_flag |= HBA_AER_ENABLED;
4880 spin_unlock_irq(&phba->hbalock);
4881 } else {
4882 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4883 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004884 "Advanced Error Reporting (AER): %d\n",
4885 rc);
James Smart0d878412009-10-02 15:16:56 -04004886 phba->cfg_aer_support = 0;
4887 }
4888 }
4889
James Smarted957682007-06-17 19:56:37 -05004890 if (phba->sli_rev == 3) {
4891 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4892 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004893 } else {
4894 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4895 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004896 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004897 }
4898
4899 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004900 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4901 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004902 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004903
4904 if (rc)
4905 goto lpfc_sli_hba_setup_error;
4906
James Smart6d368e52011-05-24 11:44:12 -04004907 /* Initialize VPIs. */
4908 if (phba->sli_rev == LPFC_SLI_REV3) {
4909 /*
4910 * The VPI bitmask and physical ID array are allocated
4911 * and initialized once only - at driver load. A port
4912 * reset doesn't need to reinitialize this memory.
4913 */
4914 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4915 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4916 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4917 GFP_KERNEL);
4918 if (!phba->vpi_bmask) {
4919 rc = -ENOMEM;
4920 goto lpfc_sli_hba_setup_error;
4921 }
4922
4923 phba->vpi_ids = kzalloc(
4924 (phba->max_vpi+1) * sizeof(uint16_t),
4925 GFP_KERNEL);
4926 if (!phba->vpi_ids) {
4927 kfree(phba->vpi_bmask);
4928 rc = -ENOMEM;
4929 goto lpfc_sli_hba_setup_error;
4930 }
4931 for (i = 0; i < phba->max_vpi; i++)
4932 phba->vpi_ids[i] = i;
4933 }
4934 }
4935
James Smart93996272008-08-24 21:50:30 -04004936 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004937 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4938 rc = lpfc_sli_hbq_setup(phba);
4939 if (rc)
4940 goto lpfc_sli_hba_setup_error;
4941 }
James Smart04c68492009-05-22 14:52:52 -04004942 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004943 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004944 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004945
4946 rc = lpfc_config_port_post(phba);
4947 if (rc)
4948 goto lpfc_sli_hba_setup_error;
4949
James Smarted957682007-06-17 19:56:37 -05004950 return rc;
4951
James Smart92d7f7b2007-06-17 19:56:38 -05004952lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004953 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004955 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004956 return rc;
4957}
4958
James Smartda0436e2009-05-22 14:51:39 -04004959/**
4960 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4961 * @phba: Pointer to HBA context object.
4962 * @mboxq: mailbox pointer.
4963 * This function issue a dump mailbox command to read config region
4964 * 23 and parse the records in the region and populate driver
4965 * data structure.
4966 **/
4967static int
James Smartff78d8f2011-12-13 13:21:35 -05004968lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004969{
James Smartff78d8f2011-12-13 13:21:35 -05004970 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004971 struct lpfc_dmabuf *mp;
4972 struct lpfc_mqe *mqe;
4973 uint32_t data_length;
4974 int rc;
4975
4976 /* Program the default value of vlan_id and fc_map */
4977 phba->valid_vlan = 0;
4978 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4979 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4980 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4981
James Smartff78d8f2011-12-13 13:21:35 -05004982 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4983 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004984 return -ENOMEM;
4985
James Smartff78d8f2011-12-13 13:21:35 -05004986 mqe = &mboxq->u.mqe;
4987 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4988 rc = -ENOMEM;
4989 goto out_free_mboxq;
4990 }
4991
James Smartda0436e2009-05-22 14:51:39 -04004992 mp = (struct lpfc_dmabuf *) mboxq->context1;
4993 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4994
4995 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4996 "(%d):2571 Mailbox cmd x%x Status x%x "
4997 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4998 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4999 "CQ: x%x x%x x%x x%x\n",
5000 mboxq->vport ? mboxq->vport->vpi : 0,
5001 bf_get(lpfc_mqe_command, mqe),
5002 bf_get(lpfc_mqe_status, mqe),
5003 mqe->un.mb_words[0], mqe->un.mb_words[1],
5004 mqe->un.mb_words[2], mqe->un.mb_words[3],
5005 mqe->un.mb_words[4], mqe->un.mb_words[5],
5006 mqe->un.mb_words[6], mqe->un.mb_words[7],
5007 mqe->un.mb_words[8], mqe->un.mb_words[9],
5008 mqe->un.mb_words[10], mqe->un.mb_words[11],
5009 mqe->un.mb_words[12], mqe->un.mb_words[13],
5010 mqe->un.mb_words[14], mqe->un.mb_words[15],
5011 mqe->un.mb_words[16], mqe->un.mb_words[50],
5012 mboxq->mcqe.word0,
5013 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5014 mboxq->mcqe.trailer);
5015
5016 if (rc) {
5017 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5018 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005019 rc = -EIO;
5020 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005021 }
5022 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005023 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005024 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 Smartd11e31d2009-06-10 17:23:06 -04005028 }
James Smartda0436e2009-05-22 14:51:39 -04005029
5030 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5031 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5032 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005033 rc = 0;
5034
5035out_free_mboxq:
5036 mempool_free(mboxq, phba->mbox_mem_pool);
5037 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005038}
5039
5040/**
5041 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5042 * @phba: pointer to lpfc hba data structure.
5043 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5044 * @vpd: pointer to the memory to hold resulting port vpd data.
5045 * @vpd_size: On input, the number of bytes allocated to @vpd.
5046 * On output, the number of data bytes in @vpd.
5047 *
5048 * This routine executes a READ_REV SLI4 mailbox command. In
5049 * addition, this routine gets the port vpd data.
5050 *
5051 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005052 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005053 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005054 **/
5055static int
5056lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5057 uint8_t *vpd, uint32_t *vpd_size)
5058{
5059 int rc = 0;
5060 uint32_t dma_size;
5061 struct lpfc_dmabuf *dmabuf;
5062 struct lpfc_mqe *mqe;
5063
5064 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5065 if (!dmabuf)
5066 return -ENOMEM;
5067
5068 /*
5069 * Get a DMA buffer for the vpd data resulting from the READ_REV
5070 * mailbox command.
5071 */
5072 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005073 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5074 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005075 if (!dmabuf->virt) {
5076 kfree(dmabuf);
5077 return -ENOMEM;
5078 }
James Smartda0436e2009-05-22 14:51:39 -04005079
5080 /*
5081 * The SLI4 implementation of READ_REV conflicts at word1,
5082 * bits 31:16 and SLI4 adds vpd functionality not present
5083 * in SLI3. This code corrects the conflicts.
5084 */
5085 lpfc_read_rev(phba, mboxq);
5086 mqe = &mboxq->u.mqe;
5087 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5088 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5089 mqe->un.read_rev.word1 &= 0x0000FFFF;
5090 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5091 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5092
5093 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5094 if (rc) {
5095 dma_free_coherent(&phba->pcidev->dev, dma_size,
5096 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005097 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005098 return -EIO;
5099 }
5100
James Smartda0436e2009-05-22 14:51:39 -04005101 /*
5102 * The available vpd length cannot be bigger than the
5103 * DMA buffer passed to the port. Catch the less than
5104 * case and update the caller's size.
5105 */
5106 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5107 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5108
James Smartd7c47992010-06-08 18:31:54 -04005109 memcpy(vpd, dmabuf->virt, *vpd_size);
5110
James Smartda0436e2009-05-22 14:51:39 -04005111 dma_free_coherent(&phba->pcidev->dev, dma_size,
5112 dmabuf->virt, dmabuf->phys);
5113 kfree(dmabuf);
5114 return 0;
5115}
5116
5117/**
James Smartcd1c8302011-10-10 21:33:25 -04005118 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5119 * @phba: pointer to lpfc hba data structure.
5120 *
5121 * This routine retrieves SLI4 device physical port name this PCI function
5122 * is attached to.
5123 *
5124 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005125 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005126 * otherwise - failed to retrieve physical port name
5127 **/
5128static int
5129lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5130{
5131 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005132 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5133 struct lpfc_controller_attribute *cntl_attr;
5134 struct lpfc_mbx_get_port_name *get_port_name;
5135 void *virtaddr = NULL;
5136 uint32_t alloclen, reqlen;
5137 uint32_t shdr_status, shdr_add_status;
5138 union lpfc_sli4_cfg_shdr *shdr;
5139 char cport_name = 0;
5140 int rc;
5141
5142 /* We assume nothing at this point */
5143 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5144 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5145
5146 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5147 if (!mboxq)
5148 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005149 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005150 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5151 lpfc_sli4_read_config(phba);
5152 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5153 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005154
5155 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5156 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5157 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5158 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5159 LPFC_SLI4_MBX_NEMBED);
5160 if (alloclen < reqlen) {
5161 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5162 "3084 Allocated DMA memory size (%d) is "
5163 "less than the requested DMA memory size "
5164 "(%d)\n", alloclen, reqlen);
5165 rc = -ENOMEM;
5166 goto out_free_mboxq;
5167 }
5168 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5169 virtaddr = mboxq->sge_array->addr[0];
5170 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5171 shdr = &mbx_cntl_attr->cfg_shdr;
5172 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5173 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5174 if (shdr_status || shdr_add_status || rc) {
5175 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5176 "3085 Mailbox x%x (x%x/x%x) failed, "
5177 "rc:x%x, status:x%x, add_status:x%x\n",
5178 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5179 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5180 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5181 rc, shdr_status, shdr_add_status);
5182 rc = -ENXIO;
5183 goto out_free_mboxq;
5184 }
5185 cntl_attr = &mbx_cntl_attr->cntl_attr;
5186 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5187 phba->sli4_hba.lnk_info.lnk_tp =
5188 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5189 phba->sli4_hba.lnk_info.lnk_no =
5190 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5191 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5192 "3086 lnk_type:%d, lnk_numb:%d\n",
5193 phba->sli4_hba.lnk_info.lnk_tp,
5194 phba->sli4_hba.lnk_info.lnk_no);
5195
5196retrieve_ppname:
5197 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5198 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5199 sizeof(struct lpfc_mbx_get_port_name) -
5200 sizeof(struct lpfc_sli4_cfg_mhdr),
5201 LPFC_SLI4_MBX_EMBED);
5202 get_port_name = &mboxq->u.mqe.un.get_port_name;
5203 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5204 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5205 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5206 phba->sli4_hba.lnk_info.lnk_tp);
5207 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5208 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5209 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5210 if (shdr_status || shdr_add_status || rc) {
5211 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5212 "3087 Mailbox x%x (x%x/x%x) failed: "
5213 "rc:x%x, status:x%x, add_status:x%x\n",
5214 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5215 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5216 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5217 rc, shdr_status, shdr_add_status);
5218 rc = -ENXIO;
5219 goto out_free_mboxq;
5220 }
5221 switch (phba->sli4_hba.lnk_info.lnk_no) {
5222 case LPFC_LINK_NUMBER_0:
5223 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5224 &get_port_name->u.response);
5225 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5226 break;
5227 case LPFC_LINK_NUMBER_1:
5228 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5229 &get_port_name->u.response);
5230 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5231 break;
5232 case LPFC_LINK_NUMBER_2:
5233 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5234 &get_port_name->u.response);
5235 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5236 break;
5237 case LPFC_LINK_NUMBER_3:
5238 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5239 &get_port_name->u.response);
5240 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5241 break;
5242 default:
5243 break;
5244 }
5245
5246 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5247 phba->Port[0] = cport_name;
5248 phba->Port[1] = '\0';
5249 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5250 "3091 SLI get port name: %s\n", phba->Port);
5251 }
5252
5253out_free_mboxq:
5254 if (rc != MBX_TIMEOUT) {
5255 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5256 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5257 else
5258 mempool_free(mboxq, phba->mbox_mem_pool);
5259 }
5260 return rc;
5261}
5262
5263/**
James Smartda0436e2009-05-22 14:51:39 -04005264 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5265 * @phba: pointer to lpfc hba data structure.
5266 *
5267 * This routine is called to explicitly arm the SLI4 device's completion and
5268 * event queues
5269 **/
5270static void
5271lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5272{
James Smart895427b2017-02-12 13:52:30 -08005273 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04005274
5275 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5276 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005277 if (phba->sli4_hba.nvmels_cq)
5278 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5279 LPFC_QUEUE_REARM);
5280
5281 if (phba->sli4_hba.fcp_cq)
5282 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5283 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5284 LPFC_QUEUE_REARM);
5285
5286 if (phba->sli4_hba.nvme_cq)
5287 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5288 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5289 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005290
James Smartf38fa0b2014-04-04 13:52:21 -04005291 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005292 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5293
James Smart895427b2017-02-12 13:52:30 -08005294 if (phba->sli4_hba.hba_eq)
5295 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5296 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5297 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005298
James Smart2d7dbc42017-02-12 13:52:35 -08005299 if (phba->nvmet_support) {
5300 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5301 lpfc_sli4_cq_release(
5302 phba->sli4_hba.nvmet_cqset[qidx],
5303 LPFC_QUEUE_REARM);
5304 }
5305 }
5306
James Smart1ba981f2014-02-20 09:56:45 -05005307 if (phba->cfg_fof)
5308 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005309}
5310
5311/**
James Smart6d368e52011-05-24 11:44:12 -04005312 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5313 * @phba: Pointer to HBA context object.
5314 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005315 * @extnt_count: buffer to hold port available extent count.
5316 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005317 *
James Smartb76f2dc2011-07-22 18:37:42 -04005318 * This function calls the port and retrievs the number of available
5319 * extents and their size for a particular extent type.
5320 *
5321 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005322 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005323int
James Smart6d368e52011-05-24 11:44:12 -04005324lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5325 uint16_t *extnt_count, uint16_t *extnt_size)
5326{
5327 int rc = 0;
5328 uint32_t length;
5329 uint32_t mbox_tmo;
5330 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5331 LPFC_MBOXQ_t *mbox;
5332
5333 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5334 if (!mbox)
5335 return -ENOMEM;
5336
5337 /* Find out how many extents are available for this resource type */
5338 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5339 sizeof(struct lpfc_sli4_cfg_mhdr));
5340 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5341 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5342 length, LPFC_SLI4_MBX_EMBED);
5343
5344 /* Send an extents count of 0 - the GET doesn't use it. */
5345 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5346 LPFC_SLI4_MBX_EMBED);
5347 if (unlikely(rc)) {
5348 rc = -EIO;
5349 goto err_exit;
5350 }
5351
5352 if (!phba->sli4_hba.intr_enable)
5353 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5354 else {
James Smarta183a152011-10-10 21:32:43 -04005355 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005356 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5357 }
5358 if (unlikely(rc)) {
5359 rc = -EIO;
5360 goto err_exit;
5361 }
5362
5363 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5364 if (bf_get(lpfc_mbox_hdr_status,
5365 &rsrc_info->header.cfg_shdr.response)) {
5366 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5367 "2930 Failed to get resource extents "
5368 "Status 0x%x Add'l Status 0x%x\n",
5369 bf_get(lpfc_mbox_hdr_status,
5370 &rsrc_info->header.cfg_shdr.response),
5371 bf_get(lpfc_mbox_hdr_add_status,
5372 &rsrc_info->header.cfg_shdr.response));
5373 rc = -EIO;
5374 goto err_exit;
5375 }
5376
5377 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5378 &rsrc_info->u.rsp);
5379 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5380 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005381
5382 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5383 "3162 Retrieved extents type-%d from port: count:%d, "
5384 "size:%d\n", type, *extnt_count, *extnt_size);
5385
5386err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005387 mempool_free(mbox, phba->mbox_mem_pool);
5388 return rc;
5389}
5390
5391/**
5392 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5393 * @phba: Pointer to HBA context object.
5394 * @type: The extent type to check.
5395 *
5396 * This function reads the current available extents from the port and checks
5397 * if the extent count or extent size has changed since the last access.
5398 * Callers use this routine post port reset to understand if there is a
5399 * extent reprovisioning requirement.
5400 *
5401 * Returns:
5402 * -Error: error indicates problem.
5403 * 1: Extent count or size has changed.
5404 * 0: No changes.
5405 **/
5406static int
5407lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5408{
5409 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5410 uint16_t size_diff, rsrc_ext_size;
5411 int rc = 0;
5412 struct lpfc_rsrc_blks *rsrc_entry;
5413 struct list_head *rsrc_blk_list = NULL;
5414
5415 size_diff = 0;
5416 curr_ext_cnt = 0;
5417 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5418 &rsrc_ext_cnt,
5419 &rsrc_ext_size);
5420 if (unlikely(rc))
5421 return -EIO;
5422
5423 switch (type) {
5424 case LPFC_RSC_TYPE_FCOE_RPI:
5425 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5426 break;
5427 case LPFC_RSC_TYPE_FCOE_VPI:
5428 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5429 break;
5430 case LPFC_RSC_TYPE_FCOE_XRI:
5431 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5432 break;
5433 case LPFC_RSC_TYPE_FCOE_VFI:
5434 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5435 break;
5436 default:
5437 break;
5438 }
5439
5440 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5441 curr_ext_cnt++;
5442 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5443 size_diff++;
5444 }
5445
5446 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5447 rc = 1;
5448
5449 return rc;
5450}
5451
5452/**
5453 * lpfc_sli4_cfg_post_extnts -
5454 * @phba: Pointer to HBA context object.
5455 * @extnt_cnt - number of available extents.
5456 * @type - the extent type (rpi, xri, vfi, vpi).
5457 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5458 * @mbox - pointer to the caller's allocated mailbox structure.
5459 *
5460 * This function executes the extents allocation request. It also
5461 * takes care of the amount of memory needed to allocate or get the
5462 * allocated extents. It is the caller's responsibility to evaluate
5463 * the response.
5464 *
5465 * Returns:
5466 * -Error: Error value describes the condition found.
5467 * 0: if successful
5468 **/
5469static int
James Smart8a9d2e82012-05-09 21:16:12 -04005470lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005471 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5472{
5473 int rc = 0;
5474 uint32_t req_len;
5475 uint32_t emb_len;
5476 uint32_t alloc_len, mbox_tmo;
5477
5478 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005479 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005480
5481 /*
5482 * Calculate the size of an embedded mailbox. The uint32_t
5483 * accounts for extents-specific word.
5484 */
5485 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5486 sizeof(uint32_t);
5487
5488 /*
5489 * Presume the allocation and response will fit into an embedded
5490 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5491 */
5492 *emb = LPFC_SLI4_MBX_EMBED;
5493 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005494 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005495 sizeof(union lpfc_sli4_cfg_shdr) +
5496 sizeof(uint32_t);
5497 *emb = LPFC_SLI4_MBX_NEMBED;
5498 }
5499
5500 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5501 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5502 req_len, *emb);
5503 if (alloc_len < req_len) {
5504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005505 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005506 "less than the requested DMA memory "
5507 "size (x%x)\n", alloc_len, req_len);
5508 return -ENOMEM;
5509 }
James Smart8a9d2e82012-05-09 21:16:12 -04005510 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005511 if (unlikely(rc))
5512 return -EIO;
5513
5514 if (!phba->sli4_hba.intr_enable)
5515 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5516 else {
James Smarta183a152011-10-10 21:32:43 -04005517 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005518 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5519 }
5520
5521 if (unlikely(rc))
5522 rc = -EIO;
5523 return rc;
5524}
5525
5526/**
5527 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5528 * @phba: Pointer to HBA context object.
5529 * @type: The resource extent type to allocate.
5530 *
5531 * This function allocates the number of elements for the specified
5532 * resource type.
5533 **/
5534static int
5535lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5536{
5537 bool emb = false;
5538 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5539 uint16_t rsrc_id, rsrc_start, j, k;
5540 uint16_t *ids;
5541 int i, rc;
5542 unsigned long longs;
5543 unsigned long *bmask;
5544 struct lpfc_rsrc_blks *rsrc_blks;
5545 LPFC_MBOXQ_t *mbox;
5546 uint32_t length;
5547 struct lpfc_id_range *id_array = NULL;
5548 void *virtaddr = NULL;
5549 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5550 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5551 struct list_head *ext_blk_list;
5552
5553 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5554 &rsrc_cnt,
5555 &rsrc_size);
5556 if (unlikely(rc))
5557 return -EIO;
5558
5559 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5560 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5561 "3009 No available Resource Extents "
5562 "for resource type 0x%x: Count: 0x%x, "
5563 "Size 0x%x\n", type, rsrc_cnt,
5564 rsrc_size);
5565 return -ENOMEM;
5566 }
5567
James Smart8a9d2e82012-05-09 21:16:12 -04005568 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5569 "2903 Post resource extents type-0x%x: "
5570 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005571
5572 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5573 if (!mbox)
5574 return -ENOMEM;
5575
James Smart8a9d2e82012-05-09 21:16:12 -04005576 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005577 if (unlikely(rc)) {
5578 rc = -EIO;
5579 goto err_exit;
5580 }
5581
5582 /*
5583 * Figure out where the response is located. Then get local pointers
5584 * to the response data. The port does not guarantee to respond to
5585 * all extents counts request so update the local variable with the
5586 * allocated count from the port.
5587 */
5588 if (emb == LPFC_SLI4_MBX_EMBED) {
5589 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5590 id_array = &rsrc_ext->u.rsp.id[0];
5591 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5592 } else {
5593 virtaddr = mbox->sge_array->addr[0];
5594 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5595 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5596 id_array = &n_rsrc->id;
5597 }
5598
5599 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5600 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5601
5602 /*
5603 * Based on the resource size and count, correct the base and max
5604 * resource values.
5605 */
5606 length = sizeof(struct lpfc_rsrc_blks);
5607 switch (type) {
5608 case LPFC_RSC_TYPE_FCOE_RPI:
5609 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5610 sizeof(unsigned long),
5611 GFP_KERNEL);
5612 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5613 rc = -ENOMEM;
5614 goto err_exit;
5615 }
5616 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5617 sizeof(uint16_t),
5618 GFP_KERNEL);
5619 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5620 kfree(phba->sli4_hba.rpi_bmask);
5621 rc = -ENOMEM;
5622 goto err_exit;
5623 }
5624
5625 /*
5626 * The next_rpi was initialized with the maximum available
5627 * count but the port may allocate a smaller number. Catch
5628 * that case and update the next_rpi.
5629 */
5630 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5631
5632 /* Initialize local ptrs for common extent processing later. */
5633 bmask = phba->sli4_hba.rpi_bmask;
5634 ids = phba->sli4_hba.rpi_ids;
5635 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5636 break;
5637 case LPFC_RSC_TYPE_FCOE_VPI:
5638 phba->vpi_bmask = kzalloc(longs *
5639 sizeof(unsigned long),
5640 GFP_KERNEL);
5641 if (unlikely(!phba->vpi_bmask)) {
5642 rc = -ENOMEM;
5643 goto err_exit;
5644 }
5645 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5646 sizeof(uint16_t),
5647 GFP_KERNEL);
5648 if (unlikely(!phba->vpi_ids)) {
5649 kfree(phba->vpi_bmask);
5650 rc = -ENOMEM;
5651 goto err_exit;
5652 }
5653
5654 /* Initialize local ptrs for common extent processing later. */
5655 bmask = phba->vpi_bmask;
5656 ids = phba->vpi_ids;
5657 ext_blk_list = &phba->lpfc_vpi_blk_list;
5658 break;
5659 case LPFC_RSC_TYPE_FCOE_XRI:
5660 phba->sli4_hba.xri_bmask = kzalloc(longs *
5661 sizeof(unsigned long),
5662 GFP_KERNEL);
5663 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5664 rc = -ENOMEM;
5665 goto err_exit;
5666 }
James Smart8a9d2e82012-05-09 21:16:12 -04005667 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005668 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5669 sizeof(uint16_t),
5670 GFP_KERNEL);
5671 if (unlikely(!phba->sli4_hba.xri_ids)) {
5672 kfree(phba->sli4_hba.xri_bmask);
5673 rc = -ENOMEM;
5674 goto err_exit;
5675 }
5676
5677 /* Initialize local ptrs for common extent processing later. */
5678 bmask = phba->sli4_hba.xri_bmask;
5679 ids = phba->sli4_hba.xri_ids;
5680 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5681 break;
5682 case LPFC_RSC_TYPE_FCOE_VFI:
5683 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5684 sizeof(unsigned long),
5685 GFP_KERNEL);
5686 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5687 rc = -ENOMEM;
5688 goto err_exit;
5689 }
5690 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5691 sizeof(uint16_t),
5692 GFP_KERNEL);
5693 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5694 kfree(phba->sli4_hba.vfi_bmask);
5695 rc = -ENOMEM;
5696 goto err_exit;
5697 }
5698
5699 /* Initialize local ptrs for common extent processing later. */
5700 bmask = phba->sli4_hba.vfi_bmask;
5701 ids = phba->sli4_hba.vfi_ids;
5702 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5703 break;
5704 default:
5705 /* Unsupported Opcode. Fail call. */
5706 id_array = NULL;
5707 bmask = NULL;
5708 ids = NULL;
5709 ext_blk_list = NULL;
5710 goto err_exit;
5711 }
5712
5713 /*
5714 * Complete initializing the extent configuration with the
5715 * allocated ids assigned to this function. The bitmask serves
5716 * as an index into the array and manages the available ids. The
5717 * array just stores the ids communicated to the port via the wqes.
5718 */
5719 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5720 if ((i % 2) == 0)
5721 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5722 &id_array[k]);
5723 else
5724 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5725 &id_array[k]);
5726
5727 rsrc_blks = kzalloc(length, GFP_KERNEL);
5728 if (unlikely(!rsrc_blks)) {
5729 rc = -ENOMEM;
5730 kfree(bmask);
5731 kfree(ids);
5732 goto err_exit;
5733 }
5734 rsrc_blks->rsrc_start = rsrc_id;
5735 rsrc_blks->rsrc_size = rsrc_size;
5736 list_add_tail(&rsrc_blks->list, ext_blk_list);
5737 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005738 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005739 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005740 lpfc_sli4_get_iocb_cnt(phba);
5741 phba->sli4_hba.nvme_xri_start =
5742 phba->sli4_hba.scsi_xri_start +
5743 phba->sli4_hba.scsi_xri_max;
5744 }
James Smart6d368e52011-05-24 11:44:12 -04005745
5746 while (rsrc_id < (rsrc_start + rsrc_size)) {
5747 ids[j] = rsrc_id;
5748 rsrc_id++;
5749 j++;
5750 }
5751 /* Entire word processed. Get next word.*/
5752 if ((i % 2) == 1)
5753 k++;
5754 }
5755 err_exit:
5756 lpfc_sli4_mbox_cmd_free(phba, mbox);
5757 return rc;
5758}
5759
James Smart895427b2017-02-12 13:52:30 -08005760
5761
James Smart6d368e52011-05-24 11:44:12 -04005762/**
5763 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5764 * @phba: Pointer to HBA context object.
5765 * @type: the extent's type.
5766 *
5767 * This function deallocates all extents of a particular resource type.
5768 * SLI4 does not allow for deallocating a particular extent range. It
5769 * is the caller's responsibility to release all kernel memory resources.
5770 **/
5771static int
5772lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5773{
5774 int rc;
5775 uint32_t length, mbox_tmo = 0;
5776 LPFC_MBOXQ_t *mbox;
5777 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5778 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5779
5780 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5781 if (!mbox)
5782 return -ENOMEM;
5783
5784 /*
5785 * This function sends an embedded mailbox because it only sends the
5786 * the resource type. All extents of this type are released by the
5787 * port.
5788 */
5789 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5790 sizeof(struct lpfc_sli4_cfg_mhdr));
5791 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5792 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5793 length, LPFC_SLI4_MBX_EMBED);
5794
5795 /* Send an extents count of 0 - the dealloc doesn't use it. */
5796 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5797 LPFC_SLI4_MBX_EMBED);
5798 if (unlikely(rc)) {
5799 rc = -EIO;
5800 goto out_free_mbox;
5801 }
5802 if (!phba->sli4_hba.intr_enable)
5803 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5804 else {
James Smarta183a152011-10-10 21:32:43 -04005805 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005806 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5807 }
5808 if (unlikely(rc)) {
5809 rc = -EIO;
5810 goto out_free_mbox;
5811 }
5812
5813 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5814 if (bf_get(lpfc_mbox_hdr_status,
5815 &dealloc_rsrc->header.cfg_shdr.response)) {
5816 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5817 "2919 Failed to release resource extents "
5818 "for type %d - Status 0x%x Add'l Status 0x%x. "
5819 "Resource memory not released.\n",
5820 type,
5821 bf_get(lpfc_mbox_hdr_status,
5822 &dealloc_rsrc->header.cfg_shdr.response),
5823 bf_get(lpfc_mbox_hdr_add_status,
5824 &dealloc_rsrc->header.cfg_shdr.response));
5825 rc = -EIO;
5826 goto out_free_mbox;
5827 }
5828
5829 /* Release kernel memory resources for the specific type. */
5830 switch (type) {
5831 case LPFC_RSC_TYPE_FCOE_VPI:
5832 kfree(phba->vpi_bmask);
5833 kfree(phba->vpi_ids);
5834 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5835 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5836 &phba->lpfc_vpi_blk_list, list) {
5837 list_del_init(&rsrc_blk->list);
5838 kfree(rsrc_blk);
5839 }
James Smart16a3a202013-04-17 20:14:38 -04005840 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005841 break;
5842 case LPFC_RSC_TYPE_FCOE_XRI:
5843 kfree(phba->sli4_hba.xri_bmask);
5844 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005845 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5846 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5847 list_del_init(&rsrc_blk->list);
5848 kfree(rsrc_blk);
5849 }
5850 break;
5851 case LPFC_RSC_TYPE_FCOE_VFI:
5852 kfree(phba->sli4_hba.vfi_bmask);
5853 kfree(phba->sli4_hba.vfi_ids);
5854 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5855 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5856 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5857 list_del_init(&rsrc_blk->list);
5858 kfree(rsrc_blk);
5859 }
5860 break;
5861 case LPFC_RSC_TYPE_FCOE_RPI:
5862 /* RPI bitmask and physical id array are cleaned up earlier. */
5863 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5864 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5865 list_del_init(&rsrc_blk->list);
5866 kfree(rsrc_blk);
5867 }
5868 break;
5869 default:
5870 break;
5871 }
5872
5873 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5874
5875 out_free_mbox:
5876 mempool_free(mbox, phba->mbox_mem_pool);
5877 return rc;
5878}
5879
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005880static void
James Smart7bdedb32016-07-06 12:36:00 -07005881lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5882 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005883{
James Smart65791f12016-07-06 12:35:56 -07005884 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005885
James Smart65791f12016-07-06 12:35:56 -07005886 len = sizeof(struct lpfc_mbx_set_feature) -
5887 sizeof(struct lpfc_sli4_cfg_mhdr);
5888 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5889 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5890 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005891
James Smart7bdedb32016-07-06 12:36:00 -07005892 switch (feature) {
5893 case LPFC_SET_UE_RECOVERY:
5894 bf_set(lpfc_mbx_set_feature_UER,
5895 &mbox->u.mqe.un.set_feature, 1);
5896 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5897 mbox->u.mqe.un.set_feature.param_len = 8;
5898 break;
5899 case LPFC_SET_MDS_DIAGS:
5900 bf_set(lpfc_mbx_set_feature_mds,
5901 &mbox->u.mqe.un.set_feature, 1);
5902 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5903 &mbox->u.mqe.un.set_feature, 0);
5904 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5905 mbox->u.mqe.un.set_feature.param_len = 8;
5906 break;
James Smart65791f12016-07-06 12:35:56 -07005907 }
James Smart7bdedb32016-07-06 12:36:00 -07005908
5909 return;
James Smart65791f12016-07-06 12:35:56 -07005910}
5911
James Smart6d368e52011-05-24 11:44:12 -04005912/**
5913 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5914 * @phba: Pointer to HBA context object.
5915 *
5916 * This function allocates all SLI4 resource identifiers.
5917 **/
5918int
5919lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5920{
5921 int i, rc, error = 0;
5922 uint16_t count, base;
5923 unsigned long longs;
5924
James Smartff78d8f2011-12-13 13:21:35 -05005925 if (!phba->sli4_hba.rpi_hdrs_in_use)
5926 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005927 if (phba->sli4_hba.extents_in_use) {
5928 /*
5929 * The port supports resource extents. The XRI, VPI, VFI, RPI
5930 * resource extent count must be read and allocated before
5931 * provisioning the resource id arrays.
5932 */
5933 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5934 LPFC_IDX_RSRC_RDY) {
5935 /*
5936 * Extent-based resources are set - the driver could
5937 * be in a port reset. Figure out if any corrective
5938 * actions need to be taken.
5939 */
5940 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5941 LPFC_RSC_TYPE_FCOE_VFI);
5942 if (rc != 0)
5943 error++;
5944 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5945 LPFC_RSC_TYPE_FCOE_VPI);
5946 if (rc != 0)
5947 error++;
5948 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5949 LPFC_RSC_TYPE_FCOE_XRI);
5950 if (rc != 0)
5951 error++;
5952 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5953 LPFC_RSC_TYPE_FCOE_RPI);
5954 if (rc != 0)
5955 error++;
5956
5957 /*
5958 * It's possible that the number of resources
5959 * provided to this port instance changed between
5960 * resets. Detect this condition and reallocate
5961 * resources. Otherwise, there is no action.
5962 */
5963 if (error) {
5964 lpfc_printf_log(phba, KERN_INFO,
5965 LOG_MBOX | LOG_INIT,
5966 "2931 Detected extent resource "
5967 "change. Reallocating all "
5968 "extents.\n");
5969 rc = lpfc_sli4_dealloc_extent(phba,
5970 LPFC_RSC_TYPE_FCOE_VFI);
5971 rc = lpfc_sli4_dealloc_extent(phba,
5972 LPFC_RSC_TYPE_FCOE_VPI);
5973 rc = lpfc_sli4_dealloc_extent(phba,
5974 LPFC_RSC_TYPE_FCOE_XRI);
5975 rc = lpfc_sli4_dealloc_extent(phba,
5976 LPFC_RSC_TYPE_FCOE_RPI);
5977 } else
5978 return 0;
5979 }
5980
5981 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5982 if (unlikely(rc))
5983 goto err_exit;
5984
5985 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5986 if (unlikely(rc))
5987 goto err_exit;
5988
5989 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5990 if (unlikely(rc))
5991 goto err_exit;
5992
5993 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5994 if (unlikely(rc))
5995 goto err_exit;
5996 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5997 LPFC_IDX_RSRC_RDY);
5998 return rc;
5999 } else {
6000 /*
6001 * The port does not support resource extents. The XRI, VPI,
6002 * VFI, RPI resource ids were determined from READ_CONFIG.
6003 * Just allocate the bitmasks and provision the resource id
6004 * arrays. If a port reset is active, the resources don't
6005 * need any action - just exit.
6006 */
6007 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006008 LPFC_IDX_RSRC_RDY) {
6009 lpfc_sli4_dealloc_resource_identifiers(phba);
6010 lpfc_sli4_remove_rpis(phba);
6011 }
James Smart6d368e52011-05-24 11:44:12 -04006012 /* RPIs. */
6013 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006014 if (count <= 0) {
6015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6016 "3279 Invalid provisioning of "
6017 "rpi:%d\n", count);
6018 rc = -EINVAL;
6019 goto err_exit;
6020 }
James Smart6d368e52011-05-24 11:44:12 -04006021 base = phba->sli4_hba.max_cfg_param.rpi_base;
6022 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6023 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6024 sizeof(unsigned long),
6025 GFP_KERNEL);
6026 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6027 rc = -ENOMEM;
6028 goto err_exit;
6029 }
6030 phba->sli4_hba.rpi_ids = kzalloc(count *
6031 sizeof(uint16_t),
6032 GFP_KERNEL);
6033 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6034 rc = -ENOMEM;
6035 goto free_rpi_bmask;
6036 }
6037
6038 for (i = 0; i < count; i++)
6039 phba->sli4_hba.rpi_ids[i] = base + i;
6040
6041 /* VPIs. */
6042 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006043 if (count <= 0) {
6044 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6045 "3280 Invalid provisioning of "
6046 "vpi:%d\n", count);
6047 rc = -EINVAL;
6048 goto free_rpi_ids;
6049 }
James Smart6d368e52011-05-24 11:44:12 -04006050 base = phba->sli4_hba.max_cfg_param.vpi_base;
6051 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6052 phba->vpi_bmask = kzalloc(longs *
6053 sizeof(unsigned long),
6054 GFP_KERNEL);
6055 if (unlikely(!phba->vpi_bmask)) {
6056 rc = -ENOMEM;
6057 goto free_rpi_ids;
6058 }
6059 phba->vpi_ids = kzalloc(count *
6060 sizeof(uint16_t),
6061 GFP_KERNEL);
6062 if (unlikely(!phba->vpi_ids)) {
6063 rc = -ENOMEM;
6064 goto free_vpi_bmask;
6065 }
6066
6067 for (i = 0; i < count; i++)
6068 phba->vpi_ids[i] = base + i;
6069
6070 /* XRIs. */
6071 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006072 if (count <= 0) {
6073 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6074 "3281 Invalid provisioning of "
6075 "xri:%d\n", count);
6076 rc = -EINVAL;
6077 goto free_vpi_ids;
6078 }
James Smart6d368e52011-05-24 11:44:12 -04006079 base = phba->sli4_hba.max_cfg_param.xri_base;
6080 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6081 phba->sli4_hba.xri_bmask = kzalloc(longs *
6082 sizeof(unsigned long),
6083 GFP_KERNEL);
6084 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6085 rc = -ENOMEM;
6086 goto free_vpi_ids;
6087 }
James Smart41899be2012-03-01 22:34:19 -05006088 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006089 phba->sli4_hba.xri_ids = kzalloc(count *
6090 sizeof(uint16_t),
6091 GFP_KERNEL);
6092 if (unlikely(!phba->sli4_hba.xri_ids)) {
6093 rc = -ENOMEM;
6094 goto free_xri_bmask;
6095 }
6096
6097 for (i = 0; i < count; i++)
6098 phba->sli4_hba.xri_ids[i] = base + i;
6099
6100 /* VFIs. */
6101 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006102 if (count <= 0) {
6103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6104 "3282 Invalid provisioning of "
6105 "vfi:%d\n", count);
6106 rc = -EINVAL;
6107 goto free_xri_ids;
6108 }
James Smart6d368e52011-05-24 11:44:12 -04006109 base = phba->sli4_hba.max_cfg_param.vfi_base;
6110 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6111 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6112 sizeof(unsigned long),
6113 GFP_KERNEL);
6114 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6115 rc = -ENOMEM;
6116 goto free_xri_ids;
6117 }
6118 phba->sli4_hba.vfi_ids = kzalloc(count *
6119 sizeof(uint16_t),
6120 GFP_KERNEL);
6121 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6122 rc = -ENOMEM;
6123 goto free_vfi_bmask;
6124 }
6125
6126 for (i = 0; i < count; i++)
6127 phba->sli4_hba.vfi_ids[i] = base + i;
6128
6129 /*
6130 * Mark all resources ready. An HBA reset doesn't need
6131 * to reset the initialization.
6132 */
6133 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6134 LPFC_IDX_RSRC_RDY);
6135 return 0;
6136 }
6137
6138 free_vfi_bmask:
6139 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006140 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006141 free_xri_ids:
6142 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006143 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006144 free_xri_bmask:
6145 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006146 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006147 free_vpi_ids:
6148 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006149 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006150 free_vpi_bmask:
6151 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006152 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006153 free_rpi_ids:
6154 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006155 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006156 free_rpi_bmask:
6157 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006158 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006159 err_exit:
6160 return rc;
6161}
6162
6163/**
6164 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6165 * @phba: Pointer to HBA context object.
6166 *
6167 * This function allocates the number of elements for the specified
6168 * resource type.
6169 **/
6170int
6171lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6172{
6173 if (phba->sli4_hba.extents_in_use) {
6174 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6175 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6176 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6177 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6178 } else {
6179 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006180 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006181 kfree(phba->vpi_ids);
6182 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6183 kfree(phba->sli4_hba.xri_bmask);
6184 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006185 kfree(phba->sli4_hba.vfi_bmask);
6186 kfree(phba->sli4_hba.vfi_ids);
6187 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6188 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6189 }
6190
6191 return 0;
6192}
6193
6194/**
James Smartb76f2dc2011-07-22 18:37:42 -04006195 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6196 * @phba: Pointer to HBA context object.
6197 * @type: The resource extent type.
6198 * @extnt_count: buffer to hold port extent count response
6199 * @extnt_size: buffer to hold port extent size response.
6200 *
6201 * This function calls the port to read the host allocated extents
6202 * for a particular type.
6203 **/
6204int
6205lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6206 uint16_t *extnt_cnt, uint16_t *extnt_size)
6207{
6208 bool emb;
6209 int rc = 0;
6210 uint16_t curr_blks = 0;
6211 uint32_t req_len, emb_len;
6212 uint32_t alloc_len, mbox_tmo;
6213 struct list_head *blk_list_head;
6214 struct lpfc_rsrc_blks *rsrc_blk;
6215 LPFC_MBOXQ_t *mbox;
6216 void *virtaddr = NULL;
6217 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6218 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6219 union lpfc_sli4_cfg_shdr *shdr;
6220
6221 switch (type) {
6222 case LPFC_RSC_TYPE_FCOE_VPI:
6223 blk_list_head = &phba->lpfc_vpi_blk_list;
6224 break;
6225 case LPFC_RSC_TYPE_FCOE_XRI:
6226 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6227 break;
6228 case LPFC_RSC_TYPE_FCOE_VFI:
6229 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6230 break;
6231 case LPFC_RSC_TYPE_FCOE_RPI:
6232 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6233 break;
6234 default:
6235 return -EIO;
6236 }
6237
6238 /* Count the number of extents currently allocatd for this type. */
6239 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6240 if (curr_blks == 0) {
6241 /*
6242 * The GET_ALLOCATED mailbox does not return the size,
6243 * just the count. The size should be just the size
6244 * stored in the current allocated block and all sizes
6245 * for an extent type are the same so set the return
6246 * value now.
6247 */
6248 *extnt_size = rsrc_blk->rsrc_size;
6249 }
6250 curr_blks++;
6251 }
6252
James Smartb76f2dc2011-07-22 18:37:42 -04006253 /*
6254 * Calculate the size of an embedded mailbox. The uint32_t
6255 * accounts for extents-specific word.
6256 */
6257 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6258 sizeof(uint32_t);
6259
6260 /*
6261 * Presume the allocation and response will fit into an embedded
6262 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6263 */
6264 emb = LPFC_SLI4_MBX_EMBED;
6265 req_len = emb_len;
6266 if (req_len > emb_len) {
6267 req_len = curr_blks * sizeof(uint16_t) +
6268 sizeof(union lpfc_sli4_cfg_shdr) +
6269 sizeof(uint32_t);
6270 emb = LPFC_SLI4_MBX_NEMBED;
6271 }
6272
6273 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6274 if (!mbox)
6275 return -ENOMEM;
6276 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6277
6278 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6279 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6280 req_len, emb);
6281 if (alloc_len < req_len) {
6282 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6283 "2983 Allocated DMA memory size (x%x) is "
6284 "less than the requested DMA memory "
6285 "size (x%x)\n", alloc_len, req_len);
6286 rc = -ENOMEM;
6287 goto err_exit;
6288 }
6289 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6290 if (unlikely(rc)) {
6291 rc = -EIO;
6292 goto err_exit;
6293 }
6294
6295 if (!phba->sli4_hba.intr_enable)
6296 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6297 else {
James Smarta183a152011-10-10 21:32:43 -04006298 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006299 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6300 }
6301
6302 if (unlikely(rc)) {
6303 rc = -EIO;
6304 goto err_exit;
6305 }
6306
6307 /*
6308 * Figure out where the response is located. Then get local pointers
6309 * to the response data. The port does not guarantee to respond to
6310 * all extents counts request so update the local variable with the
6311 * allocated count from the port.
6312 */
6313 if (emb == LPFC_SLI4_MBX_EMBED) {
6314 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6315 shdr = &rsrc_ext->header.cfg_shdr;
6316 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6317 } else {
6318 virtaddr = mbox->sge_array->addr[0];
6319 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6320 shdr = &n_rsrc->cfg_shdr;
6321 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6322 }
6323
6324 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6325 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6326 "2984 Failed to read allocated resources "
6327 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6328 type,
6329 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6330 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6331 rc = -EIO;
6332 goto err_exit;
6333 }
6334 err_exit:
6335 lpfc_sli4_mbox_cmd_free(phba, mbox);
6336 return rc;
6337}
6338
6339/**
James Smart895427b2017-02-12 13:52:30 -08006340 * lpfc_sli4_repost_sgl_list - Repsot the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006341 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006342 * @pring: Pointer to driver SLI ring object.
6343 * @sgl_list: linked link of sgl buffers to post
6344 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006345 *
James Smart895427b2017-02-12 13:52:30 -08006346 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006347 * repost them to the port by using SGL block post. This is needed after a
6348 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006349 * of buffer sgls which contains contiguous xris and uses the non-embedded
6350 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006351 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6352 * mailbox command for posting.
6353 *
6354 * Returns: 0 = success, non-zero failure.
6355 **/
6356static int
James Smart895427b2017-02-12 13:52:30 -08006357lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6358 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006359{
6360 struct lpfc_sglq *sglq_entry = NULL;
6361 struct lpfc_sglq *sglq_entry_next = NULL;
6362 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006363 int status, total_cnt;
6364 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006365 int last_xritag = NO_XRI;
6366 LIST_HEAD(prep_sgl_list);
6367 LIST_HEAD(blck_sgl_list);
6368 LIST_HEAD(allc_sgl_list);
6369 LIST_HEAD(post_sgl_list);
6370 LIST_HEAD(free_sgl_list);
6371
James Smart38c20672013-03-01 16:37:44 -05006372 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006373 spin_lock(&phba->sli4_hba.sgl_list_lock);
6374 list_splice_init(sgl_list, &allc_sgl_list);
6375 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006376 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006377
James Smart895427b2017-02-12 13:52:30 -08006378 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006379 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6380 &allc_sgl_list, list) {
6381 list_del_init(&sglq_entry->list);
6382 block_cnt++;
6383 if ((last_xritag != NO_XRI) &&
6384 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6385 /* a hole in xri block, form a sgl posting block */
6386 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6387 post_cnt = block_cnt - 1;
6388 /* prepare list for next posting block */
6389 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6390 block_cnt = 1;
6391 } else {
6392 /* prepare list for next posting block */
6393 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6394 /* enough sgls for non-embed sgl mbox command */
6395 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6396 list_splice_init(&prep_sgl_list,
6397 &blck_sgl_list);
6398 post_cnt = block_cnt;
6399 block_cnt = 0;
6400 }
6401 }
6402 num_posted++;
6403
6404 /* keep track of last sgl's xritag */
6405 last_xritag = sglq_entry->sli4_xritag;
6406
James Smart895427b2017-02-12 13:52:30 -08006407 /* end of repost sgl list condition for buffers */
6408 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006409 if (post_cnt == 0) {
6410 list_splice_init(&prep_sgl_list,
6411 &blck_sgl_list);
6412 post_cnt = block_cnt;
6413 } else if (block_cnt == 1) {
6414 status = lpfc_sli4_post_sgl(phba,
6415 sglq_entry->phys, 0,
6416 sglq_entry->sli4_xritag);
6417 if (!status) {
6418 /* successful, put sgl to posted list */
6419 list_add_tail(&sglq_entry->list,
6420 &post_sgl_list);
6421 } else {
6422 /* Failure, put sgl to free list */
6423 lpfc_printf_log(phba, KERN_WARNING,
6424 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006425 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006426 "sgl, xritag:x%x\n",
6427 sglq_entry->sli4_xritag);
6428 list_add_tail(&sglq_entry->list,
6429 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006430 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006431 }
6432 }
6433 }
6434
6435 /* continue until a nembed page worth of sgls */
6436 if (post_cnt == 0)
6437 continue;
6438
James Smart895427b2017-02-12 13:52:30 -08006439 /* post the buffer list sgls as a block */
6440 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6441 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006442
6443 if (!status) {
6444 /* success, put sgl list to posted sgl list */
6445 list_splice_init(&blck_sgl_list, &post_sgl_list);
6446 } else {
6447 /* Failure, put sgl list to free sgl list */
6448 sglq_entry_first = list_first_entry(&blck_sgl_list,
6449 struct lpfc_sglq,
6450 list);
6451 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006452 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006453 "xritag:x%x-x%x\n",
6454 sglq_entry_first->sli4_xritag,
6455 (sglq_entry_first->sli4_xritag +
6456 post_cnt - 1));
6457 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006458 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006459 }
6460
6461 /* don't reset xirtag due to hole in xri block */
6462 if (block_cnt == 0)
6463 last_xritag = NO_XRI;
6464
James Smart895427b2017-02-12 13:52:30 -08006465 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006466 post_cnt = 0;
6467 }
6468
James Smart895427b2017-02-12 13:52:30 -08006469 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006470 lpfc_free_sgl_list(phba, &free_sgl_list);
6471
James Smart895427b2017-02-12 13:52:30 -08006472 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006473 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006474 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006475 spin_lock(&phba->sli4_hba.sgl_list_lock);
6476 list_splice_init(&post_sgl_list, sgl_list);
6477 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006478 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006479 } else {
6480 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006481 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006482 return -EIO;
6483 }
James Smart895427b2017-02-12 13:52:30 -08006484
6485 /* return the number of XRIs actually posted */
6486 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006487}
6488
James Smart61bda8f2016-10-13 15:06:05 -07006489void
6490lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6491{
6492 uint32_t len;
6493
6494 len = sizeof(struct lpfc_mbx_set_host_data) -
6495 sizeof(struct lpfc_sli4_cfg_mhdr);
6496 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6497 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6498 LPFC_SLI4_MBX_EMBED);
6499
6500 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006501 mbox->u.mqe.un.set_host_data.param_len =
6502 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006503 snprintf(mbox->u.mqe.un.set_host_data.data,
6504 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6505 "Linux %s v"LPFC_DRIVER_VERSION,
6506 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6507}
6508
James Smart8a9d2e82012-05-09 21:16:12 -04006509/**
James Smartda0436e2009-05-22 14:51:39 -04006510 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6511 * @phba: Pointer to HBA context object.
6512 *
6513 * This function is the main SLI4 device intialization PCI function. This
6514 * function is called by the HBA intialization code, HBA reset code and
6515 * HBA error attention handler code. Caller is not required to hold any
6516 * locks.
6517 **/
6518int
6519lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6520{
James Smart2d7dbc42017-02-12 13:52:35 -08006521 int rc, i;
James Smartda0436e2009-05-22 14:51:39 -04006522 LPFC_MBOXQ_t *mboxq;
6523 struct lpfc_mqe *mqe;
6524 uint8_t *vpd;
6525 uint32_t vpd_size;
6526 uint32_t ftr_rsp = 0;
6527 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6528 struct lpfc_vport *vport = phba->pport;
6529 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006530 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006531
6532 /* Perform a PCI function reset to start from clean */
6533 rc = lpfc_pci_function_reset(phba);
6534 if (unlikely(rc))
6535 return -ENODEV;
6536
6537 /* Check the HBA Host Status Register for readyness */
6538 rc = lpfc_sli4_post_status_check(phba);
6539 if (unlikely(rc))
6540 return -ENODEV;
6541 else {
6542 spin_lock_irq(&phba->hbalock);
6543 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6544 spin_unlock_irq(&phba->hbalock);
6545 }
6546
6547 /*
6548 * Allocate a single mailbox container for initializing the
6549 * port.
6550 */
6551 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6552 if (!mboxq)
6553 return -ENOMEM;
6554
James Smartda0436e2009-05-22 14:51:39 -04006555 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006556 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006557 vpd = kzalloc(vpd_size, GFP_KERNEL);
6558 if (!vpd) {
6559 rc = -ENOMEM;
6560 goto out_free_mbox;
6561 }
6562
6563 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006564 if (unlikely(rc)) {
6565 kfree(vpd);
6566 goto out_free_mbox;
6567 }
James Smart572709e2013-07-15 18:32:43 -04006568
James Smartda0436e2009-05-22 14:51:39 -04006569 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006570 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006571 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006572 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006573 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6574 } else {
James Smart76a95d72010-11-20 23:11:48 -05006575 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006576 }
James Smart45ed1192009-10-02 15:17:02 -04006577
6578 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6579 LPFC_DCBX_CEE_MODE)
6580 phba->hba_flag |= HBA_FIP_SUPPORT;
6581 else
6582 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6583
James Smart4f2e66c2012-05-09 21:17:07 -04006584 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6585
James Smartc31098c2011-04-16 11:03:33 -04006586 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006587 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6588 "0376 READ_REV Error. SLI Level %d "
6589 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006590 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006591 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006592 kfree(vpd);
6593 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006594 }
James Smartcd1c8302011-10-10 21:33:25 -04006595
6596 /*
James Smartff78d8f2011-12-13 13:21:35 -05006597 * Continue initialization with default values even if driver failed
6598 * to read FCoE param config regions, only read parameters if the
6599 * board is FCoE
6600 */
6601 if (phba->hba_flag & HBA_FCOE_MODE &&
6602 lpfc_sli4_read_fcoe_params(phba))
6603 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6604 "2570 Failed to read FCoE parameters\n");
6605
6606 /*
James Smartcd1c8302011-10-10 21:33:25 -04006607 * Retrieve sli4 device physical port name, failure of doing it
6608 * is considered as non-fatal.
6609 */
6610 rc = lpfc_sli4_retrieve_pport_name(phba);
6611 if (!rc)
6612 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6613 "3080 Successful retrieving SLI4 device "
6614 "physical port name: %s.\n", phba->Port);
6615
James Smartda0436e2009-05-22 14:51:39 -04006616 /*
6617 * Evaluate the read rev and vpd data. Populate the driver
6618 * state with the results. If this routine fails, the failure
6619 * is not fatal as the driver will use generic values.
6620 */
6621 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6622 if (unlikely(!rc)) {
6623 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6624 "0377 Error %d parsing vpd. "
6625 "Using defaults.\n", rc);
6626 rc = 0;
6627 }
James Smart76a95d72010-11-20 23:11:48 -05006628 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006629
James Smartf1126682009-06-10 17:22:44 -04006630 /* Save information as VPD data */
6631 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6632 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6633 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6634 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6635 &mqe->un.read_rev);
6636 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6637 &mqe->un.read_rev);
6638 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6639 &mqe->un.read_rev);
6640 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6641 &mqe->un.read_rev);
6642 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6643 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6644 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6645 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6646 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6647 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6648 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6649 "(%d):0380 READ_REV Status x%x "
6650 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6651 mboxq->vport ? mboxq->vport->vpi : 0,
6652 bf_get(lpfc_mqe_status, mqe),
6653 phba->vpd.rev.opFwName,
6654 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6655 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006656
James Smart572709e2013-07-15 18:32:43 -04006657 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6658 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6659 if (phba->pport->cfg_lun_queue_depth > rc) {
6660 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6661 "3362 LUN queue depth changed from %d to %d\n",
6662 phba->pport->cfg_lun_queue_depth, rc);
6663 phba->pport->cfg_lun_queue_depth = rc;
6664 }
6665
James Smart65791f12016-07-06 12:35:56 -07006666 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006667 LPFC_SLI_INTF_IF_TYPE_0) {
6668 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6669 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6670 if (rc == MBX_SUCCESS) {
6671 phba->hba_flag |= HBA_RECOVERABLE_UE;
6672 /* Set 1Sec interval to detect UE */
6673 phba->eratt_poll_interval = 1;
6674 phba->sli4_hba.ue_to_sr = bf_get(
6675 lpfc_mbx_set_feature_UESR,
6676 &mboxq->u.mqe.un.set_feature);
6677 phba->sli4_hba.ue_to_rp = bf_get(
6678 lpfc_mbx_set_feature_UERP,
6679 &mboxq->u.mqe.un.set_feature);
6680 }
6681 }
6682
6683 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6684 /* Enable MDS Diagnostics only if the SLI Port supports it */
6685 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6686 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6687 if (rc != MBX_SUCCESS)
6688 phba->mds_diags_support = 0;
6689 }
James Smart572709e2013-07-15 18:32:43 -04006690
James Smartda0436e2009-05-22 14:51:39 -04006691 /*
6692 * Discover the port's supported feature set and match it against the
6693 * hosts requests.
6694 */
6695 lpfc_request_features(phba, mboxq);
6696 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6697 if (unlikely(rc)) {
6698 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006699 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006700 }
6701
6702 /*
6703 * The port must support FCP initiator mode as this is the
6704 * only mode running in the host.
6705 */
6706 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6707 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6708 "0378 No support for fcpi mode.\n");
6709 ftr_rsp++;
6710 }
James Smartfedd3b72011-02-16 12:39:24 -05006711 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6712 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6713 else
6714 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006715 /*
6716 * If the port cannot support the host's requested features
6717 * then turn off the global config parameters to disable the
6718 * feature in the driver. This is not a fatal error.
6719 */
James Smartbf086112011-08-21 21:48:13 -04006720 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6721 if (phba->cfg_enable_bg) {
6722 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6723 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6724 else
6725 ftr_rsp++;
6726 }
James Smartda0436e2009-05-22 14:51:39 -04006727
6728 if (phba->max_vpi && phba->cfg_enable_npiv &&
6729 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6730 ftr_rsp++;
6731
6732 if (ftr_rsp) {
6733 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6734 "0379 Feature Mismatch Data: x%08x %08x "
6735 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6736 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6737 phba->cfg_enable_npiv, phba->max_vpi);
6738 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6739 phba->cfg_enable_bg = 0;
6740 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6741 phba->cfg_enable_npiv = 0;
6742 }
6743
6744 /* These SLI3 features are assumed in SLI4 */
6745 spin_lock_irq(&phba->hbalock);
6746 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6747 spin_unlock_irq(&phba->hbalock);
6748
James Smart6d368e52011-05-24 11:44:12 -04006749 /*
6750 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6751 * calls depends on these resources to complete port setup.
6752 */
6753 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6754 if (rc) {
6755 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6756 "2920 Failed to alloc Resource IDs "
6757 "rc = x%x\n", rc);
6758 goto out_free_mbox;
6759 }
6760
James Smart61bda8f2016-10-13 15:06:05 -07006761 lpfc_set_host_data(phba, mboxq);
6762
6763 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6764 if (rc) {
6765 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6766 "2134 Failed to set host os driver version %x",
6767 rc);
6768 }
6769
James Smartda0436e2009-05-22 14:51:39 -04006770 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006771 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6772 if (rc) {
6773 phba->link_state = LPFC_HBA_ERROR;
6774 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006775 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006776 }
6777
James Smartda0436e2009-05-22 14:51:39 -04006778 mboxq->vport = vport;
6779 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6780 mp = (struct lpfc_dmabuf *) mboxq->context1;
6781 if (rc == MBX_SUCCESS) {
6782 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6783 rc = 0;
6784 }
6785
6786 /*
6787 * This memory was allocated by the lpfc_read_sparam routine. Release
6788 * it to the mbuf pool.
6789 */
6790 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6791 kfree(mp);
6792 mboxq->context1 = NULL;
6793 if (unlikely(rc)) {
6794 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6795 "0382 READ_SPARAM command failed "
6796 "status %d, mbxStatus x%x\n",
6797 rc, bf_get(lpfc_mqe_status, mqe));
6798 phba->link_state = LPFC_HBA_ERROR;
6799 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006800 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006801 }
6802
James Smart05580562011-05-24 11:40:48 -04006803 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006804
6805 /* Update the fc_host data structures with new wwn. */
6806 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6807 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6808
James Smart895427b2017-02-12 13:52:30 -08006809 /* Create all the SLI4 queues */
6810 rc = lpfc_sli4_queue_create(phba);
6811 if (rc) {
6812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6813 "3089 Failed to allocate queues\n");
6814 rc = -ENODEV;
6815 goto out_free_mbox;
6816 }
6817 /* Set up all the queues to the device */
6818 rc = lpfc_sli4_queue_setup(phba);
6819 if (unlikely(rc)) {
6820 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6821 "0381 Error %d during queue setup.\n ", rc);
6822 goto out_stop_timers;
6823 }
6824 /* Initialize the driver internal SLI layer lists. */
6825 lpfc_sli4_setup(phba);
6826 lpfc_sli4_queue_init(phba);
6827
6828 /* update host els xri-sgl sizes and mappings */
6829 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006830 if (unlikely(rc)) {
6831 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6832 "1400 Failed to update xri-sgl size and "
6833 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08006834 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006835 }
6836
James Smart8a9d2e82012-05-09 21:16:12 -04006837 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08006838 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6839 phba->sli4_hba.els_xri_cnt);
6840 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04006841 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6842 "0582 Error %d during els sgl post "
6843 "operation\n", rc);
6844 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006845 goto out_destroy_queue;
6846 }
6847 phba->sli4_hba.els_xri_cnt = rc;
6848
James Smartf358dd02017-02-12 13:52:34 -08006849 if (phba->nvmet_support) {
6850 /* update host nvmet xri-sgl sizes and mappings */
6851 rc = lpfc_sli4_nvmet_sgl_update(phba);
6852 if (unlikely(rc)) {
6853 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6854 "6308 Failed to update nvmet-sgl size "
6855 "and mapping: %d\n", rc);
6856 goto out_destroy_queue;
6857 }
6858
6859 /* register the nvmet sgl pool to the port */
6860 rc = lpfc_sli4_repost_sgl_list(
6861 phba,
6862 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6863 phba->sli4_hba.nvmet_xri_cnt);
6864 if (unlikely(rc < 0)) {
6865 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6866 "3117 Error %d during nvmet "
6867 "sgl post\n", rc);
6868 rc = -ENODEV;
6869 goto out_destroy_queue;
6870 }
6871 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smartd613b6a2017-02-12 13:52:37 -08006872 lpfc_nvmet_create_targetport(phba);
James Smartf358dd02017-02-12 13:52:34 -08006873 } else {
James Smart895427b2017-02-12 13:52:30 -08006874 /* update host scsi xri-sgl sizes and mappings */
6875 rc = lpfc_sli4_scsi_sgl_update(phba);
6876 if (unlikely(rc)) {
6877 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6878 "6309 Failed to update scsi-sgl size "
6879 "and mapping: %d\n", rc);
6880 goto out_destroy_queue;
6881 }
6882
6883 /* update host nvme xri-sgl sizes and mappings */
6884 rc = lpfc_sli4_nvme_sgl_update(phba);
6885 if (unlikely(rc)) {
6886 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6887 "6082 Failed to update nvme-sgl size "
6888 "and mapping: %d\n", rc);
6889 goto out_destroy_queue;
6890 }
James Smart8a9d2e82012-05-09 21:16:12 -04006891 }
6892
James Smart2d7dbc42017-02-12 13:52:35 -08006893 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
6894
6895 /* Post initial buffers to all RQs created */
6896 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6897 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6898 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6899 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6900 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
6901 rqbp->entry_count = 256;
6902 rqbp->buffer_count = 0;
6903
6904 /* Divide by 4 and round down to multiple of 16 */
6905 rc = (phba->cfg_nvmet_mrq_post >> 2) & 0xfff8;
6906 phba->sli4_hba.nvmet_mrq_hdr[i]->entry_repost = rc;
6907 phba->sli4_hba.nvmet_mrq_data[i]->entry_repost = rc;
6908
6909 lpfc_post_rq_buffer(
6910 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6911 phba->sli4_hba.nvmet_mrq_data[i],
6912 phba->cfg_nvmet_mrq_post);
6913 }
6914 }
6915
James Smart895427b2017-02-12 13:52:30 -08006916 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6917 /* register the allocated scsi sgl pool to the port */
6918 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6919 if (unlikely(rc)) {
6920 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6921 "0383 Error %d during scsi sgl post "
6922 "operation\n", rc);
6923 /* Some Scsi buffers were moved to abort scsi list */
6924 /* A pci function reset will repost them */
6925 rc = -ENODEV;
6926 goto out_destroy_queue;
6927 }
James Smartda0436e2009-05-22 14:51:39 -04006928 }
6929
James Smart01649562017-02-12 13:52:32 -08006930 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
6931 (phba->nvmet_support == 0)) {
6932
6933 /* register the allocated nvme sgl pool to the port */
6934 rc = lpfc_repost_nvme_sgl_list(phba);
6935 if (unlikely(rc)) {
6936 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6937 "6116 Error %d during nvme sgl post "
6938 "operation\n", rc);
6939 /* Some NVME buffers were moved to abort nvme list */
6940 /* A pci function reset will repost them */
6941 rc = -ENODEV;
6942 goto out_destroy_queue;
6943 }
6944 }
6945
James Smartda0436e2009-05-22 14:51:39 -04006946 /* Post the rpi header region to the device. */
6947 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6948 if (unlikely(rc)) {
6949 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6950 "0393 Error %d during rpi post operation\n",
6951 rc);
6952 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006953 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006954 }
James Smart97f2ecf2012-03-01 22:35:23 -05006955 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006956
James Smart895427b2017-02-12 13:52:30 -08006957 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08006958 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08006959 /*
6960 * The FC Port needs to register FCFI (index 0)
6961 */
6962 lpfc_reg_fcfi(phba, mboxq);
6963 mboxq->vport = phba->pport;
6964 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6965 if (rc != MBX_SUCCESS)
6966 goto out_unset_queue;
6967 rc = 0;
6968 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6969 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08006970 } else {
6971 /* We are a NVME Target mode with MRQ > 1 */
6972
6973 /* First register the FCFI */
6974 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
6975 mboxq->vport = phba->pport;
6976 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6977 if (rc != MBX_SUCCESS)
6978 goto out_unset_queue;
6979 rc = 0;
6980 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
6981 &mboxq->u.mqe.un.reg_fcfi_mrq);
6982
6983 /* Next register the MRQs */
6984 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
6985 mboxq->vport = phba->pport;
6986 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6987 if (rc != MBX_SUCCESS)
6988 goto out_unset_queue;
6989 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08006990 }
6991 /* Check if the port is configured to be disabled */
6992 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04006993 }
6994
6995 /* Arm the CQs and then EQs on device */
6996 lpfc_sli4_arm_cqeq_intr(phba);
6997
6998 /* Indicate device interrupt mode */
6999 phba->sli4_hba.intr_enable = 1;
7000
7001 /* Allow asynchronous mailbox command to go through */
7002 spin_lock_irq(&phba->hbalock);
7003 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7004 spin_unlock_irq(&phba->hbalock);
7005
7006 /* Post receive buffers to the device */
7007 lpfc_sli4_rb_setup(phba);
7008
James Smartfc2b9892010-02-26 14:15:29 -05007009 /* Reset HBA FCF states after HBA reset */
7010 phba->fcf.fcf_flag = 0;
7011 phba->fcf.current_rec.flag = 0;
7012
James Smartda0436e2009-05-22 14:51:39 -04007013 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007014 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007015 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007016
7017 /* Start heart beat timer */
7018 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007019 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007020 phba->hb_outstanding = 0;
7021 phba->last_completion_time = jiffies;
7022
7023 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007024 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007025 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007026
James Smart75baf692010-06-08 18:31:21 -04007027 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7028 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7029 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7030 if (!rc) {
7031 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7032 "2829 This device supports "
7033 "Advanced Error Reporting (AER)\n");
7034 spin_lock_irq(&phba->hbalock);
7035 phba->hba_flag |= HBA_AER_ENABLED;
7036 spin_unlock_irq(&phba->hbalock);
7037 } else {
7038 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7039 "2830 This device does not support "
7040 "Advanced Error Reporting (AER)\n");
7041 phba->cfg_aer_support = 0;
7042 }
James Smart0a96e972011-07-22 18:37:28 -04007043 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007044 }
7045
James Smartda0436e2009-05-22 14:51:39 -04007046 /*
7047 * The port is ready, set the host's link state to LINK_DOWN
7048 * in preparation for link interrupts.
7049 */
James Smartda0436e2009-05-22 14:51:39 -04007050 spin_lock_irq(&phba->hbalock);
7051 phba->link_state = LPFC_LINK_DOWN;
7052 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007053 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7054 (phba->hba_flag & LINK_DISABLED)) {
7055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7056 "3103 Adapter Link is disabled.\n");
7057 lpfc_down_link(phba, mboxq);
7058 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7059 if (rc != MBX_SUCCESS) {
7060 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7061 "3104 Adapter failed to issue "
7062 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7063 goto out_unset_queue;
7064 }
7065 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007066 /* don't perform init_link on SLI4 FC port loopback test */
7067 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7068 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7069 if (rc)
7070 goto out_unset_queue;
7071 }
James Smart5350d872011-10-10 21:33:49 -04007072 }
7073 mempool_free(mboxq, phba->mbox_mem_pool);
7074 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007075out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007076 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007077 lpfc_sli4_queue_unset(phba);
7078out_destroy_queue:
7079 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007080out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007081 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007082out_free_mbox:
7083 mempool_free(mboxq, phba->mbox_mem_pool);
7084 return rc;
7085}
James Smarte59058c2008-08-24 21:49:00 -04007086
7087/**
James Smart3621a712009-04-06 18:47:14 -04007088 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007089 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007090 *
James Smarte59058c2008-08-24 21:49:00 -04007091 * This is the callback function for mailbox timer. The mailbox
7092 * timer is armed when a new mailbox command is issued and the timer
7093 * is deleted when the mailbox complete. The function is called by
7094 * the kernel timer code when a mailbox does not complete within
7095 * expected time. This function wakes up the worker thread to
7096 * process the mailbox timeout and returns. All the processing is
7097 * done by the worker thread function lpfc_mbox_timeout_handler.
7098 **/
dea31012005-04-17 16:05:31 -05007099void
7100lpfc_mbox_timeout(unsigned long ptr)
7101{
James Smart92d7f7b2007-06-17 19:56:38 -05007102 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05007103 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007104 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007105
James Smart2e0fef82007-06-17 19:56:36 -05007106 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007107 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007108 if (!tmo_posted)
7109 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7110 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7111
James Smart5e9d9b82008-06-14 22:52:53 -04007112 if (!tmo_posted)
7113 lpfc_worker_wake_up(phba);
7114 return;
dea31012005-04-17 16:05:31 -05007115}
7116
James Smarte8d3c3b2013-10-10 12:21:30 -04007117/**
7118 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7119 * are pending
7120 * @phba: Pointer to HBA context object.
7121 *
7122 * This function checks if any mailbox completions are present on the mailbox
7123 * completion queue.
7124 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007125static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007126lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7127{
7128
7129 uint32_t idx;
7130 struct lpfc_queue *mcq;
7131 struct lpfc_mcqe *mcqe;
7132 bool pending_completions = false;
7133
7134 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7135 return false;
7136
7137 /* Check for completions on mailbox completion queue */
7138
7139 mcq = phba->sli4_hba.mbx_cq;
7140 idx = mcq->hba_index;
7141 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7142 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7143 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7144 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7145 pending_completions = true;
7146 break;
7147 }
7148 idx = (idx + 1) % mcq->entry_count;
7149 if (mcq->hba_index == idx)
7150 break;
7151 }
7152 return pending_completions;
7153
7154}
7155
7156/**
7157 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7158 * that were missed.
7159 * @phba: Pointer to HBA context object.
7160 *
7161 * For sli4, it is possible to miss an interrupt. As such mbox completions
7162 * maybe missed causing erroneous mailbox timeouts to occur. This function
7163 * checks to see if mbox completions are on the mailbox completion queue
7164 * and will process all the completions associated with the eq for the
7165 * mailbox completion queue.
7166 **/
7167bool
7168lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7169{
7170
7171 uint32_t eqidx;
7172 struct lpfc_queue *fpeq = NULL;
7173 struct lpfc_eqe *eqe;
7174 bool mbox_pending;
7175
7176 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7177 return false;
7178
7179 /* Find the eq associated with the mcq */
7180
7181 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007182 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007183 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7184 phba->sli4_hba.mbx_cq->assoc_qid) {
7185 fpeq = phba->sli4_hba.hba_eq[eqidx];
7186 break;
7187 }
7188 if (!fpeq)
7189 return false;
7190
7191 /* Turn off interrupts from this EQ */
7192
7193 lpfc_sli4_eq_clr_intr(fpeq);
7194
7195 /* Check to see if a mbox completion is pending */
7196
7197 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7198
7199 /*
7200 * If a mbox completion is pending, process all the events on EQ
7201 * associated with the mbox completion queue (this could include
7202 * mailbox commands, async events, els commands, receive queue data
7203 * and fcp commands)
7204 */
7205
7206 if (mbox_pending)
7207 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7208 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7209 fpeq->EQ_processed++;
7210 }
7211
7212 /* Always clear and re-arm the EQ */
7213
7214 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7215
7216 return mbox_pending;
7217
7218}
James Smarte59058c2008-08-24 21:49:00 -04007219
7220/**
James Smart3621a712009-04-06 18:47:14 -04007221 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007222 * @phba: Pointer to HBA context object.
7223 *
7224 * This function is called from worker thread when a mailbox command times out.
7225 * The caller is not required to hold any locks. This function will reset the
7226 * HBA and recover all the pending commands.
7227 **/
dea31012005-04-17 16:05:31 -05007228void
7229lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7230{
James Smart2e0fef82007-06-17 19:56:36 -05007231 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007232 MAILBOX_t *mb = NULL;
7233
James Smart1dcb58e2007-04-25 09:51:30 -04007234 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007235
James Smarte8d3c3b2013-10-10 12:21:30 -04007236 /* If the mailbox completed, process the completion and return */
7237 if (lpfc_sli4_process_missed_mbox_completions(phba))
7238 return;
7239
James Smarteb016562014-09-03 12:58:06 -04007240 if (pmbox != NULL)
7241 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007242 /* Check the pmbox pointer first. There is a race condition
7243 * between the mbox timeout handler getting executed in the
7244 * worklist and the mailbox actually completing. When this
7245 * race condition occurs, the mbox_active will be NULL.
7246 */
7247 spin_lock_irq(&phba->hbalock);
7248 if (pmbox == NULL) {
7249 lpfc_printf_log(phba, KERN_WARNING,
7250 LOG_MBOX | LOG_SLI,
7251 "0353 Active Mailbox cleared - mailbox timeout "
7252 "exiting\n");
7253 spin_unlock_irq(&phba->hbalock);
7254 return;
7255 }
7256
dea31012005-04-17 16:05:31 -05007257 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007258 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007259 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007260 mb->mbxCommand,
7261 phba->pport->port_state,
7262 phba->sli.sli_flag,
7263 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007264 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007265
James Smart1dcb58e2007-04-25 09:51:30 -04007266 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7267 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007268 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007269 */
James Smart2e0fef82007-06-17 19:56:36 -05007270 spin_lock_irq(&phba->pport->work_port_lock);
7271 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7272 spin_unlock_irq(&phba->pport->work_port_lock);
7273 spin_lock_irq(&phba->hbalock);
7274 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007275 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007276 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007277
James Smartdb55fba2014-04-04 13:52:02 -04007278 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007279
7280 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007281 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007282
7283 /* Reset the HBA device */
7284 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007285}
7286
James Smarte59058c2008-08-24 21:49:00 -04007287/**
James Smart3772a992009-05-22 14:50:54 -04007288 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007289 * @phba: Pointer to HBA context object.
7290 * @pmbox: Pointer to mailbox object.
7291 * @flag: Flag indicating how the mailbox need to be processed.
7292 *
7293 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007294 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7295 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007296 * The mailbox command can be submitted in polling mode, in which case
7297 * this function will wait in a polling loop for the completion of the
7298 * mailbox.
7299 * If the mailbox is submitted in no_wait mode (not polling) the
7300 * function will submit the command and returns immediately without waiting
7301 * for the mailbox completion. The no_wait is supported only when HBA
7302 * is in SLI2/SLI3 mode - interrupts are enabled.
7303 * The SLI interface allows only one mailbox pending at a time. If the
7304 * mailbox is issued in polling mode and there is already a mailbox
7305 * pending, then the function will return an error. If the mailbox is issued
7306 * in NO_WAIT mode and there is a mailbox pending already, the function
7307 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7308 * The sli layer owns the mailbox object until the completion of mailbox
7309 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7310 * return codes the caller owns the mailbox command after the return of
7311 * the function.
7312 **/
James Smart3772a992009-05-22 14:50:54 -04007313static int
7314lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7315 uint32_t flag)
dea31012005-04-17 16:05:31 -05007316{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007317 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007318 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007319 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007320 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007321 int i;
James Smart09372822008-01-11 01:52:54 -05007322 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007323 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007324 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007325 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007326 int processing_queue = 0;
7327
7328 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7329 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007330 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007331 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007332 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7333 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7334 return MBX_SUCCESS;
7335 }
James Smart58da1ff2008-04-07 10:15:56 -04007336 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007337 pmbox = lpfc_mbox_get(phba);
7338 if (!pmbox) {
7339 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7340 return MBX_SUCCESS;
7341 }
7342 }
dea31012005-04-17 16:05:31 -05007343
James Smarted957682007-06-17 19:56:37 -05007344 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007345 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007346 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007347 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007348 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007349 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007350 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007351 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007352 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007353 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007354 }
7355 }
7356
Linas Vepstas8d63f372007-02-14 14:28:36 -06007357 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007358 if (unlikely(pci_channel_offline(phba->pcidev))) {
7359 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7360 goto out_not_finished;
7361 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007362
James Smarta257bf92009-04-06 18:48:10 -04007363 /* If HBA has a deferred error attention, fail the iocb. */
7364 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7365 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7366 goto out_not_finished;
7367 }
7368
dea31012005-04-17 16:05:31 -05007369 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007370
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007371 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007372 status = MBX_SUCCESS;
7373
James Smart2e0fef82007-06-17 19:56:36 -05007374 if (phba->link_state == LPFC_HBA_ERROR) {
7375 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007376
7377 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007378 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7379 "(%d):0311 Mailbox command x%x cannot "
7380 "issue Data: x%x x%x\n",
7381 pmbox->vport ? pmbox->vport->vpi : 0,
7382 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007383 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007384 }
7385
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007386 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007387 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7388 !(hc_copy & HC_MBINT_ENA)) {
7389 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7390 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007391 "(%d):2528 Mailbox command x%x cannot "
7392 "issue Data: x%x x%x\n",
7393 pmbox->vport ? pmbox->vport->vpi : 0,
7394 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007395 goto out_not_finished;
7396 }
James Smart92908312006-03-07 15:04:13 -05007397 }
7398
dea31012005-04-17 16:05:31 -05007399 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7400 /* Polling for a mbox command when another one is already active
7401 * is not allowed in SLI. Also, the driver must have established
7402 * SLI2 mode to queue and process multiple mbox commands.
7403 */
7404
7405 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007406 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007407
7408 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007409 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7410 "(%d):2529 Mailbox command x%x "
7411 "cannot issue Data: x%x x%x\n",
7412 pmbox->vport ? pmbox->vport->vpi : 0,
7413 pmbox->u.mb.mbxCommand,
7414 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007415 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007416 }
7417
James Smart3772a992009-05-22 14:50:54 -04007418 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007419 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007420 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007421 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7422 "(%d):2530 Mailbox command x%x "
7423 "cannot issue Data: x%x x%x\n",
7424 pmbox->vport ? pmbox->vport->vpi : 0,
7425 pmbox->u.mb.mbxCommand,
7426 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007427 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007428 }
7429
dea31012005-04-17 16:05:31 -05007430 /* Another mailbox command is still being processed, queue this
7431 * command to be processed later.
7432 */
7433 lpfc_mbox_put(phba, pmbox);
7434
7435 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007436 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007437 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007438 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007439 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007440 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007441 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007442
7443 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007444 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007445
James Smart858c9f62007-06-17 19:56:39 -05007446 if (pmbox->vport) {
7447 lpfc_debugfs_disc_trc(pmbox->vport,
7448 LPFC_DISC_TRC_MBOX_VPORT,
7449 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007450 (uint32_t)mbx->mbxCommand,
7451 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007452 }
7453 else {
7454 lpfc_debugfs_disc_trc(phba->pport,
7455 LPFC_DISC_TRC_MBOX,
7456 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007457 (uint32_t)mbx->mbxCommand,
7458 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007459 }
7460
James Smart2e0fef82007-06-17 19:56:36 -05007461 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007462 }
7463
dea31012005-04-17 16:05:31 -05007464 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7465
7466 /* If we are not polling, we MUST be in SLI2 mode */
7467 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007468 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007469 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007470 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007471 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007472 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007473 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7474 "(%d):2531 Mailbox command x%x "
7475 "cannot issue Data: x%x x%x\n",
7476 pmbox->vport ? pmbox->vport->vpi : 0,
7477 pmbox->u.mb.mbxCommand,
7478 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007479 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007480 }
7481 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007482 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7483 1000);
7484 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007485 }
7486
7487 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007488 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007489 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007490 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007491 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007492 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007493 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007494
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007495 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007496 if (pmbox->vport) {
7497 lpfc_debugfs_disc_trc(pmbox->vport,
7498 LPFC_DISC_TRC_MBOX_VPORT,
7499 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007500 (uint32_t)mbx->mbxCommand,
7501 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007502 }
7503 else {
7504 lpfc_debugfs_disc_trc(phba->pport,
7505 LPFC_DISC_TRC_MBOX,
7506 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007507 (uint32_t)mbx->mbxCommand,
7508 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007509 }
7510 }
7511
dea31012005-04-17 16:05:31 -05007512 psli->slistat.mbox_cmd++;
7513 evtctr = psli->slistat.mbox_event;
7514
7515 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007516 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007517
James Smart3772a992009-05-22 14:50:54 -04007518 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007519 /* Populate mbox extension offset word. */
7520 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007521 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007522 = (uint8_t *)phba->mbox_ext
7523 - (uint8_t *)phba->mbox;
7524 }
7525
7526 /* Copy the mailbox extension data */
7527 if (pmbox->in_ext_byte_len && pmbox->context2) {
7528 lpfc_sli_pcimem_bcopy(pmbox->context2,
7529 (uint8_t *)phba->mbox_ext,
7530 pmbox->in_ext_byte_len);
7531 }
7532 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007533 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007534 } else {
James Smart7a470272010-03-15 11:25:20 -04007535 /* Populate mbox extension offset word. */
7536 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007537 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007538 = MAILBOX_HBA_EXT_OFFSET;
7539
7540 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007541 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007542 lpfc_memcpy_to_slim(phba->MBslimaddr +
7543 MAILBOX_HBA_EXT_OFFSET,
7544 pmbox->context2, pmbox->in_ext_byte_len);
7545
James Smart895427b2017-02-12 13:52:30 -08007546 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007547 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007548 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7549 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007550
7551 /* First copy mbox command data to HBA SLIM, skip past first
7552 word */
7553 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007554 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007555 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7556
7557 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007558 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007559 to_slim = phba->MBslimaddr;
7560 writel(ldata, to_slim);
7561 readl(to_slim); /* flush */
7562
James Smart895427b2017-02-12 13:52:30 -08007563 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007564 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007565 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007566 }
7567
7568 wmb();
dea31012005-04-17 16:05:31 -05007569
7570 switch (flag) {
7571 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007572 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007573 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007574 /* Interrupt board to do it */
7575 writel(CA_MBATT, phba->CAregaddr);
7576 readl(phba->CAregaddr); /* flush */
7577 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007578 break;
7579
7580 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007581 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007582 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007583 /* Interrupt board to do it */
7584 writel(CA_MBATT, phba->CAregaddr);
7585 readl(phba->CAregaddr); /* flush */
7586
James Smart3772a992009-05-22 14:50:54 -04007587 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007588 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007589 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007590 word0 = le32_to_cpu(word0);
7591 } else {
7592 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007593 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7594 spin_unlock_irqrestore(&phba->hbalock,
7595 drvr_flag);
7596 goto out_not_finished;
7597 }
dea31012005-04-17 16:05:31 -05007598 }
7599
7600 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007601 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7602 spin_unlock_irqrestore(&phba->hbalock,
7603 drvr_flag);
7604 goto out_not_finished;
7605 }
James Smarta183a152011-10-10 21:32:43 -04007606 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7607 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007608 i = 0;
dea31012005-04-17 16:05:31 -05007609 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007610 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7611 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007612 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007613 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007614 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007615 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007616 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007617 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007618 }
7619
7620 /* Check if we took a mbox interrupt while we were
7621 polling */
7622 if (((word0 & OWN_CHIP) != OWN_CHIP)
7623 && (evtctr != psli->slistat.mbox_event))
7624 break;
7625
James Smart09372822008-01-11 01:52:54 -05007626 if (i++ > 10) {
7627 spin_unlock_irqrestore(&phba->hbalock,
7628 drvr_flag);
7629 msleep(1);
7630 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7631 }
dea31012005-04-17 16:05:31 -05007632
James Smart3772a992009-05-22 14:50:54 -04007633 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007634 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007635 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007636 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007637 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007638 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007639 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007640 /* Check real SLIM for any errors */
7641 slimword0 = readl(phba->MBslimaddr);
7642 slimmb = (MAILBOX_t *) & slimword0;
7643 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7644 && slimmb->mbxStatus) {
7645 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007646 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007647 word0 = slimword0;
7648 }
7649 }
7650 } else {
7651 /* First copy command data */
7652 word0 = readl(phba->MBslimaddr);
7653 }
7654 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007655 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7656 spin_unlock_irqrestore(&phba->hbalock,
7657 drvr_flag);
7658 goto out_not_finished;
7659 }
dea31012005-04-17 16:05:31 -05007660 }
7661
James Smart3772a992009-05-22 14:50:54 -04007662 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007663 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007664 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7665 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007666 /* Copy the mailbox extension data */
7667 if (pmbox->out_ext_byte_len && pmbox->context2) {
7668 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7669 pmbox->context2,
7670 pmbox->out_ext_byte_len);
7671 }
dea31012005-04-17 16:05:31 -05007672 } else {
7673 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007674 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007675 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007676 /* Copy the mailbox extension data */
7677 if (pmbox->out_ext_byte_len && pmbox->context2) {
7678 lpfc_memcpy_from_slim(pmbox->context2,
7679 phba->MBslimaddr +
7680 MAILBOX_HBA_EXT_OFFSET,
7681 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007682 }
7683 }
7684
7685 writel(HA_MBATT, phba->HAregaddr);
7686 readl(phba->HAregaddr); /* flush */
7687
7688 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007689 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007690 }
7691
James Smart2e0fef82007-06-17 19:56:36 -05007692 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7693 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007694
7695out_not_finished:
7696 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007697 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007698 lpfc_mbox_cmpl_put(phba, pmbox);
7699 }
7700 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007701}
7702
James Smarte59058c2008-08-24 21:49:00 -04007703/**
James Smartf1126682009-06-10 17:22:44 -04007704 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7705 * @phba: Pointer to HBA context object.
7706 *
7707 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7708 * the driver internal pending mailbox queue. It will then try to wait out the
7709 * possible outstanding mailbox command before return.
7710 *
7711 * Returns:
7712 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7713 * the outstanding mailbox command timed out.
7714 **/
7715static int
7716lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7717{
7718 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007719 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007720 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007721
7722 /* Mark the asynchronous mailbox command posting as blocked */
7723 spin_lock_irq(&phba->hbalock);
7724 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007725 /* Determine how long we might wait for the active mailbox
7726 * command to be gracefully completed by firmware.
7727 */
James Smarta183a152011-10-10 21:32:43 -04007728 if (phba->sli.mbox_active)
7729 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7730 phba->sli.mbox_active) *
7731 1000) + jiffies;
7732 spin_unlock_irq(&phba->hbalock);
7733
James Smarte8d3c3b2013-10-10 12:21:30 -04007734 /* Make sure the mailbox is really active */
7735 if (timeout)
7736 lpfc_sli4_process_missed_mbox_completions(phba);
7737
James Smartf1126682009-06-10 17:22:44 -04007738 /* Wait for the outstnading mailbox command to complete */
7739 while (phba->sli.mbox_active) {
7740 /* Check active mailbox complete status every 2ms */
7741 msleep(2);
7742 if (time_after(jiffies, timeout)) {
7743 /* Timeout, marked the outstanding cmd not complete */
7744 rc = 1;
7745 break;
7746 }
7747 }
7748
7749 /* Can not cleanly block async mailbox command, fails it */
7750 if (rc) {
7751 spin_lock_irq(&phba->hbalock);
7752 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7753 spin_unlock_irq(&phba->hbalock);
7754 }
7755 return rc;
7756}
7757
7758/**
7759 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7760 * @phba: Pointer to HBA context object.
7761 *
7762 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7763 * commands from the driver internal pending mailbox queue. It makes sure
7764 * that there is no outstanding mailbox command before resuming posting
7765 * asynchronous mailbox commands. If, for any reason, there is outstanding
7766 * mailbox command, it will try to wait it out before resuming asynchronous
7767 * mailbox command posting.
7768 **/
7769static void
7770lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7771{
7772 struct lpfc_sli *psli = &phba->sli;
7773
7774 spin_lock_irq(&phba->hbalock);
7775 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7776 /* Asynchronous mailbox posting is not blocked, do nothing */
7777 spin_unlock_irq(&phba->hbalock);
7778 return;
7779 }
7780
7781 /* Outstanding synchronous mailbox command is guaranteed to be done,
7782 * successful or timeout, after timing-out the outstanding mailbox
7783 * command shall always be removed, so just unblock posting async
7784 * mailbox command and resume
7785 */
7786 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7787 spin_unlock_irq(&phba->hbalock);
7788
7789 /* wake up worker thread to post asynchronlous mailbox command */
7790 lpfc_worker_wake_up(phba);
7791}
7792
7793/**
James Smart2d843ed2012-09-29 11:29:06 -04007794 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7795 * @phba: Pointer to HBA context object.
7796 * @mboxq: Pointer to mailbox object.
7797 *
7798 * The function waits for the bootstrap mailbox register ready bit from
7799 * port for twice the regular mailbox command timeout value.
7800 *
7801 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7802 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7803 **/
7804static int
7805lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7806{
7807 uint32_t db_ready;
7808 unsigned long timeout;
7809 struct lpfc_register bmbx_reg;
7810
7811 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7812 * 1000) + jiffies;
7813
7814 do {
7815 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7816 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7817 if (!db_ready)
7818 msleep(2);
7819
7820 if (time_after(jiffies, timeout))
7821 return MBXERR_ERROR;
7822 } while (!db_ready);
7823
7824 return 0;
7825}
7826
7827/**
James Smartda0436e2009-05-22 14:51:39 -04007828 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7829 * @phba: Pointer to HBA context object.
7830 * @mboxq: Pointer to mailbox object.
7831 *
7832 * The function posts a mailbox to the port. The mailbox is expected
7833 * to be comletely filled in and ready for the port to operate on it.
7834 * This routine executes a synchronous completion operation on the
7835 * mailbox by polling for its completion.
7836 *
7837 * The caller must not be holding any locks when calling this routine.
7838 *
7839 * Returns:
7840 * MBX_SUCCESS - mailbox posted successfully
7841 * Any of the MBX error values.
7842 **/
7843static int
7844lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7845{
7846 int rc = MBX_SUCCESS;
7847 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007848 uint32_t mcqe_status;
7849 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007850 struct lpfc_sli *psli = &phba->sli;
7851 struct lpfc_mqe *mb = &mboxq->u.mqe;
7852 struct lpfc_bmbx_create *mbox_rgn;
7853 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007854
7855 /*
7856 * Only one mailbox can be active to the bootstrap mailbox region
7857 * at a time and there is no queueing provided.
7858 */
7859 spin_lock_irqsave(&phba->hbalock, iflag);
7860 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7861 spin_unlock_irqrestore(&phba->hbalock, iflag);
7862 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007863 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007864 "cannot issue Data: x%x x%x\n",
7865 mboxq->vport ? mboxq->vport->vpi : 0,
7866 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007867 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7868 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007869 psli->sli_flag, MBX_POLL);
7870 return MBXERR_ERROR;
7871 }
7872 /* The server grabs the token and owns it until release */
7873 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7874 phba->sli.mbox_active = mboxq;
7875 spin_unlock_irqrestore(&phba->hbalock, iflag);
7876
James Smart2d843ed2012-09-29 11:29:06 -04007877 /* wait for bootstrap mbox register for readyness */
7878 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7879 if (rc)
7880 goto exit;
7881
James Smartda0436e2009-05-22 14:51:39 -04007882 /*
7883 * Initialize the bootstrap memory region to avoid stale data areas
7884 * in the mailbox post. Then copy the caller's mailbox contents to
7885 * the bmbx mailbox region.
7886 */
7887 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7888 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7889 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7890 sizeof(struct lpfc_mqe));
7891
7892 /* Post the high mailbox dma address to the port and wait for ready. */
7893 dma_address = &phba->sli4_hba.bmbx.dma_address;
7894 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7895
James Smart2d843ed2012-09-29 11:29:06 -04007896 /* wait for bootstrap mbox register for hi-address write done */
7897 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7898 if (rc)
7899 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007900
7901 /* Post the low mailbox dma address to the port. */
7902 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007903
James Smart2d843ed2012-09-29 11:29:06 -04007904 /* wait for bootstrap mbox register for low address write done */
7905 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7906 if (rc)
7907 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007908
7909 /*
7910 * Read the CQ to ensure the mailbox has completed.
7911 * If so, update the mailbox status so that the upper layers
7912 * can complete the request normally.
7913 */
7914 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7915 sizeof(struct lpfc_mqe));
7916 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7917 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7918 sizeof(struct lpfc_mcqe));
7919 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007920 /*
7921 * When the CQE status indicates a failure and the mailbox status
7922 * indicates success then copy the CQE status into the mailbox status
7923 * (and prefix it with x4000).
7924 */
James Smartda0436e2009-05-22 14:51:39 -04007925 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007926 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7927 bf_set(lpfc_mqe_status, mb,
7928 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007929 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007930 } else
7931 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007932
7933 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007934 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007935 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7936 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007937 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7938 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7939 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007940 bf_get(lpfc_mqe_status, mb),
7941 mb->un.mb_words[0], mb->un.mb_words[1],
7942 mb->un.mb_words[2], mb->un.mb_words[3],
7943 mb->un.mb_words[4], mb->un.mb_words[5],
7944 mb->un.mb_words[6], mb->un.mb_words[7],
7945 mb->un.mb_words[8], mb->un.mb_words[9],
7946 mb->un.mb_words[10], mb->un.mb_words[11],
7947 mb->un.mb_words[12], mboxq->mcqe.word0,
7948 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7949 mboxq->mcqe.trailer);
7950exit:
7951 /* We are holding the token, no needed for lock when release */
7952 spin_lock_irqsave(&phba->hbalock, iflag);
7953 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7954 phba->sli.mbox_active = NULL;
7955 spin_unlock_irqrestore(&phba->hbalock, iflag);
7956 return rc;
7957}
7958
7959/**
7960 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7961 * @phba: Pointer to HBA context object.
7962 * @pmbox: Pointer to mailbox object.
7963 * @flag: Flag indicating how the mailbox need to be processed.
7964 *
7965 * This function is called by discovery code and HBA management code to submit
7966 * a mailbox command to firmware with SLI-4 interface spec.
7967 *
7968 * Return codes the caller owns the mailbox command after the return of the
7969 * function.
7970 **/
7971static int
7972lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7973 uint32_t flag)
7974{
7975 struct lpfc_sli *psli = &phba->sli;
7976 unsigned long iflags;
7977 int rc;
7978
James Smartb76f2dc2011-07-22 18:37:42 -04007979 /* dump from issue mailbox command if setup */
7980 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7981
James Smart8fa38512009-07-19 10:01:03 -04007982 rc = lpfc_mbox_dev_check(phba);
7983 if (unlikely(rc)) {
7984 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007985 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007986 "cannot issue Data: x%x x%x\n",
7987 mboxq->vport ? mboxq->vport->vpi : 0,
7988 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007989 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7990 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007991 psli->sli_flag, flag);
7992 goto out_not_finished;
7993 }
7994
James Smartda0436e2009-05-22 14:51:39 -04007995 /* Detect polling mode and jump to a handler */
7996 if (!phba->sli4_hba.intr_enable) {
7997 if (flag == MBX_POLL)
7998 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7999 else
8000 rc = -EIO;
8001 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008002 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008003 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008004 "(x%x/x%x) failure: "
8005 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8006 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008007 mboxq->vport ? mboxq->vport->vpi : 0,
8008 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008009 lpfc_sli_config_mbox_subsys_get(phba,
8010 mboxq),
8011 lpfc_sli_config_mbox_opcode_get(phba,
8012 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008013 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8014 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8015 bf_get(lpfc_mcqe_ext_status,
8016 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008017 psli->sli_flag, flag);
8018 return rc;
8019 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008020 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8021 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04008022 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04008023 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008024 mboxq->vport ? mboxq->vport->vpi : 0,
8025 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008026 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8027 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008028 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008029 /* Try to block the asynchronous mailbox posting */
8030 rc = lpfc_sli4_async_mbox_block(phba);
8031 if (!rc) {
8032 /* Successfully blocked, now issue sync mbox cmd */
8033 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8034 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008035 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008036 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008037 "(%d):2597 Sync Mailbox command "
8038 "x%x (x%x/x%x) failure: "
8039 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8040 "Data: x%x x%x\n,",
8041 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008042 mboxq->u.mb.mbxCommand,
8043 lpfc_sli_config_mbox_subsys_get(phba,
8044 mboxq),
8045 lpfc_sli_config_mbox_opcode_get(phba,
8046 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008047 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8048 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8049 bf_get(lpfc_mcqe_ext_status,
8050 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008051 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008052 /* Unblock the async mailbox posting afterward */
8053 lpfc_sli4_async_mbox_unblock(phba);
8054 }
8055 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008056 }
8057
8058 /* Now, interrupt mode asynchrous mailbox command */
8059 rc = lpfc_mbox_cmd_check(phba, mboxq);
8060 if (rc) {
8061 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008062 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008063 "cannot issue Data: x%x x%x\n",
8064 mboxq->vport ? mboxq->vport->vpi : 0,
8065 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008066 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8067 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008068 psli->sli_flag, flag);
8069 goto out_not_finished;
8070 }
James Smartda0436e2009-05-22 14:51:39 -04008071
8072 /* Put the mailbox command to the driver internal FIFO */
8073 psli->slistat.mbox_busy++;
8074 spin_lock_irqsave(&phba->hbalock, iflags);
8075 lpfc_mbox_put(phba, mboxq);
8076 spin_unlock_irqrestore(&phba->hbalock, iflags);
8077 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8078 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008079 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008080 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8081 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008082 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8083 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008084 phba->pport->port_state,
8085 psli->sli_flag, MBX_NOWAIT);
8086 /* Wake up worker thread to transport mailbox command from head */
8087 lpfc_worker_wake_up(phba);
8088
8089 return MBX_BUSY;
8090
8091out_not_finished:
8092 return MBX_NOT_FINISHED;
8093}
8094
8095/**
8096 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8097 * @phba: Pointer to HBA context object.
8098 *
8099 * This function is called by worker thread to send a mailbox command to
8100 * SLI4 HBA firmware.
8101 *
8102 **/
8103int
8104lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8105{
8106 struct lpfc_sli *psli = &phba->sli;
8107 LPFC_MBOXQ_t *mboxq;
8108 int rc = MBX_SUCCESS;
8109 unsigned long iflags;
8110 struct lpfc_mqe *mqe;
8111 uint32_t mbx_cmnd;
8112
8113 /* Check interrupt mode before post async mailbox command */
8114 if (unlikely(!phba->sli4_hba.intr_enable))
8115 return MBX_NOT_FINISHED;
8116
8117 /* Check for mailbox command service token */
8118 spin_lock_irqsave(&phba->hbalock, iflags);
8119 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8120 spin_unlock_irqrestore(&phba->hbalock, iflags);
8121 return MBX_NOT_FINISHED;
8122 }
8123 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8124 spin_unlock_irqrestore(&phba->hbalock, iflags);
8125 return MBX_NOT_FINISHED;
8126 }
8127 if (unlikely(phba->sli.mbox_active)) {
8128 spin_unlock_irqrestore(&phba->hbalock, iflags);
8129 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8130 "0384 There is pending active mailbox cmd\n");
8131 return MBX_NOT_FINISHED;
8132 }
8133 /* Take the mailbox command service token */
8134 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8135
8136 /* Get the next mailbox command from head of queue */
8137 mboxq = lpfc_mbox_get(phba);
8138
8139 /* If no more mailbox command waiting for post, we're done */
8140 if (!mboxq) {
8141 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8142 spin_unlock_irqrestore(&phba->hbalock, iflags);
8143 return MBX_SUCCESS;
8144 }
8145 phba->sli.mbox_active = mboxq;
8146 spin_unlock_irqrestore(&phba->hbalock, iflags);
8147
8148 /* Check device readiness for posting mailbox command */
8149 rc = lpfc_mbox_dev_check(phba);
8150 if (unlikely(rc))
8151 /* Driver clean routine will clean up pending mailbox */
8152 goto out_not_finished;
8153
8154 /* Prepare the mbox command to be posted */
8155 mqe = &mboxq->u.mqe;
8156 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8157
8158 /* Start timer for the mbox_tmo and log some mailbox post messages */
8159 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008160 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008161
8162 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008163 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008164 "x%x x%x\n",
8165 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008166 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8167 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008168 phba->pport->port_state, psli->sli_flag);
8169
8170 if (mbx_cmnd != MBX_HEARTBEAT) {
8171 if (mboxq->vport) {
8172 lpfc_debugfs_disc_trc(mboxq->vport,
8173 LPFC_DISC_TRC_MBOX_VPORT,
8174 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8175 mbx_cmnd, mqe->un.mb_words[0],
8176 mqe->un.mb_words[1]);
8177 } else {
8178 lpfc_debugfs_disc_trc(phba->pport,
8179 LPFC_DISC_TRC_MBOX,
8180 "MBOX Send: cmd:x%x mb:x%x x%x",
8181 mbx_cmnd, mqe->un.mb_words[0],
8182 mqe->un.mb_words[1]);
8183 }
8184 }
8185 psli->slistat.mbox_cmd++;
8186
8187 /* Post the mailbox command to the port */
8188 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8189 if (rc != MBX_SUCCESS) {
8190 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008191 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008192 "cannot issue Data: x%x x%x\n",
8193 mboxq->vport ? mboxq->vport->vpi : 0,
8194 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008195 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8196 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008197 psli->sli_flag, MBX_NOWAIT);
8198 goto out_not_finished;
8199 }
8200
8201 return rc;
8202
8203out_not_finished:
8204 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008205 if (phba->sli.mbox_active) {
8206 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8207 __lpfc_mbox_cmpl_put(phba, mboxq);
8208 /* Release the token */
8209 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8210 phba->sli.mbox_active = NULL;
8211 }
James Smartda0436e2009-05-22 14:51:39 -04008212 spin_unlock_irqrestore(&phba->hbalock, iflags);
8213
8214 return MBX_NOT_FINISHED;
8215}
8216
8217/**
8218 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8219 * @phba: Pointer to HBA context object.
8220 * @pmbox: Pointer to mailbox object.
8221 * @flag: Flag indicating how the mailbox need to be processed.
8222 *
8223 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8224 * the API jump table function pointer from the lpfc_hba struct.
8225 *
8226 * Return codes the caller owns the mailbox command after the return of the
8227 * function.
8228 **/
8229int
8230lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8231{
8232 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8233}
8234
8235/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008236 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008237 * @phba: The hba struct for which this call is being executed.
8238 * @dev_grp: The HBA PCI-Device group number.
8239 *
8240 * This routine sets up the mbox interface API function jump table in @phba
8241 * struct.
8242 * Returns: 0 - success, -ENODEV - failure.
8243 **/
8244int
8245lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8246{
8247
8248 switch (dev_grp) {
8249 case LPFC_PCI_DEV_LP:
8250 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8251 phba->lpfc_sli_handle_slow_ring_event =
8252 lpfc_sli_handle_slow_ring_event_s3;
8253 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8254 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8255 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8256 break;
8257 case LPFC_PCI_DEV_OC:
8258 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8259 phba->lpfc_sli_handle_slow_ring_event =
8260 lpfc_sli_handle_slow_ring_event_s4;
8261 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8262 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8263 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8264 break;
8265 default:
8266 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8267 "1420 Invalid HBA PCI-device group: 0x%x\n",
8268 dev_grp);
8269 return -ENODEV;
8270 break;
8271 }
8272 return 0;
8273}
8274
8275/**
James Smart3621a712009-04-06 18:47:14 -04008276 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008277 * @phba: Pointer to HBA context object.
8278 * @pring: Pointer to driver SLI ring object.
8279 * @piocb: Pointer to address of newly added command iocb.
8280 *
8281 * This function is called with hbalock held to add a command
8282 * iocb to the txq when SLI layer cannot submit the command iocb
8283 * to the ring.
8284 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008285void
James Smart92d7f7b2007-06-17 19:56:38 -05008286__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008287 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008288{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008289 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008290 /* Insert the caller's iocb in the txq tail for later processing. */
8291 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008292}
8293
James Smarte59058c2008-08-24 21:49:00 -04008294/**
James Smart3621a712009-04-06 18:47:14 -04008295 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008296 * @phba: Pointer to HBA context object.
8297 * @pring: Pointer to driver SLI ring object.
8298 * @piocb: Pointer to address of newly added command iocb.
8299 *
8300 * This function is called with hbalock held before a new
8301 * iocb is submitted to the firmware. This function checks
8302 * txq to flush the iocbs in txq to Firmware before
8303 * submitting new iocbs to the Firmware.
8304 * If there are iocbs in the txq which need to be submitted
8305 * to firmware, lpfc_sli_next_iocb returns the first element
8306 * of the txq after dequeuing it from txq.
8307 * If there is no iocb in the txq then the function will return
8308 * *piocb and *piocb is set to NULL. Caller needs to check
8309 * *piocb to find if there are more commands in the txq.
8310 **/
dea31012005-04-17 16:05:31 -05008311static struct lpfc_iocbq *
8312lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008313 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008314{
8315 struct lpfc_iocbq * nextiocb;
8316
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008317 lockdep_assert_held(&phba->hbalock);
8318
dea31012005-04-17 16:05:31 -05008319 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8320 if (!nextiocb) {
8321 nextiocb = *piocb;
8322 *piocb = NULL;
8323 }
8324
8325 return nextiocb;
8326}
8327
James Smarte59058c2008-08-24 21:49:00 -04008328/**
James Smart3772a992009-05-22 14:50:54 -04008329 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008330 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008331 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008332 * @piocb: Pointer to command iocb.
8333 * @flag: Flag indicating if this command can be put into txq.
8334 *
James Smart3772a992009-05-22 14:50:54 -04008335 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8336 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8337 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8338 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8339 * this function allows only iocbs for posting buffers. This function finds
8340 * next available slot in the command ring and posts the command to the
8341 * available slot and writes the port attention register to request HBA start
8342 * processing new iocb. If there is no slot available in the ring and
8343 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8344 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008345 *
James Smart3772a992009-05-22 14:50:54 -04008346 * This function is called with hbalock held. The function will return success
8347 * after it successfully submit the iocb to firmware or after adding to the
8348 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008349 **/
James Smart98c9ea52007-10-27 13:37:33 -04008350static int
James Smart3772a992009-05-22 14:50:54 -04008351__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008352 struct lpfc_iocbq *piocb, uint32_t flag)
8353{
8354 struct lpfc_iocbq *nextiocb;
8355 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008356 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008357
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008358 lockdep_assert_held(&phba->hbalock);
8359
James Smart92d7f7b2007-06-17 19:56:38 -05008360 if (piocb->iocb_cmpl && (!piocb->vport) &&
8361 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8362 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8363 lpfc_printf_log(phba, KERN_ERR,
8364 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008365 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008366 piocb->iocb.ulpCommand);
8367 dump_stack();
8368 return IOCB_ERROR;
8369 }
8370
8371
Linas Vepstas8d63f372007-02-14 14:28:36 -06008372 /* If the PCI channel is in offline state, do not post iocbs. */
8373 if (unlikely(pci_channel_offline(phba->pcidev)))
8374 return IOCB_ERROR;
8375
James Smarta257bf92009-04-06 18:48:10 -04008376 /* If HBA has a deferred error attention, fail the iocb. */
8377 if (unlikely(phba->hba_flag & DEFER_ERATT))
8378 return IOCB_ERROR;
8379
dea31012005-04-17 16:05:31 -05008380 /*
8381 * We should never get an IOCB if we are in a < LINK_DOWN state
8382 */
James Smart2e0fef82007-06-17 19:56:36 -05008383 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008384 return IOCB_ERROR;
8385
8386 /*
8387 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008388 * outstanding event.
dea31012005-04-17 16:05:31 -05008389 */
James Smart0b727fe2007-10-27 13:37:25 -04008390 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008391 goto iocb_busy;
8392
James Smart2e0fef82007-06-17 19:56:36 -05008393 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008394 /*
James Smart2680eea2007-04-25 09:52:55 -04008395 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008396 * can be issued if the link is not up.
8397 */
8398 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008399 case CMD_GEN_REQUEST64_CR:
8400 case CMD_GEN_REQUEST64_CX:
8401 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8402 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008403 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008404 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8405 MENLO_TRANSPORT_TYPE))
8406
8407 goto iocb_busy;
8408 break;
dea31012005-04-17 16:05:31 -05008409 case CMD_QUE_RING_BUF_CN:
8410 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008411 /*
8412 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8413 * completion, iocb_cmpl MUST be 0.
8414 */
8415 if (piocb->iocb_cmpl)
8416 piocb->iocb_cmpl = NULL;
8417 /*FALLTHROUGH*/
8418 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008419 case CMD_CLOSE_XRI_CN:
8420 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008421 break;
8422 default:
8423 goto iocb_busy;
8424 }
8425
8426 /*
8427 * For FCP commands, we must be in a state where we can process link
8428 * attention events.
8429 */
James Smart895427b2017-02-12 13:52:30 -08008430 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008431 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008432 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008433 }
dea31012005-04-17 16:05:31 -05008434
dea31012005-04-17 16:05:31 -05008435 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8436 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8437 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8438
8439 if (iocb)
8440 lpfc_sli_update_ring(phba, pring);
8441 else
8442 lpfc_sli_update_full_ring(phba, pring);
8443
8444 if (!piocb)
8445 return IOCB_SUCCESS;
8446
8447 goto out_busy;
8448
8449 iocb_busy:
8450 pring->stats.iocb_cmd_delay++;
8451
8452 out_busy:
8453
8454 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008455 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008456 return IOCB_SUCCESS;
8457 }
8458
8459 return IOCB_BUSY;
8460}
8461
James Smart3772a992009-05-22 14:50:54 -04008462/**
James Smart4f774512009-05-22 14:52:35 -04008463 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8464 * @phba: Pointer to HBA context object.
8465 * @piocb: Pointer to command iocb.
8466 * @sglq: Pointer to the scatter gather queue object.
8467 *
8468 * This routine converts the bpl or bde that is in the IOCB
8469 * to a sgl list for the sli4 hardware. The physical address
8470 * of the bpl/bde is converted back to a virtual address.
8471 * If the IOCB contains a BPL then the list of BDE's is
8472 * converted to sli4_sge's. If the IOCB contains a single
8473 * BDE then it is converted to a single sli_sge.
8474 * The IOCB is still in cpu endianess so the contents of
8475 * the bpl can be used without byte swapping.
8476 *
8477 * Returns valid XRI = Success, NO_XRI = Failure.
8478**/
8479static uint16_t
8480lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8481 struct lpfc_sglq *sglq)
8482{
8483 uint16_t xritag = NO_XRI;
8484 struct ulp_bde64 *bpl = NULL;
8485 struct ulp_bde64 bde;
8486 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008487 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008488 IOCB_t *icmd;
8489 int numBdes = 0;
8490 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008491 uint32_t offset = 0; /* accumulated offset in the sg request list */
8492 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008493
8494 if (!piocbq || !sglq)
8495 return xritag;
8496
8497 sgl = (struct sli4_sge *)sglq->sgl;
8498 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008499 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8500 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008501 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8502 numBdes = icmd->un.genreq64.bdl.bdeSize /
8503 sizeof(struct ulp_bde64);
8504 /* The addrHigh and addrLow fields within the IOCB
8505 * have not been byteswapped yet so there is no
8506 * need to swap them back.
8507 */
James Smart1b511972011-12-13 13:23:09 -05008508 if (piocbq->context3)
8509 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8510 else
8511 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008512
James Smart1b511972011-12-13 13:23:09 -05008513 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008514 if (!bpl)
8515 return xritag;
8516
8517 for (i = 0; i < numBdes; i++) {
8518 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008519 sgl->addr_hi = bpl->addrHigh;
8520 sgl->addr_lo = bpl->addrLow;
8521
James Smart05580562011-05-24 11:40:48 -04008522 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008523 if ((i+1) == numBdes)
8524 bf_set(lpfc_sli4_sge_last, sgl, 1);
8525 else
8526 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008527 /* swap the size field back to the cpu so we
8528 * can assign it to the sgl.
8529 */
8530 bde.tus.w = le32_to_cpu(bpl->tus.w);
8531 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008532 /* The offsets in the sgl need to be accumulated
8533 * separately for the request and reply lists.
8534 * The request is always first, the reply follows.
8535 */
8536 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8537 /* add up the reply sg entries */
8538 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8539 inbound++;
8540 /* first inbound? reset the offset */
8541 if (inbound == 1)
8542 offset = 0;
8543 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008544 bf_set(lpfc_sli4_sge_type, sgl,
8545 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008546 offset += bde.tus.f.bdeSize;
8547 }
James Smart546fc852011-03-11 16:06:29 -05008548 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008549 bpl++;
8550 sgl++;
8551 }
8552 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8553 /* The addrHigh and addrLow fields of the BDE have not
8554 * been byteswapped yet so they need to be swapped
8555 * before putting them in the sgl.
8556 */
8557 sgl->addr_hi =
8558 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8559 sgl->addr_lo =
8560 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008561 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008562 bf_set(lpfc_sli4_sge_last, sgl, 1);
8563 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008564 sgl->sge_len =
8565 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008566 }
8567 return sglq->sli4_xritag;
8568}
8569
8570/**
James Smart4f774512009-05-22 14:52:35 -04008571 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8572 * @phba: Pointer to HBA context object.
8573 * @piocb: Pointer to command iocb.
8574 * @wqe: Pointer to the work queue entry.
8575 *
8576 * This routine converts the iocb command to its Work Queue Entry
8577 * equivalent. The wqe pointer should not have any fields set when
8578 * this routine is called because it will memcpy over them.
8579 * This routine does not set the CQ_ID or the WQEC bits in the
8580 * wqe.
8581 *
8582 * Returns: 0 = Success, IOCB_ERROR = Failure.
8583 **/
8584static int
8585lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8586 union lpfc_wqe *wqe)
8587{
James Smart5ffc2662009-11-18 15:39:44 -05008588 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008589 uint8_t ct = 0;
8590 uint32_t fip;
8591 uint32_t abort_tag;
8592 uint8_t command_type = ELS_COMMAND_NON_FIP;
8593 uint8_t cmnd;
8594 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008595 uint16_t abrt_iotag;
8596 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008597 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008598 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008599 int numBdes, i;
8600 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008601 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008602 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008603 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008604
James Smart45ed1192009-10-02 15:17:02 -04008605 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008606 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008607 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008608 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008609 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008610 command_type = ELS_COMMAND_FIP;
8611 else
8612 command_type = ELS_COMMAND_NON_FIP;
8613
James Smartb5c53952016-03-31 14:12:30 -07008614 if (phba->fcp_embed_io)
8615 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008616 /* Some of the fields are in the right position already */
8617 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008618 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008619 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008620
8621 abort_tag = (uint32_t) iocbq->iotag;
8622 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008623 /* words0-2 bpl convert bde */
8624 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008625 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8626 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008627 bpl = (struct ulp_bde64 *)
8628 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8629 if (!bpl)
8630 return IOCB_ERROR;
8631
8632 /* Should already be byte swapped. */
8633 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8634 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8635 /* swap the size field back to the cpu so we
8636 * can assign it to the sgl.
8637 */
8638 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008639 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8640 total_len = 0;
8641 for (i = 0; i < numBdes; i++) {
8642 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8643 total_len += bde.tus.f.bdeSize;
8644 }
James Smart4f774512009-05-22 14:52:35 -04008645 } else
James Smart5ffc2662009-11-18 15:39:44 -05008646 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008647
8648 iocbq->iocb.ulpIoTag = iocbq->iotag;
8649 cmnd = iocbq->iocb.ulpCommand;
8650
8651 switch (iocbq->iocb.ulpCommand) {
8652 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008653 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8654 ndlp = iocbq->context_un.ndlp;
8655 else
8656 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008657 if (!iocbq->iocb.ulpLe) {
8658 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8659 "2007 Only Limited Edition cmd Format"
8660 " supported 0x%x\n",
8661 iocbq->iocb.ulpCommand);
8662 return IOCB_ERROR;
8663 }
James Smartff78d8f2011-12-13 13:21:35 -05008664
James Smart5ffc2662009-11-18 15:39:44 -05008665 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008666 /* Els_reguest64 has a TMO */
8667 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8668 iocbq->iocb.ulpTimeout);
8669 /* Need a VF for word 4 set the vf bit*/
8670 bf_set(els_req64_vf, &wqe->els_req, 0);
8671 /* And a VFID for word 12 */
8672 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008673 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008674 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8675 iocbq->iocb.ulpContext);
8676 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8677 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008678 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008679 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008680 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8681 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008682 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8683 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008684 if_type = bf_get(lpfc_sli_intf_if_type,
8685 &phba->sli4_hba.sli_intf);
8686 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008687 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008688 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008689 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008690 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008691 *pcmd == ELS_CMD_PLOGI)) {
8692 bf_set(els_req64_sp, &wqe->els_req, 1);
8693 bf_set(els_req64_sid, &wqe->els_req,
8694 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008695 if ((*pcmd == ELS_CMD_FLOGI) &&
8696 !(phba->fc_topology ==
8697 LPFC_TOPOLOGY_LOOP))
8698 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008699 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8700 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008701 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008702 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008703 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8704 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8705 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8706 }
James Smartc8685952009-11-18 15:39:16 -05008707 }
James Smart6d368e52011-05-24 11:44:12 -04008708 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8709 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008710 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8711 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8712 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8713 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8714 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8715 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008716 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008717 break;
James Smart5ffc2662009-11-18 15:39:44 -05008718 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008719 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8720 iocbq->iocb.un.ulpWord[3]);
8721 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008722 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008723 /* The entire sequence is transmitted for this IOCB */
8724 xmit_len = total_len;
8725 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008726 if (phba->link_flag & LS_LOOPBACK_MODE)
8727 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008728 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008729 /* word3 iocb=io_tag32 wqe=reserved */
8730 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008731 /* word4 relative_offset memcpy */
8732 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008733 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8734 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8735 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8736 LPFC_WQE_IOD_WRITE);
8737 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8738 LPFC_WQE_LENLOC_WORD12);
8739 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008740 wqe->xmit_sequence.xmit_len = xmit_len;
8741 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008742 break;
James Smart4f774512009-05-22 14:52:35 -04008743 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008744 /* word3 iocb=iotag32 wqe=seq_payload_len */
8745 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008746 /* word4 iocb=rsvd wqe=rsvd */
8747 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8748 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008749 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008750 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008751 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8752 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8753 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8754 LPFC_WQE_LENLOC_WORD3);
8755 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008756 break;
James Smart4f774512009-05-22 14:52:35 -04008757 case CMD_FCP_IWRITE64_CR:
8758 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008759 /* word3 iocb=iotag wqe=payload_offset_len */
8760 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008761 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8762 xmit_len + sizeof(struct fcp_rsp));
8763 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8764 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008765 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8766 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8767 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8768 iocbq->iocb.ulpFCP2Rcvy);
8769 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8770 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008771 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8772 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8773 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008774 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008775 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008776 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8777 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008778 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8779 if (iocbq->priority) {
8780 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8781 (iocbq->priority << 1));
8782 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008783 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8784 (phba->cfg_XLanePriority << 1));
8785 }
8786 }
James Smartb5c53952016-03-31 14:12:30 -07008787 /* Note, word 10 is already initialized to 0 */
8788
8789 if (phba->fcp_embed_io) {
8790 struct lpfc_scsi_buf *lpfc_cmd;
8791 struct sli4_sge *sgl;
8792 union lpfc_wqe128 *wqe128;
8793 struct fcp_cmnd *fcp_cmnd;
8794 uint32_t *ptr;
8795
8796 /* 128 byte wqe support here */
8797 wqe128 = (union lpfc_wqe128 *)wqe;
8798
8799 lpfc_cmd = iocbq->context1;
8800 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8801 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8802
8803 /* Word 0-2 - FCP_CMND */
8804 wqe128->generic.bde.tus.f.bdeFlags =
8805 BUFF_TYPE_BDE_IMMED;
8806 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8807 wqe128->generic.bde.addrHigh = 0;
8808 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8809
8810 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8811
8812 /* Word 22-29 FCP CMND Payload */
8813 ptr = &wqe128->words[22];
8814 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8815 }
James Smart7851fe22011-07-22 18:36:52 -04008816 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008817 case CMD_FCP_IREAD64_CR:
8818 /* word3 iocb=iotag wqe=payload_offset_len */
8819 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008820 bf_set(payload_offset_len, &wqe->fcp_iread,
8821 xmit_len + sizeof(struct fcp_rsp));
8822 bf_set(cmd_buff_len, &wqe->fcp_iread,
8823 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008824 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8825 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8826 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8827 iocbq->iocb.ulpFCP2Rcvy);
8828 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008829 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008830 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8831 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8832 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008833 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008834 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008835 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8836 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008837 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8838 if (iocbq->priority) {
8839 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8840 (iocbq->priority << 1));
8841 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008842 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8843 (phba->cfg_XLanePriority << 1));
8844 }
8845 }
James Smartb5c53952016-03-31 14:12:30 -07008846 /* Note, word 10 is already initialized to 0 */
8847
8848 if (phba->fcp_embed_io) {
8849 struct lpfc_scsi_buf *lpfc_cmd;
8850 struct sli4_sge *sgl;
8851 union lpfc_wqe128 *wqe128;
8852 struct fcp_cmnd *fcp_cmnd;
8853 uint32_t *ptr;
8854
8855 /* 128 byte wqe support here */
8856 wqe128 = (union lpfc_wqe128 *)wqe;
8857
8858 lpfc_cmd = iocbq->context1;
8859 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8860 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8861
8862 /* Word 0-2 - FCP_CMND */
8863 wqe128->generic.bde.tus.f.bdeFlags =
8864 BUFF_TYPE_BDE_IMMED;
8865 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8866 wqe128->generic.bde.addrHigh = 0;
8867 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8868
8869 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8870
8871 /* Word 22-29 FCP CMND Payload */
8872 ptr = &wqe128->words[22];
8873 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8874 }
James Smart7851fe22011-07-22 18:36:52 -04008875 break;
James Smartf1126682009-06-10 17:22:44 -04008876 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008877 /* word3 iocb=iotag wqe=payload_offset_len */
8878 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8879 bf_set(payload_offset_len, &wqe->fcp_icmd,
8880 xmit_len + sizeof(struct fcp_rsp));
8881 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8882 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008883 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008884 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008885 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008886 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8887 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8888 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8889 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8890 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008891 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8892 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008893 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8894 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008895 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8896 if (iocbq->priority) {
8897 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8898 (iocbq->priority << 1));
8899 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008900 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8901 (phba->cfg_XLanePriority << 1));
8902 }
8903 }
James Smartb5c53952016-03-31 14:12:30 -07008904 /* Note, word 10 is already initialized to 0 */
8905
8906 if (phba->fcp_embed_io) {
8907 struct lpfc_scsi_buf *lpfc_cmd;
8908 struct sli4_sge *sgl;
8909 union lpfc_wqe128 *wqe128;
8910 struct fcp_cmnd *fcp_cmnd;
8911 uint32_t *ptr;
8912
8913 /* 128 byte wqe support here */
8914 wqe128 = (union lpfc_wqe128 *)wqe;
8915
8916 lpfc_cmd = iocbq->context1;
8917 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8918 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8919
8920 /* Word 0-2 - FCP_CMND */
8921 wqe128->generic.bde.tus.f.bdeFlags =
8922 BUFF_TYPE_BDE_IMMED;
8923 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8924 wqe128->generic.bde.addrHigh = 0;
8925 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8926
8927 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8928
8929 /* Word 22-29 FCP CMND Payload */
8930 ptr = &wqe128->words[22];
8931 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8932 }
James Smart7851fe22011-07-22 18:36:52 -04008933 break;
James Smart4f774512009-05-22 14:52:35 -04008934 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008935 /* For this command calculate the xmit length of the
8936 * request bde.
8937 */
8938 xmit_len = 0;
8939 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8940 sizeof(struct ulp_bde64);
8941 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008942 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008943 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8944 break;
James Smart63e801c2010-11-20 23:14:19 -05008945 xmit_len += bde.tus.f.bdeSize;
8946 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008947 /* word3 iocb=IO_TAG wqe=request_payload_len */
8948 wqe->gen_req.request_payload_len = xmit_len;
8949 /* word4 iocb=parameter wqe=relative_offset memcpy */
8950 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008951 /* word6 context tag copied in memcpy */
8952 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8953 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8954 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8955 "2015 Invalid CT %x command 0x%x\n",
8956 ct, iocbq->iocb.ulpCommand);
8957 return IOCB_ERROR;
8958 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008959 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8960 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8961 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8962 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8963 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8964 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8965 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8966 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008967 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008968 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008969 break;
James Smart4f774512009-05-22 14:52:35 -04008970 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008971 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008972 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008973 /* word3 iocb=iotag32 wqe=response_payload_len */
8974 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008975 /* word4 */
8976 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008977 /* word5 iocb=rsvd wge=did */
8978 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008979 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8980
8981 if_type = bf_get(lpfc_sli_intf_if_type,
8982 &phba->sli4_hba.sli_intf);
8983 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8984 if (iocbq->vport->fc_flag & FC_PT2PT) {
8985 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8986 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8987 iocbq->vport->fc_myDID);
8988 if (iocbq->vport->fc_myDID == Fabric_DID) {
8989 bf_set(wqe_els_did,
8990 &wqe->xmit_els_rsp.wqe_dest, 0);
8991 }
8992 }
8993 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008994 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8995 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8996 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8997 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008998 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008999 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009000 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009001 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009002 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9003 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9004 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9005 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9006 LPFC_WQE_LENLOC_WORD3);
9007 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009008 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9009 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009010 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9011 iocbq->context2)->virt);
9012 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009013 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9014 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009015 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009016 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9017 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009018 phba->vpi_ids[phba->pport->vpi]);
9019 }
James Smart4f774512009-05-22 14:52:35 -04009020 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009021 break;
James Smart4f774512009-05-22 14:52:35 -04009022 case CMD_CLOSE_XRI_CN:
9023 case CMD_ABORT_XRI_CN:
9024 case CMD_ABORT_XRI_CX:
9025 /* words 0-2 memcpy should be 0 rserved */
9026 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009027 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9028 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9029 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9030 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9031 } else
9032 fip = 0;
9033
9034 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009035 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009036 * The link is down, or the command was ELS_FIP
9037 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009038 * on the wire.
9039 */
9040 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9041 else
9042 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9043 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009044 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9045 wqe->abort_cmd.rsrvd5 = 0;
9046 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009047 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9048 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009049 /*
9050 * The abort handler will send us CMD_ABORT_XRI_CN or
9051 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9052 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009053 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9054 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9055 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9056 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009057 cmnd = CMD_ABORT_XRI_CX;
9058 command_type = OTHER_COMMAND;
9059 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009060 break;
James Smart6669f9b2009-10-02 15:16:45 -04009061 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009062 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009063 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009064 * we re-construct this WQE here based on information in
9065 * iocbq from scratch.
9066 */
9067 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009068 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009069 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009070 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9071 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009072 LPFC_ABTS_UNSOL_INT) {
9073 /* ABTS sent by initiator to CT exchange, the
9074 * RX_ID field will be filled with the newly
9075 * allocated responder XRI.
9076 */
9077 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9078 iocbq->sli4_xritag);
9079 } else {
9080 /* ABTS sent by responder to CT exchange, the
9081 * RX_ID field will be filled with the responder
9082 * RX_ID from ABTS.
9083 */
9084 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009085 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009086 }
James Smart6669f9b2009-10-02 15:16:45 -04009087 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9088 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009089
9090 /* Use CT=VPI */
9091 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9092 ndlp->nlp_DID);
9093 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9094 iocbq->iocb.ulpContext);
9095 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009096 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009097 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009098 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9099 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9100 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009101 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9102 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009103 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9104 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9105 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9106 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9107 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9108 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9109 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9110 }
9111
James Smart7851fe22011-07-22 18:36:52 -04009112 break;
James Smart4f774512009-05-22 14:52:35 -04009113 case CMD_XRI_ABORTED_CX:
9114 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009115 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9116 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9117 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9118 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9119 default:
9120 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9121 "2014 Invalid command 0x%x\n",
9122 iocbq->iocb.ulpCommand);
9123 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009124 break;
James Smart4f774512009-05-22 14:52:35 -04009125 }
James Smart6d368e52011-05-24 11:44:12 -04009126
James Smart8012cc32012-10-31 14:44:49 -04009127 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9128 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9129 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9130 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9131 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9132 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9133 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9134 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009135 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9136 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9137 wqe->generic.wqe_com.abort_tag = abort_tag;
9138 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9139 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9140 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9141 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009142 return 0;
9143}
9144
9145/**
9146 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9147 * @phba: Pointer to HBA context object.
9148 * @ring_number: SLI ring number to issue iocb on.
9149 * @piocb: Pointer to command iocb.
9150 * @flag: Flag indicating if this command can be put into txq.
9151 *
9152 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9153 * an iocb command to an HBA with SLI-4 interface spec.
9154 *
9155 * This function is called with hbalock held. The function will return success
9156 * after it successfully submit the iocb to firmware or after adding to the
9157 * txq.
9158 **/
9159static int
9160__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9161 struct lpfc_iocbq *piocb, uint32_t flag)
9162{
9163 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009164 union lpfc_wqe *wqe;
9165 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009166 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009167 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009168
James Smart895427b2017-02-12 13:52:30 -08009169 /* Get the WQ */
9170 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9171 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9172 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9173 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9174 else
9175 wq = phba->sli4_hba.oas_wq;
9176 } else {
9177 wq = phba->sli4_hba.els_wq;
9178 }
9179
9180 /* Get corresponding ring */
9181 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009182
James Smartb5c53952016-03-31 14:12:30 -07009183 /*
9184 * The WQE can be either 64 or 128 bytes,
9185 * so allocate space on the stack assuming the largest.
9186 */
9187 wqe = (union lpfc_wqe *)&wqe128;
9188
James Smart895427b2017-02-12 13:52:30 -08009189 lockdep_assert_held(&phba->hbalock);
9190
James Smart4f774512009-05-22 14:52:35 -04009191 if (piocb->sli4_xritag == NO_XRI) {
9192 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009193 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009194 sglq = NULL;
9195 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009196 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009197 if (!(flag & SLI_IOCB_RET_IOCB)) {
9198 __lpfc_sli_ringtx_put(phba,
9199 pring, piocb);
9200 return IOCB_SUCCESS;
9201 } else {
9202 return IOCB_BUSY;
9203 }
9204 } else {
James Smart895427b2017-02-12 13:52:30 -08009205 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009206 if (!sglq) {
9207 if (!(flag & SLI_IOCB_RET_IOCB)) {
9208 __lpfc_sli_ringtx_put(phba,
9209 pring,
9210 piocb);
9211 return IOCB_SUCCESS;
9212 } else
9213 return IOCB_BUSY;
9214 }
9215 }
James Smart4f774512009-05-22 14:52:35 -04009216 }
James Smart2ea259e2017-02-12 13:52:27 -08009217 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009218 /* These IO's already have an XRI and a mapped sgl. */
9219 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009220 else {
James Smart6d368e52011-05-24 11:44:12 -04009221 /*
9222 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009223 * sglq is on the active list
9224 */
James Smartedccdc12013-01-03 15:43:45 -05009225 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009226 if (!sglq)
9227 return IOCB_ERROR;
9228 }
9229
9230 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009231 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009232 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009233 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009234 return IOCB_ERROR;
9235 }
9236
James Smartb5c53952016-03-31 14:12:30 -07009237 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009238 return IOCB_ERROR;
9239
James Smart895427b2017-02-12 13:52:30 -08009240 if (lpfc_sli4_wq_put(wq, wqe))
9241 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009242 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9243
9244 return 0;
9245}
9246
9247/**
James Smart3772a992009-05-22 14:50:54 -04009248 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9249 *
9250 * This routine wraps the actual lockless version for issusing IOCB function
9251 * pointer from the lpfc_hba struct.
9252 *
9253 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009254 * IOCB_ERROR - Error
9255 * IOCB_SUCCESS - Success
9256 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009257 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009258int
James Smart3772a992009-05-22 14:50:54 -04009259__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9260 struct lpfc_iocbq *piocb, uint32_t flag)
9261{
9262 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9263}
9264
9265/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009266 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009267 * @phba: The hba struct for which this call is being executed.
9268 * @dev_grp: The HBA PCI-Device group number.
9269 *
9270 * This routine sets up the SLI interface API function jump table in @phba
9271 * struct.
9272 * Returns: 0 - success, -ENODEV - failure.
9273 **/
9274int
9275lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9276{
9277
9278 switch (dev_grp) {
9279 case LPFC_PCI_DEV_LP:
9280 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9281 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9282 break;
James Smart4f774512009-05-22 14:52:35 -04009283 case LPFC_PCI_DEV_OC:
9284 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9285 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9286 break;
James Smart3772a992009-05-22 14:50:54 -04009287 default:
9288 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9289 "1419 Invalid HBA PCI-device group: 0x%x\n",
9290 dev_grp);
9291 return -ENODEV;
9292 break;
9293 }
9294 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9295 return 0;
9296}
James Smart92d7f7b2007-06-17 19:56:38 -05009297
James Smarta1efe162015-05-21 13:55:20 -04009298/**
James Smart895427b2017-02-12 13:52:30 -08009299 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009300 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009301 * @piocb: Pointer to command iocb.
9302 *
James Smart895427b2017-02-12 13:52:30 -08009303 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9304 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009305 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009306 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009307 */
James Smart895427b2017-02-12 13:52:30 -08009308struct lpfc_sli_ring *
9309lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009310{
James Smart895427b2017-02-12 13:52:30 -08009311 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009312 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009313 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009314 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009315 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009316 /*
James Smart895427b2017-02-12 13:52:30 -08009317 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009318 * be setup based on what work queue we used.
9319 */
9320 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
James Smart895427b2017-02-12 13:52:30 -08009321 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009322 lpfc_sli4_scmd_to_wqidx_distr(phba,
9323 piocb->context1);
James Smart895427b2017-02-12 13:52:30 -08009324 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009325 } else {
9326 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009327 return NULL;
9328 piocb->hba_wqidx = 0;
9329 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009330 }
James Smart895427b2017-02-12 13:52:30 -08009331 } else {
9332 if (unlikely(!phba->sli4_hba.els_wq))
9333 return NULL;
9334 piocb->hba_wqidx = 0;
9335 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009336 }
James Smart9bd2bff52014-09-03 12:57:30 -04009337}
9338
James Smarte59058c2008-08-24 21:49:00 -04009339/**
James Smart3621a712009-04-06 18:47:14 -04009340 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009341 * @phba: Pointer to HBA context object.
9342 * @pring: Pointer to driver SLI ring object.
9343 * @piocb: Pointer to command iocb.
9344 * @flag: Flag indicating if this command can be put into txq.
9345 *
9346 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9347 * function. This function gets the hbalock and calls
9348 * __lpfc_sli_issue_iocb function and will return the error returned
9349 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9350 * functions which do not hold hbalock.
9351 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009352int
James Smart3772a992009-05-22 14:50:54 -04009353lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009354 struct lpfc_iocbq *piocb, uint32_t flag)
9355{
James Smart895427b2017-02-12 13:52:30 -08009356 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009357 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009358 struct lpfc_queue *fpeq;
9359 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009360 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009361 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009362
James Smart7e56aa22012-08-03 12:35:34 -04009363 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009364 pring = lpfc_sli4_calc_ring(phba, piocb);
9365 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009366 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009367
James Smart9bd2bff52014-09-03 12:57:30 -04009368 spin_lock_irqsave(&pring->ring_lock, iflags);
9369 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9370 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009371
James Smart9bd2bff52014-09-03 12:57:30 -04009372 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009373 idx = piocb->hba_wqidx;
9374 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009375
James Smart895427b2017-02-12 13:52:30 -08009376 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009377
James Smart9bd2bff52014-09-03 12:57:30 -04009378 /* Get associated EQ with this index */
9379 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009380
James Smart9bd2bff52014-09-03 12:57:30 -04009381 /* Turn off interrupts from this EQ */
9382 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009383
James Smart9bd2bff52014-09-03 12:57:30 -04009384 /*
9385 * Process all the events on FCP EQ
9386 */
9387 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9388 lpfc_sli4_hba_handle_eqe(phba,
9389 eqe, idx);
9390 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009391 }
James Smartba20c852012-08-03 12:36:52 -04009392
James Smart9bd2bff52014-09-03 12:57:30 -04009393 /* Always clear and re-arm the EQ */
9394 lpfc_sli4_eq_release(fpeq,
9395 LPFC_QUEUE_REARM);
9396 }
James Smart895427b2017-02-12 13:52:30 -08009397 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009398 }
James Smart7e56aa22012-08-03 12:35:34 -04009399 } else {
9400 /* For now, SLI2/3 will still use hbalock */
9401 spin_lock_irqsave(&phba->hbalock, iflags);
9402 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9403 spin_unlock_irqrestore(&phba->hbalock, iflags);
9404 }
James Smart92d7f7b2007-06-17 19:56:38 -05009405 return rc;
9406}
9407
James Smarte59058c2008-08-24 21:49:00 -04009408/**
James Smart3621a712009-04-06 18:47:14 -04009409 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009410 * @phba: Pointer to HBA context object.
9411 *
9412 * This function is called while driver attaches with the
9413 * HBA to setup the extra ring. The extra ring is used
9414 * only when driver needs to support target mode functionality
9415 * or IP over FC functionalities.
9416 *
James Smart895427b2017-02-12 13:52:30 -08009417 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009418 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009419static int
9420lpfc_extra_ring_setup( struct lpfc_hba *phba)
9421{
9422 struct lpfc_sli *psli;
9423 struct lpfc_sli_ring *pring;
9424
9425 psli = &phba->sli;
9426
9427 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009428
9429 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009430 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009431 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9432 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9433 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9434 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009435
James Smarta4bc3372006-12-02 13:34:16 -05009436 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009437 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009438
James Smart7e56aa22012-08-03 12:35:34 -04009439 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9440 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9441 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9442 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009443
9444 /* Setup default profile for this ring */
9445 pring->iotag_max = 4096;
9446 pring->num_mask = 1;
9447 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009448 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9449 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009450 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9451 return 0;
9452}
9453
James Smartcb69f7d2011-12-13 13:21:57 -05009454/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9455 * @phba: Pointer to HBA context object.
9456 * @iocbq: Pointer to iocb object.
9457 *
9458 * The async_event handler calls this routine when it receives
9459 * an ASYNC_STATUS_CN event from the port. The port generates
9460 * this event when an Abort Sequence request to an rport fails
9461 * twice in succession. The abort could be originated by the
9462 * driver or by the port. The ABTS could have been for an ELS
9463 * or FCP IO. The port only generates this event when an ABTS
9464 * fails to complete after one retry.
9465 */
9466static void
9467lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9468 struct lpfc_iocbq *iocbq)
9469{
9470 struct lpfc_nodelist *ndlp = NULL;
9471 uint16_t rpi = 0, vpi = 0;
9472 struct lpfc_vport *vport = NULL;
9473
9474 /* The rpi in the ulpContext is vport-sensitive. */
9475 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9476 rpi = iocbq->iocb.ulpContext;
9477
9478 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9479 "3092 Port generated ABTS async event "
9480 "on vpi %d rpi %d status 0x%x\n",
9481 vpi, rpi, iocbq->iocb.ulpStatus);
9482
9483 vport = lpfc_find_vport_by_vpid(phba, vpi);
9484 if (!vport)
9485 goto err_exit;
9486 ndlp = lpfc_findnode_rpi(vport, rpi);
9487 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9488 goto err_exit;
9489
9490 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9491 lpfc_sli_abts_recover_port(vport, ndlp);
9492 return;
9493
9494 err_exit:
9495 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9496 "3095 Event Context not found, no "
9497 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9498 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9499 vpi, rpi);
9500}
9501
9502/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9503 * @phba: pointer to HBA context object.
9504 * @ndlp: nodelist pointer for the impacted rport.
9505 * @axri: pointer to the wcqe containing the failed exchange.
9506 *
9507 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9508 * port. The port generates this event when an abort exchange request to an
9509 * rport fails twice in succession with no reply. The abort could be originated
9510 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9511 */
9512void
9513lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9514 struct lpfc_nodelist *ndlp,
9515 struct sli4_wcqe_xri_aborted *axri)
9516{
9517 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009518 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009519
James Smart6b5151f2012-01-18 16:24:06 -05009520 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009521 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9522 "3115 Node Context not found, driver "
9523 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009524 return;
9525 }
9526
James Smartcb69f7d2011-12-13 13:21:57 -05009527 vport = ndlp->vport;
9528 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9529 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009530 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009531 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009532 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009533 bf_get(lpfc_wcqe_xa_status, axri),
9534 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009535
James Smart5c1db2a2012-03-01 22:34:36 -05009536 /*
9537 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9538 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9539 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9540 */
James Smarte3d2b802012-08-14 14:25:43 -04009541 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009542 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9543 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009544 lpfc_sli_abts_recover_port(vport, ndlp);
9545}
9546
James Smarte59058c2008-08-24 21:49:00 -04009547/**
James Smart3621a712009-04-06 18:47:14 -04009548 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009549 * @phba: Pointer to HBA context object.
9550 * @pring: Pointer to driver SLI ring object.
9551 * @iocbq: Pointer to iocb object.
9552 *
9553 * This function is called by the slow ring event handler
9554 * function when there is an ASYNC event iocb in the ring.
9555 * This function is called with no lock held.
9556 * Currently this function handles only temperature related
9557 * ASYNC events. The function decodes the temperature sensor
9558 * event message and posts events for the management applications.
9559 **/
James Smart98c9ea52007-10-27 13:37:33 -04009560static void
James Smart57127f12007-10-27 13:37:05 -04009561lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9562 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9563{
9564 IOCB_t *icmd;
9565 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009566 struct temp_event temp_event_data;
9567 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009568 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009569
9570 icmd = &iocbq->iocb;
9571 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009572
James Smartcb69f7d2011-12-13 13:21:57 -05009573 switch (evt_code) {
9574 case ASYNC_TEMP_WARN:
9575 case ASYNC_TEMP_SAFE:
9576 temp_event_data.data = (uint32_t) icmd->ulpContext;
9577 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9578 if (evt_code == ASYNC_TEMP_WARN) {
9579 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9580 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9581 "0347 Adapter is very hot, please take "
9582 "corrective action. temperature : %d Celsius\n",
9583 (uint32_t) icmd->ulpContext);
9584 } else {
9585 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9586 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9587 "0340 Adapter temperature is OK now. "
9588 "temperature : %d Celsius\n",
9589 (uint32_t) icmd->ulpContext);
9590 }
9591
9592 /* Send temperature change event to applications */
9593 shost = lpfc_shost_from_vport(phba->pport);
9594 fc_host_post_vendor_event(shost, fc_get_event_number(),
9595 sizeof(temp_event_data), (char *) &temp_event_data,
9596 LPFC_NL_VENDOR_ID);
9597 break;
9598 case ASYNC_STATUS_CN:
9599 lpfc_sli_abts_err_handler(phba, iocbq);
9600 break;
9601 default:
James Smarta257bf92009-04-06 18:48:10 -04009602 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009604 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009605 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009606 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9607 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9608 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9609 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009610 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009611 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9612 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9613 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9614 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9615
James Smartcb69f7d2011-12-13 13:21:57 -05009616 break;
James Smart57127f12007-10-27 13:37:05 -04009617 }
James Smart57127f12007-10-27 13:37:05 -04009618}
9619
9620
James Smarte59058c2008-08-24 21:49:00 -04009621/**
James Smart895427b2017-02-12 13:52:30 -08009622 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009623 * @phba: Pointer to HBA context object.
9624 *
9625 * lpfc_sli_setup sets up rings of the SLI interface with
9626 * number of iocbs per ring and iotags. This function is
9627 * called while driver attach to the HBA and before the
9628 * interrupts are enabled. So there is no need for locking.
9629 *
9630 * This function always returns 0.
9631 **/
dea31012005-04-17 16:05:31 -05009632int
James Smart895427b2017-02-12 13:52:30 -08009633lpfc_sli4_setup(struct lpfc_hba *phba)
9634{
9635 struct lpfc_sli_ring *pring;
9636
9637 pring = phba->sli4_hba.els_wq->pring;
9638 pring->num_mask = LPFC_MAX_RING_MASK;
9639 pring->prt[0].profile = 0; /* Mask 0 */
9640 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9641 pring->prt[0].type = FC_TYPE_ELS;
9642 pring->prt[0].lpfc_sli_rcv_unsol_event =
9643 lpfc_els_unsol_event;
9644 pring->prt[1].profile = 0; /* Mask 1 */
9645 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9646 pring->prt[1].type = FC_TYPE_ELS;
9647 pring->prt[1].lpfc_sli_rcv_unsol_event =
9648 lpfc_els_unsol_event;
9649 pring->prt[2].profile = 0; /* Mask 2 */
9650 /* NameServer Inquiry */
9651 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9652 /* NameServer */
9653 pring->prt[2].type = FC_TYPE_CT;
9654 pring->prt[2].lpfc_sli_rcv_unsol_event =
9655 lpfc_ct_unsol_event;
9656 pring->prt[3].profile = 0; /* Mask 3 */
9657 /* NameServer response */
9658 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9659 /* NameServer */
9660 pring->prt[3].type = FC_TYPE_CT;
9661 pring->prt[3].lpfc_sli_rcv_unsol_event =
9662 lpfc_ct_unsol_event;
9663 return 0;
9664}
9665
9666/**
9667 * lpfc_sli_setup - SLI ring setup function
9668 * @phba: Pointer to HBA context object.
9669 *
9670 * lpfc_sli_setup sets up rings of the SLI interface with
9671 * number of iocbs per ring and iotags. This function is
9672 * called while driver attach to the HBA and before the
9673 * interrupts are enabled. So there is no need for locking.
9674 *
9675 * This function always returns 0. SLI3 only.
9676 **/
9677int
dea31012005-04-17 16:05:31 -05009678lpfc_sli_setup(struct lpfc_hba *phba)
9679{
James Smarted957682007-06-17 19:56:37 -05009680 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009681 struct lpfc_sli *psli = &phba->sli;
9682 struct lpfc_sli_ring *pring;
9683
James Smart2a76a282012-08-03 12:35:54 -04009684 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009685 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009686
James Bottomley604a3e32005-10-29 10:28:33 -05009687 psli->iocbq_lookup = NULL;
9688 psli->iocbq_lookup_len = 0;
9689 psli->last_iotag = 0;
9690
dea31012005-04-17 16:05:31 -05009691 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009692 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009693 switch (i) {
9694 case LPFC_FCP_RING: /* ring 0 - FCP */
9695 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009696 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9697 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9698 pring->sli.sli3.numCiocb +=
9699 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9700 pring->sli.sli3.numRiocb +=
9701 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9702 pring->sli.sli3.numCiocb +=
9703 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9704 pring->sli.sli3.numRiocb +=
9705 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9706 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009707 SLI3_IOCB_CMD_SIZE :
9708 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009709 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009710 SLI3_IOCB_RSP_SIZE :
9711 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009712 pring->iotag_ctr = 0;
9713 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009714 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009715 pring->fast_iotag = pring->iotag_max;
9716 pring->num_mask = 0;
9717 break;
James Smarta4bc3372006-12-02 13:34:16 -05009718 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009719 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009720 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9721 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9722 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009723 SLI3_IOCB_CMD_SIZE :
9724 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009725 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009726 SLI3_IOCB_RSP_SIZE :
9727 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009728 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009729 pring->num_mask = 0;
9730 break;
9731 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9732 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009733 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9734 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9735 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009736 SLI3_IOCB_CMD_SIZE :
9737 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009738 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009739 SLI3_IOCB_RSP_SIZE :
9740 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009741 pring->fast_iotag = 0;
9742 pring->iotag_ctr = 0;
9743 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009744 pring->lpfc_sli_rcv_async_status =
9745 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009746 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009747 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009748 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9749 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009750 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009751 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009752 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009753 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9754 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009755 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009756 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009757 pring->prt[2].profile = 0; /* Mask 2 */
9758 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009759 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009760 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009761 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009762 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009763 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009764 pring->prt[3].profile = 0; /* Mask 3 */
9765 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009766 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009767 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009768 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009769 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009770 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009771 break;
9772 }
James Smart7e56aa22012-08-03 12:35:34 -04009773 totiocbsize += (pring->sli.sli3.numCiocb *
9774 pring->sli.sli3.sizeCiocb) +
9775 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009776 }
James Smarted957682007-06-17 19:56:37 -05009777 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009778 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009779 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9780 "SLI2 SLIM Data: x%x x%lx\n",
9781 phba->brd_no, totiocbsize,
9782 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009783 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009784 if (phba->cfg_multi_ring_support == 2)
9785 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009786
9787 return 0;
9788}
9789
James Smarte59058c2008-08-24 21:49:00 -04009790/**
James Smart895427b2017-02-12 13:52:30 -08009791 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009792 * @phba: Pointer to HBA context object.
9793 *
James Smart895427b2017-02-12 13:52:30 -08009794 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009795 * ring. This function also initializes ring indices of each ring.
9796 * This function is called during the initialization of the SLI
9797 * interface of an HBA.
9798 * This function is called with no lock held and always returns
9799 * 1.
9800 **/
James Smart895427b2017-02-12 13:52:30 -08009801void
9802lpfc_sli4_queue_init(struct lpfc_hba *phba)
9803{
9804 struct lpfc_sli *psli;
9805 struct lpfc_sli_ring *pring;
9806 int i;
9807
9808 psli = &phba->sli;
9809 spin_lock_irq(&phba->hbalock);
9810 INIT_LIST_HEAD(&psli->mboxq);
9811 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9812 /* Initialize list headers for txq and txcmplq as double linked lists */
9813 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9814 pring = phba->sli4_hba.fcp_wq[i]->pring;
9815 pring->flag = 0;
9816 pring->ringno = LPFC_FCP_RING;
9817 INIT_LIST_HEAD(&pring->txq);
9818 INIT_LIST_HEAD(&pring->txcmplq);
9819 INIT_LIST_HEAD(&pring->iocb_continueq);
9820 spin_lock_init(&pring->ring_lock);
9821 }
9822 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9823 pring = phba->sli4_hba.nvme_wq[i]->pring;
9824 pring->flag = 0;
9825 pring->ringno = LPFC_FCP_RING;
9826 INIT_LIST_HEAD(&pring->txq);
9827 INIT_LIST_HEAD(&pring->txcmplq);
9828 INIT_LIST_HEAD(&pring->iocb_continueq);
9829 spin_lock_init(&pring->ring_lock);
9830 }
9831 pring = phba->sli4_hba.els_wq->pring;
9832 pring->flag = 0;
9833 pring->ringno = LPFC_ELS_RING;
9834 INIT_LIST_HEAD(&pring->txq);
9835 INIT_LIST_HEAD(&pring->txcmplq);
9836 INIT_LIST_HEAD(&pring->iocb_continueq);
9837 spin_lock_init(&pring->ring_lock);
9838
9839 if (phba->cfg_nvme_io_channel) {
9840 pring = phba->sli4_hba.nvmels_wq->pring;
9841 pring->flag = 0;
9842 pring->ringno = LPFC_ELS_RING;
9843 INIT_LIST_HEAD(&pring->txq);
9844 INIT_LIST_HEAD(&pring->txcmplq);
9845 INIT_LIST_HEAD(&pring->iocb_continueq);
9846 spin_lock_init(&pring->ring_lock);
9847 }
9848
9849 if (phba->cfg_fof) {
9850 pring = phba->sli4_hba.oas_wq->pring;
9851 pring->flag = 0;
9852 pring->ringno = LPFC_FCP_RING;
9853 INIT_LIST_HEAD(&pring->txq);
9854 INIT_LIST_HEAD(&pring->txcmplq);
9855 INIT_LIST_HEAD(&pring->iocb_continueq);
9856 spin_lock_init(&pring->ring_lock);
9857 }
9858
9859 spin_unlock_irq(&phba->hbalock);
9860}
9861
9862/**
9863 * lpfc_sli_queue_init - Queue initialization function
9864 * @phba: Pointer to HBA context object.
9865 *
9866 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9867 * ring. This function also initializes ring indices of each ring.
9868 * This function is called during the initialization of the SLI
9869 * interface of an HBA.
9870 * This function is called with no lock held and always returns
9871 * 1.
9872 **/
9873void
9874lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009875{
9876 struct lpfc_sli *psli;
9877 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009878 int i;
dea31012005-04-17 16:05:31 -05009879
9880 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009881 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009882 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009883 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009884 /* Initialize list headers for txq and txcmplq as double linked lists */
9885 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009886 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009887 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009888 pring->sli.sli3.next_cmdidx = 0;
9889 pring->sli.sli3.local_getidx = 0;
9890 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009891 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009892 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009893 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -08009894 pring->flag = 0;
9895 INIT_LIST_HEAD(&pring->txq);
9896 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -04009897 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009898 }
James Smart2e0fef82007-06-17 19:56:36 -05009899 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009900}
9901
James Smarte59058c2008-08-24 21:49:00 -04009902/**
James Smart04c68492009-05-22 14:52:52 -04009903 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9904 * @phba: Pointer to HBA context object.
9905 *
9906 * This routine flushes the mailbox command subsystem. It will unconditionally
9907 * flush all the mailbox commands in the three possible stages in the mailbox
9908 * command sub-system: pending mailbox command queue; the outstanding mailbox
9909 * command; and completed mailbox command queue. It is caller's responsibility
9910 * to make sure that the driver is in the proper state to flush the mailbox
9911 * command sub-system. Namely, the posting of mailbox commands into the
9912 * pending mailbox command queue from the various clients must be stopped;
9913 * either the HBA is in a state that it will never works on the outstanding
9914 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9915 * mailbox command has been completed.
9916 **/
9917static void
9918lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9919{
9920 LIST_HEAD(completions);
9921 struct lpfc_sli *psli = &phba->sli;
9922 LPFC_MBOXQ_t *pmb;
9923 unsigned long iflag;
9924
9925 /* Flush all the mailbox commands in the mbox system */
9926 spin_lock_irqsave(&phba->hbalock, iflag);
9927 /* The pending mailbox command queue */
9928 list_splice_init(&phba->sli.mboxq, &completions);
9929 /* The outstanding active mailbox command */
9930 if (psli->mbox_active) {
9931 list_add_tail(&psli->mbox_active->list, &completions);
9932 psli->mbox_active = NULL;
9933 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9934 }
9935 /* The completed mailbox command queue */
9936 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9937 spin_unlock_irqrestore(&phba->hbalock, iflag);
9938
9939 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9940 while (!list_empty(&completions)) {
9941 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9942 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9943 if (pmb->mbox_cmpl)
9944 pmb->mbox_cmpl(phba, pmb);
9945 }
9946}
9947
9948/**
James Smart3621a712009-04-06 18:47:14 -04009949 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009950 * @vport: Pointer to virtual port object.
9951 *
9952 * lpfc_sli_host_down is called to clean up the resources
9953 * associated with a vport before destroying virtual
9954 * port data structures.
9955 * This function does following operations:
9956 * - Free discovery resources associated with this virtual
9957 * port.
9958 * - Free iocbs associated with this virtual port in
9959 * the txq.
9960 * - Send abort for all iocb commands associated with this
9961 * vport in txcmplq.
9962 *
9963 * This function is called with no lock held and always returns 1.
9964 **/
dea31012005-04-17 16:05:31 -05009965int
James Smart92d7f7b2007-06-17 19:56:38 -05009966lpfc_sli_host_down(struct lpfc_vport *vport)
9967{
James Smart858c9f62007-06-17 19:56:39 -05009968 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009969 struct lpfc_hba *phba = vport->phba;
9970 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08009971 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -05009972 struct lpfc_sli_ring *pring;
9973 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009974 int i;
9975 unsigned long flags = 0;
9976 uint16_t prev_pring_flag;
9977
9978 lpfc_cleanup_discovery_resources(vport);
9979
9980 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009981
James Smart895427b2017-02-12 13:52:30 -08009982 /*
9983 * Error everything on the txq since these iocbs
9984 * have not been given to the FW yet.
9985 * Also issue ABTS for everything on the txcmplq
9986 */
9987 if (phba->sli_rev != LPFC_SLI_REV4) {
9988 for (i = 0; i < psli->num_rings; i++) {
9989 pring = &psli->sli3_ring[i];
9990 prev_pring_flag = pring->flag;
9991 /* Only slow rings */
9992 if (pring->ringno == LPFC_ELS_RING) {
9993 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9994 /* Set the lpfc data pending flag */
9995 set_bit(LPFC_DATA_READY, &phba->data_flags);
9996 }
9997 list_for_each_entry_safe(iocb, next_iocb,
9998 &pring->txq, list) {
9999 if (iocb->vport != vport)
10000 continue;
10001 list_move_tail(&iocb->list, &completions);
10002 }
10003 list_for_each_entry_safe(iocb, next_iocb,
10004 &pring->txcmplq, list) {
10005 if (iocb->vport != vport)
10006 continue;
10007 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10008 }
10009 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010010 }
James Smart895427b2017-02-12 13:52:30 -080010011 } else {
10012 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10013 pring = qp->pring;
10014 if (!pring)
10015 continue;
10016 if (pring == phba->sli4_hba.els_wq->pring) {
10017 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10018 /* Set the lpfc data pending flag */
10019 set_bit(LPFC_DATA_READY, &phba->data_flags);
10020 }
10021 prev_pring_flag = pring->flag;
10022 spin_lock_irq(&pring->ring_lock);
10023 list_for_each_entry_safe(iocb, next_iocb,
10024 &pring->txq, list) {
10025 if (iocb->vport != vport)
10026 continue;
10027 list_move_tail(&iocb->list, &completions);
10028 }
10029 spin_unlock_irq(&pring->ring_lock);
10030 list_for_each_entry_safe(iocb, next_iocb,
10031 &pring->txcmplq, list) {
10032 if (iocb->vport != vport)
10033 continue;
10034 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10035 }
10036 pring->flag = prev_pring_flag;
10037 }
James Smart92d7f7b2007-06-17 19:56:38 -050010038 }
James Smart92d7f7b2007-06-17 19:56:38 -050010039 spin_unlock_irqrestore(&phba->hbalock, flags);
10040
James Smarta257bf92009-04-06 18:48:10 -040010041 /* Cancel all the IOCBs from the completions list */
10042 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10043 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010044 return 1;
10045}
10046
James Smarte59058c2008-08-24 21:49:00 -040010047/**
James Smart3621a712009-04-06 18:47:14 -040010048 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010049 * @phba: Pointer to HBA context object.
10050 *
10051 * This function cleans up all iocb, buffers, mailbox commands
10052 * while shutting down the HBA. This function is called with no
10053 * lock held and always returns 1.
10054 * This function does the following to cleanup driver resources:
10055 * - Free discovery resources for each virtual port
10056 * - Cleanup any pending fabric iocbs
10057 * - Iterate through the iocb txq and free each entry
10058 * in the list.
10059 * - Free up any buffer posted to the HBA
10060 * - Free mailbox commands in the mailbox queue.
10061 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010062int
James Smart2e0fef82007-06-17 19:56:36 -050010063lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010064{
James Smart2534ba72007-04-25 09:52:20 -040010065 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010066 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010067 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010068 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010069 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010070 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010071 int i;
10072
10073 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010074 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010075
dea31012005-04-17 16:05:31 -050010076 lpfc_hba_down_prep(phba);
10077
James Smart92d7f7b2007-06-17 19:56:38 -050010078 lpfc_fabric_abort_hba(phba);
10079
James Smart2e0fef82007-06-17 19:56:36 -050010080 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010081
James Smart895427b2017-02-12 13:52:30 -080010082 /*
10083 * Error everything on the txq since these iocbs
10084 * have not been given to the FW yet.
10085 */
10086 if (phba->sli_rev != LPFC_SLI_REV4) {
10087 for (i = 0; i < psli->num_rings; i++) {
10088 pring = &psli->sli3_ring[i];
10089 /* Only slow rings */
10090 if (pring->ringno == LPFC_ELS_RING) {
10091 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10092 /* Set the lpfc data pending flag */
10093 set_bit(LPFC_DATA_READY, &phba->data_flags);
10094 }
10095 list_splice_init(&pring->txq, &completions);
10096 }
10097 } else {
10098 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10099 pring = qp->pring;
10100 if (!pring)
10101 continue;
10102 spin_lock_irq(&pring->ring_lock);
10103 list_splice_init(&pring->txq, &completions);
10104 spin_unlock_irq(&pring->ring_lock);
10105 if (pring == phba->sli4_hba.els_wq->pring) {
10106 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10107 /* Set the lpfc data pending flag */
10108 set_bit(LPFC_DATA_READY, &phba->data_flags);
10109 }
10110 }
dea31012005-04-17 16:05:31 -050010111 }
James Smart2e0fef82007-06-17 19:56:36 -050010112 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010113
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 Smart2534ba72007-04-25 09:52:20 -040010117
James Smart0ff10d42008-01-11 01:52:36 -050010118 spin_lock_irqsave(&phba->hbalock, flags);
10119 list_splice_init(&phba->elsbuf, &completions);
10120 phba->elsbuf_cnt = 0;
10121 phba->elsbuf_prev_cnt = 0;
10122 spin_unlock_irqrestore(&phba->hbalock, flags);
10123
10124 while (!list_empty(&completions)) {
10125 list_remove_head(&completions, buf_ptr,
10126 struct lpfc_dmabuf, list);
10127 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10128 kfree(buf_ptr);
10129 }
10130
dea31012005-04-17 16:05:31 -050010131 /* Return any active mbox cmds */
10132 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010133
James Smartda0436e2009-05-22 14:51:39 -040010134 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010135 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010136 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010137
James Smartda0436e2009-05-22 14:51:39 -040010138 return 1;
10139}
James Smart92d7f7b2007-06-17 19:56:38 -050010140
James Smartda0436e2009-05-22 14:51:39 -040010141/**
James Smart3621a712009-04-06 18:47:14 -040010142 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010143 * @srcp: Source memory pointer.
10144 * @destp: Destination memory pointer.
10145 * @cnt: Number of words required to be copied.
10146 *
10147 * This function is used for copying data between driver memory
10148 * and the SLI memory. This function also changes the endianness
10149 * of each word if native endianness is different from SLI
10150 * endianness. This function can be called with or without
10151 * lock.
10152 **/
dea31012005-04-17 16:05:31 -050010153void
10154lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10155{
10156 uint32_t *src = srcp;
10157 uint32_t *dest = destp;
10158 uint32_t ldata;
10159 int i;
10160
10161 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10162 ldata = *src;
10163 ldata = le32_to_cpu(ldata);
10164 *dest = ldata;
10165 src++;
10166 dest++;
10167 }
10168}
10169
James Smarte59058c2008-08-24 21:49:00 -040010170
10171/**
James Smarta0c87cb2009-07-19 10:01:10 -040010172 * lpfc_sli_bemem_bcopy - SLI memory copy function
10173 * @srcp: Source memory pointer.
10174 * @destp: Destination memory pointer.
10175 * @cnt: Number of words required to be copied.
10176 *
10177 * This function is used for copying data between a data structure
10178 * with big endian representation to local endianness.
10179 * This function can be called with or without lock.
10180 **/
10181void
10182lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10183{
10184 uint32_t *src = srcp;
10185 uint32_t *dest = destp;
10186 uint32_t ldata;
10187 int i;
10188
10189 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10190 ldata = *src;
10191 ldata = be32_to_cpu(ldata);
10192 *dest = ldata;
10193 src++;
10194 dest++;
10195 }
10196}
10197
10198/**
James Smart3621a712009-04-06 18:47:14 -040010199 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010200 * @phba: Pointer to HBA context object.
10201 * @pring: Pointer to driver SLI ring object.
10202 * @mp: Pointer to driver buffer object.
10203 *
10204 * This function is called with no lock held.
10205 * It always return zero after adding the buffer to the postbufq
10206 * buffer list.
10207 **/
dea31012005-04-17 16:05:31 -050010208int
James Smart2e0fef82007-06-17 19:56:36 -050010209lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10210 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010211{
10212 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10213 later */
James Smart2e0fef82007-06-17 19:56:36 -050010214 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010215 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010216 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010217 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010218 return 0;
10219}
10220
James Smarte59058c2008-08-24 21:49:00 -040010221/**
James Smart3621a712009-04-06 18:47:14 -040010222 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010223 * @phba: Pointer to HBA context object.
10224 *
10225 * When HBQ is enabled, buffers are searched based on tags. This function
10226 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10227 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10228 * does not conflict with tags of buffer posted for unsolicited events.
10229 * The function returns the allocated tag. The function is called with
10230 * no locks held.
10231 **/
James Smart76bb24e2007-10-27 13:38:00 -040010232uint32_t
10233lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10234{
10235 spin_lock_irq(&phba->hbalock);
10236 phba->buffer_tag_count++;
10237 /*
10238 * Always set the QUE_BUFTAG_BIT to distiguish between
10239 * a tag assigned by HBQ.
10240 */
10241 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10242 spin_unlock_irq(&phba->hbalock);
10243 return phba->buffer_tag_count;
10244}
10245
James Smarte59058c2008-08-24 21:49:00 -040010246/**
James Smart3621a712009-04-06 18:47:14 -040010247 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010248 * @phba: Pointer to HBA context object.
10249 * @pring: Pointer to driver SLI ring object.
10250 * @tag: Buffer tag.
10251 *
10252 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10253 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10254 * iocb is posted to the response ring with the tag of the buffer.
10255 * This function searches the pring->postbufq list using the tag
10256 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10257 * iocb. If the buffer is found then lpfc_dmabuf object of the
10258 * buffer is returned to the caller else NULL is returned.
10259 * This function is called with no lock held.
10260 **/
James Smart76bb24e2007-10-27 13:38:00 -040010261struct lpfc_dmabuf *
10262lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10263 uint32_t tag)
10264{
10265 struct lpfc_dmabuf *mp, *next_mp;
10266 struct list_head *slp = &pring->postbufq;
10267
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010268 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010269 spin_lock_irq(&phba->hbalock);
10270 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10271 if (mp->buffer_tag == tag) {
10272 list_del_init(&mp->list);
10273 pring->postbufq_cnt--;
10274 spin_unlock_irq(&phba->hbalock);
10275 return mp;
10276 }
10277 }
10278
10279 spin_unlock_irq(&phba->hbalock);
10280 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010281 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010282 "ring %d Data x%lx x%p x%p x%x\n",
10283 pring->ringno, (unsigned long) tag,
10284 slp->next, slp->prev, pring->postbufq_cnt);
10285
10286 return NULL;
10287}
dea31012005-04-17 16:05:31 -050010288
James Smarte59058c2008-08-24 21:49:00 -040010289/**
James Smart3621a712009-04-06 18:47:14 -040010290 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010291 * @phba: Pointer to HBA context object.
10292 * @pring: Pointer to driver SLI ring object.
10293 * @phys: DMA address of the buffer.
10294 *
10295 * This function searches the buffer list using the dma_address
10296 * of unsolicited event to find the driver's lpfc_dmabuf object
10297 * corresponding to the dma_address. The function returns the
10298 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10299 * This function is called by the ct and els unsolicited event
10300 * handlers to get the buffer associated with the unsolicited
10301 * event.
10302 *
10303 * This function is called with no lock held.
10304 **/
dea31012005-04-17 16:05:31 -050010305struct lpfc_dmabuf *
10306lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10307 dma_addr_t phys)
10308{
10309 struct lpfc_dmabuf *mp, *next_mp;
10310 struct list_head *slp = &pring->postbufq;
10311
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010312 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010313 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010314 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10315 if (mp->phys == phys) {
10316 list_del_init(&mp->list);
10317 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010318 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010319 return mp;
10320 }
10321 }
10322
James Smart2e0fef82007-06-17 19:56:36 -050010323 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010325 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010326 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010327 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010328 slp->next, slp->prev, pring->postbufq_cnt);
10329 return NULL;
10330}
10331
James Smarte59058c2008-08-24 21:49:00 -040010332/**
James Smart3621a712009-04-06 18:47:14 -040010333 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010334 * @phba: Pointer to HBA context object.
10335 * @cmdiocb: Pointer to driver command iocb object.
10336 * @rspiocb: Pointer to driver response iocb object.
10337 *
10338 * This function is the completion handler for the abort iocbs for
10339 * ELS commands. This function is called from the ELS ring event
10340 * handler with no lock held. This function frees memory resources
10341 * associated with the abort iocb.
10342 **/
dea31012005-04-17 16:05:31 -050010343static void
James Smart2e0fef82007-06-17 19:56:36 -050010344lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10345 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010346{
James Smart2e0fef82007-06-17 19:56:36 -050010347 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010348 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010349 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010350
10351 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010352
10353 /*
10354 * Assume that the port already completed and returned, or
10355 * will return the iocb. Just Log the message.
10356 */
James Smart2680eea2007-04-25 09:52:55 -040010357 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10358 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10359
James Smart2e0fef82007-06-17 19:56:36 -050010360 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010361 if (phba->sli_rev < LPFC_SLI_REV4) {
10362 if (abort_iotag != 0 &&
10363 abort_iotag <= phba->sli.last_iotag)
10364 abort_iocb =
10365 phba->sli.iocbq_lookup[abort_iotag];
10366 } else
10367 /* For sli4 the abort_tag is the XRI,
10368 * so the abort routine puts the iotag of the iocb
10369 * being aborted in the context field of the abort
10370 * IOCB.
10371 */
10372 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010373
James Smart2a9bf3d2010-06-07 15:24:45 -040010374 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10375 "0327 Cannot abort els iocb %p "
10376 "with tag %x context %x, abort status %x, "
10377 "abort code %x\n",
10378 abort_iocb, abort_iotag, abort_context,
10379 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010380
James Smartff78d8f2011-12-13 13:21:35 -050010381 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010382 }
James Bottomley604a3e32005-10-29 10:28:33 -050010383 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010384 return;
10385}
10386
James Smarte59058c2008-08-24 21:49:00 -040010387/**
James Smart3621a712009-04-06 18:47:14 -040010388 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010389 * @phba: Pointer to HBA context object.
10390 * @cmdiocb: Pointer to driver command iocb object.
10391 * @rspiocb: Pointer to driver response iocb object.
10392 *
10393 * The function is called from SLI ring event handler with no
10394 * lock held. This function is the completion handler for ELS commands
10395 * which are aborted. The function frees memory resources used for
10396 * the aborted ELS commands.
10397 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010398static void
10399lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10400 struct lpfc_iocbq *rspiocb)
10401{
10402 IOCB_t *irsp = &rspiocb->iocb;
10403
10404 /* ELS cmd tag <ulpIoTag> completes */
10405 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010406 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010407 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010408 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010409 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010410 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10411 lpfc_ct_free_iocb(phba, cmdiocb);
10412 else
10413 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010414 return;
10415}
10416
James Smarte59058c2008-08-24 21:49:00 -040010417/**
James Smart5af5eee2010-10-22 11:06:38 -040010418 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010419 * @phba: Pointer to HBA context object.
10420 * @pring: Pointer to driver SLI ring object.
10421 * @cmdiocb: Pointer to driver command iocb object.
10422 *
James Smart5af5eee2010-10-22 11:06:38 -040010423 * This function issues an abort iocb for the provided command iocb down to
10424 * the port. Other than the case the outstanding command iocb is an abort
10425 * request, this function issues abort out unconditionally. This function is
10426 * called with hbalock held. The function returns 0 when it fails due to
10427 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010428 **/
James Smart5af5eee2010-10-22 11:06:38 -040010429static int
10430lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010431 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010432{
James Smart2e0fef82007-06-17 19:56:36 -050010433 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010434 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010435 IOCB_t *icmd = NULL;
10436 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010437 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010438 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010439
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010440 lockdep_assert_held(&phba->hbalock);
10441
James Smart92d7f7b2007-06-17 19:56:38 -050010442 /*
10443 * There are certain command types we don't want to abort. And we
10444 * don't want to abort commands that are already in the process of
10445 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010446 */
10447 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010448 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010449 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10450 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010451 return 0;
10452
dea31012005-04-17 16:05:31 -050010453 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010454 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010455 if (abtsiocbp == NULL)
10456 return 0;
dea31012005-04-17 16:05:31 -050010457
James Smart07951072007-04-25 09:51:38 -040010458 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010459 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010460 */
10461 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10462
dea31012005-04-17 16:05:31 -050010463 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010464 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10465 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010466 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010467 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010468 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10469 }
James Smartda0436e2009-05-22 14:51:39 -040010470 else
10471 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010472 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010473 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010474
James Smart5ffc2662009-11-18 15:39:44 -050010475 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010476 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010477 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10478 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010479 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10480 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010481
James Smart2e0fef82007-06-17 19:56:36 -050010482 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010483 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10484 else
10485 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10486
10487 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010488 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010489
James Smarte8b62012007-08-02 11:10:09 -040010490 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10491 "0339 Abort xri x%x, original iotag x%x, "
10492 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010493 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010494 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010495 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010496
10497 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010498 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10499 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010500 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010501 /* Note: both hbalock and ring_lock need to be set here */
10502 spin_lock_irqsave(&pring->ring_lock, iflags);
10503 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10504 abtsiocbp, 0);
10505 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10506 } else {
10507 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10508 abtsiocbp, 0);
10509 }
James Smart07951072007-04-25 09:51:38 -040010510
James Smartd7c255b2008-08-24 21:50:00 -040010511 if (retval)
10512 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010513
10514 /*
10515 * Caller to this routine should check for IOCB_ERROR
10516 * and handle it properly. This routine no longer removes
10517 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10518 */
10519 return retval;
10520}
10521
10522/**
10523 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10524 * @phba: Pointer to HBA context object.
10525 * @pring: Pointer to driver SLI ring object.
10526 * @cmdiocb: Pointer to driver command iocb object.
10527 *
10528 * This function issues an abort iocb for the provided command iocb. In case
10529 * of unloading, the abort iocb will not be issued to commands on the ELS
10530 * ring. Instead, the callback function shall be changed to those commands
10531 * so that nothing happens when them finishes. This function is called with
10532 * hbalock held. The function returns 0 when the command iocb is an abort
10533 * request.
10534 **/
10535int
10536lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10537 struct lpfc_iocbq *cmdiocb)
10538{
10539 struct lpfc_vport *vport = cmdiocb->vport;
10540 int retval = IOCB_ERROR;
10541 IOCB_t *icmd = NULL;
10542
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010543 lockdep_assert_held(&phba->hbalock);
10544
James Smart5af5eee2010-10-22 11:06:38 -040010545 /*
10546 * There are certain command types we don't want to abort. And we
10547 * don't want to abort commands that are already in the process of
10548 * being aborted.
10549 */
10550 icmd = &cmdiocb->iocb;
10551 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10552 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10553 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10554 return 0;
10555
10556 /*
10557 * If we're unloading, don't abort iocb on the ELS ring, but change
10558 * the callback so that nothing happens when it finishes.
10559 */
10560 if ((vport->load_flag & FC_UNLOADING) &&
10561 (pring->ringno == LPFC_ELS_RING)) {
10562 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10563 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10564 else
10565 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10566 goto abort_iotag_exit;
10567 }
10568
10569 /* Now, we try to issue the abort to the cmdiocb out */
10570 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10571
James Smart07951072007-04-25 09:51:38 -040010572abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010573 /*
10574 * Caller to this routine should check for IOCB_ERROR
10575 * and handle it properly. This routine no longer removes
10576 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010577 */
James Smart2e0fef82007-06-17 19:56:36 -050010578 return retval;
dea31012005-04-17 16:05:31 -050010579}
10580
James Smarte59058c2008-08-24 21:49:00 -040010581/**
James Smart895427b2017-02-12 13:52:30 -080010582 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10583 * @phba: Pointer to HBA context object.
10584 * @pring: Pointer to driver SLI ring object.
10585 * @cmdiocb: Pointer to driver command iocb object.
10586 *
10587 * This function issues an abort iocb for the provided command iocb down to
10588 * the port. Other than the case the outstanding command iocb is an abort
10589 * request, this function issues abort out unconditionally. This function is
10590 * called with hbalock held. The function returns 0 when it fails due to
10591 * memory allocation failure or when the command iocb is an abort request.
10592 **/
10593static int
10594lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10595 struct lpfc_iocbq *cmdiocb)
10596{
10597 struct lpfc_vport *vport = cmdiocb->vport;
10598 struct lpfc_iocbq *abtsiocbp;
10599 union lpfc_wqe *abts_wqe;
10600 int retval;
10601
10602 /*
10603 * There are certain command types we don't want to abort. And we
10604 * don't want to abort commands that are already in the process of
10605 * being aborted.
10606 */
10607 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10608 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10609 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10610 return 0;
10611
10612 /* issue ABTS for this io based on iotag */
10613 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10614 if (abtsiocbp == NULL)
10615 return 0;
10616
10617 /* This signals the response to set the correct status
10618 * before calling the completion handler
10619 */
10620 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10621
10622 /* Complete prepping the abort wqe and issue to the FW. */
10623 abts_wqe = &abtsiocbp->wqe;
10624 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10625 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10626
10627 /* Explicitly set reserved fields to zero.*/
10628 abts_wqe->abort_cmd.rsrvd4 = 0;
10629 abts_wqe->abort_cmd.rsrvd5 = 0;
10630
10631 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10632 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10633 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10634
10635 /* word 7 */
10636 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10637 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10638 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10639 cmdiocb->iocb.ulpClass);
10640
10641 /* word 8 - tell the FW to abort the IO associated with this
10642 * outstanding exchange ID.
10643 */
10644 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10645
10646 /* word 9 - this is the iotag for the abts_wqe completion. */
10647 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10648 abtsiocbp->iotag);
10649
10650 /* word 10 */
10651 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10652 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10653 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10654
10655 /* word 11 */
10656 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10657 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10658 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10659
10660 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10661 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10662 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010663 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010664 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10665 if (retval == IOCB_ERROR) {
10666 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10667 "6147 Failed abts issue_wqe with status x%x "
10668 "for oxid x%x\n",
10669 retval, cmdiocb->sli4_xritag);
10670 lpfc_sli_release_iocbq(phba, abtsiocbp);
10671 return retval;
10672 }
10673
10674 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10675 "6148 Drv Abort NVME Request Issued for "
10676 "ox_id x%x on reqtag x%x\n",
10677 cmdiocb->sli4_xritag,
10678 abtsiocbp->iotag);
10679
10680 return retval;
10681}
10682
10683/**
James Smart5af5eee2010-10-22 11:06:38 -040010684 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10685 * @phba: pointer to lpfc HBA data structure.
10686 *
10687 * This routine will abort all pending and outstanding iocbs to an HBA.
10688 **/
10689void
10690lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10691{
10692 struct lpfc_sli *psli = &phba->sli;
10693 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010694 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010695 int i;
10696
James Smart895427b2017-02-12 13:52:30 -080010697 if (phba->sli_rev != LPFC_SLI_REV4) {
10698 for (i = 0; i < psli->num_rings; i++) {
10699 pring = &psli->sli3_ring[i];
10700 lpfc_sli_abort_iocb_ring(phba, pring);
10701 }
10702 return;
10703 }
10704 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10705 pring = qp->pring;
10706 if (!pring)
10707 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010708 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010709 }
10710}
10711
10712/**
James Smart3621a712009-04-06 18:47:14 -040010713 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010714 * @iocbq: Pointer to driver iocb object.
10715 * @vport: Pointer to driver virtual port object.
10716 * @tgt_id: SCSI ID of the target.
10717 * @lun_id: LUN ID of the scsi device.
10718 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10719 *
James Smart3621a712009-04-06 18:47:14 -040010720 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010721 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10722 * 0 if the filtering criteria is met for the given iocb and will return
10723 * 1 if the filtering criteria is not met.
10724 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10725 * given iocb is for the SCSI device specified by vport, tgt_id and
10726 * lun_id parameter.
10727 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10728 * given iocb is for the SCSI target specified by vport and tgt_id
10729 * parameters.
10730 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10731 * given iocb is for the SCSI host associated with the given vport.
10732 * This function is called with no locks held.
10733 **/
dea31012005-04-17 16:05:31 -050010734static int
James Smart51ef4c22007-08-02 11:10:31 -040010735lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10736 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010737 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010738{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010739 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010740 int rc = 1;
10741
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010742 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10743 return rc;
10744
James Smart51ef4c22007-08-02 11:10:31 -040010745 if (iocbq->vport != vport)
10746 return rc;
10747
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010748 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010749
James Smart495a7142008-06-14 22:52:59 -040010750 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010751 return rc;
10752
10753 switch (ctx_cmd) {
10754 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010755 if ((lpfc_cmd->rdata->pnode) &&
10756 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10757 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010758 rc = 0;
10759 break;
10760 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010761 if ((lpfc_cmd->rdata->pnode) &&
10762 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010763 rc = 0;
10764 break;
dea31012005-04-17 16:05:31 -050010765 case LPFC_CTX_HOST:
10766 rc = 0;
10767 break;
10768 default:
10769 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010770 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010771 break;
10772 }
10773
10774 return rc;
10775}
10776
James Smarte59058c2008-08-24 21:49:00 -040010777/**
James Smart3621a712009-04-06 18:47:14 -040010778 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010779 * @vport: Pointer to virtual port.
10780 * @tgt_id: SCSI ID of the target.
10781 * @lun_id: LUN ID of the scsi device.
10782 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10783 *
10784 * This function returns number of FCP commands pending for the vport.
10785 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10786 * commands pending on the vport associated with SCSI device specified
10787 * by tgt_id and lun_id parameters.
10788 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10789 * commands pending on the vport associated with SCSI target specified
10790 * by tgt_id parameter.
10791 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10792 * commands pending on the vport.
10793 * This function returns the number of iocbs which satisfy the filter.
10794 * This function is called without any lock held.
10795 **/
dea31012005-04-17 16:05:31 -050010796int
James Smart51ef4c22007-08-02 11:10:31 -040010797lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10798 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010799{
James Smart51ef4c22007-08-02 11:10:31 -040010800 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010801 struct lpfc_iocbq *iocbq;
10802 int sum, i;
dea31012005-04-17 16:05:31 -050010803
Johannes Thumshirn31979002016-07-18 16:06:03 +020010804 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010805 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10806 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010807
James Smart51ef4c22007-08-02 11:10:31 -040010808 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10809 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010810 sum++;
dea31012005-04-17 16:05:31 -050010811 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010812 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010813
dea31012005-04-17 16:05:31 -050010814 return sum;
10815}
10816
James Smarte59058c2008-08-24 21:49:00 -040010817/**
James Smart3621a712009-04-06 18:47:14 -040010818 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010819 * @phba: Pointer to HBA context object
10820 * @cmdiocb: Pointer to command iocb object.
10821 * @rspiocb: Pointer to response iocb object.
10822 *
10823 * This function is called when an aborted FCP iocb completes. This
10824 * function is called by the ring event handler with no lock held.
10825 * This function frees the iocb.
10826 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010827void
James Smart2e0fef82007-06-17 19:56:36 -050010828lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10829 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010830{
James Smartcb69f7d2011-12-13 13:21:57 -050010831 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010832 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010833 "original iotag x%x, abort cmd iotag x%x "
10834 "status 0x%x, reason 0x%x\n",
10835 cmdiocb->iocb.un.acxri.abortContextTag,
10836 cmdiocb->iocb.un.acxri.abortIoTag,
10837 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10838 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010839 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010840 return;
10841}
10842
James Smarte59058c2008-08-24 21:49:00 -040010843/**
James Smart3621a712009-04-06 18:47:14 -040010844 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010845 * @vport: Pointer to virtual port.
10846 * @pring: Pointer to driver SLI ring object.
10847 * @tgt_id: SCSI ID of the target.
10848 * @lun_id: LUN ID of the scsi device.
10849 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10850 *
10851 * This function sends an abort command for every SCSI command
10852 * associated with the given virtual port pending on the ring
10853 * filtered by lpfc_sli_validate_fcp_iocb function.
10854 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10855 * FCP iocbs associated with lun specified by tgt_id and lun_id
10856 * parameters
10857 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10858 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10859 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10860 * FCP iocbs associated with virtual port.
10861 * This function returns number of iocbs it failed to abort.
10862 * This function is called with no locks held.
10863 **/
dea31012005-04-17 16:05:31 -050010864int
James Smart51ef4c22007-08-02 11:10:31 -040010865lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10866 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010867{
James Smart51ef4c22007-08-02 11:10:31 -040010868 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010869 struct lpfc_iocbq *iocbq;
10870 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010871 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010872 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010873 int i;
dea31012005-04-17 16:05:31 -050010874
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010875 for (i = 1; i <= phba->sli.last_iotag; i++) {
10876 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010877
James Smart51ef4c22007-08-02 11:10:31 -040010878 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010879 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010880 continue;
10881
James Smartafbd8d82013-09-06 12:22:13 -040010882 /*
10883 * If the iocbq is already being aborted, don't take a second
10884 * action, but do count it.
10885 */
10886 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10887 continue;
10888
dea31012005-04-17 16:05:31 -050010889 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010890 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010891 if (abtsiocb == NULL) {
10892 errcnt++;
10893 continue;
10894 }
dea31012005-04-17 16:05:31 -050010895
James Smartafbd8d82013-09-06 12:22:13 -040010896 /* indicate the IO is being aborted by the driver. */
10897 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10898
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010899 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010900 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10901 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010902 if (phba->sli_rev == LPFC_SLI_REV4)
10903 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10904 else
10905 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010906 abtsiocb->iocb.ulpLe = 1;
10907 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010908 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010909
James Smart5ffc2662009-11-18 15:39:44 -050010910 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010911 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010912 if (iocbq->iocb_flag & LPFC_IO_FCP)
10913 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010914 if (iocbq->iocb_flag & LPFC_IO_FOF)
10915 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010916
James Smart2e0fef82007-06-17 19:56:36 -050010917 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010918 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10919 else
10920 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10921
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010922 /* Setup callback routine and issue the command. */
10923 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010924 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10925 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010926 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010927 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010928 errcnt++;
10929 continue;
10930 }
10931 }
10932
10933 return errcnt;
10934}
10935
James Smarte59058c2008-08-24 21:49:00 -040010936/**
James Smart98912dda2014-04-04 13:52:31 -040010937 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10938 * @vport: Pointer to virtual port.
10939 * @pring: Pointer to driver SLI ring object.
10940 * @tgt_id: SCSI ID of the target.
10941 * @lun_id: LUN ID of the scsi device.
10942 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10943 *
10944 * This function sends an abort command for every SCSI command
10945 * associated with the given virtual port pending on the ring
10946 * filtered by lpfc_sli_validate_fcp_iocb function.
10947 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10948 * FCP iocbs associated with lun specified by tgt_id and lun_id
10949 * parameters
10950 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10951 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10952 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10953 * FCP iocbs associated with virtual port.
10954 * This function returns number of iocbs it aborted .
10955 * This function is called with no locks held right after a taskmgmt
10956 * command is sent.
10957 **/
10958int
10959lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10960 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10961{
10962 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010963 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010964 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010965 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010966 struct lpfc_iocbq *iocbq;
10967 IOCB_t *icmd;
10968 int sum, i, ret_val;
10969 unsigned long iflags;
10970 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040010971
10972 spin_lock_irq(&phba->hbalock);
10973
10974 /* all I/Os are in process of being flushed */
10975 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10976 spin_unlock_irq(&phba->hbalock);
10977 return 0;
10978 }
10979 sum = 0;
10980
10981 for (i = 1; i <= phba->sli.last_iotag; i++) {
10982 iocbq = phba->sli.iocbq_lookup[i];
10983
10984 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10985 cmd) != 0)
10986 continue;
10987
10988 /*
10989 * If the iocbq is already being aborted, don't take a second
10990 * action, but do count it.
10991 */
10992 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10993 continue;
10994
10995 /* issue ABTS for this IOCB based on iotag */
10996 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10997 if (abtsiocbq == NULL)
10998 continue;
10999
11000 icmd = &iocbq->iocb;
11001 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11002 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11003 if (phba->sli_rev == LPFC_SLI_REV4)
11004 abtsiocbq->iocb.un.acxri.abortIoTag =
11005 iocbq->sli4_xritag;
11006 else
11007 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11008 abtsiocbq->iocb.ulpLe = 1;
11009 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11010 abtsiocbq->vport = vport;
11011
11012 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011013 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011014 if (iocbq->iocb_flag & LPFC_IO_FCP)
11015 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011016 if (iocbq->iocb_flag & LPFC_IO_FOF)
11017 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011018
James Smart8c50d252014-09-03 12:58:16 -040011019 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11020 ndlp = lpfc_cmd->rdata->pnode;
11021
11022 if (lpfc_is_link_up(phba) &&
11023 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011024 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11025 else
11026 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11027
11028 /* Setup callback routine and issue the command. */
11029 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11030
11031 /*
11032 * Indicate the IO is being aborted by the driver and set
11033 * the caller's flag into the aborted IO.
11034 */
11035 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11036
11037 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011038 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11039 if (pring_s4 == NULL)
11040 continue;
James Smart98912dda2014-04-04 13:52:31 -040011041 /* Note: both hbalock and ring_lock must be set here */
11042 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11043 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11044 abtsiocbq, 0);
11045 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11046 } else {
11047 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11048 abtsiocbq, 0);
11049 }
11050
11051
11052 if (ret_val == IOCB_ERROR)
11053 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11054 else
11055 sum++;
11056 }
11057 spin_unlock_irq(&phba->hbalock);
11058 return sum;
11059}
11060
11061/**
James Smart3621a712009-04-06 18:47:14 -040011062 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011063 * @phba: Pointer to HBA context object.
11064 * @cmdiocbq: Pointer to command iocb.
11065 * @rspiocbq: Pointer to response iocb.
11066 *
11067 * This function is the completion handler for iocbs issued using
11068 * lpfc_sli_issue_iocb_wait function. This function is called by the
11069 * ring event handler function without any lock held. This function
11070 * can be called from both worker thread context and interrupt
11071 * context. This function also can be called from other thread which
11072 * cleans up the SLI layer objects.
11073 * This function copy the contents of the response iocb to the
11074 * response iocb memory object provided by the caller of
11075 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11076 * sleeps for the iocb completion.
11077 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011078static void
11079lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11080 struct lpfc_iocbq *cmdiocbq,
11081 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011082{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011083 wait_queue_head_t *pdone_q;
11084 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011085 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011086
James Smart2e0fef82007-06-17 19:56:36 -050011087 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011088 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11089
11090 /*
11091 * A time out has occurred for the iocb. If a time out
11092 * completion handler has been supplied, call it. Otherwise,
11093 * just free the iocbq.
11094 */
11095
11096 spin_unlock_irqrestore(&phba->hbalock, iflags);
11097 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11098 cmdiocbq->wait_iocb_cmpl = NULL;
11099 if (cmdiocbq->iocb_cmpl)
11100 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11101 else
11102 lpfc_sli_release_iocbq(phba, cmdiocbq);
11103 return;
11104 }
11105
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011106 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11107 if (cmdiocbq->context2 && rspiocbq)
11108 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11109 &rspiocbq->iocb, sizeof(IOCB_t));
11110
James Smart0f65ff62010-02-26 14:14:23 -050011111 /* Set the exchange busy flag for task management commands */
11112 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11113 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11114 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11115 cur_iocbq);
11116 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11117 }
11118
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011119 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011120 if (pdone_q)
11121 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011122 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011123 return;
11124}
11125
James Smarte59058c2008-08-24 21:49:00 -040011126/**
James Smartd11e31d2009-06-10 17:23:06 -040011127 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11128 * @phba: Pointer to HBA context object..
11129 * @piocbq: Pointer to command iocb.
11130 * @flag: Flag to test.
11131 *
11132 * This routine grabs the hbalock and then test the iocb_flag to
11133 * see if the passed in flag is set.
11134 * Returns:
11135 * 1 if flag is set.
11136 * 0 if flag is not set.
11137 **/
11138static int
11139lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11140 struct lpfc_iocbq *piocbq, uint32_t flag)
11141{
11142 unsigned long iflags;
11143 int ret;
11144
11145 spin_lock_irqsave(&phba->hbalock, iflags);
11146 ret = piocbq->iocb_flag & flag;
11147 spin_unlock_irqrestore(&phba->hbalock, iflags);
11148 return ret;
11149
11150}
11151
11152/**
James Smart3621a712009-04-06 18:47:14 -040011153 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011154 * @phba: Pointer to HBA context object..
11155 * @pring: Pointer to sli ring.
11156 * @piocb: Pointer to command iocb.
11157 * @prspiocbq: Pointer to response iocb.
11158 * @timeout: Timeout in number of seconds.
11159 *
11160 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011161 * iocb to complete. The iocb_cmpl field of the shall be used
11162 * to handle iocbs which time out. If the field is NULL, the
11163 * function shall free the iocbq structure. If more clean up is
11164 * needed, the caller is expected to provide a completion function
11165 * that will provide the needed clean up. If the iocb command is
11166 * not completed within timeout seconds, the function will either
11167 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11168 * completion function set in the iocb_cmpl field and then return
11169 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11170 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011171 * The function waits for the iocb completion using an
11172 * non-interruptible wait.
11173 * This function will sleep while waiting for iocb completion.
11174 * So, this function should not be called from any context which
11175 * does not allow sleeping. Due to the same reason, this function
11176 * cannot be called with interrupt disabled.
11177 * This function assumes that the iocb completions occur while
11178 * this function sleep. So, this function cannot be called from
11179 * the thread which process iocb completion for this ring.
11180 * This function clears the iocb_flag of the iocb object before
11181 * issuing the iocb and the iocb completion handler sets this
11182 * flag and wakes this thread when the iocb completes.
11183 * The contents of the response iocb will be copied to prspiocbq
11184 * by the completion handler when the command completes.
11185 * This function returns IOCB_SUCCESS when success.
11186 * This function is called with no lock held.
11187 **/
dea31012005-04-17 16:05:31 -050011188int
James Smart2e0fef82007-06-17 19:56:36 -050011189lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011190 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011191 struct lpfc_iocbq *piocb,
11192 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011193 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011194{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011195 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011196 long timeleft, timeout_req = 0;
11197 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011198 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011199 struct lpfc_iocbq *iocb;
11200 int txq_cnt = 0;
11201 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011202 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011203 unsigned long iflags;
11204 bool iocb_completed = true;
11205
James Smart895427b2017-02-12 13:52:30 -080011206 if (phba->sli_rev >= LPFC_SLI_REV4)
11207 pring = lpfc_sli4_calc_ring(phba, piocb);
11208 else
11209 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011210 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011211 * If the caller has provided a response iocbq buffer, then context2
11212 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011213 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011214 if (prspiocbq) {
11215 if (piocb->context2)
11216 return IOCB_ERROR;
11217 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011218 }
11219
James Smart5a0916b2013-07-15 18:31:42 -040011220 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011221 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11222 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011223 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011224
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011225 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011226 if (lpfc_readl(phba->HCregaddr, &creg_val))
11227 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011228 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11229 writel(creg_val, phba->HCregaddr);
11230 readl(phba->HCregaddr); /* flush */
11231 }
11232
James Smart2a9bf3d2010-06-07 15:24:45 -040011233 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11234 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011235 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011236 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011237 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011238 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011239 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011240 spin_lock_irqsave(&phba->hbalock, iflags);
11241 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011242
James Smart5a0916b2013-07-15 18:31:42 -040011243 /*
11244 * IOCB timed out. Inform the wake iocb wait
11245 * completion function and set local status
11246 */
11247
11248 iocb_completed = false;
11249 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11250 }
11251 spin_unlock_irqrestore(&phba->hbalock, iflags);
11252 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011253 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011254 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011255 /* Note: we are not indicating if the IOCB has a success
11256 * status or not - that's for the caller to check.
11257 * IOCB_SUCCESS means just that the command was sent and
11258 * completed. Not that it completed successfully.
11259 * */
James Smart7054a602007-04-25 09:52:34 -040011260 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011262 "0338 IOCB wait timeout error - no "
11263 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011264 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011265 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011266 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011267 "0330 IOCB wake NOT set, "
11268 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011269 timeout, (timeleft / jiffies));
11270 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011271 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011272 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011273 if (phba->cfg_log_verbose & LOG_SLI) {
11274 list_for_each_entry(iocb, &pring->txq, list) {
11275 txq_cnt++;
11276 }
11277 list_for_each_entry(iocb, &pring->txcmplq, list) {
11278 txcmplq_cnt++;
11279 }
11280 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11281 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11282 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11283 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011284 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011285 } else {
11286 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011287 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011288 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011289 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011290 }
11291
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011292 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011293 if (lpfc_readl(phba->HCregaddr, &creg_val))
11294 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011295 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11296 writel(creg_val, phba->HCregaddr);
11297 readl(phba->HCregaddr); /* flush */
11298 }
11299
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011300 if (prspiocbq)
11301 piocb->context2 = NULL;
11302
11303 piocb->context_un.wait_queue = NULL;
11304 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011305 return retval;
11306}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011307
James Smarte59058c2008-08-24 21:49:00 -040011308/**
James Smart3621a712009-04-06 18:47:14 -040011309 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011310 * @phba: Pointer to HBA context object.
11311 * @pmboxq: Pointer to driver mailbox object.
11312 * @timeout: Timeout in number of seconds.
11313 *
11314 * This function issues the mailbox to firmware and waits for the
11315 * mailbox command to complete. If the mailbox command is not
11316 * completed within timeout seconds, it returns MBX_TIMEOUT.
11317 * The function waits for the mailbox completion using an
11318 * interruptible wait. If the thread is woken up due to a
11319 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11320 * should not free the mailbox resources, if this function returns
11321 * MBX_TIMEOUT.
11322 * This function will sleep while waiting for mailbox completion.
11323 * So, this function should not be called from any context which
11324 * does not allow sleeping. Due to the same reason, this function
11325 * cannot be called with interrupt disabled.
11326 * This function assumes that the mailbox completion occurs while
11327 * this function sleep. So, this function cannot be called from
11328 * the worker thread which processes mailbox completion.
11329 * This function is called in the context of HBA management
11330 * applications.
11331 * This function returns MBX_SUCCESS when successful.
11332 * This function is called with no lock held.
11333 **/
dea31012005-04-17 16:05:31 -050011334int
James Smart2e0fef82007-06-17 19:56:36 -050011335lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011336 uint32_t timeout)
11337{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011338 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011339 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011340 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011341 unsigned long flag;
dea31012005-04-17 16:05:31 -050011342
James Smartb230b8a2013-05-31 17:05:27 -040011343 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011344 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011345 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011346
James Smart495a7142008-06-14 22:52:59 -040011347 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011348 /* setup wake call as IOCB callback */
11349 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11350 /* setup context field to pass wait_queue pointer to wake function */
11351 pmboxq->context1 = &done_q;
11352
dea31012005-04-17 16:05:31 -050011353 /* now issue the command */
11354 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011355 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011356 wait_event_interruptible_timeout(done_q,
11357 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011358 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011359
James Smart858c9f62007-06-17 19:56:39 -050011360 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011361 /* restore the possible extended buffer for free resource */
11362 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011363 /*
11364 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11365 * else do not free the resources.
11366 */
James Smartd7c47992010-06-08 18:31:54 -040011367 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011368 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011369 } else {
James Smart7054a602007-04-25 09:52:34 -040011370 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011371 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11372 }
11373 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011374 } else {
11375 /* restore the possible extended buffer for free resource */
11376 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011377 }
11378
dea31012005-04-17 16:05:31 -050011379 return retval;
11380}
11381
James Smarte59058c2008-08-24 21:49:00 -040011382/**
James Smart3772a992009-05-22 14:50:54 -040011383 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011384 * @phba: Pointer to HBA context.
11385 *
James Smart3772a992009-05-22 14:50:54 -040011386 * This function is called to shutdown the driver's mailbox sub-system.
11387 * It first marks the mailbox sub-system is in a block state to prevent
11388 * the asynchronous mailbox command from issued off the pending mailbox
11389 * command queue. If the mailbox command sub-system shutdown is due to
11390 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11391 * the mailbox sub-system flush routine to forcefully bring down the
11392 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11393 * as with offline or HBA function reset), this routine will wait for the
11394 * outstanding mailbox command to complete before invoking the mailbox
11395 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011396 **/
James Smart3772a992009-05-22 14:50:54 -040011397void
James Smart618a5232012-06-12 13:54:36 -040011398lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011399{
James Smart3772a992009-05-22 14:50:54 -040011400 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011401 unsigned long timeout;
11402
James Smart618a5232012-06-12 13:54:36 -040011403 if (mbx_action == LPFC_MBX_NO_WAIT) {
11404 /* delay 100ms for port state */
11405 msleep(100);
11406 lpfc_sli_mbox_sys_flush(phba);
11407 return;
11408 }
James Smarta183a152011-10-10 21:32:43 -040011409 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011410
James Smart3772a992009-05-22 14:50:54 -040011411 spin_lock_irq(&phba->hbalock);
11412 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011413
11414 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011415 /* Determine how long we might wait for the active mailbox
11416 * command to be gracefully completed by firmware.
11417 */
James Smarta183a152011-10-10 21:32:43 -040011418 if (phba->sli.mbox_active)
11419 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11420 phba->sli.mbox_active) *
11421 1000) + jiffies;
11422 spin_unlock_irq(&phba->hbalock);
11423
James Smart3772a992009-05-22 14:50:54 -040011424 while (phba->sli.mbox_active) {
11425 /* Check active mailbox complete status every 2ms */
11426 msleep(2);
11427 if (time_after(jiffies, timeout))
11428 /* Timeout, let the mailbox flush routine to
11429 * forcefully release active mailbox command
11430 */
11431 break;
11432 }
James Smartd7069f02012-03-01 22:36:29 -050011433 } else
11434 spin_unlock_irq(&phba->hbalock);
11435
James Smart3772a992009-05-22 14:50:54 -040011436 lpfc_sli_mbox_sys_flush(phba);
11437}
11438
11439/**
11440 * lpfc_sli_eratt_read - read sli-3 error attention events
11441 * @phba: Pointer to HBA context.
11442 *
11443 * This function is called to read the SLI3 device error attention registers
11444 * for possible error attention events. The caller must hold the hostlock
11445 * with spin_lock_irq().
11446 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011447 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011448 * Register and returns 0 otherwise.
11449 **/
11450static int
11451lpfc_sli_eratt_read(struct lpfc_hba *phba)
11452{
James Smarted957682007-06-17 19:56:37 -050011453 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011454
James Smart3772a992009-05-22 14:50:54 -040011455 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011456 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11457 goto unplug_err;
11458
James Smart3772a992009-05-22 14:50:54 -040011459 if (ha_copy & HA_ERATT) {
11460 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011461 if (lpfc_sli_read_hs(phba))
11462 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011463
James Smart3772a992009-05-22 14:50:54 -040011464 /* Check if there is a deferred error condition is active */
11465 if ((HS_FFER1 & phba->work_hs) &&
11466 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011467 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011468 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011469 /* Clear all interrupt enable conditions */
11470 writel(0, phba->HCregaddr);
11471 readl(phba->HCregaddr);
11472 }
11473
11474 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011475 phba->work_ha |= HA_ERATT;
11476 /* Indicate polling handles this ERATT */
11477 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011478 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011479 }
James Smart3772a992009-05-22 14:50:54 -040011480 return 0;
James Smart9940b972011-03-11 16:06:12 -050011481
11482unplug_err:
11483 /* Set the driver HS work bitmap */
11484 phba->work_hs |= UNPLUG_ERR;
11485 /* Set the driver HA work bitmap */
11486 phba->work_ha |= HA_ERATT;
11487 /* Indicate polling handles this ERATT */
11488 phba->hba_flag |= HBA_ERATT_HANDLED;
11489 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011490}
11491
James Smarte59058c2008-08-24 21:49:00 -040011492/**
James Smartda0436e2009-05-22 14:51:39 -040011493 * lpfc_sli4_eratt_read - read sli-4 error attention events
11494 * @phba: Pointer to HBA context.
11495 *
11496 * This function is called to read the SLI4 device error attention registers
11497 * for possible error attention events. The caller must hold the hostlock
11498 * with spin_lock_irq().
11499 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011500 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011501 * Register and returns 0 otherwise.
11502 **/
11503static int
11504lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11505{
11506 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011507 uint32_t if_type, portsmphr;
11508 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011509
James Smart2fcee4b2010-12-15 17:57:46 -050011510 /*
11511 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011512 * registers for error attention. This can be changed later.
11513 */
James Smart2fcee4b2010-12-15 17:57:46 -050011514 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11515 switch (if_type) {
11516 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011517 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11518 &uerr_sta_lo) ||
11519 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11520 &uerr_sta_hi)) {
11521 phba->work_hs |= UNPLUG_ERR;
11522 phba->work_ha |= HA_ERATT;
11523 phba->hba_flag |= HBA_ERATT_HANDLED;
11524 return 1;
11525 }
James Smart2fcee4b2010-12-15 17:57:46 -050011526 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11527 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11529 "1423 HBA Unrecoverable error: "
11530 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11531 "ue_mask_lo_reg=0x%x, "
11532 "ue_mask_hi_reg=0x%x\n",
11533 uerr_sta_lo, uerr_sta_hi,
11534 phba->sli4_hba.ue_mask_lo,
11535 phba->sli4_hba.ue_mask_hi);
11536 phba->work_status[0] = uerr_sta_lo;
11537 phba->work_status[1] = uerr_sta_hi;
11538 phba->work_ha |= HA_ERATT;
11539 phba->hba_flag |= HBA_ERATT_HANDLED;
11540 return 1;
11541 }
11542 break;
11543 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011544 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11545 &portstat_reg.word0) ||
11546 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11547 &portsmphr)){
11548 phba->work_hs |= UNPLUG_ERR;
11549 phba->work_ha |= HA_ERATT;
11550 phba->hba_flag |= HBA_ERATT_HANDLED;
11551 return 1;
11552 }
James Smart2fcee4b2010-12-15 17:57:46 -050011553 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11554 phba->work_status[0] =
11555 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11556 phba->work_status[1] =
11557 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11558 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011559 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011560 "port status reg 0x%x, "
11561 "port smphr reg 0x%x, "
11562 "error 1=0x%x, error 2=0x%x\n",
11563 portstat_reg.word0,
11564 portsmphr,
11565 phba->work_status[0],
11566 phba->work_status[1]);
11567 phba->work_ha |= HA_ERATT;
11568 phba->hba_flag |= HBA_ERATT_HANDLED;
11569 return 1;
11570 }
11571 break;
11572 case LPFC_SLI_INTF_IF_TYPE_1:
11573 default:
James Smarta747c9c2009-11-18 15:41:10 -050011574 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011575 "2886 HBA Error Attention on unsupported "
11576 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011577 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011578 }
James Smart2fcee4b2010-12-15 17:57:46 -050011579
James Smartda0436e2009-05-22 14:51:39 -040011580 return 0;
11581}
11582
11583/**
James Smart3621a712009-04-06 18:47:14 -040011584 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011585 * @phba: Pointer to HBA context.
11586 *
James Smart3772a992009-05-22 14:50:54 -040011587 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011588 * error attention register bit for error attention events.
11589 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011590 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011591 * Register and returns 0 otherwise.
11592 **/
11593int
11594lpfc_sli_check_eratt(struct lpfc_hba *phba)
11595{
11596 uint32_t ha_copy;
11597
11598 /* If somebody is waiting to handle an eratt, don't process it
11599 * here. The brdkill function will do this.
11600 */
11601 if (phba->link_flag & LS_IGNORE_ERATT)
11602 return 0;
11603
11604 /* Check if interrupt handler handles this ERATT */
11605 spin_lock_irq(&phba->hbalock);
11606 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11607 /* Interrupt handler has handled ERATT */
11608 spin_unlock_irq(&phba->hbalock);
11609 return 0;
11610 }
11611
James Smarta257bf92009-04-06 18:48:10 -040011612 /*
11613 * If there is deferred error attention, do not check for error
11614 * attention
11615 */
11616 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11617 spin_unlock_irq(&phba->hbalock);
11618 return 0;
11619 }
11620
James Smart3772a992009-05-22 14:50:54 -040011621 /* If PCI channel is offline, don't process it */
11622 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011623 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011624 return 0;
11625 }
11626
11627 switch (phba->sli_rev) {
11628 case LPFC_SLI_REV2:
11629 case LPFC_SLI_REV3:
11630 /* Read chip Host Attention (HA) register */
11631 ha_copy = lpfc_sli_eratt_read(phba);
11632 break;
James Smartda0436e2009-05-22 14:51:39 -040011633 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011634 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011635 ha_copy = lpfc_sli4_eratt_read(phba);
11636 break;
James Smart3772a992009-05-22 14:50:54 -040011637 default:
11638 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11639 "0299 Invalid SLI revision (%d)\n",
11640 phba->sli_rev);
11641 ha_copy = 0;
11642 break;
James Smart93996272008-08-24 21:50:30 -040011643 }
11644 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011645
11646 return ha_copy;
11647}
11648
11649/**
11650 * lpfc_intr_state_check - Check device state for interrupt handling
11651 * @phba: Pointer to HBA context.
11652 *
11653 * This inline routine checks whether a device or its PCI slot is in a state
11654 * that the interrupt should be handled.
11655 *
11656 * This function returns 0 if the device or the PCI slot is in a state that
11657 * interrupt should be handled, otherwise -EIO.
11658 */
11659static inline int
11660lpfc_intr_state_check(struct lpfc_hba *phba)
11661{
11662 /* If the pci channel is offline, ignore all the interrupts */
11663 if (unlikely(pci_channel_offline(phba->pcidev)))
11664 return -EIO;
11665
11666 /* Update device level interrupt statistics */
11667 phba->sli.slistat.sli_intr++;
11668
11669 /* Ignore all interrupts during initialization. */
11670 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11671 return -EIO;
11672
James Smart93996272008-08-24 21:50:30 -040011673 return 0;
11674}
11675
11676/**
James Smart3772a992009-05-22 14:50:54 -040011677 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011678 * @irq: Interrupt number.
11679 * @dev_id: The device context pointer.
11680 *
James Smart93996272008-08-24 21:50:30 -040011681 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011682 * service routine when device with SLI-3 interface spec is enabled with
11683 * MSI-X multi-message interrupt mode and there are slow-path events in
11684 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11685 * interrupt mode, this function is called as part of the device-level
11686 * interrupt handler. When the PCI slot is in error recovery or the HBA
11687 * is undergoing initialization, the interrupt handler will not process
11688 * the interrupt. The link attention and ELS ring attention events are
11689 * handled by the worker thread. The interrupt handler signals the worker
11690 * thread and returns for these events. This function is called without
11691 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011692 * structures.
11693 *
11694 * This function returns IRQ_HANDLED when interrupt is handled else it
11695 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011696 **/
dea31012005-04-17 16:05:31 -050011697irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011698lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011699{
James Smart2e0fef82007-06-17 19:56:36 -050011700 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011701 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011702 uint32_t work_ha_copy;
11703 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011704 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011705 uint32_t control;
11706
James Smart92d7f7b2007-06-17 19:56:38 -050011707 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011708 struct lpfc_vport *vport;
11709 struct lpfc_nodelist *ndlp;
11710 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011711 LPFC_MBOXQ_t *pmb;
11712 int rc;
11713
dea31012005-04-17 16:05:31 -050011714 /*
11715 * Get the driver's phba structure from the dev_id and
11716 * assume the HBA is not interrupting.
11717 */
James Smart93996272008-08-24 21:50:30 -040011718 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011719
11720 if (unlikely(!phba))
11721 return IRQ_NONE;
11722
dea31012005-04-17 16:05:31 -050011723 /*
James Smart93996272008-08-24 21:50:30 -040011724 * Stuff needs to be attented to when this function is invoked as an
11725 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011726 */
James Smart93996272008-08-24 21:50:30 -040011727 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011728 /* Check device state for handling interrupt */
11729 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011730 return IRQ_NONE;
11731 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011732 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011733 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11734 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011735 /* If somebody is waiting to handle an eratt don't process it
11736 * here. The brdkill function will do this.
11737 */
11738 if (phba->link_flag & LS_IGNORE_ERATT)
11739 ha_copy &= ~HA_ERATT;
11740 /* Check the need for handling ERATT in interrupt handler */
11741 if (ha_copy & HA_ERATT) {
11742 if (phba->hba_flag & HBA_ERATT_HANDLED)
11743 /* ERATT polling has handled ERATT */
11744 ha_copy &= ~HA_ERATT;
11745 else
11746 /* Indicate interrupt handler handles ERATT */
11747 phba->hba_flag |= HBA_ERATT_HANDLED;
11748 }
James Smarta257bf92009-04-06 18:48:10 -040011749
11750 /*
11751 * If there is deferred error attention, do not check for any
11752 * interrupt.
11753 */
11754 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011755 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011756 return IRQ_NONE;
11757 }
11758
James Smart93996272008-08-24 21:50:30 -040011759 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011760 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11761 goto unplug_error;
11762
James Smarta747c9c2009-11-18 15:41:10 -050011763 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11764 HC_LAINT_ENA | HC_ERINT_ENA),
11765 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011766 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11767 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011768 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011769 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011770 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011771 } else
11772 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011773
dea31012005-04-17 16:05:31 -050011774 work_ha_copy = ha_copy & phba->work_ha_mask;
11775
James Smart93996272008-08-24 21:50:30 -040011776 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011777 if (work_ha_copy & HA_LATT) {
11778 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11779 /*
11780 * Turn off Link Attention interrupts
11781 * until CLEAR_LA done
11782 */
James Smart5b75da22008-12-04 22:39:35 -050011783 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011784 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011785 if (lpfc_readl(phba->HCregaddr, &control))
11786 goto unplug_error;
dea31012005-04-17 16:05:31 -050011787 control &= ~HC_LAINT_ENA;
11788 writel(control, phba->HCregaddr);
11789 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011790 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011791 }
11792 else
11793 work_ha_copy &= ~HA_LATT;
11794 }
11795
James Smart93996272008-08-24 21:50:30 -040011796 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011797 /*
11798 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11799 * the only slow ring.
11800 */
11801 status = (work_ha_copy &
11802 (HA_RXMASK << (4*LPFC_ELS_RING)));
11803 status >>= (4*LPFC_ELS_RING);
11804 if (status & HA_RXMASK) {
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->HCregaddr, &control))
11807 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011808
11809 lpfc_debugfs_slow_ring_trc(phba,
11810 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11811 control, status,
11812 (uint32_t)phba->sli.slistat.sli_intr);
11813
James Smart858c9f62007-06-17 19:56:39 -050011814 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011815 lpfc_debugfs_slow_ring_trc(phba,
11816 "ISR Disable ring:"
11817 "pwork:x%x hawork:x%x wait:x%x",
11818 phba->work_ha, work_ha_copy,
11819 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011820 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011821
James Smart858c9f62007-06-17 19:56:39 -050011822 control &=
11823 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011824 writel(control, phba->HCregaddr);
11825 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011826 }
James Smarta58cbd52007-08-02 11:09:43 -040011827 else {
11828 lpfc_debugfs_slow_ring_trc(phba,
11829 "ISR slow ring: pwork:"
11830 "x%x hawork:x%x wait:x%x",
11831 phba->work_ha, work_ha_copy,
11832 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011833 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011834 }
James Smart5b75da22008-12-04 22:39:35 -050011835 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011836 }
11837 }
James Smart5b75da22008-12-04 22:39:35 -050011838 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011839 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011840 if (lpfc_sli_read_hs(phba))
11841 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011842 /*
11843 * Check if there is a deferred error condition
11844 * is active
11845 */
11846 if ((HS_FFER1 & phba->work_hs) &&
11847 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011848 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11849 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011850 phba->hba_flag |= DEFER_ERATT;
11851 /* Clear all interrupt enable conditions */
11852 writel(0, phba->HCregaddr);
11853 readl(phba->HCregaddr);
11854 }
11855 }
11856
James Smart93996272008-08-24 21:50:30 -040011857 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011858 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011859 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011860 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011861 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011862
11863 /* First check out the status word */
11864 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11865 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011866 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011867 /*
11868 * Stray Mailbox Interrupt, mbxCommand <cmd>
11869 * mbxStatus <status>
11870 */
James Smart09372822008-01-11 01:52:54 -050011871 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011872 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011873 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011874 "Interrupt mbxCommand x%x "
11875 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011876 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011877 pmbox->mbxCommand,
11878 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011879 /* clear mailbox attention bit */
11880 work_ha_copy &= ~HA_MBATT;
11881 } else {
James Smart97eab632008-04-07 10:16:05 -040011882 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011883 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011884 phba->last_completion_time = jiffies;
11885 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011886 if (pmb->mbox_cmpl) {
11887 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11888 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011889 if (pmb->out_ext_byte_len &&
11890 pmb->context2)
11891 lpfc_sli_pcimem_bcopy(
11892 phba->mbox_ext,
11893 pmb->context2,
11894 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011895 }
James Smart09372822008-01-11 01:52:54 -050011896 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11897 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11898
11899 lpfc_debugfs_disc_trc(vport,
11900 LPFC_DISC_TRC_MBOX_VPORT,
11901 "MBOX dflt rpi: : "
11902 "status:x%x rpi:x%x",
11903 (uint32_t)pmbox->mbxStatus,
11904 pmbox->un.varWords[0], 0);
11905
11906 if (!pmbox->mbxStatus) {
11907 mp = (struct lpfc_dmabuf *)
11908 (pmb->context1);
11909 ndlp = (struct lpfc_nodelist *)
11910 pmb->context2;
11911
11912 /* Reg_LOGIN of dflt RPI was
11913 * successful. new lets get
11914 * rid of the RPI using the
11915 * same mbox buffer.
11916 */
11917 lpfc_unreg_login(phba,
11918 vport->vpi,
11919 pmbox->un.varWords[0],
11920 pmb);
11921 pmb->mbox_cmpl =
11922 lpfc_mbx_cmpl_dflt_rpi;
11923 pmb->context1 = mp;
11924 pmb->context2 = ndlp;
11925 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011926 rc = lpfc_sli_issue_mbox(phba,
11927 pmb,
11928 MBX_NOWAIT);
11929 if (rc != MBX_BUSY)
11930 lpfc_printf_log(phba,
11931 KERN_ERR,
11932 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011933 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011934 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011935 if (rc != MBX_NOT_FINISHED)
11936 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011937 }
11938 }
James Smart5b75da22008-12-04 22:39:35 -050011939 spin_lock_irqsave(
11940 &phba->pport->work_port_lock,
11941 iflag);
James Smart09372822008-01-11 01:52:54 -050011942 phba->pport->work_port_events &=
11943 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011944 spin_unlock_irqrestore(
11945 &phba->pport->work_port_lock,
11946 iflag);
James Smart09372822008-01-11 01:52:54 -050011947 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011948 }
James Smart97eab632008-04-07 10:16:05 -040011949 } else
James Smart5b75da22008-12-04 22:39:35 -050011950 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011951
James Smart92d7f7b2007-06-17 19:56:38 -050011952 if ((work_ha_copy & HA_MBATT) &&
11953 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011954send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011955 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011956 do {
11957 rc = lpfc_sli_issue_mbox(phba, NULL,
11958 MBX_NOWAIT);
11959 } while (rc == MBX_NOT_FINISHED);
11960 if (rc != MBX_SUCCESS)
11961 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11962 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011963 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011964 }
11965
James Smart5b75da22008-12-04 22:39:35 -050011966 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011967 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011968 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011969 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011970 }
James Smart93996272008-08-24 21:50:30 -040011971 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011972unplug_error:
11973 spin_unlock_irqrestore(&phba->hbalock, iflag);
11974 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011975
James Smart3772a992009-05-22 14:50:54 -040011976} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011977
11978/**
James Smart3772a992009-05-22 14:50:54 -040011979 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011980 * @irq: Interrupt number.
11981 * @dev_id: The device context pointer.
11982 *
11983 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011984 * service routine when device with SLI-3 interface spec is enabled with
11985 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11986 * ring event in the HBA. However, when the device is enabled with either
11987 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11988 * device-level interrupt handler. When the PCI slot is in error recovery
11989 * or the HBA is undergoing initialization, the interrupt handler will not
11990 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11991 * the intrrupt context. This function is called without any lock held.
11992 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011993 *
11994 * This function returns IRQ_HANDLED when interrupt is handled else it
11995 * returns IRQ_NONE.
11996 **/
11997irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011998lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011999{
12000 struct lpfc_hba *phba;
12001 uint32_t ha_copy;
12002 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012003 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012004 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012005
12006 /* Get the driver's phba structure from the dev_id and
12007 * assume the HBA is not interrupting.
12008 */
12009 phba = (struct lpfc_hba *) dev_id;
12010
12011 if (unlikely(!phba))
12012 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012013
12014 /*
James Smart93996272008-08-24 21:50:30 -040012015 * Stuff needs to be attented to when this function is invoked as an
12016 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012017 */
James Smart93996272008-08-24 21:50:30 -040012018 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012019 /* Check device state for handling interrupt */
12020 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012021 return IRQ_NONE;
12022 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012023 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12024 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012025 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012026 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012027 /*
12028 * If there is deferred error attention, do not check for
12029 * any interrupt.
12030 */
12031 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012032 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012033 return IRQ_NONE;
12034 }
James Smart93996272008-08-24 21:50:30 -040012035 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12036 phba->HAregaddr);
12037 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012038 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012039 } else
12040 ha_copy = phba->ha_copy;
12041
12042 /*
12043 * Process all events on FCP ring. Take the optimized path for FCP IO.
12044 */
12045 ha_copy &= ~(phba->work_ha_mask);
12046
12047 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012048 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012049 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012050 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012051 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012052
12053 if (phba->cfg_multi_ring_support == 2) {
12054 /*
James Smart93996272008-08-24 21:50:30 -040012055 * Process all events on extra ring. Take the optimized path
12056 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012057 */
James Smart93996272008-08-24 21:50:30 -040012058 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012059 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012060 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012061 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012062 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012063 status);
12064 }
12065 }
dea31012005-04-17 16:05:31 -050012066 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012067} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012068
James Smart93996272008-08-24 21:50:30 -040012069/**
James Smart3772a992009-05-22 14:50:54 -040012070 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012071 * @irq: Interrupt number.
12072 * @dev_id: The device context pointer.
12073 *
James Smart3772a992009-05-22 14:50:54 -040012074 * This function is the HBA device-level interrupt handler to device with
12075 * SLI-3 interface spec, called from the PCI layer when either MSI or
12076 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12077 * requires driver attention. This function invokes the slow-path interrupt
12078 * attention handling function and fast-path interrupt attention handling
12079 * function in turn to process the relevant HBA attention events. This
12080 * function is called without any lock held. It gets the hbalock to access
12081 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012082 *
12083 * This function returns IRQ_HANDLED when interrupt is handled, else it
12084 * returns IRQ_NONE.
12085 **/
12086irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012087lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012088{
12089 struct lpfc_hba *phba;
12090 irqreturn_t sp_irq_rc, fp_irq_rc;
12091 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012092 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012093
12094 /*
12095 * Get the driver's phba structure from the dev_id and
12096 * assume the HBA is not interrupting.
12097 */
12098 phba = (struct lpfc_hba *) dev_id;
12099
12100 if (unlikely(!phba))
12101 return IRQ_NONE;
12102
James Smart3772a992009-05-22 14:50:54 -040012103 /* Check device state for handling interrupt */
12104 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012105 return IRQ_NONE;
12106
12107 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012108 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12109 spin_unlock(&phba->hbalock);
12110 return IRQ_HANDLED;
12111 }
12112
James Smart93996272008-08-24 21:50:30 -040012113 if (unlikely(!phba->ha_copy)) {
12114 spin_unlock(&phba->hbalock);
12115 return IRQ_NONE;
12116 } else if (phba->ha_copy & HA_ERATT) {
12117 if (phba->hba_flag & HBA_ERATT_HANDLED)
12118 /* ERATT polling has handled ERATT */
12119 phba->ha_copy &= ~HA_ERATT;
12120 else
12121 /* Indicate interrupt handler handles ERATT */
12122 phba->hba_flag |= HBA_ERATT_HANDLED;
12123 }
12124
James Smarta257bf92009-04-06 18:48:10 -040012125 /*
12126 * If there is deferred error attention, do not check for any interrupt.
12127 */
12128 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012129 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012130 return IRQ_NONE;
12131 }
12132
James Smart93996272008-08-24 21:50:30 -040012133 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012134 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12135 spin_unlock(&phba->hbalock);
12136 return IRQ_HANDLED;
12137 }
James Smarta747c9c2009-11-18 15:41:10 -050012138 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12139 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12140 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012141 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012142 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012143 readl(phba->HAregaddr); /* flush */
12144 spin_unlock(&phba->hbalock);
12145
12146 /*
12147 * Invokes slow-path host attention interrupt handling as appropriate.
12148 */
12149
12150 /* status of events with mailbox and link attention */
12151 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12152
12153 /* status of events with ELS ring */
12154 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12155 status2 >>= (4*LPFC_ELS_RING);
12156
12157 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012158 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012159 else
12160 sp_irq_rc = IRQ_NONE;
12161
12162 /*
12163 * Invoke fast-path host attention interrupt handling as appropriate.
12164 */
12165
12166 /* status of events with FCP ring */
12167 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12168 status1 >>= (4*LPFC_FCP_RING);
12169
12170 /* status of events with extra ring */
12171 if (phba->cfg_multi_ring_support == 2) {
12172 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12173 status2 >>= (4*LPFC_EXTRA_RING);
12174 } else
12175 status2 = 0;
12176
12177 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012178 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012179 else
12180 fp_irq_rc = IRQ_NONE;
12181
12182 /* Return device-level interrupt handling status */
12183 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012184} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012185
12186/**
12187 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12188 * @phba: pointer to lpfc hba data structure.
12189 *
12190 * This routine is invoked by the worker thread to process all the pending
12191 * SLI4 FCP abort XRI events.
12192 **/
12193void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12194{
12195 struct lpfc_cq_event *cq_event;
12196
12197 /* First, declare the fcp xri abort event has been handled */
12198 spin_lock_irq(&phba->hbalock);
12199 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12200 spin_unlock_irq(&phba->hbalock);
12201 /* Now, handle all the fcp xri abort events */
12202 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12203 /* Get the first event from the head of the event queue */
12204 spin_lock_irq(&phba->hbalock);
12205 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12206 cq_event, struct lpfc_cq_event, list);
12207 spin_unlock_irq(&phba->hbalock);
12208 /* Notify aborted XRI for FCP work queue */
12209 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12210 /* Free the event processed back to the free pool */
12211 lpfc_sli4_cq_event_release(phba, cq_event);
12212 }
12213}
12214
12215/**
James Smart318083a2017-03-04 09:30:30 -080012216 * lpfc_sli4_nvme_xri_abort_event_proc - Process nvme xri abort event
12217 * @phba: pointer to lpfc hba data structure.
12218 *
12219 * This routine is invoked by the worker thread to process all the pending
12220 * SLI4 NVME abort XRI events.
12221 **/
12222void lpfc_sli4_nvme_xri_abort_event_proc(struct lpfc_hba *phba)
12223{
12224 struct lpfc_cq_event *cq_event;
12225
12226 /* First, declare the fcp xri abort event has been handled */
12227 spin_lock_irq(&phba->hbalock);
12228 phba->hba_flag &= ~NVME_XRI_ABORT_EVENT;
12229 spin_unlock_irq(&phba->hbalock);
12230 /* Now, handle all the fcp xri abort events */
12231 while (!list_empty(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue)) {
12232 /* Get the first event from the head of the event queue */
12233 spin_lock_irq(&phba->hbalock);
12234 list_remove_head(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
12235 cq_event, struct lpfc_cq_event, list);
12236 spin_unlock_irq(&phba->hbalock);
12237 /* Notify aborted XRI for NVME work queue */
12238 if (phba->nvmet_support) {
12239 lpfc_sli4_nvmet_xri_aborted(phba,
12240 &cq_event->cqe.wcqe_axri);
12241 } else {
12242 lpfc_sli4_nvme_xri_aborted(phba,
12243 &cq_event->cqe.wcqe_axri);
12244 }
12245 /* Free the event processed back to the free pool */
12246 lpfc_sli4_cq_event_release(phba, cq_event);
12247 }
12248}
12249
12250/**
James Smart4f774512009-05-22 14:52:35 -040012251 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12252 * @phba: pointer to lpfc hba data structure.
12253 *
12254 * This routine is invoked by the worker thread to process all the pending
12255 * SLI4 els abort xri events.
12256 **/
12257void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12258{
12259 struct lpfc_cq_event *cq_event;
12260
12261 /* First, declare the els xri abort event has been handled */
12262 spin_lock_irq(&phba->hbalock);
12263 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12264 spin_unlock_irq(&phba->hbalock);
12265 /* Now, handle all the els xri abort events */
12266 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12267 /* Get the first event from the head of the event queue */
12268 spin_lock_irq(&phba->hbalock);
12269 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12270 cq_event, struct lpfc_cq_event, list);
12271 spin_unlock_irq(&phba->hbalock);
12272 /* Notify aborted XRI for ELS work queue */
12273 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12274 /* Free the event processed back to the free pool */
12275 lpfc_sli4_cq_event_release(phba, cq_event);
12276 }
12277}
12278
James Smart341af102010-01-26 23:07:37 -050012279/**
12280 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12281 * @phba: pointer to lpfc hba data structure
12282 * @pIocbIn: pointer to the rspiocbq
12283 * @pIocbOut: pointer to the cmdiocbq
12284 * @wcqe: pointer to the complete wcqe
12285 *
12286 * This routine transfers the fields of a command iocbq to a response iocbq
12287 * by copying all the IOCB fields from command iocbq and transferring the
12288 * completion status information from the complete wcqe.
12289 **/
James Smart4f774512009-05-22 14:52:35 -040012290static void
James Smart341af102010-01-26 23:07:37 -050012291lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12292 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012293 struct lpfc_iocbq *pIocbOut,
12294 struct lpfc_wcqe_complete *wcqe)
12295{
James Smartaf227412013-10-10 12:23:10 -040012296 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012297 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012298 uint32_t status, max_response;
12299 struct lpfc_dmabuf *dmabuf;
12300 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012301 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12302
12303 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12304 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012305 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012306 status = bf_get(lpfc_wcqe_c_status, wcqe);
12307 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012308 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12309 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12310 pIocbIn->iocb.un.fcpi.fcpi_parm =
12311 pIocbOut->iocb.un.fcpi.fcpi_parm -
12312 wcqe->total_data_placed;
12313 else
12314 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012315 else {
James Smart4f774512009-05-22 14:52:35 -040012316 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012317 switch (pIocbOut->iocb.ulpCommand) {
12318 case CMD_ELS_REQUEST64_CR:
12319 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12320 bpl = (struct ulp_bde64 *)dmabuf->virt;
12321 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12322 max_response = bde.tus.f.bdeSize;
12323 break;
12324 case CMD_GEN_REQUEST64_CR:
12325 max_response = 0;
12326 if (!pIocbOut->context3)
12327 break;
12328 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12329 sizeof(struct ulp_bde64);
12330 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12331 bpl = (struct ulp_bde64 *)dmabuf->virt;
12332 for (i = 0; i < numBdes; i++) {
12333 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12334 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12335 max_response += bde.tus.f.bdeSize;
12336 }
12337 break;
12338 default:
12339 max_response = wcqe->total_data_placed;
12340 break;
12341 }
12342 if (max_response < wcqe->total_data_placed)
12343 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12344 else
12345 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12346 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012347 }
James Smart341af102010-01-26 23:07:37 -050012348
James Smartacd68592012-01-18 16:25:09 -050012349 /* Convert BG errors for completion status */
12350 if (status == CQE_STATUS_DI_ERROR) {
12351 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12352
12353 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12354 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12355 else
12356 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12357
12358 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12359 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12360 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12361 BGS_GUARD_ERR_MASK;
12362 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12363 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12364 BGS_APPTAG_ERR_MASK;
12365 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12366 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12367 BGS_REFTAG_ERR_MASK;
12368
12369 /* Check to see if there was any good data before the error */
12370 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12371 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12372 BGS_HI_WATER_MARK_PRESENT_MASK;
12373 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12374 wcqe->total_data_placed;
12375 }
12376
12377 /*
12378 * Set ALL the error bits to indicate we don't know what
12379 * type of error it is.
12380 */
12381 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12382 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12383 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12384 BGS_GUARD_ERR_MASK);
12385 }
12386
James Smart341af102010-01-26 23:07:37 -050012387 /* Pick up HBA exchange busy condition */
12388 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12389 spin_lock_irqsave(&phba->hbalock, iflags);
12390 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12391 spin_unlock_irqrestore(&phba->hbalock, iflags);
12392 }
James Smart4f774512009-05-22 14:52:35 -040012393}
12394
12395/**
James Smart45ed1192009-10-02 15:17:02 -040012396 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12397 * @phba: Pointer to HBA context object.
12398 * @wcqe: Pointer to work-queue completion queue entry.
12399 *
12400 * This routine handles an ELS work-queue completion event and construct
12401 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12402 * discovery engine to handle.
12403 *
12404 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12405 **/
12406static struct lpfc_iocbq *
12407lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12408 struct lpfc_iocbq *irspiocbq)
12409{
James Smart895427b2017-02-12 13:52:30 -080012410 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012411 struct lpfc_iocbq *cmdiocbq;
12412 struct lpfc_wcqe_complete *wcqe;
12413 unsigned long iflags;
12414
James Smart895427b2017-02-12 13:52:30 -080012415 pring = lpfc_phba_elsring(phba);
12416
James Smart45ed1192009-10-02 15:17:02 -040012417 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012418 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012419 pring->stats.iocb_event++;
12420 /* Look up the ELS command IOCB and create pseudo response IOCB */
12421 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12422 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart89533e92016-10-13 15:06:15 -070012423 /* Put the iocb back on the txcmplq */
12424 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
James Smart7e56aa22012-08-03 12:35:34 -040012425 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012426
12427 if (unlikely(!cmdiocbq)) {
12428 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12429 "0386 ELS complete with no corresponding "
12430 "cmdiocb: iotag (%d)\n",
12431 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12432 lpfc_sli_release_iocbq(phba, irspiocbq);
12433 return NULL;
12434 }
12435
12436 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012437 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012438
12439 return irspiocbq;
12440}
12441
12442/**
James Smart04c68492009-05-22 14:52:52 -040012443 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12444 * @phba: Pointer to HBA context object.
12445 * @cqe: Pointer to mailbox completion queue entry.
12446 *
12447 * This routine process a mailbox completion queue entry with asynchrous
12448 * event.
12449 *
12450 * Return: true if work posted to worker thread, otherwise false.
12451 **/
12452static bool
12453lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12454{
12455 struct lpfc_cq_event *cq_event;
12456 unsigned long iflags;
12457
12458 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12459 "0392 Async Event: word0:x%x, word1:x%x, "
12460 "word2:x%x, word3:x%x\n", mcqe->word0,
12461 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12462
12463 /* Allocate a new internal CQ_EVENT entry */
12464 cq_event = lpfc_sli4_cq_event_alloc(phba);
12465 if (!cq_event) {
12466 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12467 "0394 Failed to allocate CQ_EVENT entry\n");
12468 return false;
12469 }
12470
12471 /* Move the CQE into an asynchronous event entry */
12472 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12473 spin_lock_irqsave(&phba->hbalock, iflags);
12474 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12475 /* Set the async event flag */
12476 phba->hba_flag |= ASYNC_EVENT;
12477 spin_unlock_irqrestore(&phba->hbalock, iflags);
12478
12479 return true;
12480}
12481
12482/**
12483 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12484 * @phba: Pointer to HBA context object.
12485 * @cqe: Pointer to mailbox completion queue entry.
12486 *
12487 * This routine process a mailbox completion queue entry with mailbox
12488 * completion event.
12489 *
12490 * Return: true if work posted to worker thread, otherwise false.
12491 **/
12492static bool
12493lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12494{
12495 uint32_t mcqe_status;
12496 MAILBOX_t *mbox, *pmbox;
12497 struct lpfc_mqe *mqe;
12498 struct lpfc_vport *vport;
12499 struct lpfc_nodelist *ndlp;
12500 struct lpfc_dmabuf *mp;
12501 unsigned long iflags;
12502 LPFC_MBOXQ_t *pmb;
12503 bool workposted = false;
12504 int rc;
12505
12506 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12507 if (!bf_get(lpfc_trailer_completed, mcqe))
12508 goto out_no_mqe_complete;
12509
12510 /* Get the reference to the active mbox command */
12511 spin_lock_irqsave(&phba->hbalock, iflags);
12512 pmb = phba->sli.mbox_active;
12513 if (unlikely(!pmb)) {
12514 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12515 "1832 No pending MBOX command to handle\n");
12516 spin_unlock_irqrestore(&phba->hbalock, iflags);
12517 goto out_no_mqe_complete;
12518 }
12519 spin_unlock_irqrestore(&phba->hbalock, iflags);
12520 mqe = &pmb->u.mqe;
12521 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12522 mbox = phba->mbox;
12523 vport = pmb->vport;
12524
12525 /* Reset heartbeat timer */
12526 phba->last_completion_time = jiffies;
12527 del_timer(&phba->sli.mbox_tmo);
12528
12529 /* Move mbox data to caller's mailbox region, do endian swapping */
12530 if (pmb->mbox_cmpl && mbox)
12531 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012532
James Smart73d91e52011-10-10 21:32:10 -040012533 /*
12534 * For mcqe errors, conditionally move a modified error code to
12535 * the mbox so that the error will not be missed.
12536 */
12537 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12538 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12539 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12540 bf_set(lpfc_mqe_status, mqe,
12541 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12542 }
James Smart04c68492009-05-22 14:52:52 -040012543 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12544 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12545 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12546 "MBOX dflt rpi: status:x%x rpi:x%x",
12547 mcqe_status,
12548 pmbox->un.varWords[0], 0);
12549 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12550 mp = (struct lpfc_dmabuf *)(pmb->context1);
12551 ndlp = (struct lpfc_nodelist *)pmb->context2;
12552 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12553 * RID of the PPI using the same mbox buffer.
12554 */
12555 lpfc_unreg_login(phba, vport->vpi,
12556 pmbox->un.varWords[0], pmb);
12557 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12558 pmb->context1 = mp;
12559 pmb->context2 = ndlp;
12560 pmb->vport = vport;
12561 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12562 if (rc != MBX_BUSY)
12563 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12564 LOG_SLI, "0385 rc should "
12565 "have been MBX_BUSY\n");
12566 if (rc != MBX_NOT_FINISHED)
12567 goto send_current_mbox;
12568 }
12569 }
12570 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12571 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12572 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12573
12574 /* There is mailbox completion work to do */
12575 spin_lock_irqsave(&phba->hbalock, iflags);
12576 __lpfc_mbox_cmpl_put(phba, pmb);
12577 phba->work_ha |= HA_MBATT;
12578 spin_unlock_irqrestore(&phba->hbalock, iflags);
12579 workposted = true;
12580
12581send_current_mbox:
12582 spin_lock_irqsave(&phba->hbalock, iflags);
12583 /* Release the mailbox command posting token */
12584 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12585 /* Setting active mailbox pointer need to be in sync to flag clear */
12586 phba->sli.mbox_active = NULL;
12587 spin_unlock_irqrestore(&phba->hbalock, iflags);
12588 /* Wake up worker thread to post the next pending mailbox command */
12589 lpfc_worker_wake_up(phba);
12590out_no_mqe_complete:
12591 if (bf_get(lpfc_trailer_consumed, mcqe))
12592 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12593 return workposted;
12594}
12595
12596/**
12597 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12598 * @phba: Pointer to HBA context object.
12599 * @cqe: Pointer to mailbox completion queue entry.
12600 *
12601 * This routine process a mailbox completion queue entry, it invokes the
12602 * proper mailbox complete handling or asynchrous event handling routine
12603 * according to the MCQE's async bit.
12604 *
12605 * Return: true if work posted to worker thread, otherwise false.
12606 **/
12607static bool
12608lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12609{
12610 struct lpfc_mcqe mcqe;
12611 bool workposted;
12612
12613 /* Copy the mailbox MCQE and convert endian order as needed */
12614 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12615
12616 /* Invoke the proper event handling routine */
12617 if (!bf_get(lpfc_trailer_async, &mcqe))
12618 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12619 else
12620 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12621 return workposted;
12622}
12623
12624/**
James Smart4f774512009-05-22 14:52:35 -040012625 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12626 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012627 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012628 * @wcqe: Pointer to work-queue completion queue entry.
12629 *
12630 * This routine handles an ELS work-queue completion event.
12631 *
12632 * Return: true if work posted to worker thread, otherwise false.
12633 **/
12634static bool
James Smart2a76a282012-08-03 12:35:54 -040012635lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012636 struct lpfc_wcqe_complete *wcqe)
12637{
James Smart4f774512009-05-22 14:52:35 -040012638 struct lpfc_iocbq *irspiocbq;
12639 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012640 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012641 int txq_cnt = 0;
12642 int txcmplq_cnt = 0;
12643 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012644
James Smart45ed1192009-10-02 15:17:02 -040012645 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012646 irspiocbq = lpfc_sli_get_iocbq(phba);
12647 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012648 if (!list_empty(&pring->txq))
12649 txq_cnt++;
12650 if (!list_empty(&pring->txcmplq))
12651 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012652 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012653 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12654 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012655 txq_cnt, phba->iocb_cnt,
12656 fcp_txcmplq_cnt,
12657 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012658 return false;
James Smart4f774512009-05-22 14:52:35 -040012659 }
James Smart4f774512009-05-22 14:52:35 -040012660
James Smart45ed1192009-10-02 15:17:02 -040012661 /* Save off the slow-path queue event for work thread to process */
12662 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012663 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012664 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012665 &phba->sli4_hba.sp_queue_event);
12666 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012667 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012668
James Smart45ed1192009-10-02 15:17:02 -040012669 return true;
James Smart4f774512009-05-22 14:52:35 -040012670}
12671
12672/**
12673 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12674 * @phba: Pointer to HBA context object.
12675 * @wcqe: Pointer to work-queue completion queue entry.
12676 *
12677 * This routine handles slow-path WQ entry comsumed event by invoking the
12678 * proper WQ release routine to the slow-path WQ.
12679 **/
12680static void
12681lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12682 struct lpfc_wcqe_release *wcqe)
12683{
James Smart2e90f4b2011-12-13 13:22:37 -050012684 /* sanity check on queue memory */
12685 if (unlikely(!phba->sli4_hba.els_wq))
12686 return;
James Smart4f774512009-05-22 14:52:35 -040012687 /* Check for the slow-path ELS work queue */
12688 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12689 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12690 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12691 else
12692 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12693 "2579 Slow-path wqe consume event carries "
12694 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12695 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12696 phba->sli4_hba.els_wq->queue_id);
12697}
12698
12699/**
12700 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12701 * @phba: Pointer to HBA context object.
12702 * @cq: Pointer to a WQ completion queue.
12703 * @wcqe: Pointer to work-queue completion queue entry.
12704 *
12705 * This routine handles an XRI abort event.
12706 *
12707 * Return: true if work posted to worker thread, otherwise false.
12708 **/
12709static bool
12710lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12711 struct lpfc_queue *cq,
12712 struct sli4_wcqe_xri_aborted *wcqe)
12713{
12714 bool workposted = false;
12715 struct lpfc_cq_event *cq_event;
12716 unsigned long iflags;
12717
12718 /* Allocate a new internal CQ_EVENT entry */
12719 cq_event = lpfc_sli4_cq_event_alloc(phba);
12720 if (!cq_event) {
12721 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12722 "0602 Failed to allocate CQ_EVENT entry\n");
12723 return false;
12724 }
12725
12726 /* Move the CQE into the proper xri abort event list */
12727 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12728 switch (cq->subtype) {
12729 case LPFC_FCP:
12730 spin_lock_irqsave(&phba->hbalock, iflags);
12731 list_add_tail(&cq_event->list,
12732 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12733 /* Set the fcp xri abort event flag */
12734 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12735 spin_unlock_irqrestore(&phba->hbalock, iflags);
12736 workposted = true;
12737 break;
12738 case LPFC_ELS:
12739 spin_lock_irqsave(&phba->hbalock, iflags);
12740 list_add_tail(&cq_event->list,
12741 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12742 /* Set the els xri abort event flag */
12743 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12744 spin_unlock_irqrestore(&phba->hbalock, iflags);
12745 workposted = true;
12746 break;
James Smart318083a2017-03-04 09:30:30 -080012747 case LPFC_NVME:
12748 spin_lock_irqsave(&phba->hbalock, iflags);
12749 list_add_tail(&cq_event->list,
12750 &phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
12751 /* Set the nvme xri abort event flag */
12752 phba->hba_flag |= NVME_XRI_ABORT_EVENT;
12753 spin_unlock_irqrestore(&phba->hbalock, iflags);
12754 workposted = true;
12755 break;
James Smart4f774512009-05-22 14:52:35 -040012756 default:
12757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080012758 "0603 Invalid CQ subtype %d: "
12759 "%08x %08x %08x %08x\n",
12760 cq->subtype, wcqe->word0, wcqe->parameter,
12761 wcqe->word2, wcqe->word3);
12762 lpfc_sli4_cq_event_release(phba, cq_event);
James Smart4f774512009-05-22 14:52:35 -040012763 workposted = false;
12764 break;
12765 }
12766 return workposted;
12767}
12768
12769/**
James Smart4d9ab992009-10-02 15:16:39 -040012770 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012771 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012772 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012773 *
James Smart4d9ab992009-10-02 15:16:39 -040012774 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012775 *
12776 * Return: true if work posted to worker thread, otherwise false.
12777 **/
12778static bool
James Smart4d9ab992009-10-02 15:16:39 -040012779lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12780{
12781 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012782 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012783 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12784 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12785 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012786 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012787 unsigned long iflags;
12788
James Smart2e90f4b2011-12-13 13:22:37 -050012789 /* sanity check on queue memory */
12790 if (unlikely(!hrq) || unlikely(!drq))
12791 return workposted;
12792
James Smart7851fe22011-07-22 18:36:52 -040012793 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12794 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12795 else
12796 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12797 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012798 goto out;
12799
12800 status = bf_get(lpfc_rcqe_status, rcqe);
12801 switch (status) {
12802 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12803 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12804 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012805 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012806 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012807 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012808 spin_lock_irqsave(&phba->hbalock, iflags);
12809 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12810 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012811 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012812 spin_unlock_irqrestore(&phba->hbalock, iflags);
12813 goto out;
12814 }
James Smartb84daac2012-08-03 12:35:13 -040012815 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012816 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012817
12818 /* If a NVME LS event (type 0x28), treat it as Fast path */
12819 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12820
James Smart4d9ab992009-10-02 15:16:39 -040012821 /* save off the frame for the word thread to process */
12822 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012823 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012824 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012825 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012826 spin_unlock_irqrestore(&phba->hbalock, iflags);
12827 workposted = true;
12828 break;
12829 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12830 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012831 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012832 /* Post more buffers if possible */
12833 spin_lock_irqsave(&phba->hbalock, iflags);
12834 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12835 spin_unlock_irqrestore(&phba->hbalock, iflags);
12836 workposted = true;
12837 break;
12838 }
12839out:
12840 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012841}
12842
12843/**
12844 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12845 * @phba: Pointer to HBA context object.
12846 * @cq: Pointer to the completion queue.
12847 * @wcqe: Pointer to a completion queue entry.
12848 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012849 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012850 * entry.
12851 *
12852 * Return: true if work posted to worker thread, otherwise false.
12853 **/
12854static bool
12855lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012856 struct lpfc_cqe *cqe)
12857{
James Smart45ed1192009-10-02 15:17:02 -040012858 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012859 bool workposted = false;
12860
12861 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012862 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012863
12864 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012865 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012866 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012867 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012868 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012869 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012870 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012871 break;
12872 case CQE_CODE_RELEASE_WQE:
12873 /* Process the WQ release event */
12874 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012875 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012876 break;
12877 case CQE_CODE_XRI_ABORTED:
12878 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012879 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012880 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012881 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012882 break;
James Smart4d9ab992009-10-02 15:16:39 -040012883 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012884 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012885 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012886 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012887 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012888 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012889 break;
James Smart4f774512009-05-22 14:52:35 -040012890 default:
12891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12892 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012893 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012894 break;
12895 }
12896 return workposted;
12897}
12898
12899/**
James Smart4f774512009-05-22 14:52:35 -040012900 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12901 * @phba: Pointer to HBA context object.
12902 * @eqe: Pointer to fast-path event queue entry.
12903 *
12904 * This routine process a event queue entry from the slow-path event queue.
12905 * It will check the MajorCode and MinorCode to determine this is for a
12906 * completion event on a completion queue, if not, an error shall be logged
12907 * and just return. Otherwise, it will get to the corresponding completion
12908 * queue and process all the entries on that completion queue, rearm the
12909 * completion queue, and then return.
12910 *
12911 **/
12912static void
James Smart67d12732012-08-03 12:36:13 -040012913lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12914 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012915{
James Smart67d12732012-08-03 12:36:13 -040012916 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012917 struct lpfc_cqe *cqe;
12918 bool workposted = false;
12919 int ecount = 0;
12920 uint16_t cqid;
12921
James Smart4f774512009-05-22 14:52:35 -040012922 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012923 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012924
James Smart4f774512009-05-22 14:52:35 -040012925 list_for_each_entry(childq, &speq->child_list, list) {
12926 if (childq->queue_id == cqid) {
12927 cq = childq;
12928 break;
12929 }
12930 }
12931 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012932 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12934 "0365 Slow-path CQ identifier "
12935 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012936 return;
12937 }
12938
James Smart895427b2017-02-12 13:52:30 -080012939 /* Save EQ associated with this CQ */
12940 cq->assoc_qp = speq;
12941
James Smart4f774512009-05-22 14:52:35 -040012942 /* Process all the entries to the CQ */
12943 switch (cq->type) {
12944 case LPFC_MCQ:
12945 while ((cqe = lpfc_sli4_cq_get(cq))) {
12946 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012947 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012948 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012949 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012950 }
12951 break;
12952 case LPFC_WCQ:
12953 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080012954 if ((cq->subtype == LPFC_FCP) ||
12955 (cq->subtype == LPFC_NVME))
12956 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040012957 cqe);
12958 else
12959 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12960 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012961 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012962 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12963 }
James Smartb84daac2012-08-03 12:35:13 -040012964
12965 /* Track the max number of CQEs processed in 1 EQ */
12966 if (ecount > cq->CQ_max_cqe)
12967 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012968 break;
12969 default:
12970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12971 "0370 Invalid completion queue type (%d)\n",
12972 cq->type);
12973 return;
12974 }
12975
12976 /* Catch the no cq entry condition, log an error */
12977 if (unlikely(ecount == 0))
12978 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12979 "0371 No entry from the CQ: identifier "
12980 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12981
12982 /* In any case, flash and re-arm the RCQ */
12983 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12984
12985 /* wake up worker thread if there are works to be done */
12986 if (workposted)
12987 lpfc_worker_wake_up(phba);
12988}
12989
12990/**
12991 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012992 * @phba: Pointer to HBA context object.
12993 * @cq: Pointer to associated CQ
12994 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012995 *
12996 * This routine process a fast-path work queue completion entry from fast-path
12997 * event queue for FCP command response completion.
12998 **/
12999static void
James Smart2a76a282012-08-03 12:35:54 -040013000lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013001 struct lpfc_wcqe_complete *wcqe)
13002{
James Smart2a76a282012-08-03 12:35:54 -040013003 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013004 struct lpfc_iocbq *cmdiocbq;
13005 struct lpfc_iocbq irspiocbq;
13006 unsigned long iflags;
13007
James Smart4f774512009-05-22 14:52:35 -040013008 /* Check for response status */
13009 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13010 /* If resource errors reported from HBA, reduce queue
13011 * depth of the SCSI device.
13012 */
James Smarte3d2b802012-08-14 14:25:43 -040013013 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13014 IOSTAT_LOCAL_REJECT)) &&
13015 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13016 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013017 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013018
James Smart4f774512009-05-22 14:52:35 -040013019 /* Log the error status */
13020 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13021 "0373 FCP complete error: status=x%x, "
13022 "hw_status=x%x, total_data_specified=%d, "
13023 "parameter=x%x, word3=x%x\n",
13024 bf_get(lpfc_wcqe_c_status, wcqe),
13025 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13026 wcqe->total_data_placed, wcqe->parameter,
13027 wcqe->word3);
13028 }
13029
13030 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013031 spin_lock_irqsave(&pring->ring_lock, iflags);
13032 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013033 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13034 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013035 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013036 if (unlikely(!cmdiocbq)) {
13037 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13038 "0374 FCP complete with no corresponding "
13039 "cmdiocb: iotag (%d)\n",
13040 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13041 return;
13042 }
James Smart895427b2017-02-12 13:52:30 -080013043
13044 if (cq->assoc_qp)
13045 cmdiocbq->isr_timestamp =
13046 cq->assoc_qp->isr_timestamp;
13047
13048 if (cmdiocbq->iocb_cmpl == NULL) {
13049 if (cmdiocbq->wqe_cmpl) {
13050 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13051 spin_lock_irqsave(&phba->hbalock, iflags);
13052 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13053 spin_unlock_irqrestore(&phba->hbalock, iflags);
13054 }
13055
13056 /* Pass the cmd_iocb and the wcqe to the upper layer */
13057 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13058 return;
13059 }
James Smart4f774512009-05-22 14:52:35 -040013060 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13061 "0375 FCP cmdiocb not callback function "
13062 "iotag: (%d)\n",
13063 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13064 return;
13065 }
13066
13067 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013068 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013069
James Smart0f65ff62010-02-26 14:14:23 -050013070 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13071 spin_lock_irqsave(&phba->hbalock, iflags);
13072 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13073 spin_unlock_irqrestore(&phba->hbalock, iflags);
13074 }
13075
James Smart4f774512009-05-22 14:52:35 -040013076 /* Pass the cmd_iocb and the rsp state to the upper layer */
13077 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13078}
13079
13080/**
13081 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13082 * @phba: Pointer to HBA context object.
13083 * @cq: Pointer to completion queue.
13084 * @wcqe: Pointer to work-queue completion queue entry.
13085 *
13086 * This routine handles an fast-path WQ entry comsumed event by invoking the
13087 * proper WQ release routine to the slow-path WQ.
13088 **/
13089static void
13090lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13091 struct lpfc_wcqe_release *wcqe)
13092{
13093 struct lpfc_queue *childwq;
13094 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013095 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013096
13097 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013098 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013099 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013100 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013101 lpfc_sli4_wq_release(childwq,
13102 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13103 wqid_matched = true;
13104 break;
13105 }
13106 }
13107 /* Report warning log message if no match found */
13108 if (wqid_matched != true)
13109 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13110 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013111 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013112}
13113
13114/**
James Smart2d7dbc42017-02-12 13:52:35 -080013115 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13116 * @phba: Pointer to HBA context object.
13117 * @rcqe: Pointer to receive-queue completion queue entry.
13118 *
13119 * This routine process a receive-queue completion queue entry.
13120 *
13121 * Return: true if work posted to worker thread, otherwise false.
13122 **/
13123static bool
13124lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13125 struct lpfc_rcqe *rcqe)
13126{
13127 bool workposted = false;
13128 struct lpfc_queue *hrq;
13129 struct lpfc_queue *drq;
13130 struct rqb_dmabuf *dma_buf;
13131 struct fc_frame_header *fc_hdr;
13132 uint32_t status, rq_id;
13133 unsigned long iflags;
13134 uint32_t fctl, idx;
13135
13136 if ((phba->nvmet_support == 0) ||
13137 (phba->sli4_hba.nvmet_cqset == NULL))
13138 return workposted;
13139
13140 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13141 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13142 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13143
13144 /* sanity check on queue memory */
13145 if (unlikely(!hrq) || unlikely(!drq))
13146 return workposted;
13147
13148 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13149 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13150 else
13151 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13152
13153 if ((phba->nvmet_support == 0) ||
13154 (rq_id != hrq->queue_id))
13155 return workposted;
13156
13157 status = bf_get(lpfc_rcqe_status, rcqe);
13158 switch (status) {
13159 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13160 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13161 "6126 Receive Frame Truncated!!\n");
13162 hrq->RQ_buf_trunc++;
13163 break;
13164 case FC_STATUS_RQ_SUCCESS:
13165 lpfc_sli4_rq_release(hrq, drq);
13166 spin_lock_irqsave(&phba->hbalock, iflags);
13167 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13168 if (!dma_buf) {
13169 hrq->RQ_no_buf_found++;
13170 spin_unlock_irqrestore(&phba->hbalock, iflags);
13171 goto out;
13172 }
13173 spin_unlock_irqrestore(&phba->hbalock, iflags);
13174 hrq->RQ_rcv_buf++;
13175 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13176
13177 /* Just some basic sanity checks on FCP Command frame */
13178 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13179 fc_hdr->fh_f_ctl[1] << 8 |
13180 fc_hdr->fh_f_ctl[2]);
13181 if (((fctl &
13182 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13183 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13184 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13185 goto drop;
13186
13187 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13188 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013189 lpfc_nvmet_unsol_fcp_event(
13190 phba, phba->sli4_hba.els_wq->pring, dma_buf,
13191 cq->assoc_qp->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013192 return false;
13193 }
13194drop:
13195 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13196 break;
13197 case FC_STATUS_INSUFF_BUF_NEED_BUF:
13198 case FC_STATUS_INSUFF_BUF_FRM_DISC:
13199 hrq->RQ_no_posted_buf++;
13200 /* Post more buffers if possible */
13201 spin_lock_irqsave(&phba->hbalock, iflags);
13202 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13203 spin_unlock_irqrestore(&phba->hbalock, iflags);
13204 workposted = true;
13205 break;
13206 }
13207out:
13208 return workposted;
13209}
13210
13211/**
James Smart895427b2017-02-12 13:52:30 -080013212 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013213 * @cq: Pointer to the completion queue.
13214 * @eqe: Pointer to fast-path completion queue entry.
13215 *
13216 * This routine process a fast-path work queue completion entry from fast-path
13217 * event queue for FCP command response completion.
13218 **/
13219static int
James Smart895427b2017-02-12 13:52:30 -080013220lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013221 struct lpfc_cqe *cqe)
13222{
13223 struct lpfc_wcqe_release wcqe;
13224 bool workposted = false;
13225
13226 /* Copy the work queue CQE and convert endian order if needed */
13227 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13228
13229 /* Check and process for different type of WCQE and dispatch */
13230 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13231 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013232 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013233 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013234 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013235 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013236 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13237 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13238 (struct lpfc_wcqe_complete *)&wcqe);
13239 if (cq->subtype == LPFC_NVME_LS)
13240 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013241 (struct lpfc_wcqe_complete *)&wcqe);
13242 break;
13243 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013244 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013245 /* Process the WQ release event */
13246 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13247 (struct lpfc_wcqe_release *)&wcqe);
13248 break;
13249 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013250 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013251 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013252 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013253 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13254 (struct sli4_wcqe_xri_aborted *)&wcqe);
13255 break;
James Smart895427b2017-02-12 13:52:30 -080013256 case CQE_CODE_RECEIVE_V1:
13257 case CQE_CODE_RECEIVE:
13258 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013259 if (cq->subtype == LPFC_NVMET) {
13260 workposted = lpfc_sli4_nvmet_handle_rcqe(
13261 phba, cq, (struct lpfc_rcqe *)&wcqe);
13262 }
James Smart895427b2017-02-12 13:52:30 -080013263 break;
James Smart4f774512009-05-22 14:52:35 -040013264 default:
13265 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013266 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013267 bf_get(lpfc_wcqe_c_code, &wcqe));
13268 break;
13269 }
13270 return workposted;
13271}
13272
13273/**
James Smart67d12732012-08-03 12:36:13 -040013274 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013275 * @phba: Pointer to HBA context object.
13276 * @eqe: Pointer to fast-path event queue entry.
13277 *
13278 * This routine process a event queue entry from the fast-path event queue.
13279 * It will check the MajorCode and MinorCode to determine this is for a
13280 * completion event on a completion queue, if not, an error shall be logged
13281 * and just return. Otherwise, it will get to the corresponding completion
13282 * queue and process all the entries on the completion queue, rearm the
13283 * completion queue, and then return.
13284 **/
13285static void
James Smart67d12732012-08-03 12:36:13 -040013286lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13287 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013288{
James Smart895427b2017-02-12 13:52:30 -080013289 struct lpfc_queue *cq = NULL;
James Smart4f774512009-05-22 14:52:35 -040013290 struct lpfc_cqe *cqe;
13291 bool workposted = false;
James Smart2d7dbc42017-02-12 13:52:35 -080013292 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013293 int ecount = 0;
13294
James Smartcb5172e2010-03-15 11:25:07 -040013295 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013296 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013297 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013298 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013299 bf_get_le32(lpfc_eqe_major_code, eqe),
13300 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040013301 return;
13302 }
13303
James Smart67d12732012-08-03 12:36:13 -040013304 /* Get the reference to the corresponding CQ */
13305 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13306
James Smart2d7dbc42017-02-12 13:52:35 -080013307 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13308 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13309 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13310 /* Process NVMET unsol rcv */
13311 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13312 goto process_cq;
13313 }
13314 }
13315
James Smart895427b2017-02-12 13:52:30 -080013316 if (phba->sli4_hba.nvme_cq_map &&
13317 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013318 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013319 cq = phba->sli4_hba.nvme_cq[qidx];
13320 goto process_cq;
13321 }
13322
13323 if (phba->sli4_hba.fcp_cq_map &&
13324 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13325 /* Process FCP command completion */
13326 cq = phba->sli4_hba.fcp_cq[qidx];
13327 goto process_cq;
13328 }
13329
13330 if (phba->sli4_hba.nvmels_cq &&
13331 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13332 /* Process NVME unsol rcv */
13333 cq = phba->sli4_hba.nvmels_cq;
13334 }
13335
13336 /* Otherwise this is a Slow path event */
13337 if (cq == NULL) {
13338 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013339 return;
13340 }
13341
James Smart895427b2017-02-12 13:52:30 -080013342process_cq:
James Smart4f774512009-05-22 14:52:35 -040013343 if (unlikely(cqid != cq->queue_id)) {
13344 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13345 "0368 Miss-matched fast-path completion "
13346 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13347 cqid, cq->queue_id);
13348 return;
13349 }
13350
James Smart895427b2017-02-12 13:52:30 -080013351 /* Save EQ associated with this CQ */
13352 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13353
James Smart4f774512009-05-22 14:52:35 -040013354 /* Process all the entries to the CQ */
13355 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013356 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013357 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040013358 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13359 }
13360
James Smartb84daac2012-08-03 12:35:13 -040013361 /* Track the max number of CQEs processed in 1 EQ */
13362 if (ecount > cq->CQ_max_cqe)
13363 cq->CQ_max_cqe = ecount;
13364
James Smart4f774512009-05-22 14:52:35 -040013365 /* Catch the no cq entry condition */
13366 if (unlikely(ecount == 0))
13367 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13368 "0369 No entry from fast-path completion "
13369 "queue fcpcqid=%d\n", cq->queue_id);
13370
13371 /* In any case, flash and re-arm the CQ */
13372 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13373
13374 /* wake up worker thread if there are works to be done */
13375 if (workposted)
13376 lpfc_worker_wake_up(phba);
13377}
13378
13379static void
13380lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13381{
13382 struct lpfc_eqe *eqe;
13383
13384 /* walk all the EQ entries and drop on the floor */
13385 while ((eqe = lpfc_sli4_eq_get(eq)))
13386 ;
13387
13388 /* Clear and re-arm the EQ */
13389 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13390}
13391
James Smart1ba981f2014-02-20 09:56:45 -050013392
13393/**
13394 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13395 * entry
13396 * @phba: Pointer to HBA context object.
13397 * @eqe: Pointer to fast-path event queue entry.
13398 *
13399 * This routine process a event queue entry from the Flash Optimized Fabric
13400 * event queue. It will check the MajorCode and MinorCode to determine this
13401 * is for a completion event on a completion queue, if not, an error shall be
13402 * logged and just return. Otherwise, it will get to the corresponding
13403 * completion queue and process all the entries on the completion queue, rearm
13404 * the completion queue, and then return.
13405 **/
13406static void
13407lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13408{
13409 struct lpfc_queue *cq;
13410 struct lpfc_cqe *cqe;
13411 bool workposted = false;
13412 uint16_t cqid;
13413 int ecount = 0;
13414
13415 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13416 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13417 "9147 Not a valid completion "
13418 "event: majorcode=x%x, minorcode=x%x\n",
13419 bf_get_le32(lpfc_eqe_major_code, eqe),
13420 bf_get_le32(lpfc_eqe_minor_code, eqe));
13421 return;
13422 }
13423
13424 /* Get the reference to the corresponding CQ */
13425 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13426
13427 /* Next check for OAS */
13428 cq = phba->sli4_hba.oas_cq;
13429 if (unlikely(!cq)) {
13430 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13431 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13432 "9148 OAS completion queue "
13433 "does not exist\n");
13434 return;
13435 }
13436
13437 if (unlikely(cqid != cq->queue_id)) {
13438 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13439 "9149 Miss-matched fast-path compl "
13440 "queue id: eqcqid=%d, fcpcqid=%d\n",
13441 cqid, cq->queue_id);
13442 return;
13443 }
13444
13445 /* Process all the entries to the OAS CQ */
13446 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013447 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart1ba981f2014-02-20 09:56:45 -050013448 if (!(++ecount % cq->entry_repost))
13449 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13450 }
13451
13452 /* Track the max number of CQEs processed in 1 EQ */
13453 if (ecount > cq->CQ_max_cqe)
13454 cq->CQ_max_cqe = ecount;
13455
13456 /* Catch the no cq entry condition */
13457 if (unlikely(ecount == 0))
13458 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13459 "9153 No entry from fast-path completion "
13460 "queue fcpcqid=%d\n", cq->queue_id);
13461
13462 /* In any case, flash and re-arm the CQ */
13463 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13464
13465 /* wake up worker thread if there are works to be done */
13466 if (workposted)
13467 lpfc_worker_wake_up(phba);
13468}
13469
13470/**
13471 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13472 * @irq: Interrupt number.
13473 * @dev_id: The device context pointer.
13474 *
13475 * This function is directly called from the PCI layer as an interrupt
13476 * service routine when device with SLI-4 interface spec is enabled with
13477 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13478 * IOCB ring event in the HBA. However, when the device is enabled with either
13479 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13480 * device-level interrupt handler. When the PCI slot is in error recovery
13481 * or the HBA is undergoing initialization, the interrupt handler will not
13482 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13483 * the intrrupt context. This function is called without any lock held.
13484 * It gets the hbalock to access and update SLI data structures. Note that,
13485 * the EQ to CQ are one-to-one map such that the EQ index is
13486 * equal to that of CQ index.
13487 *
13488 * This function returns IRQ_HANDLED when interrupt is handled else it
13489 * returns IRQ_NONE.
13490 **/
13491irqreturn_t
13492lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13493{
13494 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013495 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013496 struct lpfc_queue *eq;
13497 struct lpfc_eqe *eqe;
13498 unsigned long iflag;
13499 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013500
13501 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013502 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13503 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013504
13505 if (unlikely(!phba))
13506 return IRQ_NONE;
13507
13508 /* Get to the EQ struct associated with this vector */
13509 eq = phba->sli4_hba.fof_eq;
13510 if (unlikely(!eq))
13511 return IRQ_NONE;
13512
13513 /* Check device state for handling interrupt */
13514 if (unlikely(lpfc_intr_state_check(phba))) {
13515 eq->EQ_badstate++;
13516 /* Check again for link_state with lock held */
13517 spin_lock_irqsave(&phba->hbalock, iflag);
13518 if (phba->link_state < LPFC_LINK_DOWN)
13519 /* Flush, clear interrupt, and rearm the EQ */
13520 lpfc_sli4_eq_flush(phba, eq);
13521 spin_unlock_irqrestore(&phba->hbalock, iflag);
13522 return IRQ_NONE;
13523 }
13524
13525 /*
13526 * Process all the event on FCP fast-path EQ
13527 */
13528 while ((eqe = lpfc_sli4_eq_get(eq))) {
13529 lpfc_sli4_fof_handle_eqe(phba, eqe);
13530 if (!(++ecount % eq->entry_repost))
13531 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
13532 eq->EQ_processed++;
13533 }
13534
13535 /* Track the max number of EQEs processed in 1 intr */
13536 if (ecount > eq->EQ_max_eqe)
13537 eq->EQ_max_eqe = ecount;
13538
13539
13540 if (unlikely(ecount == 0)) {
13541 eq->EQ_no_entry++;
13542
13543 if (phba->intr_type == MSIX)
13544 /* MSI-X treated interrupt served as no EQ share INT */
13545 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13546 "9145 MSI-X interrupt with no EQE\n");
13547 else {
13548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13549 "9146 ISR interrupt with no EQE\n");
13550 /* Non MSI-X treated on interrupt as EQ share INT */
13551 return IRQ_NONE;
13552 }
13553 }
13554 /* Always clear and re-arm the fast-path EQ */
13555 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13556 return IRQ_HANDLED;
13557}
13558
James Smart4f774512009-05-22 14:52:35 -040013559/**
James Smart67d12732012-08-03 12:36:13 -040013560 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013561 * @irq: Interrupt number.
13562 * @dev_id: The device context pointer.
13563 *
13564 * This function is directly called from the PCI layer as an interrupt
13565 * service routine when device with SLI-4 interface spec is enabled with
13566 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13567 * ring event in the HBA. However, when the device is enabled with either
13568 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13569 * device-level interrupt handler. When the PCI slot is in error recovery
13570 * or the HBA is undergoing initialization, the interrupt handler will not
13571 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13572 * the intrrupt context. This function is called without any lock held.
13573 * It gets the hbalock to access and update SLI data structures. Note that,
13574 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13575 * equal to that of FCP CQ index.
13576 *
James Smart67d12732012-08-03 12:36:13 -040013577 * The link attention and ELS ring attention events are handled
13578 * by the worker thread. The interrupt handler signals the worker thread
13579 * and returns for these events. This function is called without any lock
13580 * held. It gets the hbalock to access and update SLI data structures.
13581 *
James Smart4f774512009-05-22 14:52:35 -040013582 * This function returns IRQ_HANDLED when interrupt is handled else it
13583 * returns IRQ_NONE.
13584 **/
13585irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013586lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013587{
13588 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013589 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013590 struct lpfc_queue *fpeq;
13591 struct lpfc_eqe *eqe;
13592 unsigned long iflag;
13593 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013594 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013595
13596 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013597 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13598 phba = hba_eq_hdl->phba;
13599 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013600
13601 if (unlikely(!phba))
13602 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013603 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013604 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013605
13606 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013607 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013608 if (unlikely(!fpeq))
13609 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013610
James Smartbd2cdd52017-02-12 13:52:33 -080013611#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13612 if (phba->ktime_on)
13613 fpeq->isr_timestamp = ktime_get_ns();
13614#endif
13615
James Smartba20c852012-08-03 12:36:52 -040013616 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013617 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013618 lpfc_sli4_eq_clr_intr(fpeq);
13619 else {
James Smart895427b2017-02-12 13:52:30 -080013620 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013621 return IRQ_NONE;
13622 }
13623 }
13624
James Smart4f774512009-05-22 14:52:35 -040013625 /* Check device state for handling interrupt */
13626 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040013627 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040013628 /* Check again for link_state with lock held */
13629 spin_lock_irqsave(&phba->hbalock, iflag);
13630 if (phba->link_state < LPFC_LINK_DOWN)
13631 /* Flush, clear interrupt, and rearm the EQ */
13632 lpfc_sli4_eq_flush(phba, fpeq);
13633 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013634 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013635 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013636 return IRQ_NONE;
13637 }
13638
13639 /*
13640 * Process all the event on FCP fast-path EQ
13641 */
13642 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040013643 if (eqe == NULL)
13644 break;
13645
James Smart895427b2017-02-12 13:52:30 -080013646 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040013647 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040013648 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040013649 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013650 }
13651
James Smartb84daac2012-08-03 12:35:13 -040013652 /* Track the max number of EQEs processed in 1 intr */
13653 if (ecount > fpeq->EQ_max_eqe)
13654 fpeq->EQ_max_eqe = ecount;
13655
James Smart4f774512009-05-22 14:52:35 -040013656 /* Always clear and re-arm the fast-path EQ */
13657 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13658
13659 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013660 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013661
13662 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013663 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013664 return IRQ_NONE;
13665 }
13666
James Smart4f774512009-05-22 14:52:35 -040013667 if (phba->intr_type == MSIX)
13668 /* MSI-X treated interrupt served as no EQ share INT */
13669 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13670 "0358 MSI-X interrupt with no EQE\n");
13671 else
13672 /* Non MSI-X treated on interrupt as EQ share INT */
13673 return IRQ_NONE;
13674 }
13675
James Smartba20c852012-08-03 12:36:52 -040013676 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013677 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13678
James Smart4f774512009-05-22 14:52:35 -040013679 return IRQ_HANDLED;
13680} /* lpfc_sli4_fp_intr_handler */
13681
13682/**
13683 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13684 * @irq: Interrupt number.
13685 * @dev_id: The device context pointer.
13686 *
13687 * This function is the device-level interrupt handler to device with SLI-4
13688 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13689 * interrupt mode is enabled and there is an event in the HBA which requires
13690 * driver attention. This function invokes the slow-path interrupt attention
13691 * handling function and fast-path interrupt attention handling function in
13692 * turn to process the relevant HBA attention events. This function is called
13693 * without any lock held. It gets the hbalock to access and update SLI data
13694 * structures.
13695 *
13696 * This function returns IRQ_HANDLED when interrupt is handled, else it
13697 * returns IRQ_NONE.
13698 **/
13699irqreturn_t
13700lpfc_sli4_intr_handler(int irq, void *dev_id)
13701{
13702 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013703 irqreturn_t hba_irq_rc;
13704 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013705 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013706
13707 /* Get the driver's phba structure from the dev_id */
13708 phba = (struct lpfc_hba *)dev_id;
13709
13710 if (unlikely(!phba))
13711 return IRQ_NONE;
13712
13713 /*
James Smart4f774512009-05-22 14:52:35 -040013714 * Invoke fast-path host attention interrupt handling as appropriate.
13715 */
James Smart895427b2017-02-12 13:52:30 -080013716 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013717 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013718 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013719 if (hba_irq_rc == IRQ_HANDLED)
13720 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013721 }
13722
James Smart1ba981f2014-02-20 09:56:45 -050013723 if (phba->cfg_fof) {
13724 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013725 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013726 if (hba_irq_rc == IRQ_HANDLED)
13727 hba_handled |= true;
13728 }
13729
James Smart67d12732012-08-03 12:36:13 -040013730 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013731} /* lpfc_sli4_intr_handler */
13732
13733/**
13734 * lpfc_sli4_queue_free - free a queue structure and associated memory
13735 * @queue: The queue structure to free.
13736 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013737 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013738 * the host resident queue. This function must be called after destroying the
13739 * queue on the HBA.
13740 **/
13741void
13742lpfc_sli4_queue_free(struct lpfc_queue *queue)
13743{
13744 struct lpfc_dmabuf *dmabuf;
13745
13746 if (!queue)
13747 return;
13748
13749 while (!list_empty(&queue->page_list)) {
13750 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13751 list);
James Smart49198b32010-04-06 15:04:33 -040013752 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040013753 dmabuf->virt, dmabuf->phys);
13754 kfree(dmabuf);
13755 }
James Smart895427b2017-02-12 13:52:30 -080013756 if (queue->rqbp) {
13757 lpfc_free_rq_buffer(queue->phba, queue);
13758 kfree(queue->rqbp);
13759 }
13760 kfree(queue->pring);
James Smart4f774512009-05-22 14:52:35 -040013761 kfree(queue);
13762 return;
13763}
13764
13765/**
13766 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13767 * @phba: The HBA that this queue is being created on.
13768 * @entry_size: The size of each queue entry for this queue.
13769 * @entry count: The number of entries that this queue will handle.
13770 *
13771 * This function allocates a queue structure and the DMAable memory used for
13772 * the host resident queue. This function must be called before creating the
13773 * queue on the HBA.
13774 **/
13775struct lpfc_queue *
13776lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13777 uint32_t entry_count)
13778{
13779 struct lpfc_queue *queue;
13780 struct lpfc_dmabuf *dmabuf;
13781 int x, total_qe_count;
13782 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013783 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013784
James Smartcb5172e2010-03-15 11:25:07 -040013785 if (!phba->sli4_hba.pc_sli4_params.supported)
13786 hw_page_size = SLI4_PAGE_SIZE;
13787
James Smart4f774512009-05-22 14:52:35 -040013788 queue = kzalloc(sizeof(struct lpfc_queue) +
13789 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13790 if (!queue)
13791 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013792 queue->page_count = (ALIGN(entry_size * entry_count,
13793 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013794
13795 /* If needed, Adjust page count to match the max the adapter supports */
13796 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13797 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13798
James Smart4f774512009-05-22 14:52:35 -040013799 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013800 INIT_LIST_HEAD(&queue->wq_list);
James Smart4f774512009-05-22 14:52:35 -040013801 INIT_LIST_HEAD(&queue->page_list);
13802 INIT_LIST_HEAD(&queue->child_list);
13803 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13804 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13805 if (!dmabuf)
13806 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013807 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13808 hw_page_size, &dmabuf->phys,
13809 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013810 if (!dmabuf->virt) {
13811 kfree(dmabuf);
13812 goto out_fail;
13813 }
13814 dmabuf->buffer_tag = x;
13815 list_add_tail(&dmabuf->list, &queue->page_list);
13816 /* initialize queue's entry array */
13817 dma_pointer = dmabuf->virt;
13818 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013819 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013820 total_qe_count++, dma_pointer += entry_size) {
13821 queue->qe[total_qe_count].address = dma_pointer;
13822 }
13823 }
13824 queue->entry_size = entry_size;
13825 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040013826
13827 /*
13828 * entry_repost is calculated based on the number of entries in the
13829 * queue. This works out except for RQs. If buffers are NOT initially
13830 * posted for every RQE, entry_repost should be adjusted accordingly.
13831 */
13832 queue->entry_repost = (entry_count >> 3);
13833 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13834 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013835 queue->phba = phba;
13836
13837 return queue;
13838out_fail:
13839 lpfc_sli4_queue_free(queue);
13840 return NULL;
13841}
13842
13843/**
James Smart962bc512013-01-03 15:44:00 -050013844 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13845 * @phba: HBA structure that indicates port to create a queue on.
13846 * @pci_barset: PCI BAR set flag.
13847 *
13848 * This function shall perform iomap of the specified PCI BAR address to host
13849 * memory address if not already done so and return it. The returned host
13850 * memory address can be NULL.
13851 */
13852static void __iomem *
13853lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13854{
James Smart962bc512013-01-03 15:44:00 -050013855 if (!phba->pcidev)
13856 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013857
13858 switch (pci_barset) {
13859 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013860 return phba->pci_bar0_memmap_p;
13861 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013862 return phba->pci_bar2_memmap_p;
13863 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013864 return phba->pci_bar4_memmap_p;
13865 default:
13866 break;
13867 }
13868 return NULL;
13869}
13870
13871/**
James Smart895427b2017-02-12 13:52:30 -080013872 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040013873 * @phba: HBA structure that indicates port to create a queue on.
13874 * @startq: The starting FCP EQ to modify
13875 *
13876 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13877 *
13878 * The @phba struct is used to send mailbox command to HBA. The @startq
13879 * is used to get the starting FCP EQ to change.
13880 * This function is asynchronous and will wait for the mailbox
13881 * command to finish before continuing.
13882 *
13883 * On success this function will return a zero. If unable to allocate enough
13884 * memory this function will return -ENOMEM. If the queue create mailbox command
13885 * fails this function will return -ENXIO.
13886 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013887int
James Smart895427b2017-02-12 13:52:30 -080013888lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013889{
13890 struct lpfc_mbx_modify_eq_delay *eq_delay;
13891 LPFC_MBOXQ_t *mbox;
13892 struct lpfc_queue *eq;
13893 int cnt, rc, length, status = 0;
13894 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013895 uint32_t result;
James Smart895427b2017-02-12 13:52:30 -080013896 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040013897 union lpfc_sli4_cfg_shdr *shdr;
13898 uint16_t dmult;
13899
James Smart895427b2017-02-12 13:52:30 -080013900 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040013901 return 0;
13902
13903 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13904 if (!mbox)
13905 return -ENOMEM;
13906 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13907 sizeof(struct lpfc_sli4_cfg_mhdr));
13908 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13909 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13910 length, LPFC_SLI4_MBX_EMBED);
13911 eq_delay = &mbox->u.mqe.un.eq_delay;
13912
13913 /* Calculate delay multiper from maximum interrupt per second */
James Smart895427b2017-02-12 13:52:30 -080013914 result = phba->cfg_fcp_imax / phba->io_channel_irqs;
13915 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040013916 dmult = 0;
13917 else
13918 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013919
13920 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080013921 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
13922 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040013923 if (!eq)
13924 continue;
13925 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13926 eq_delay->u.request.eq[cnt].phase = 0;
13927 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13928 cnt++;
13929 if (cnt >= LPFC_MAX_EQ_DELAY)
13930 break;
13931 }
13932 eq_delay->u.request.num_eq = cnt;
13933
13934 mbox->vport = phba->pport;
13935 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13936 mbox->context1 = NULL;
13937 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13938 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13939 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13940 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13941 if (shdr_status || shdr_add_status || rc) {
13942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13943 "2512 MODIFY_EQ_DELAY mailbox failed with "
13944 "status x%x add_status x%x, mbx status x%x\n",
13945 shdr_status, shdr_add_status, rc);
13946 status = -ENXIO;
13947 }
13948 mempool_free(mbox, phba->mbox_mem_pool);
13949 return status;
13950}
13951
13952/**
James Smart4f774512009-05-22 14:52:35 -040013953 * lpfc_eq_create - Create an Event Queue on the HBA
13954 * @phba: HBA structure that indicates port to create a queue on.
13955 * @eq: The queue structure to use to create the event queue.
13956 * @imax: The maximum interrupt per second limit.
13957 *
13958 * This function creates an event queue, as detailed in @eq, on a port,
13959 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13960 *
13961 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13962 * is used to get the entry count and entry size that are necessary to
13963 * determine the number of pages to allocate and use for this queue. This
13964 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13965 * event queue. This function is asynchronous and will wait for the mailbox
13966 * command to finish before continuing.
13967 *
13968 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013969 * memory this function will return -ENOMEM. If the queue create mailbox command
13970 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013971 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013972int
James Smartee020062012-09-29 11:28:52 -040013973lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013974{
13975 struct lpfc_mbx_eq_create *eq_create;
13976 LPFC_MBOXQ_t *mbox;
13977 int rc, length, status = 0;
13978 struct lpfc_dmabuf *dmabuf;
13979 uint32_t shdr_status, shdr_add_status;
13980 union lpfc_sli4_cfg_shdr *shdr;
13981 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013982 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13983
James Smart2e90f4b2011-12-13 13:22:37 -050013984 /* sanity check on queue memory */
13985 if (!eq)
13986 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013987 if (!phba->sli4_hba.pc_sli4_params.supported)
13988 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013989
13990 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13991 if (!mbox)
13992 return -ENOMEM;
13993 length = (sizeof(struct lpfc_mbx_eq_create) -
13994 sizeof(struct lpfc_sli4_cfg_mhdr));
13995 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13996 LPFC_MBOX_OPCODE_EQ_CREATE,
13997 length, LPFC_SLI4_MBX_EMBED);
13998 eq_create = &mbox->u.mqe.un.eq_create;
13999 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14000 eq->page_count);
14001 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14002 LPFC_EQE_SIZE);
14003 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040014004 /* don't setup delay multiplier using EQ_CREATE */
14005 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014006 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14007 dmult);
14008 switch (eq->entry_count) {
14009 default:
14010 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14011 "0360 Unsupported EQ count. (%d)\n",
14012 eq->entry_count);
14013 if (eq->entry_count < 256)
14014 return -EINVAL;
14015 /* otherwise default to smallest count (drop through) */
14016 case 256:
14017 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14018 LPFC_EQ_CNT_256);
14019 break;
14020 case 512:
14021 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14022 LPFC_EQ_CNT_512);
14023 break;
14024 case 1024:
14025 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14026 LPFC_EQ_CNT_1024);
14027 break;
14028 case 2048:
14029 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14030 LPFC_EQ_CNT_2048);
14031 break;
14032 case 4096:
14033 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14034 LPFC_EQ_CNT_4096);
14035 break;
14036 }
14037 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014038 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014039 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14040 putPaddrLow(dmabuf->phys);
14041 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14042 putPaddrHigh(dmabuf->phys);
14043 }
14044 mbox->vport = phba->pport;
14045 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14046 mbox->context1 = NULL;
14047 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14048 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14049 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14050 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14051 if (shdr_status || shdr_add_status || rc) {
14052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14053 "2500 EQ_CREATE mailbox failed with "
14054 "status x%x add_status x%x, mbx status x%x\n",
14055 shdr_status, shdr_add_status, rc);
14056 status = -ENXIO;
14057 }
14058 eq->type = LPFC_EQ;
14059 eq->subtype = LPFC_NONE;
14060 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14061 if (eq->queue_id == 0xFFFF)
14062 status = -ENXIO;
14063 eq->host_index = 0;
14064 eq->hba_index = 0;
14065
James Smart8fa38512009-07-19 10:01:03 -040014066 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014067 return status;
14068}
14069
14070/**
14071 * lpfc_cq_create - Create a Completion Queue on the HBA
14072 * @phba: HBA structure that indicates port to create a queue on.
14073 * @cq: The queue structure to use to create the completion queue.
14074 * @eq: The event queue to bind this completion queue to.
14075 *
14076 * This function creates a completion queue, as detailed in @wq, on a port,
14077 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14078 *
14079 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14080 * is used to get the entry count and entry size that are necessary to
14081 * determine the number of pages to allocate and use for this queue. The @eq
14082 * is used to indicate which event queue to bind this completion queue to. This
14083 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14084 * completion queue. This function is asynchronous and will wait for the mailbox
14085 * command to finish before continuing.
14086 *
14087 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014088 * memory this function will return -ENOMEM. If the queue create mailbox command
14089 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014090 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014091int
James Smart4f774512009-05-22 14:52:35 -040014092lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14093 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14094{
14095 struct lpfc_mbx_cq_create *cq_create;
14096 struct lpfc_dmabuf *dmabuf;
14097 LPFC_MBOXQ_t *mbox;
14098 int rc, length, status = 0;
14099 uint32_t shdr_status, shdr_add_status;
14100 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014101 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14102
James Smart2e90f4b2011-12-13 13:22:37 -050014103 /* sanity check on queue memory */
14104 if (!cq || !eq)
14105 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014106 if (!phba->sli4_hba.pc_sli4_params.supported)
14107 hw_page_size = SLI4_PAGE_SIZE;
14108
James Smart4f774512009-05-22 14:52:35 -040014109 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14110 if (!mbox)
14111 return -ENOMEM;
14112 length = (sizeof(struct lpfc_mbx_cq_create) -
14113 sizeof(struct lpfc_sli4_cfg_mhdr));
14114 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14115 LPFC_MBOX_OPCODE_CQ_CREATE,
14116 length, LPFC_SLI4_MBX_EMBED);
14117 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014118 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014119 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14120 cq->page_count);
14121 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14122 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014123 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14124 phba->sli4_hba.pc_sli4_params.cqv);
14125 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040014126 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
14127 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014128 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14129 eq->queue_id);
14130 } else {
14131 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14132 eq->queue_id);
14133 }
James Smart4f774512009-05-22 14:52:35 -040014134 switch (cq->entry_count) {
14135 default:
14136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014137 "0361 Unsupported CQ count: "
14138 "entry cnt %d sz %d pg cnt %d repost %d\n",
14139 cq->entry_count, cq->entry_size,
14140 cq->page_count, cq->entry_repost);
James Smart4f4c1862012-06-12 13:54:02 -040014141 if (cq->entry_count < 256) {
14142 status = -EINVAL;
14143 goto out;
14144 }
James Smart4f774512009-05-22 14:52:35 -040014145 /* otherwise default to smallest count (drop through) */
14146 case 256:
14147 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14148 LPFC_CQ_CNT_256);
14149 break;
14150 case 512:
14151 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14152 LPFC_CQ_CNT_512);
14153 break;
14154 case 1024:
14155 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14156 LPFC_CQ_CNT_1024);
14157 break;
14158 }
14159 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014160 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014161 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14162 putPaddrLow(dmabuf->phys);
14163 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14164 putPaddrHigh(dmabuf->phys);
14165 }
14166 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14167
14168 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014169 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14170 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14171 if (shdr_status || shdr_add_status || rc) {
14172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14173 "2501 CQ_CREATE mailbox failed with "
14174 "status x%x add_status x%x, mbx status x%x\n",
14175 shdr_status, shdr_add_status, rc);
14176 status = -ENXIO;
14177 goto out;
14178 }
14179 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14180 if (cq->queue_id == 0xFFFF) {
14181 status = -ENXIO;
14182 goto out;
14183 }
14184 /* link the cq onto the parent eq child list */
14185 list_add_tail(&cq->list, &eq->child_list);
14186 /* Set up completion queue's type and subtype */
14187 cq->type = type;
14188 cq->subtype = subtype;
14189 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014190 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014191 cq->host_index = 0;
14192 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040014193
James Smart8fa38512009-07-19 10:01:03 -040014194out:
14195 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014196 return status;
14197}
14198
14199/**
James Smart2d7dbc42017-02-12 13:52:35 -080014200 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14201 * @phba: HBA structure that indicates port to create a queue on.
14202 * @cqp: The queue structure array to use to create the completion queues.
14203 * @eqp: The event queue array to bind these completion queues to.
14204 *
14205 * This function creates a set of completion queue, s to support MRQ
14206 * as detailed in @cqp, on a port,
14207 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14208 *
14209 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14210 * is used to get the entry count and entry size that are necessary to
14211 * determine the number of pages to allocate and use for this queue. The @eq
14212 * is used to indicate which event queue to bind this completion queue to. This
14213 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14214 * completion queue. This function is asynchronous and will wait for the mailbox
14215 * command to finish before continuing.
14216 *
14217 * On success this function will return a zero. If unable to allocate enough
14218 * memory this function will return -ENOMEM. If the queue create mailbox command
14219 * fails this function will return -ENXIO.
14220 **/
14221int
14222lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14223 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14224{
14225 struct lpfc_queue *cq;
14226 struct lpfc_queue *eq;
14227 struct lpfc_mbx_cq_create_set *cq_set;
14228 struct lpfc_dmabuf *dmabuf;
14229 LPFC_MBOXQ_t *mbox;
14230 int rc, length, alloclen, status = 0;
14231 int cnt, idx, numcq, page_idx = 0;
14232 uint32_t shdr_status, shdr_add_status;
14233 union lpfc_sli4_cfg_shdr *shdr;
14234 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14235
14236 /* sanity check on queue memory */
14237 numcq = phba->cfg_nvmet_mrq;
14238 if (!cqp || !eqp || !numcq)
14239 return -ENODEV;
14240 if (!phba->sli4_hba.pc_sli4_params.supported)
14241 hw_page_size = SLI4_PAGE_SIZE;
14242
14243 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14244 if (!mbox)
14245 return -ENOMEM;
14246
14247 length = sizeof(struct lpfc_mbx_cq_create_set);
14248 length += ((numcq * cqp[0]->page_count) *
14249 sizeof(struct dma_address));
14250 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14251 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14252 LPFC_SLI4_MBX_NEMBED);
14253 if (alloclen < length) {
14254 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14255 "3098 Allocated DMA memory size (%d) is "
14256 "less than the requested DMA memory size "
14257 "(%d)\n", alloclen, length);
14258 status = -ENOMEM;
14259 goto out;
14260 }
14261 cq_set = mbox->sge_array->addr[0];
14262 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14263 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14264
14265 for (idx = 0; idx < numcq; idx++) {
14266 cq = cqp[idx];
14267 eq = eqp[idx];
14268 if (!cq || !eq) {
14269 status = -ENOMEM;
14270 goto out;
14271 }
14272
14273 switch (idx) {
14274 case 0:
14275 bf_set(lpfc_mbx_cq_create_set_page_size,
14276 &cq_set->u.request,
14277 (hw_page_size / SLI4_PAGE_SIZE));
14278 bf_set(lpfc_mbx_cq_create_set_num_pages,
14279 &cq_set->u.request, cq->page_count);
14280 bf_set(lpfc_mbx_cq_create_set_evt,
14281 &cq_set->u.request, 1);
14282 bf_set(lpfc_mbx_cq_create_set_valid,
14283 &cq_set->u.request, 1);
14284 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14285 &cq_set->u.request, 0);
14286 bf_set(lpfc_mbx_cq_create_set_num_cq,
14287 &cq_set->u.request, numcq);
14288 switch (cq->entry_count) {
14289 default:
14290 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14291 "3118 Bad CQ count. (%d)\n",
14292 cq->entry_count);
14293 if (cq->entry_count < 256) {
14294 status = -EINVAL;
14295 goto out;
14296 }
14297 /* otherwise default to smallest (drop thru) */
14298 case 256:
14299 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14300 &cq_set->u.request, LPFC_CQ_CNT_256);
14301 break;
14302 case 512:
14303 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14304 &cq_set->u.request, LPFC_CQ_CNT_512);
14305 break;
14306 case 1024:
14307 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14308 &cq_set->u.request, LPFC_CQ_CNT_1024);
14309 break;
14310 }
14311 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14312 &cq_set->u.request, eq->queue_id);
14313 break;
14314 case 1:
14315 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14316 &cq_set->u.request, eq->queue_id);
14317 break;
14318 case 2:
14319 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14320 &cq_set->u.request, eq->queue_id);
14321 break;
14322 case 3:
14323 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14324 &cq_set->u.request, eq->queue_id);
14325 break;
14326 case 4:
14327 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14328 &cq_set->u.request, eq->queue_id);
14329 break;
14330 case 5:
14331 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14332 &cq_set->u.request, eq->queue_id);
14333 break;
14334 case 6:
14335 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14336 &cq_set->u.request, eq->queue_id);
14337 break;
14338 case 7:
14339 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14340 &cq_set->u.request, eq->queue_id);
14341 break;
14342 case 8:
14343 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14344 &cq_set->u.request, eq->queue_id);
14345 break;
14346 case 9:
14347 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14348 &cq_set->u.request, eq->queue_id);
14349 break;
14350 case 10:
14351 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14352 &cq_set->u.request, eq->queue_id);
14353 break;
14354 case 11:
14355 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14356 &cq_set->u.request, eq->queue_id);
14357 break;
14358 case 12:
14359 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14360 &cq_set->u.request, eq->queue_id);
14361 break;
14362 case 13:
14363 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14364 &cq_set->u.request, eq->queue_id);
14365 break;
14366 case 14:
14367 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14368 &cq_set->u.request, eq->queue_id);
14369 break;
14370 case 15:
14371 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14372 &cq_set->u.request, eq->queue_id);
14373 break;
14374 }
14375
14376 /* link the cq onto the parent eq child list */
14377 list_add_tail(&cq->list, &eq->child_list);
14378 /* Set up completion queue's type and subtype */
14379 cq->type = type;
14380 cq->subtype = subtype;
14381 cq->assoc_qid = eq->queue_id;
14382 cq->host_index = 0;
14383 cq->hba_index = 0;
14384
14385 rc = 0;
14386 list_for_each_entry(dmabuf, &cq->page_list, list) {
14387 memset(dmabuf->virt, 0, hw_page_size);
14388 cnt = page_idx + dmabuf->buffer_tag;
14389 cq_set->u.request.page[cnt].addr_lo =
14390 putPaddrLow(dmabuf->phys);
14391 cq_set->u.request.page[cnt].addr_hi =
14392 putPaddrHigh(dmabuf->phys);
14393 rc++;
14394 }
14395 page_idx += rc;
14396 }
14397
14398 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14399
14400 /* The IOCTL status is embedded in the mailbox subheader. */
14401 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14402 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14403 if (shdr_status || shdr_add_status || rc) {
14404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14405 "3119 CQ_CREATE_SET mailbox failed with "
14406 "status x%x add_status x%x, mbx status x%x\n",
14407 shdr_status, shdr_add_status, rc);
14408 status = -ENXIO;
14409 goto out;
14410 }
14411 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14412 if (rc == 0xFFFF) {
14413 status = -ENXIO;
14414 goto out;
14415 }
14416
14417 for (idx = 0; idx < numcq; idx++) {
14418 cq = cqp[idx];
14419 cq->queue_id = rc + idx;
14420 }
14421
14422out:
14423 lpfc_sli4_mbox_cmd_free(phba, mbox);
14424 return status;
14425}
14426
14427/**
James Smartb19a0612010-04-06 14:48:51 -040014428 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014429 * @phba: HBA structure that indicates port to create a queue on.
14430 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014431 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14432 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014433 *
James Smartb19a0612010-04-06 14:48:51 -040014434 * This function provides failback (fb) functionality when the
14435 * mq_create_ext fails on older FW generations. It's purpose is identical
14436 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014437 *
James Smartb19a0612010-04-06 14:48:51 -040014438 * This routine cannot fail as all attributes were previously accessed and
14439 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014440 **/
James Smartb19a0612010-04-06 14:48:51 -040014441static void
14442lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14443 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014444{
14445 struct lpfc_mbx_mq_create *mq_create;
14446 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014447 int length;
James Smart04c68492009-05-22 14:52:52 -040014448
James Smart04c68492009-05-22 14:52:52 -040014449 length = (sizeof(struct lpfc_mbx_mq_create) -
14450 sizeof(struct lpfc_sli4_cfg_mhdr));
14451 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14452 LPFC_MBOX_OPCODE_MQ_CREATE,
14453 length, LPFC_SLI4_MBX_EMBED);
14454 mq_create = &mbox->u.mqe.un.mq_create;
14455 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014456 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014457 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014458 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014459 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14460 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014461 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014462 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14463 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014464 break;
14465 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014466 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14467 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014468 break;
14469 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014470 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14471 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014472 break;
14473 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014474 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14475 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014476 break;
14477 }
14478 list_for_each_entry(dmabuf, &mq->page_list, list) {
14479 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014480 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014481 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014482 putPaddrHigh(dmabuf->phys);
14483 }
14484}
14485
14486/**
14487 * lpfc_mq_create - Create a mailbox Queue on the HBA
14488 * @phba: HBA structure that indicates port to create a queue on.
14489 * @mq: The queue structure to use to create the mailbox queue.
14490 * @cq: The completion queue to associate with this cq.
14491 * @subtype: The queue's subtype.
14492 *
14493 * This function creates a mailbox queue, as detailed in @mq, on a port,
14494 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14495 *
14496 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14497 * is used to get the entry count and entry size that are necessary to
14498 * determine the number of pages to allocate and use for this queue. This
14499 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14500 * mailbox queue. This function is asynchronous and will wait for the mailbox
14501 * command to finish before continuing.
14502 *
14503 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014504 * memory this function will return -ENOMEM. If the queue create mailbox command
14505 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014506 **/
14507int32_t
14508lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14509 struct lpfc_queue *cq, uint32_t subtype)
14510{
14511 struct lpfc_mbx_mq_create *mq_create;
14512 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14513 struct lpfc_dmabuf *dmabuf;
14514 LPFC_MBOXQ_t *mbox;
14515 int rc, length, status = 0;
14516 uint32_t shdr_status, shdr_add_status;
14517 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014518 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014519
James Smart2e90f4b2011-12-13 13:22:37 -050014520 /* sanity check on queue memory */
14521 if (!mq || !cq)
14522 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014523 if (!phba->sli4_hba.pc_sli4_params.supported)
14524 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014525
14526 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14527 if (!mbox)
14528 return -ENOMEM;
14529 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14530 sizeof(struct lpfc_sli4_cfg_mhdr));
14531 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14532 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14533 length, LPFC_SLI4_MBX_EMBED);
14534
14535 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014536 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014537 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14538 &mq_create_ext->u.request, mq->page_count);
14539 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14540 &mq_create_ext->u.request, 1);
14541 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014542 &mq_create_ext->u.request, 1);
14543 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14544 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014545 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14546 &mq_create_ext->u.request, 1);
14547 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14548 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014549 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014550 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14551 phba->sli4_hba.pc_sli4_params.mqv);
14552 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14553 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14554 cq->queue_id);
14555 else
14556 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14557 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014558 switch (mq->entry_count) {
14559 default:
14560 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14561 "0362 Unsupported MQ count. (%d)\n",
14562 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014563 if (mq->entry_count < 16) {
14564 status = -EINVAL;
14565 goto out;
14566 }
James Smartb19a0612010-04-06 14:48:51 -040014567 /* otherwise default to smallest count (drop through) */
14568 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014569 bf_set(lpfc_mq_context_ring_size,
14570 &mq_create_ext->u.request.context,
14571 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014572 break;
14573 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014574 bf_set(lpfc_mq_context_ring_size,
14575 &mq_create_ext->u.request.context,
14576 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014577 break;
14578 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014579 bf_set(lpfc_mq_context_ring_size,
14580 &mq_create_ext->u.request.context,
14581 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014582 break;
14583 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014584 bf_set(lpfc_mq_context_ring_size,
14585 &mq_create_ext->u.request.context,
14586 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014587 break;
14588 }
14589 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014590 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014591 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14592 putPaddrLow(dmabuf->phys);
14593 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014594 putPaddrHigh(dmabuf->phys);
14595 }
14596 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014597 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14598 &mq_create_ext->u.response);
14599 if (rc != MBX_SUCCESS) {
14600 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14601 "2795 MQ_CREATE_EXT failed with "
14602 "status x%x. Failback to MQ_CREATE.\n",
14603 rc);
14604 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14605 mq_create = &mbox->u.mqe.un.mq_create;
14606 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14607 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14608 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14609 &mq_create->u.response);
14610 }
14611
James Smart04c68492009-05-22 14:52:52 -040014612 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014613 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14614 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14615 if (shdr_status || shdr_add_status || rc) {
14616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14617 "2502 MQ_CREATE mailbox failed with "
14618 "status x%x add_status x%x, mbx status x%x\n",
14619 shdr_status, shdr_add_status, rc);
14620 status = -ENXIO;
14621 goto out;
14622 }
James Smart04c68492009-05-22 14:52:52 -040014623 if (mq->queue_id == 0xFFFF) {
14624 status = -ENXIO;
14625 goto out;
14626 }
14627 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014628 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014629 mq->subtype = subtype;
14630 mq->host_index = 0;
14631 mq->hba_index = 0;
14632
14633 /* link the mq onto the parent cq child list */
14634 list_add_tail(&mq->list, &cq->child_list);
14635out:
James Smart8fa38512009-07-19 10:01:03 -040014636 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014637 return status;
14638}
14639
14640/**
James Smart4f774512009-05-22 14:52:35 -040014641 * lpfc_wq_create - Create a Work Queue on the HBA
14642 * @phba: HBA structure that indicates port to create a queue on.
14643 * @wq: The queue structure to use to create the work queue.
14644 * @cq: The completion queue to bind this work queue to.
14645 * @subtype: The subtype of the work queue indicating its functionality.
14646 *
14647 * This function creates a work queue, as detailed in @wq, on a port, described
14648 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14649 *
14650 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14651 * is used to get the entry count and entry size that are necessary to
14652 * determine the number of pages to allocate and use for this queue. The @cq
14653 * is used to indicate which completion queue to bind this work queue to. This
14654 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14655 * work queue. This function is asynchronous and will wait for the mailbox
14656 * command to finish before continuing.
14657 *
14658 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014659 * memory this function will return -ENOMEM. If the queue create mailbox command
14660 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014661 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014662int
James Smart4f774512009-05-22 14:52:35 -040014663lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14664 struct lpfc_queue *cq, uint32_t subtype)
14665{
14666 struct lpfc_mbx_wq_create *wq_create;
14667 struct lpfc_dmabuf *dmabuf;
14668 LPFC_MBOXQ_t *mbox;
14669 int rc, length, status = 0;
14670 uint32_t shdr_status, shdr_add_status;
14671 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014672 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014673 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014674 void __iomem *bar_memmap_p;
14675 uint32_t db_offset;
14676 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014677
James Smart2e90f4b2011-12-13 13:22:37 -050014678 /* sanity check on queue memory */
14679 if (!wq || !cq)
14680 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014681 if (!phba->sli4_hba.pc_sli4_params.supported)
14682 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014683
14684 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14685 if (!mbox)
14686 return -ENOMEM;
14687 length = (sizeof(struct lpfc_mbx_wq_create) -
14688 sizeof(struct lpfc_sli4_cfg_mhdr));
14689 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14690 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14691 length, LPFC_SLI4_MBX_EMBED);
14692 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014693 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014694 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14695 wq->page_count);
14696 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14697 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014698
14699 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014700 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14701 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014702
James Smart0c651872013-07-15 18:33:23 -040014703 switch (phba->sli4_hba.pc_sli4_params.wqv) {
14704 case LPFC_Q_CREATE_VERSION_0:
14705 switch (wq->entry_size) {
14706 default:
14707 case 64:
14708 /* Nothing to do, version 0 ONLY supports 64 byte */
14709 page = wq_create->u.request.page;
14710 break;
14711 case 128:
14712 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14713 LPFC_WQ_SZ128_SUPPORT)) {
14714 status = -ERANGE;
14715 goto out;
14716 }
14717 /* If we get here the HBA MUST also support V1 and
14718 * we MUST use it
14719 */
14720 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14721 LPFC_Q_CREATE_VERSION_1);
14722
14723 bf_set(lpfc_mbx_wq_create_wqe_count,
14724 &wq_create->u.request_1, wq->entry_count);
14725 bf_set(lpfc_mbx_wq_create_wqe_size,
14726 &wq_create->u.request_1,
14727 LPFC_WQ_WQE_SIZE_128);
14728 bf_set(lpfc_mbx_wq_create_page_size,
14729 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014730 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014731 page = wq_create->u.request_1.page;
14732 break;
14733 }
14734 break;
14735 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014736 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14737 wq->entry_count);
14738 switch (wq->entry_size) {
14739 default:
14740 case 64:
14741 bf_set(lpfc_mbx_wq_create_wqe_size,
14742 &wq_create->u.request_1,
14743 LPFC_WQ_WQE_SIZE_64);
14744 break;
14745 case 128:
James Smart0c651872013-07-15 18:33:23 -040014746 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14747 LPFC_WQ_SZ128_SUPPORT)) {
14748 status = -ERANGE;
14749 goto out;
14750 }
James Smart5a6f1332011-03-11 16:05:35 -050014751 bf_set(lpfc_mbx_wq_create_wqe_size,
14752 &wq_create->u.request_1,
14753 LPFC_WQ_WQE_SIZE_128);
14754 break;
14755 }
James Smart8ea73db2017-02-12 13:52:25 -080014756 bf_set(lpfc_mbx_wq_create_page_size,
14757 &wq_create->u.request_1,
14758 LPFC_WQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014759 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040014760 break;
14761 default:
14762 status = -ERANGE;
14763 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050014764 }
James Smart0c651872013-07-15 18:33:23 -040014765
James Smart4f774512009-05-22 14:52:35 -040014766 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014767 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050014768 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14769 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040014770 }
James Smart962bc512013-01-03 15:44:00 -050014771
14772 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14773 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14774
James Smart4f774512009-05-22 14:52:35 -040014775 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14776 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014777 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14778 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14779 if (shdr_status || shdr_add_status || rc) {
14780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14781 "2503 WQ_CREATE mailbox failed with "
14782 "status x%x add_status x%x, mbx status x%x\n",
14783 shdr_status, shdr_add_status, rc);
14784 status = -ENXIO;
14785 goto out;
14786 }
14787 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14788 if (wq->queue_id == 0xFFFF) {
14789 status = -ENXIO;
14790 goto out;
14791 }
James Smart962bc512013-01-03 15:44:00 -050014792 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14793 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14794 &wq_create->u.response);
14795 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14796 (wq->db_format != LPFC_DB_RING_FORMAT)) {
14797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14798 "3265 WQ[%d] doorbell format not "
14799 "supported: x%x\n", wq->queue_id,
14800 wq->db_format);
14801 status = -EINVAL;
14802 goto out;
14803 }
14804 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14805 &wq_create->u.response);
14806 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14807 if (!bar_memmap_p) {
14808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14809 "3263 WQ[%d] failed to memmap pci "
14810 "barset:x%x\n", wq->queue_id,
14811 pci_barset);
14812 status = -ENOMEM;
14813 goto out;
14814 }
14815 db_offset = wq_create->u.response.doorbell_offset;
14816 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14817 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14818 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14819 "3252 WQ[%d] doorbell offset not "
14820 "supported: x%x\n", wq->queue_id,
14821 db_offset);
14822 status = -EINVAL;
14823 goto out;
14824 }
14825 wq->db_regaddr = bar_memmap_p + db_offset;
14826 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014827 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14828 "format:x%x\n", wq->queue_id, pci_barset,
14829 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014830 } else {
14831 wq->db_format = LPFC_DB_LIST_FORMAT;
14832 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14833 }
James Smart895427b2017-02-12 13:52:30 -080014834 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14835 if (wq->pring == NULL) {
14836 status = -ENOMEM;
14837 goto out;
14838 }
James Smart4f774512009-05-22 14:52:35 -040014839 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050014840 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014841 wq->subtype = subtype;
14842 wq->host_index = 0;
14843 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050014844 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040014845
14846 /* link the wq onto the parent cq child list */
14847 list_add_tail(&wq->list, &cq->child_list);
14848out:
James Smart8fa38512009-07-19 10:01:03 -040014849 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014850 return status;
14851}
14852
14853/**
James Smart73d91e52011-10-10 21:32:10 -040014854 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
14855 * @phba: HBA structure that indicates port to create a queue on.
14856 * @rq: The queue structure to use for the receive queue.
14857 * @qno: The associated HBQ number
14858 *
14859 *
14860 * For SLI4 we need to adjust the RQ repost value based on
14861 * the number of buffers that are initially posted to the RQ.
14862 */
14863void
14864lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
14865{
14866 uint32_t cnt;
14867
James Smart2e90f4b2011-12-13 13:22:37 -050014868 /* sanity check on queue memory */
14869 if (!rq)
14870 return;
James Smart73d91e52011-10-10 21:32:10 -040014871 cnt = lpfc_hbq_defs[qno]->entry_count;
14872
14873 /* Recalc repost for RQs based on buffers initially posted */
14874 cnt = (cnt >> 3);
14875 if (cnt < LPFC_QUEUE_MIN_REPOST)
14876 cnt = LPFC_QUEUE_MIN_REPOST;
14877
14878 rq->entry_repost = cnt;
14879}
14880
14881/**
James Smart4f774512009-05-22 14:52:35 -040014882 * lpfc_rq_create - Create a Receive Queue on the HBA
14883 * @phba: HBA structure that indicates port to create a queue on.
14884 * @hrq: The queue structure to use to create the header receive queue.
14885 * @drq: The queue structure to use to create the data receive queue.
14886 * @cq: The completion queue to bind this work queue to.
14887 *
14888 * This function creates a receive buffer queue pair , as detailed in @hrq and
14889 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14890 * to the HBA.
14891 *
14892 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14893 * struct is used to get the entry count that is necessary to determine the
14894 * number of pages to use for this queue. The @cq is used to indicate which
14895 * completion queue to bind received buffers that are posted to these queues to.
14896 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14897 * receive queue pair. This function is asynchronous and will wait for the
14898 * mailbox command to finish before continuing.
14899 *
14900 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014901 * memory this function will return -ENOMEM. If the queue create mailbox command
14902 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014903 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014904int
James Smart4f774512009-05-22 14:52:35 -040014905lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14906 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14907{
14908 struct lpfc_mbx_rq_create *rq_create;
14909 struct lpfc_dmabuf *dmabuf;
14910 LPFC_MBOXQ_t *mbox;
14911 int rc, length, status = 0;
14912 uint32_t shdr_status, shdr_add_status;
14913 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014914 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050014915 void __iomem *bar_memmap_p;
14916 uint32_t db_offset;
14917 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014918
James Smart2e90f4b2011-12-13 13:22:37 -050014919 /* sanity check on queue memory */
14920 if (!hrq || !drq || !cq)
14921 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014922 if (!phba->sli4_hba.pc_sli4_params.supported)
14923 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014924
14925 if (hrq->entry_count != drq->entry_count)
14926 return -EINVAL;
14927 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14928 if (!mbox)
14929 return -ENOMEM;
14930 length = (sizeof(struct lpfc_mbx_rq_create) -
14931 sizeof(struct lpfc_sli4_cfg_mhdr));
14932 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14933 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14934 length, LPFC_SLI4_MBX_EMBED);
14935 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014936 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14937 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14938 phba->sli4_hba.pc_sli4_params.rqv);
14939 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14940 bf_set(lpfc_rq_context_rqe_count_1,
14941 &rq_create->u.request.context,
14942 hrq->entry_count);
14943 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014944 bf_set(lpfc_rq_context_rqe_size,
14945 &rq_create->u.request.context,
14946 LPFC_RQE_SIZE_8);
14947 bf_set(lpfc_rq_context_page_size,
14948 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080014949 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014950 } else {
14951 switch (hrq->entry_count) {
14952 default:
14953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14954 "2535 Unsupported RQ count. (%d)\n",
14955 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014956 if (hrq->entry_count < 512) {
14957 status = -EINVAL;
14958 goto out;
14959 }
James Smart5a6f1332011-03-11 16:05:35 -050014960 /* otherwise default to smallest count (drop through) */
14961 case 512:
14962 bf_set(lpfc_rq_context_rqe_count,
14963 &rq_create->u.request.context,
14964 LPFC_RQ_RING_SIZE_512);
14965 break;
14966 case 1024:
14967 bf_set(lpfc_rq_context_rqe_count,
14968 &rq_create->u.request.context,
14969 LPFC_RQ_RING_SIZE_1024);
14970 break;
14971 case 2048:
14972 bf_set(lpfc_rq_context_rqe_count,
14973 &rq_create->u.request.context,
14974 LPFC_RQ_RING_SIZE_2048);
14975 break;
14976 case 4096:
14977 bf_set(lpfc_rq_context_rqe_count,
14978 &rq_create->u.request.context,
14979 LPFC_RQ_RING_SIZE_4096);
14980 break;
14981 }
14982 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14983 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014984 }
14985 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14986 cq->queue_id);
14987 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14988 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014989 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014990 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014991 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14992 putPaddrLow(dmabuf->phys);
14993 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14994 putPaddrHigh(dmabuf->phys);
14995 }
James Smart962bc512013-01-03 15:44:00 -050014996 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14997 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
14998
James Smart4f774512009-05-22 14:52:35 -040014999 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15000 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015001 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15002 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15003 if (shdr_status || shdr_add_status || rc) {
15004 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15005 "2504 RQ_CREATE mailbox failed with "
15006 "status x%x add_status x%x, mbx status x%x\n",
15007 shdr_status, shdr_add_status, rc);
15008 status = -ENXIO;
15009 goto out;
15010 }
15011 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15012 if (hrq->queue_id == 0xFFFF) {
15013 status = -ENXIO;
15014 goto out;
15015 }
James Smart962bc512013-01-03 15:44:00 -050015016
15017 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15018 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15019 &rq_create->u.response);
15020 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15021 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15022 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15023 "3262 RQ [%d] doorbell format not "
15024 "supported: x%x\n", hrq->queue_id,
15025 hrq->db_format);
15026 status = -EINVAL;
15027 goto out;
15028 }
15029
15030 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15031 &rq_create->u.response);
15032 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15033 if (!bar_memmap_p) {
15034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15035 "3269 RQ[%d] failed to memmap pci "
15036 "barset:x%x\n", hrq->queue_id,
15037 pci_barset);
15038 status = -ENOMEM;
15039 goto out;
15040 }
15041
15042 db_offset = rq_create->u.response.doorbell_offset;
15043 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15044 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15046 "3270 RQ[%d] doorbell offset not "
15047 "supported: x%x\n", hrq->queue_id,
15048 db_offset);
15049 status = -EINVAL;
15050 goto out;
15051 }
15052 hrq->db_regaddr = bar_memmap_p + db_offset;
15053 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015054 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15055 "format:x%x\n", hrq->queue_id, pci_barset,
15056 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015057 } else {
15058 hrq->db_format = LPFC_DB_RING_FORMAT;
15059 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15060 }
James Smart4f774512009-05-22 14:52:35 -040015061 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015062 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015063 hrq->subtype = subtype;
15064 hrq->host_index = 0;
15065 hrq->hba_index = 0;
15066
15067 /* now create the data queue */
15068 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15069 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15070 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015071 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15072 phba->sli4_hba.pc_sli4_params.rqv);
15073 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15074 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015075 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050015076 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015077 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15078 LPFC_RQE_SIZE_8);
15079 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15080 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015081 } else {
15082 switch (drq->entry_count) {
15083 default:
15084 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15085 "2536 Unsupported RQ count. (%d)\n",
15086 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015087 if (drq->entry_count < 512) {
15088 status = -EINVAL;
15089 goto out;
15090 }
James Smart5a6f1332011-03-11 16:05:35 -050015091 /* otherwise default to smallest count (drop through) */
15092 case 512:
15093 bf_set(lpfc_rq_context_rqe_count,
15094 &rq_create->u.request.context,
15095 LPFC_RQ_RING_SIZE_512);
15096 break;
15097 case 1024:
15098 bf_set(lpfc_rq_context_rqe_count,
15099 &rq_create->u.request.context,
15100 LPFC_RQ_RING_SIZE_1024);
15101 break;
15102 case 2048:
15103 bf_set(lpfc_rq_context_rqe_count,
15104 &rq_create->u.request.context,
15105 LPFC_RQ_RING_SIZE_2048);
15106 break;
15107 case 4096:
15108 bf_set(lpfc_rq_context_rqe_count,
15109 &rq_create->u.request.context,
15110 LPFC_RQ_RING_SIZE_4096);
15111 break;
15112 }
15113 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15114 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015115 }
15116 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15117 cq->queue_id);
15118 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15119 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015120 list_for_each_entry(dmabuf, &drq->page_list, list) {
15121 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15122 putPaddrLow(dmabuf->phys);
15123 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15124 putPaddrHigh(dmabuf->phys);
15125 }
James Smart962bc512013-01-03 15:44:00 -050015126 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15127 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015128 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15129 /* The IOCTL status is embedded in the mailbox subheader. */
15130 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15131 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15132 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15133 if (shdr_status || shdr_add_status || rc) {
15134 status = -ENXIO;
15135 goto out;
15136 }
15137 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15138 if (drq->queue_id == 0xFFFF) {
15139 status = -ENXIO;
15140 goto out;
15141 }
15142 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015143 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015144 drq->subtype = subtype;
15145 drq->host_index = 0;
15146 drq->hba_index = 0;
15147
15148 /* link the header and data RQs onto the parent cq child list */
15149 list_add_tail(&hrq->list, &cq->child_list);
15150 list_add_tail(&drq->list, &cq->child_list);
15151
15152out:
James Smart8fa38512009-07-19 10:01:03 -040015153 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015154 return status;
15155}
15156
15157/**
James Smart2d7dbc42017-02-12 13:52:35 -080015158 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15159 * @phba: HBA structure that indicates port to create a queue on.
15160 * @hrqp: The queue structure array to use to create the header receive queues.
15161 * @drqp: The queue structure array to use to create the data receive queues.
15162 * @cqp: The completion queue array to bind these receive queues to.
15163 *
15164 * This function creates a receive buffer queue pair , as detailed in @hrq and
15165 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15166 * to the HBA.
15167 *
15168 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15169 * struct is used to get the entry count that is necessary to determine the
15170 * number of pages to use for this queue. The @cq is used to indicate which
15171 * completion queue to bind received buffers that are posted to these queues to.
15172 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15173 * receive queue pair. This function is asynchronous and will wait for the
15174 * mailbox command to finish before continuing.
15175 *
15176 * On success this function will return a zero. If unable to allocate enough
15177 * memory this function will return -ENOMEM. If the queue create mailbox command
15178 * fails this function will return -ENXIO.
15179 **/
15180int
15181lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15182 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15183 uint32_t subtype)
15184{
15185 struct lpfc_queue *hrq, *drq, *cq;
15186 struct lpfc_mbx_rq_create_v2 *rq_create;
15187 struct lpfc_dmabuf *dmabuf;
15188 LPFC_MBOXQ_t *mbox;
15189 int rc, length, alloclen, status = 0;
15190 int cnt, idx, numrq, page_idx = 0;
15191 uint32_t shdr_status, shdr_add_status;
15192 union lpfc_sli4_cfg_shdr *shdr;
15193 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15194
15195 numrq = phba->cfg_nvmet_mrq;
15196 /* sanity check on array memory */
15197 if (!hrqp || !drqp || !cqp || !numrq)
15198 return -ENODEV;
15199 if (!phba->sli4_hba.pc_sli4_params.supported)
15200 hw_page_size = SLI4_PAGE_SIZE;
15201
15202 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15203 if (!mbox)
15204 return -ENOMEM;
15205
15206 length = sizeof(struct lpfc_mbx_rq_create_v2);
15207 length += ((2 * numrq * hrqp[0]->page_count) *
15208 sizeof(struct dma_address));
15209
15210 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15211 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15212 LPFC_SLI4_MBX_NEMBED);
15213 if (alloclen < length) {
15214 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15215 "3099 Allocated DMA memory size (%d) is "
15216 "less than the requested DMA memory size "
15217 "(%d)\n", alloclen, length);
15218 status = -ENOMEM;
15219 goto out;
15220 }
15221
15222
15223
15224 rq_create = mbox->sge_array->addr[0];
15225 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15226
15227 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15228 cnt = 0;
15229
15230 for (idx = 0; idx < numrq; idx++) {
15231 hrq = hrqp[idx];
15232 drq = drqp[idx];
15233 cq = cqp[idx];
15234
James Smart2d7dbc42017-02-12 13:52:35 -080015235 /* sanity check on queue memory */
15236 if (!hrq || !drq || !cq) {
15237 status = -ENODEV;
15238 goto out;
15239 }
15240
James Smart7aabe842017-03-04 09:30:22 -080015241 if (hrq->entry_count != drq->entry_count) {
15242 status = -EINVAL;
15243 goto out;
15244 }
15245
James Smart2d7dbc42017-02-12 13:52:35 -080015246 if (idx == 0) {
15247 bf_set(lpfc_mbx_rq_create_num_pages,
15248 &rq_create->u.request,
15249 hrq->page_count);
15250 bf_set(lpfc_mbx_rq_create_rq_cnt,
15251 &rq_create->u.request, (numrq * 2));
15252 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15253 1);
15254 bf_set(lpfc_rq_context_base_cq,
15255 &rq_create->u.request.context,
15256 cq->queue_id);
15257 bf_set(lpfc_rq_context_data_size,
15258 &rq_create->u.request.context,
15259 LPFC_DATA_BUF_SIZE);
15260 bf_set(lpfc_rq_context_hdr_size,
15261 &rq_create->u.request.context,
15262 LPFC_HDR_BUF_SIZE);
15263 bf_set(lpfc_rq_context_rqe_count_1,
15264 &rq_create->u.request.context,
15265 hrq->entry_count);
15266 bf_set(lpfc_rq_context_rqe_size,
15267 &rq_create->u.request.context,
15268 LPFC_RQE_SIZE_8);
15269 bf_set(lpfc_rq_context_page_size,
15270 &rq_create->u.request.context,
15271 (PAGE_SIZE/SLI4_PAGE_SIZE));
15272 }
15273 rc = 0;
15274 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15275 memset(dmabuf->virt, 0, hw_page_size);
15276 cnt = page_idx + dmabuf->buffer_tag;
15277 rq_create->u.request.page[cnt].addr_lo =
15278 putPaddrLow(dmabuf->phys);
15279 rq_create->u.request.page[cnt].addr_hi =
15280 putPaddrHigh(dmabuf->phys);
15281 rc++;
15282 }
15283 page_idx += rc;
15284
15285 rc = 0;
15286 list_for_each_entry(dmabuf, &drq->page_list, list) {
15287 memset(dmabuf->virt, 0, hw_page_size);
15288 cnt = page_idx + dmabuf->buffer_tag;
15289 rq_create->u.request.page[cnt].addr_lo =
15290 putPaddrLow(dmabuf->phys);
15291 rq_create->u.request.page[cnt].addr_hi =
15292 putPaddrHigh(dmabuf->phys);
15293 rc++;
15294 }
15295 page_idx += rc;
15296
15297 hrq->db_format = LPFC_DB_RING_FORMAT;
15298 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15299 hrq->type = LPFC_HRQ;
15300 hrq->assoc_qid = cq->queue_id;
15301 hrq->subtype = subtype;
15302 hrq->host_index = 0;
15303 hrq->hba_index = 0;
15304
15305 drq->db_format = LPFC_DB_RING_FORMAT;
15306 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15307 drq->type = LPFC_DRQ;
15308 drq->assoc_qid = cq->queue_id;
15309 drq->subtype = subtype;
15310 drq->host_index = 0;
15311 drq->hba_index = 0;
15312
15313 list_add_tail(&hrq->list, &cq->child_list);
15314 list_add_tail(&drq->list, &cq->child_list);
15315 }
15316
15317 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15318 /* The IOCTL status is embedded in the mailbox subheader. */
15319 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15320 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15321 if (shdr_status || shdr_add_status || rc) {
15322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15323 "3120 RQ_CREATE mailbox failed with "
15324 "status x%x add_status x%x, mbx status x%x\n",
15325 shdr_status, shdr_add_status, rc);
15326 status = -ENXIO;
15327 goto out;
15328 }
15329 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15330 if (rc == 0xFFFF) {
15331 status = -ENXIO;
15332 goto out;
15333 }
15334
15335 /* Initialize all RQs with associated queue id */
15336 for (idx = 0; idx < numrq; idx++) {
15337 hrq = hrqp[idx];
15338 hrq->queue_id = rc + (2 * idx);
15339 drq = drqp[idx];
15340 drq->queue_id = rc + (2 * idx) + 1;
15341 }
15342
15343out:
15344 lpfc_sli4_mbox_cmd_free(phba, mbox);
15345 return status;
15346}
15347
15348/**
James Smart4f774512009-05-22 14:52:35 -040015349 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15350 * @eq: The queue structure associated with the queue to destroy.
15351 *
15352 * This function destroys a queue, as detailed in @eq by sending an mailbox
15353 * command, specific to the type of queue, to the HBA.
15354 *
15355 * The @eq struct is used to get the queue ID of the queue to destroy.
15356 *
15357 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015358 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015359 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015360int
James Smart4f774512009-05-22 14:52:35 -040015361lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15362{
15363 LPFC_MBOXQ_t *mbox;
15364 int rc, length, status = 0;
15365 uint32_t shdr_status, shdr_add_status;
15366 union lpfc_sli4_cfg_shdr *shdr;
15367
James Smart2e90f4b2011-12-13 13:22:37 -050015368 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015369 if (!eq)
15370 return -ENODEV;
15371 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15372 if (!mbox)
15373 return -ENOMEM;
15374 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15375 sizeof(struct lpfc_sli4_cfg_mhdr));
15376 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15377 LPFC_MBOX_OPCODE_EQ_DESTROY,
15378 length, LPFC_SLI4_MBX_EMBED);
15379 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15380 eq->queue_id);
15381 mbox->vport = eq->phba->pport;
15382 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15383
15384 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15385 /* The IOCTL status is embedded in the mailbox subheader. */
15386 shdr = (union lpfc_sli4_cfg_shdr *)
15387 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15388 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15389 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15390 if (shdr_status || shdr_add_status || rc) {
15391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15392 "2505 EQ_DESTROY mailbox failed with "
15393 "status x%x add_status x%x, mbx status x%x\n",
15394 shdr_status, shdr_add_status, rc);
15395 status = -ENXIO;
15396 }
15397
15398 /* Remove eq from any list */
15399 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015400 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015401 return status;
15402}
15403
15404/**
15405 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15406 * @cq: The queue structure associated with the queue to destroy.
15407 *
15408 * This function destroys a queue, as detailed in @cq by sending an mailbox
15409 * command, specific to the type of queue, to the HBA.
15410 *
15411 * The @cq struct is used to get the queue ID of the queue to destroy.
15412 *
15413 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015414 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015415 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015416int
James Smart4f774512009-05-22 14:52:35 -040015417lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15418{
15419 LPFC_MBOXQ_t *mbox;
15420 int rc, length, status = 0;
15421 uint32_t shdr_status, shdr_add_status;
15422 union lpfc_sli4_cfg_shdr *shdr;
15423
James Smart2e90f4b2011-12-13 13:22:37 -050015424 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015425 if (!cq)
15426 return -ENODEV;
15427 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15428 if (!mbox)
15429 return -ENOMEM;
15430 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15431 sizeof(struct lpfc_sli4_cfg_mhdr));
15432 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15433 LPFC_MBOX_OPCODE_CQ_DESTROY,
15434 length, LPFC_SLI4_MBX_EMBED);
15435 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15436 cq->queue_id);
15437 mbox->vport = cq->phba->pport;
15438 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15439 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15440 /* The IOCTL status is embedded in the mailbox subheader. */
15441 shdr = (union lpfc_sli4_cfg_shdr *)
15442 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15443 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15444 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15445 if (shdr_status || shdr_add_status || rc) {
15446 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15447 "2506 CQ_DESTROY mailbox failed with "
15448 "status x%x add_status x%x, mbx status x%x\n",
15449 shdr_status, shdr_add_status, rc);
15450 status = -ENXIO;
15451 }
15452 /* Remove cq from any list */
15453 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015454 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015455 return status;
15456}
15457
15458/**
James Smart04c68492009-05-22 14:52:52 -040015459 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15460 * @qm: The queue structure associated with the queue to destroy.
15461 *
15462 * This function destroys a queue, as detailed in @mq by sending an mailbox
15463 * command, specific to the type of queue, to the HBA.
15464 *
15465 * The @mq struct is used to get the queue ID of the queue to destroy.
15466 *
15467 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015468 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015469 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015470int
James Smart04c68492009-05-22 14:52:52 -040015471lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15472{
15473 LPFC_MBOXQ_t *mbox;
15474 int rc, length, status = 0;
15475 uint32_t shdr_status, shdr_add_status;
15476 union lpfc_sli4_cfg_shdr *shdr;
15477
James Smart2e90f4b2011-12-13 13:22:37 -050015478 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015479 if (!mq)
15480 return -ENODEV;
15481 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15482 if (!mbox)
15483 return -ENOMEM;
15484 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15485 sizeof(struct lpfc_sli4_cfg_mhdr));
15486 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15487 LPFC_MBOX_OPCODE_MQ_DESTROY,
15488 length, LPFC_SLI4_MBX_EMBED);
15489 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15490 mq->queue_id);
15491 mbox->vport = mq->phba->pport;
15492 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15493 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15494 /* The IOCTL status is embedded in the mailbox subheader. */
15495 shdr = (union lpfc_sli4_cfg_shdr *)
15496 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15497 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15498 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15499 if (shdr_status || shdr_add_status || rc) {
15500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15501 "2507 MQ_DESTROY mailbox failed with "
15502 "status x%x add_status x%x, mbx status x%x\n",
15503 shdr_status, shdr_add_status, rc);
15504 status = -ENXIO;
15505 }
15506 /* Remove mq from any list */
15507 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015508 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015509 return status;
15510}
15511
15512/**
James Smart4f774512009-05-22 14:52:35 -040015513 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15514 * @wq: The queue structure associated with the queue to destroy.
15515 *
15516 * This function destroys a queue, as detailed in @wq by sending an mailbox
15517 * command, specific to the type of queue, to the HBA.
15518 *
15519 * The @wq struct is used to get the queue ID of the queue to destroy.
15520 *
15521 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015522 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015523 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015524int
James Smart4f774512009-05-22 14:52:35 -040015525lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15526{
15527 LPFC_MBOXQ_t *mbox;
15528 int rc, length, status = 0;
15529 uint32_t shdr_status, shdr_add_status;
15530 union lpfc_sli4_cfg_shdr *shdr;
15531
James Smart2e90f4b2011-12-13 13:22:37 -050015532 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015533 if (!wq)
15534 return -ENODEV;
15535 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15536 if (!mbox)
15537 return -ENOMEM;
15538 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15539 sizeof(struct lpfc_sli4_cfg_mhdr));
15540 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15541 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15542 length, LPFC_SLI4_MBX_EMBED);
15543 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15544 wq->queue_id);
15545 mbox->vport = wq->phba->pport;
15546 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15547 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15548 shdr = (union lpfc_sli4_cfg_shdr *)
15549 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15550 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15551 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15552 if (shdr_status || shdr_add_status || rc) {
15553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15554 "2508 WQ_DESTROY mailbox failed with "
15555 "status x%x add_status x%x, mbx status x%x\n",
15556 shdr_status, shdr_add_status, rc);
15557 status = -ENXIO;
15558 }
15559 /* Remove wq from any list */
15560 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040015561 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015562 return status;
15563}
15564
15565/**
15566 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15567 * @rq: The queue structure associated with the queue to destroy.
15568 *
15569 * This function destroys a queue, as detailed in @rq by sending an mailbox
15570 * command, specific to the type of queue, to the HBA.
15571 *
15572 * The @rq struct is used to get the queue ID of the queue to destroy.
15573 *
15574 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015575 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015576 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015577int
James Smart4f774512009-05-22 14:52:35 -040015578lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15579 struct lpfc_queue *drq)
15580{
15581 LPFC_MBOXQ_t *mbox;
15582 int rc, length, status = 0;
15583 uint32_t shdr_status, shdr_add_status;
15584 union lpfc_sli4_cfg_shdr *shdr;
15585
James Smart2e90f4b2011-12-13 13:22:37 -050015586 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015587 if (!hrq || !drq)
15588 return -ENODEV;
15589 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15590 if (!mbox)
15591 return -ENOMEM;
15592 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050015593 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040015594 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15595 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15596 length, LPFC_SLI4_MBX_EMBED);
15597 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15598 hrq->queue_id);
15599 mbox->vport = hrq->phba->pport;
15600 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15601 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15602 /* The IOCTL status is embedded in the mailbox subheader. */
15603 shdr = (union lpfc_sli4_cfg_shdr *)
15604 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15605 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15606 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15607 if (shdr_status || shdr_add_status || rc) {
15608 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15609 "2509 RQ_DESTROY mailbox failed with "
15610 "status x%x add_status x%x, mbx status x%x\n",
15611 shdr_status, shdr_add_status, rc);
15612 if (rc != MBX_TIMEOUT)
15613 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15614 return -ENXIO;
15615 }
15616 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15617 drq->queue_id);
15618 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15619 shdr = (union lpfc_sli4_cfg_shdr *)
15620 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15621 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15622 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15623 if (shdr_status || shdr_add_status || rc) {
15624 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15625 "2510 RQ_DESTROY mailbox failed with "
15626 "status x%x add_status x%x, mbx status x%x\n",
15627 shdr_status, shdr_add_status, rc);
15628 status = -ENXIO;
15629 }
15630 list_del_init(&hrq->list);
15631 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040015632 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015633 return status;
15634}
15635
15636/**
15637 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15638 * @phba: The virtual port for which this call being executed.
15639 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15640 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15641 * @xritag: the xritag that ties this io to the SGL pages.
15642 *
15643 * This routine will post the sgl pages for the IO that has the xritag
15644 * that is in the iocbq structure. The xritag is assigned during iocbq
15645 * creation and persists for as long as the driver is loaded.
15646 * if the caller has fewer than 256 scatter gather segments to map then
15647 * pdma_phys_addr1 should be 0.
15648 * If the caller needs to map more than 256 scatter gather segment then
15649 * pdma_phys_addr1 should be a valid physical address.
15650 * physical address for SGLs must be 64 byte aligned.
15651 * If you are going to map 2 SGL's then the first one must have 256 entries
15652 * the second sgl can have between 1 and 256 entries.
15653 *
15654 * Return codes:
15655 * 0 - Success
15656 * -ENXIO, -ENOMEM - Failure
15657 **/
15658int
15659lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15660 dma_addr_t pdma_phys_addr0,
15661 dma_addr_t pdma_phys_addr1,
15662 uint16_t xritag)
15663{
15664 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15665 LPFC_MBOXQ_t *mbox;
15666 int rc;
15667 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040015668 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040015669 union lpfc_sli4_cfg_shdr *shdr;
15670
15671 if (xritag == NO_XRI) {
15672 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15673 "0364 Invalid param:\n");
15674 return -EINVAL;
15675 }
15676
15677 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15678 if (!mbox)
15679 return -ENOMEM;
15680
15681 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15682 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15683 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050015684 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040015685
15686 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15687 &mbox->u.mqe.un.post_sgl_pages;
15688 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15689 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15690
15691 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15692 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15693 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15694 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15695
15696 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15697 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15698 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15699 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15700 if (!phba->sli4_hba.intr_enable)
15701 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040015702 else {
James Smarta183a152011-10-10 21:32:43 -040015703 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040015704 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15705 }
James Smart4f774512009-05-22 14:52:35 -040015706 /* The IOCTL status is embedded in the mailbox subheader. */
15707 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15708 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15709 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15710 if (rc != MBX_TIMEOUT)
15711 mempool_free(mbox, phba->mbox_mem_pool);
15712 if (shdr_status || shdr_add_status || rc) {
15713 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15714 "2511 POST_SGL mailbox failed with "
15715 "status x%x add_status x%x, mbx status x%x\n",
15716 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040015717 }
15718 return 0;
15719}
James Smart4f774512009-05-22 14:52:35 -040015720
15721/**
James Smart88a2cfb2011-07-22 18:36:33 -040015722 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040015723 * @phba: pointer to lpfc hba data structure.
15724 *
15725 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040015726 * HBA consistent with the SLI-4 interface spec. This routine
15727 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15728 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040015729 *
James Smart88a2cfb2011-07-22 18:36:33 -040015730 * Returns
15731 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15732 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15733 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015734static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015735lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15736{
15737 unsigned long xri;
15738
15739 /*
15740 * Fetch the next logical xri. Because this index is logical,
15741 * the driver starts at 0 each time.
15742 */
15743 spin_lock_irq(&phba->hbalock);
15744 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15745 phba->sli4_hba.max_cfg_param.max_xri, 0);
15746 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15747 spin_unlock_irq(&phba->hbalock);
15748 return NO_XRI;
15749 } else {
15750 set_bit(xri, phba->sli4_hba.xri_bmask);
15751 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015752 }
James Smart6d368e52011-05-24 11:44:12 -040015753 spin_unlock_irq(&phba->hbalock);
15754 return xri;
15755}
15756
15757/**
15758 * lpfc_sli4_free_xri - Release an xri for reuse.
15759 * @phba: pointer to lpfc hba data structure.
15760 *
15761 * This routine is invoked to release an xri to the pool of
15762 * available rpis maintained by the driver.
15763 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015764static void
James Smart6d368e52011-05-24 11:44:12 -040015765__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15766{
15767 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040015768 phba->sli4_hba.max_cfg_param.xri_used--;
15769 }
15770}
15771
15772/**
15773 * lpfc_sli4_free_xri - Release an xri for reuse.
15774 * @phba: pointer to lpfc hba data structure.
15775 *
15776 * This routine is invoked to release an xri to the pool of
15777 * available rpis maintained by the driver.
15778 **/
15779void
15780lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15781{
15782 spin_lock_irq(&phba->hbalock);
15783 __lpfc_sli4_free_xri(phba, xri);
15784 spin_unlock_irq(&phba->hbalock);
15785}
15786
15787/**
James Smart4f774512009-05-22 14:52:35 -040015788 * lpfc_sli4_next_xritag - Get an xritag for the io
15789 * @phba: Pointer to HBA context object.
15790 *
15791 * This function gets an xritag for the iocb. If there is no unused xritag
15792 * it will return 0xffff.
15793 * The function returns the allocated xritag if successful, else returns zero.
15794 * Zero is not a valid xritag.
15795 * The caller is not required to hold any lock.
15796 **/
15797uint16_t
15798lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15799{
James Smart6d368e52011-05-24 11:44:12 -040015800 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040015801
James Smart6d368e52011-05-24 11:44:12 -040015802 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040015803 if (xri_index == NO_XRI)
15804 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15805 "2004 Failed to allocate XRI.last XRITAG is %d"
15806 " Max XRI is %d, Used XRI is %d\n",
15807 xri_index,
15808 phba->sli4_hba.max_cfg_param.max_xri,
15809 phba->sli4_hba.max_cfg_param.xri_used);
15810 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040015811}
15812
15813/**
James Smart895427b2017-02-12 13:52:30 -080015814 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040015815 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040015816 * @post_sgl_list: pointer to els sgl entry list.
15817 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040015818 *
15819 * This routine is invoked to post a block of driver's sgl pages to the
15820 * HBA using non-embedded mailbox command. No Lock is held. This routine
15821 * is only called when the driver is loading and after all IO has been
15822 * stopped.
15823 **/
James Smart8a9d2e82012-05-09 21:16:12 -040015824static int
James Smart895427b2017-02-12 13:52:30 -080015825lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040015826 struct list_head *post_sgl_list,
15827 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040015828{
James Smart8a9d2e82012-05-09 21:16:12 -040015829 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040015830 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15831 struct sgl_page_pairs *sgl_pg_pairs;
15832 void *viraddr;
15833 LPFC_MBOXQ_t *mbox;
15834 uint32_t reqlen, alloclen, pg_pairs;
15835 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040015836 uint16_t xritag_start = 0;
15837 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040015838 uint32_t shdr_status, shdr_add_status;
15839 union lpfc_sli4_cfg_shdr *shdr;
15840
James Smart895427b2017-02-12 13:52:30 -080015841 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015842 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015843 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080015844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040015845 "2559 Block sgl registration required DMA "
15846 "size (%d) great than a page\n", reqlen);
15847 return -ENOMEM;
15848 }
James Smart895427b2017-02-12 13:52:30 -080015849
James Smart4f774512009-05-22 14:52:35 -040015850 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040015851 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040015852 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040015853
15854 /* Allocate DMA memory and set up the non-embedded mailbox command */
15855 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15856 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15857 LPFC_SLI4_MBX_NEMBED);
15858
15859 if (alloclen < reqlen) {
15860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15861 "0285 Allocated DMA memory size (%d) is "
15862 "less than the requested DMA memory "
15863 "size (%d)\n", alloclen, reqlen);
15864 lpfc_sli4_mbox_cmd_free(phba, mbox);
15865 return -ENOMEM;
15866 }
James Smart4f774512009-05-22 14:52:35 -040015867 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040015868 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040015869 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15870 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15871
James Smart8a9d2e82012-05-09 21:16:12 -040015872 pg_pairs = 0;
15873 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040015874 /* Set up the sge entry */
15875 sgl_pg_pairs->sgl_pg0_addr_lo =
15876 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15877 sgl_pg_pairs->sgl_pg0_addr_hi =
15878 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15879 sgl_pg_pairs->sgl_pg1_addr_lo =
15880 cpu_to_le32(putPaddrLow(0));
15881 sgl_pg_pairs->sgl_pg1_addr_hi =
15882 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040015883
James Smart4f774512009-05-22 14:52:35 -040015884 /* Keep the first xritag on the list */
15885 if (pg_pairs == 0)
15886 xritag_start = sglq_entry->sli4_xritag;
15887 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040015888 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040015889 }
James Smart6d368e52011-05-24 11:44:12 -040015890
15891 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040015892 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080015893 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040015894 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080015895
James Smart4f774512009-05-22 14:52:35 -040015896 if (!phba->sli4_hba.intr_enable)
15897 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15898 else {
James Smarta183a152011-10-10 21:32:43 -040015899 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040015900 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15901 }
15902 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15903 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15904 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15905 if (rc != MBX_TIMEOUT)
15906 lpfc_sli4_mbox_cmd_free(phba, mbox);
15907 if (shdr_status || shdr_add_status || rc) {
15908 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15909 "2513 POST_SGL_BLOCK mailbox command failed "
15910 "status x%x add_status x%x mbx status x%x\n",
15911 shdr_status, shdr_add_status, rc);
15912 rc = -ENXIO;
15913 }
15914 return rc;
15915}
15916
15917/**
15918 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
15919 * @phba: pointer to lpfc hba data structure.
15920 * @sblist: pointer to scsi buffer list.
15921 * @count: number of scsi buffers on the list.
15922 *
15923 * This routine is invoked to post a block of @count scsi sgl pages from a
15924 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
15925 * No Lock is held.
15926 *
15927 **/
15928int
James Smart8a9d2e82012-05-09 21:16:12 -040015929lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
15930 struct list_head *sblist,
15931 int count)
James Smart4f774512009-05-22 14:52:35 -040015932{
15933 struct lpfc_scsi_buf *psb;
15934 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15935 struct sgl_page_pairs *sgl_pg_pairs;
15936 void *viraddr;
15937 LPFC_MBOXQ_t *mbox;
15938 uint32_t reqlen, alloclen, pg_pairs;
15939 uint32_t mbox_tmo;
15940 uint16_t xritag_start = 0;
15941 int rc = 0;
15942 uint32_t shdr_status, shdr_add_status;
15943 dma_addr_t pdma_phys_bpl1;
15944 union lpfc_sli4_cfg_shdr *shdr;
15945
15946 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040015947 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015948 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015949 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040015950 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
15951 "0217 Block sgl registration required DMA "
15952 "size (%d) great than a page\n", reqlen);
15953 return -ENOMEM;
15954 }
15955 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15956 if (!mbox) {
15957 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15958 "0283 Failed to allocate mbox cmd memory\n");
15959 return -ENOMEM;
15960 }
15961
15962 /* Allocate DMA memory and set up the non-embedded mailbox command */
15963 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15964 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15965 LPFC_SLI4_MBX_NEMBED);
15966
15967 if (alloclen < reqlen) {
15968 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15969 "2561 Allocated DMA memory size (%d) is "
15970 "less than the requested DMA memory "
15971 "size (%d)\n", alloclen, reqlen);
15972 lpfc_sli4_mbox_cmd_free(phba, mbox);
15973 return -ENOMEM;
15974 }
James Smart6d368e52011-05-24 11:44:12 -040015975
James Smart4f774512009-05-22 14:52:35 -040015976 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040015977 viraddr = mbox->sge_array->addr[0];
15978
15979 /* Set up the SGL pages in the non-embedded DMA pages */
15980 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15981 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15982
15983 pg_pairs = 0;
15984 list_for_each_entry(psb, sblist, list) {
15985 /* Set up the sge entry */
15986 sgl_pg_pairs->sgl_pg0_addr_lo =
15987 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
15988 sgl_pg_pairs->sgl_pg0_addr_hi =
15989 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
15990 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
15991 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
15992 else
15993 pdma_phys_bpl1 = 0;
15994 sgl_pg_pairs->sgl_pg1_addr_lo =
15995 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
15996 sgl_pg_pairs->sgl_pg1_addr_hi =
15997 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
15998 /* Keep the first xritag on the list */
15999 if (pg_pairs == 0)
16000 xritag_start = psb->cur_iocbq.sli4_xritag;
16001 sgl_pg_pairs++;
16002 pg_pairs++;
16003 }
16004 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16005 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16006 /* Perform endian conversion if necessary */
16007 sgl->word0 = cpu_to_le32(sgl->word0);
16008
16009 if (!phba->sli4_hba.intr_enable)
16010 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16011 else {
James Smarta183a152011-10-10 21:32:43 -040016012 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016013 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16014 }
16015 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16016 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16017 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16018 if (rc != MBX_TIMEOUT)
16019 lpfc_sli4_mbox_cmd_free(phba, mbox);
16020 if (shdr_status || shdr_add_status || rc) {
16021 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16022 "2564 POST_SGL_BLOCK mailbox command failed "
16023 "status x%x add_status x%x mbx status x%x\n",
16024 shdr_status, shdr_add_status, rc);
16025 rc = -ENXIO;
16026 }
16027 return rc;
16028}
16029
James Smart2ea259e2017-02-12 13:52:27 -080016030static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
16031static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
16032
James Smart4f774512009-05-22 14:52:35 -040016033/**
16034 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16035 * @phba: pointer to lpfc_hba struct that the frame was received on
16036 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16037 *
16038 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16039 * valid type of frame that the LPFC driver will handle. This function will
16040 * return a zero if the frame is a valid frame or a non zero value when the
16041 * frame does not pass the check.
16042 **/
16043static int
16044lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16045{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016046 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016047 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016048 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016049
16050 switch (fc_hdr->fh_r_ctl) {
16051 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16052 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16053 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16054 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16055 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16056 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16057 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16058 case FC_RCTL_DD_CMD_STATUS: /* command status */
16059 case FC_RCTL_ELS_REQ: /* extended link services request */
16060 case FC_RCTL_ELS_REP: /* extended link services reply */
16061 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16062 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16063 case FC_RCTL_BA_NOP: /* basic link service NOP */
16064 case FC_RCTL_BA_ABTS: /* basic link service abort */
16065 case FC_RCTL_BA_RMC: /* remove connection */
16066 case FC_RCTL_BA_ACC: /* basic accept */
16067 case FC_RCTL_BA_RJT: /* basic reject */
16068 case FC_RCTL_BA_PRMT:
16069 case FC_RCTL_ACK_1: /* acknowledge_1 */
16070 case FC_RCTL_ACK_0: /* acknowledge_0 */
16071 case FC_RCTL_P_RJT: /* port reject */
16072 case FC_RCTL_F_RJT: /* fabric reject */
16073 case FC_RCTL_P_BSY: /* port busy */
16074 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16075 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16076 case FC_RCTL_LCR: /* link credit reset */
16077 case FC_RCTL_END: /* end */
16078 break;
16079 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16080 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16081 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16082 return lpfc_fc_frame_check(phba, fc_hdr);
16083 default:
16084 goto drop;
16085 }
16086 switch (fc_hdr->fh_type) {
16087 case FC_TYPE_BLS:
16088 case FC_TYPE_ELS:
16089 case FC_TYPE_FCP:
16090 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016091 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040016092 break;
16093 case FC_TYPE_IP:
16094 case FC_TYPE_ILS:
16095 default:
16096 goto drop;
16097 }
James Smart546fc852011-03-11 16:06:29 -050016098
James Smart4f774512009-05-22 14:52:35 -040016099 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040016100 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
16101 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart2ea259e2017-02-12 13:52:27 -080016102 lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
16103 lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050016104 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16105 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040016106 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16107 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016108 return 0;
16109drop:
16110 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16111 "2539 Dropped frame rctl:%s type:%s\n",
James Smart2ea259e2017-02-12 13:52:27 -080016112 lpfc_rctl_names[fc_hdr->fh_r_ctl],
16113 lpfc_type_names[fc_hdr->fh_type]);
James Smart4f774512009-05-22 14:52:35 -040016114 return 1;
16115}
16116
16117/**
16118 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16119 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16120 *
16121 * This function processes the FC header to retrieve the VFI from the VF
16122 * header, if one exists. This function will return the VFI if one exists
16123 * or 0 if no VSAN Header exists.
16124 **/
16125static uint32_t
16126lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16127{
16128 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16129
16130 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16131 return 0;
16132 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16133}
16134
16135/**
16136 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16137 * @phba: Pointer to the HBA structure to search for the vport on
16138 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16139 * @fcfi: The FC Fabric ID that the frame came from
16140 *
16141 * This function searches the @phba for a vport that matches the content of the
16142 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16143 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16144 * returns the matching vport pointer or NULL if unable to match frame to a
16145 * vport.
16146 **/
16147static struct lpfc_vport *
16148lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016149 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016150{
16151 struct lpfc_vport **vports;
16152 struct lpfc_vport *vport = NULL;
16153 int i;
James Smart939723a2012-05-09 21:19:03 -040016154
James Smartbf086112011-08-21 21:48:13 -040016155 if (did == Fabric_DID)
16156 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016157 if ((phba->pport->fc_flag & FC_PT2PT) &&
16158 !(phba->link_state == LPFC_HBA_READY))
16159 return phba->pport;
16160
James Smart4f774512009-05-22 14:52:35 -040016161 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016162 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016163 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16164 if (phba->fcf.fcfi == fcfi &&
16165 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16166 vports[i]->fc_myDID == did) {
16167 vport = vports[i];
16168 break;
16169 }
16170 }
James Smart895427b2017-02-12 13:52:30 -080016171 }
James Smart4f774512009-05-22 14:52:35 -040016172 lpfc_destroy_vport_work_array(phba, vports);
16173 return vport;
16174}
16175
16176/**
James Smart45ed1192009-10-02 15:17:02 -040016177 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16178 * @vport: The vport to work on.
16179 *
16180 * This function updates the receive sequence time stamp for this vport. The
16181 * receive sequence time stamp indicates the time that the last frame of the
16182 * the sequence that has been idle for the longest amount of time was received.
16183 * the driver uses this time stamp to indicate if any received sequences have
16184 * timed out.
16185 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016186static void
James Smart45ed1192009-10-02 15:17:02 -040016187lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16188{
16189 struct lpfc_dmabuf *h_buf;
16190 struct hbq_dmabuf *dmabuf = NULL;
16191
16192 /* get the oldest sequence on the rcv list */
16193 h_buf = list_get_first(&vport->rcv_buffer_list,
16194 struct lpfc_dmabuf, list);
16195 if (!h_buf)
16196 return;
16197 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16198 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16199}
16200
16201/**
16202 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16203 * @vport: The vport that the received sequences were sent to.
16204 *
16205 * This function cleans up all outstanding received sequences. This is called
16206 * by the driver when a link event or user action invalidates all the received
16207 * sequences.
16208 **/
16209void
16210lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16211{
16212 struct lpfc_dmabuf *h_buf, *hnext;
16213 struct lpfc_dmabuf *d_buf, *dnext;
16214 struct hbq_dmabuf *dmabuf = NULL;
16215
16216 /* start with the oldest sequence on the rcv list */
16217 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16218 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16219 list_del_init(&dmabuf->hbuf.list);
16220 list_for_each_entry_safe(d_buf, dnext,
16221 &dmabuf->dbuf.list, list) {
16222 list_del_init(&d_buf->list);
16223 lpfc_in_buf_free(vport->phba, d_buf);
16224 }
16225 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16226 }
16227}
16228
16229/**
16230 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16231 * @vport: The vport that the received sequences were sent to.
16232 *
16233 * This function determines whether any received sequences have timed out by
16234 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16235 * indicates that there is at least one timed out sequence this routine will
16236 * go through the received sequences one at a time from most inactive to most
16237 * active to determine which ones need to be cleaned up. Once it has determined
16238 * that a sequence needs to be cleaned up it will simply free up the resources
16239 * without sending an abort.
16240 **/
16241void
16242lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16243{
16244 struct lpfc_dmabuf *h_buf, *hnext;
16245 struct lpfc_dmabuf *d_buf, *dnext;
16246 struct hbq_dmabuf *dmabuf = NULL;
16247 unsigned long timeout;
16248 int abort_count = 0;
16249
16250 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16251 vport->rcv_buffer_time_stamp);
16252 if (list_empty(&vport->rcv_buffer_list) ||
16253 time_before(jiffies, timeout))
16254 return;
16255 /* start with the oldest sequence on the rcv list */
16256 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16257 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16258 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16259 dmabuf->time_stamp);
16260 if (time_before(jiffies, timeout))
16261 break;
16262 abort_count++;
16263 list_del_init(&dmabuf->hbuf.list);
16264 list_for_each_entry_safe(d_buf, dnext,
16265 &dmabuf->dbuf.list, list) {
16266 list_del_init(&d_buf->list);
16267 lpfc_in_buf_free(vport->phba, d_buf);
16268 }
16269 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16270 }
16271 if (abort_count)
16272 lpfc_update_rcv_time_stamp(vport);
16273}
16274
16275/**
James Smart4f774512009-05-22 14:52:35 -040016276 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16277 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16278 *
16279 * This function searches through the existing incomplete sequences that have
16280 * been sent to this @vport. If the frame matches one of the incomplete
16281 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16282 * make up that sequence. If no sequence is found that matches this frame then
16283 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16284 * This function returns a pointer to the first dmabuf in the sequence list that
16285 * the frame was linked to.
16286 **/
16287static struct hbq_dmabuf *
16288lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16289{
16290 struct fc_frame_header *new_hdr;
16291 struct fc_frame_header *temp_hdr;
16292 struct lpfc_dmabuf *d_buf;
16293 struct lpfc_dmabuf *h_buf;
16294 struct hbq_dmabuf *seq_dmabuf = NULL;
16295 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016296 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016297
James Smart4d9ab992009-10-02 15:16:39 -040016298 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016299 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016300 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016301
James Smart4f774512009-05-22 14:52:35 -040016302 /* Use the hdr_buf to find the sequence that this frame belongs to */
16303 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16304 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16305 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16306 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16307 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16308 continue;
16309 /* found a pending sequence that matches this frame */
16310 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16311 break;
16312 }
16313 if (!seq_dmabuf) {
16314 /*
16315 * This indicates first frame received for this sequence.
16316 * Queue the buffer on the vport's rcv_buffer_list.
16317 */
16318 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016319 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016320 return dmabuf;
16321 }
16322 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016323 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16324 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016325 list_del_init(&seq_dmabuf->hbuf.list);
16326 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16327 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016328 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016329 return dmabuf;
16330 }
James Smart45ed1192009-10-02 15:17:02 -040016331 /* move this sequence to the tail to indicate a young sequence */
16332 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16333 seq_dmabuf->time_stamp = jiffies;
16334 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016335 if (list_empty(&seq_dmabuf->dbuf.list)) {
16336 temp_hdr = dmabuf->hbuf.virt;
16337 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16338 return seq_dmabuf;
16339 }
James Smart4f774512009-05-22 14:52:35 -040016340 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016341 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16342 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016343 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16344 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16345 /*
16346 * If the frame's sequence count is greater than the frame on
16347 * the list then insert the frame right after this frame
16348 */
James Smarteeead812009-12-21 17:01:23 -050016349 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16350 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016351 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016352 found = 1;
16353 break;
James Smart4f774512009-05-22 14:52:35 -040016354 }
James Smart4360ca92015-12-16 18:12:04 -050016355
16356 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16357 break;
16358 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016359 }
James Smart4360ca92015-12-16 18:12:04 -050016360
16361 if (found)
16362 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016363 return NULL;
16364}
16365
16366/**
James Smart6669f9b2009-10-02 15:16:45 -040016367 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16368 * @vport: pointer to a vitural port
16369 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16370 *
16371 * This function tries to abort from the partially assembed sequence, described
16372 * by the information from basic abbort @dmabuf. It checks to see whether such
16373 * partially assembled sequence held by the driver. If so, it shall free up all
16374 * the frames from the partially assembled sequence.
16375 *
16376 * Return
16377 * true -- if there is matching partially assembled sequence present and all
16378 * the frames freed with the sequence;
16379 * false -- if there is no matching partially assembled sequence present so
16380 * nothing got aborted in the lower layer driver
16381 **/
16382static bool
16383lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16384 struct hbq_dmabuf *dmabuf)
16385{
16386 struct fc_frame_header *new_hdr;
16387 struct fc_frame_header *temp_hdr;
16388 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16389 struct hbq_dmabuf *seq_dmabuf = NULL;
16390
16391 /* Use the hdr_buf to find the sequence that matches this frame */
16392 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16393 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16394 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16395 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16396 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16397 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16398 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16399 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16400 continue;
16401 /* found a pending sequence that matches this frame */
16402 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16403 break;
16404 }
16405
16406 /* Free up all the frames from the partially assembled sequence */
16407 if (seq_dmabuf) {
16408 list_for_each_entry_safe(d_buf, n_buf,
16409 &seq_dmabuf->dbuf.list, list) {
16410 list_del_init(&d_buf->list);
16411 lpfc_in_buf_free(vport->phba, d_buf);
16412 }
16413 return true;
16414 }
16415 return false;
16416}
16417
16418/**
James Smart6dd9e312013-01-03 15:43:37 -050016419 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16420 * @vport: pointer to a vitural port
16421 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16422 *
16423 * This function tries to abort from the assembed sequence from upper level
16424 * protocol, described by the information from basic abbort @dmabuf. It
16425 * checks to see whether such pending context exists at upper level protocol.
16426 * If so, it shall clean up the pending context.
16427 *
16428 * Return
16429 * true -- if there is matching pending context of the sequence cleaned
16430 * at ulp;
16431 * false -- if there is no matching pending context of the sequence present
16432 * at ulp.
16433 **/
16434static bool
16435lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16436{
16437 struct lpfc_hba *phba = vport->phba;
16438 int handled;
16439
16440 /* Accepting abort at ulp with SLI4 only */
16441 if (phba->sli_rev < LPFC_SLI_REV4)
16442 return false;
16443
16444 /* Register all caring upper level protocols to attend abort */
16445 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16446 if (handled)
16447 return true;
16448
16449 return false;
16450}
16451
16452/**
James Smart546fc852011-03-11 16:06:29 -050016453 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016454 * @phba: Pointer to HBA context object.
16455 * @cmd_iocbq: pointer to the command iocbq structure.
16456 * @rsp_iocbq: pointer to the response iocbq structure.
16457 *
James Smart546fc852011-03-11 16:06:29 -050016458 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016459 * event. It properly releases the memory allocated to the sequence abort
16460 * accept iocb.
16461 **/
16462static void
James Smart546fc852011-03-11 16:06:29 -050016463lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016464 struct lpfc_iocbq *cmd_iocbq,
16465 struct lpfc_iocbq *rsp_iocbq)
16466{
James Smart6dd9e312013-01-03 15:43:37 -050016467 struct lpfc_nodelist *ndlp;
16468
16469 if (cmd_iocbq) {
16470 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16471 lpfc_nlp_put(ndlp);
16472 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016473 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016474 }
James Smart6b5151f2012-01-18 16:24:06 -050016475
16476 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16477 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16478 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16479 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16480 rsp_iocbq->iocb.ulpStatus,
16481 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016482}
16483
16484/**
James Smart6d368e52011-05-24 11:44:12 -040016485 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16486 * @phba: Pointer to HBA context object.
16487 * @xri: xri id in transaction.
16488 *
16489 * This function validates the xri maps to the known range of XRIs allocated an
16490 * used by the driver.
16491 **/
James Smart7851fe22011-07-22 18:36:52 -040016492uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016493lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16494 uint16_t xri)
16495{
James Smarta2fc4aef2014-09-03 12:57:55 -040016496 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016497
16498 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16499 if (xri == phba->sli4_hba.xri_ids[i])
16500 return i;
16501 }
16502 return NO_XRI;
16503}
16504
James Smart6d368e52011-05-24 11:44:12 -040016505/**
James Smart546fc852011-03-11 16:06:29 -050016506 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016507 * @phba: Pointer to HBA context object.
16508 * @fc_hdr: pointer to a FC frame header.
16509 *
James Smart546fc852011-03-11 16:06:29 -050016510 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016511 * event after aborting the sequence handling.
16512 **/
16513static void
James Smart6dd9e312013-01-03 15:43:37 -050016514lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16515 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016516{
James Smart6dd9e312013-01-03 15:43:37 -050016517 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040016518 struct lpfc_iocbq *ctiocb = NULL;
16519 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040016520 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050016521 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040016522 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050016523 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040016524
16525 if (!lpfc_is_link_up(phba))
16526 return;
16527
16528 sid = sli4_sid_from_fc_hdr(fc_hdr);
16529 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050016530 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040016531
James Smart6dd9e312013-01-03 15:43:37 -050016532 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040016533 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050016534 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
16535 if (!ndlp) {
16536 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16537 "1268 Failed to allocate ndlp for "
16538 "oxid:x%x SID:x%x\n", oxid, sid);
16539 return;
16540 }
16541 lpfc_nlp_init(vport, ndlp, sid);
16542 /* Put ndlp onto pport node list */
16543 lpfc_enqueue_node(vport, ndlp);
16544 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16545 /* re-setup ndlp without removing from node list */
16546 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16547 if (!ndlp) {
16548 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16549 "3275 Failed to active ndlp found "
16550 "for oxid:x%x SID:x%x\n", oxid, sid);
16551 return;
16552 }
James Smart6669f9b2009-10-02 15:16:45 -040016553 }
16554
James Smart546fc852011-03-11 16:06:29 -050016555 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040016556 ctiocb = lpfc_sli_get_iocbq(phba);
16557 if (!ctiocb)
16558 return;
16559
James Smart5ffc2662009-11-18 15:39:44 -050016560 /* Extract the F_CTL field from FC_HDR */
16561 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16562
James Smart6669f9b2009-10-02 15:16:45 -040016563 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040016564 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050016565 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040016566 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16567 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16568 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16569
16570 /* Fill in the rest of iocb fields */
16571 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16572 icmd->ulpBdeCount = 0;
16573 icmd->ulpLe = 1;
16574 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040016575 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050016576 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016577
James Smart6669f9b2009-10-02 15:16:45 -040016578 ctiocb->iocb_cmpl = NULL;
16579 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050016580 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040016581 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050016582 ctiocb->sli4_xritag = NO_XRI;
16583
James Smartee0f4fe2012-05-09 21:19:14 -040016584 if (fctl & FC_FC_EX_CTX)
16585 /* Exchange responder sent the abort so we
16586 * own the oxid.
16587 */
16588 xri = oxid;
16589 else
16590 xri = rxid;
16591 lxri = lpfc_sli4_xri_inrange(phba, xri);
16592 if (lxri != NO_XRI)
16593 lpfc_set_rrq_active(phba, ndlp, lxri,
16594 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050016595 /* For BA_ABTS from exchange responder, if the logical xri with
16596 * the oxid maps to the FCP XRI range, the port no longer has
16597 * that exchange context, send a BLS_RJT. Override the IOCB for
16598 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050016599 */
James Smart6dd9e312013-01-03 15:43:37 -050016600 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080016601 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050016602 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16603 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16604 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16605 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16606 }
16607
16608 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16609 * the driver no longer has that exchange, send a BLS_RJT. Override
16610 * the IOCB for a BA_RJT.
16611 */
16612 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050016613 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16614 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16615 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16616 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16617 }
James Smart6669f9b2009-10-02 15:16:45 -040016618
James Smart5ffc2662009-11-18 15:39:44 -050016619 if (fctl & FC_FC_EX_CTX) {
16620 /* ABTS sent by responder to CT exchange, construction
16621 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16622 * field and RX_ID from ABTS for RX_ID field.
16623 */
James Smart546fc852011-03-11 16:06:29 -050016624 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050016625 } else {
16626 /* ABTS sent by initiator to CT exchange, construction
16627 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050016628 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050016629 */
James Smart546fc852011-03-11 16:06:29 -050016630 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050016631 }
James Smartf09c3ac2012-03-01 22:33:29 -050016632 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050016633 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050016634
James Smart546fc852011-03-11 16:06:29 -050016635 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050016636 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16637 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16638 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050016639
16640 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16641 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050016642 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16643 "2925 Failed to issue CT ABTS RSP x%x on "
16644 "xri x%x, Data x%x\n",
16645 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16646 phba->link_state);
16647 lpfc_nlp_put(ndlp);
16648 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050016649 lpfc_sli_release_iocbq(phba, ctiocb);
16650 }
James Smart6669f9b2009-10-02 15:16:45 -040016651}
16652
16653/**
16654 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16655 * @vport: Pointer to the vport on which this sequence was received
16656 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16657 *
16658 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16659 * receive sequence is only partially assembed by the driver, it shall abort
16660 * the partially assembled frames for the sequence. Otherwise, if the
16661 * unsolicited receive sequence has been completely assembled and passed to
16662 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16663 * unsolicited sequence has been aborted. After that, it will issue a basic
16664 * accept to accept the abort.
16665 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016666static void
James Smart6669f9b2009-10-02 15:16:45 -040016667lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16668 struct hbq_dmabuf *dmabuf)
16669{
16670 struct lpfc_hba *phba = vport->phba;
16671 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050016672 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050016673 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040016674
James Smart6669f9b2009-10-02 15:16:45 -040016675 /* Make a copy of fc_hdr before the dmabuf being released */
16676 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050016677 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040016678
James Smart5ffc2662009-11-18 15:39:44 -050016679 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050016680 /* ABTS by responder to exchange, no cleanup needed */
16681 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050016682 } else {
James Smart6dd9e312013-01-03 15:43:37 -050016683 /* ABTS by initiator to exchange, need to do cleanup */
16684 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16685 if (aborted == false)
16686 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050016687 }
James Smart6dd9e312013-01-03 15:43:37 -050016688 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16689
16690 /* Respond with BA_ACC or BA_RJT accordingly */
16691 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040016692}
16693
16694/**
James Smart4f774512009-05-22 14:52:35 -040016695 * lpfc_seq_complete - Indicates if a sequence is complete
16696 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16697 *
16698 * This function checks the sequence, starting with the frame described by
16699 * @dmabuf, to see if all the frames associated with this sequence are present.
16700 * the frames associated with this sequence are linked to the @dmabuf using the
16701 * dbuf list. This function looks for two major things. 1) That the first frame
16702 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16703 * set. 3) That there are no holes in the sequence count. The function will
16704 * return 1 when the sequence is complete, otherwise it will return 0.
16705 **/
16706static int
16707lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16708{
16709 struct fc_frame_header *hdr;
16710 struct lpfc_dmabuf *d_buf;
16711 struct hbq_dmabuf *seq_dmabuf;
16712 uint32_t fctl;
16713 int seq_count = 0;
16714
16715 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16716 /* make sure first fame of sequence has a sequence count of zero */
16717 if (hdr->fh_seq_cnt != seq_count)
16718 return 0;
16719 fctl = (hdr->fh_f_ctl[0] << 16 |
16720 hdr->fh_f_ctl[1] << 8 |
16721 hdr->fh_f_ctl[2]);
16722 /* If last frame of sequence we can return success. */
16723 if (fctl & FC_FC_END_SEQ)
16724 return 1;
16725 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16726 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16727 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16728 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050016729 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040016730 return 0;
16731 fctl = (hdr->fh_f_ctl[0] << 16 |
16732 hdr->fh_f_ctl[1] << 8 |
16733 hdr->fh_f_ctl[2]);
16734 /* If last frame of sequence we can return success. */
16735 if (fctl & FC_FC_END_SEQ)
16736 return 1;
16737 }
16738 return 0;
16739}
16740
16741/**
16742 * lpfc_prep_seq - Prep sequence for ULP processing
16743 * @vport: Pointer to the vport on which this sequence was received
16744 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16745 *
16746 * This function takes a sequence, described by a list of frames, and creates
16747 * a list of iocbq structures to describe the sequence. This iocbq list will be
16748 * used to issue to the generic unsolicited sequence handler. This routine
16749 * returns a pointer to the first iocbq in the list. If the function is unable
16750 * to allocate an iocbq then it throw out the received frames that were not
16751 * able to be described and return a pointer to the first iocbq. If unable to
16752 * allocate any iocbqs (including the first) this function will return NULL.
16753 **/
16754static struct lpfc_iocbq *
16755lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16756{
James Smart7851fe22011-07-22 18:36:52 -040016757 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016758 struct lpfc_dmabuf *d_buf, *n_buf;
16759 struct lpfc_iocbq *first_iocbq, *iocbq;
16760 struct fc_frame_header *fc_hdr;
16761 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040016762 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050016763 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040016764
16765 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16766 /* remove from receive buffer list */
16767 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016768 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016769 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040016770 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040016771 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016772 /* Get an iocbq struct to fill in. */
16773 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16774 if (first_iocbq) {
16775 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040016776 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016777 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080016778 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040016779
16780 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16781 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16782 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16783 first_iocbq->iocb.un.rcvels.parmRo =
16784 sli4_did_from_fc_hdr(fc_hdr);
16785 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16786 } else
16787 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040016788 first_iocbq->iocb.ulpContext = NO_XRI;
16789 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16790 be16_to_cpu(fc_hdr->fh_ox_id);
16791 /* iocbq is prepped for internal consumption. Physical vpi. */
16792 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16793 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040016794 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040016795 tot_len = bf_get(lpfc_rcqe_length,
16796 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16797
James Smart4f774512009-05-22 14:52:35 -040016798 first_iocbq->context2 = &seq_dmabuf->dbuf;
16799 first_iocbq->context3 = NULL;
16800 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040016801 if (tot_len > LPFC_DATA_BUF_SIZE)
16802 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040016803 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040016804 else
16805 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16806
James Smart4f774512009-05-22 14:52:35 -040016807 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040016808
James Smart7851fe22011-07-22 18:36:52 -040016809 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040016810 }
16811 iocbq = first_iocbq;
16812 /*
16813 * Each IOCBq can have two Buffers assigned, so go through the list
16814 * of buffers for this sequence and save two buffers in each IOCBq
16815 */
16816 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16817 if (!iocbq) {
16818 lpfc_in_buf_free(vport->phba, d_buf);
16819 continue;
16820 }
16821 if (!iocbq->context3) {
16822 iocbq->context3 = d_buf;
16823 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040016824 /* We need to get the size out of the right CQE */
16825 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16826 len = bf_get(lpfc_rcqe_length,
16827 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016828 pbde = (struct ulp_bde64 *)
16829 &iocbq->iocb.unsli3.sli3Words[4];
16830 if (len > LPFC_DATA_BUF_SIZE)
16831 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16832 else
16833 pbde->tus.f.bdeSize = len;
16834
James Smart7851fe22011-07-22 18:36:52 -040016835 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16836 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040016837 } else {
16838 iocbq = lpfc_sli_get_iocbq(vport->phba);
16839 if (!iocbq) {
16840 if (first_iocbq) {
16841 first_iocbq->iocb.ulpStatus =
16842 IOSTAT_FCP_RSP_ERROR;
16843 first_iocbq->iocb.un.ulpWord[4] =
16844 IOERR_NO_RESOURCES;
16845 }
16846 lpfc_in_buf_free(vport->phba, d_buf);
16847 continue;
16848 }
James Smart7851fe22011-07-22 18:36:52 -040016849 /* We need to get the size out of the right CQE */
16850 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16851 len = bf_get(lpfc_rcqe_length,
16852 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016853 iocbq->context2 = d_buf;
16854 iocbq->context3 = NULL;
16855 iocbq->iocb.ulpBdeCount = 1;
16856 if (len > LPFC_DATA_BUF_SIZE)
16857 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16858 LPFC_DATA_BUF_SIZE;
16859 else
16860 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
16861
James Smart7851fe22011-07-22 18:36:52 -040016862 tot_len += len;
16863 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16864
James Smart4f774512009-05-22 14:52:35 -040016865 iocbq->iocb.un.rcvels.remoteID = sid;
16866 list_add_tail(&iocbq->list, &first_iocbq->list);
16867 }
16868 }
16869 return first_iocbq;
16870}
16871
James Smart6669f9b2009-10-02 15:16:45 -040016872static void
16873lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16874 struct hbq_dmabuf *seq_dmabuf)
16875{
16876 struct fc_frame_header *fc_hdr;
16877 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16878 struct lpfc_hba *phba = vport->phba;
16879
16880 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16881 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16882 if (!iocbq) {
16883 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16884 "2707 Ring %d handler: Failed to allocate "
16885 "iocb Rctl x%x Type x%x received\n",
16886 LPFC_ELS_RING,
16887 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16888 return;
16889 }
16890 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080016891 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040016892 iocbq, fc_hdr->fh_r_ctl,
16893 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040016894 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040016895 "2540 Ring %d handler: unexpected Rctl "
16896 "x%x Type x%x received\n",
16897 LPFC_ELS_RING,
16898 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16899
16900 /* Free iocb created in lpfc_prep_seq */
16901 list_for_each_entry_safe(curr_iocb, next_iocb,
16902 &iocbq->list, list) {
16903 list_del_init(&curr_iocb->list);
16904 lpfc_sli_release_iocbq(phba, curr_iocb);
16905 }
16906 lpfc_sli_release_iocbq(phba, iocbq);
16907}
16908
James Smart4f774512009-05-22 14:52:35 -040016909/**
16910 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
16911 * @phba: Pointer to HBA context object.
16912 *
16913 * This function is called with no lock held. This function processes all
16914 * the received buffers and gives it to upper layers when a received buffer
16915 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080016916 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040016917 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
16918 * appropriate receive function when the final frame in a sequence is received.
16919 **/
James Smart4d9ab992009-10-02 15:16:39 -040016920void
16921lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
16922 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040016923{
James Smart4d9ab992009-10-02 15:16:39 -040016924 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016925 struct fc_frame_header *fc_hdr;
16926 struct lpfc_vport *vport;
16927 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040016928 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040016929
James Smart4f774512009-05-22 14:52:35 -040016930 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040016931 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080016932
James Smart4d9ab992009-10-02 15:16:39 -040016933 /* check to see if this a valid type of frame */
16934 if (lpfc_fc_frame_check(phba, fc_hdr)) {
16935 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16936 return;
16937 }
James Smart2ea259e2017-02-12 13:52:27 -080016938
James Smart7851fe22011-07-22 18:36:52 -040016939 if ((bf_get(lpfc_cqe_code,
16940 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
16941 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
16942 &dmabuf->cq_event.cqe.rcqe_cmpl);
16943 else
16944 fcfi = bf_get(lpfc_rcqe_fcf_id,
16945 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040016946
James Smart895427b2017-02-12 13:52:30 -080016947 /* d_id this frame is directed to */
16948 did = sli4_did_from_fc_hdr(fc_hdr);
16949
16950 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040016951 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040016952 /* throw out the frame */
16953 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16954 return;
16955 }
James Smart939723a2012-05-09 21:19:03 -040016956
James Smart939723a2012-05-09 21:19:03 -040016957 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
16958 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
16959 (did != Fabric_DID)) {
16960 /*
16961 * Throw out the frame if we are not pt2pt.
16962 * The pt2pt protocol allows for discovery frames
16963 * to be received without a registered VPI.
16964 */
16965 if (!(vport->fc_flag & FC_PT2PT) ||
16966 (phba->link_state == LPFC_HBA_READY)) {
16967 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16968 return;
16969 }
16970 }
16971
James Smart6669f9b2009-10-02 15:16:45 -040016972 /* Handle the basic abort sequence (BA_ABTS) event */
16973 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
16974 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
16975 return;
16976 }
16977
James Smart4d9ab992009-10-02 15:16:39 -040016978 /* Link this frame */
16979 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
16980 if (!seq_dmabuf) {
16981 /* unable to add frame to vport - throw it out */
16982 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16983 return;
16984 }
16985 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050016986 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040016987 return;
James Smartdef9c7a2009-12-21 17:02:28 -050016988
James Smart6669f9b2009-10-02 15:16:45 -040016989 /* Send the complete sequence to the upper layer protocol */
16990 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040016991}
James Smart6fb120a2009-05-22 14:52:59 -040016992
16993/**
16994 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
16995 * @phba: pointer to lpfc hba data structure.
16996 *
16997 * This routine is invoked to post rpi header templates to the
16998 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040016999 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17000 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017001 *
17002 * This routine does not require any locks. It's usage is expected
17003 * to be driver load or reset recovery when the driver is
17004 * sequential.
17005 *
17006 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017007 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017008 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017009 * When this error occurs, the driver is not guaranteed
17010 * to have any rpi regions posted to the device and
17011 * must either attempt to repost the regions or take a
17012 * fatal error.
17013 **/
17014int
17015lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17016{
17017 struct lpfc_rpi_hdr *rpi_page;
17018 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017019 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017020
James Smart6d368e52011-05-24 11:44:12 -040017021 /* SLI4 ports that support extents do not require RPI headers. */
17022 if (!phba->sli4_hba.rpi_hdrs_in_use)
17023 goto exit;
17024 if (phba->sli4_hba.extents_in_use)
17025 return -EIO;
17026
James Smart6fb120a2009-05-22 14:52:59 -040017027 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017028 /*
17029 * Assign the rpi headers a physical rpi only if the driver
17030 * has not initialized those resources. A port reset only
17031 * needs the headers posted.
17032 */
17033 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17034 LPFC_RPI_RSRC_RDY)
17035 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17036
James Smart6fb120a2009-05-22 14:52:59 -040017037 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17038 if (rc != MBX_SUCCESS) {
17039 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17040 "2008 Error %d posting all rpi "
17041 "headers\n", rc);
17042 rc = -EIO;
17043 break;
17044 }
17045 }
17046
James Smart6d368e52011-05-24 11:44:12 -040017047 exit:
17048 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17049 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017050 return rc;
17051}
17052
17053/**
17054 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17055 * @phba: pointer to lpfc hba data structure.
17056 * @rpi_page: pointer to the rpi memory region.
17057 *
17058 * This routine is invoked to post a single rpi header to the
17059 * HBA consistent with the SLI-4 interface spec. This memory region
17060 * maps up to 64 rpi context regions.
17061 *
17062 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017063 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017064 * -ENOMEM - No available memory
17065 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017066 **/
17067int
17068lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17069{
17070 LPFC_MBOXQ_t *mboxq;
17071 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17072 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017073 uint32_t shdr_status, shdr_add_status;
17074 union lpfc_sli4_cfg_shdr *shdr;
17075
James Smart6d368e52011-05-24 11:44:12 -040017076 /* SLI4 ports that support extents do not require RPI headers. */
17077 if (!phba->sli4_hba.rpi_hdrs_in_use)
17078 return rc;
17079 if (phba->sli4_hba.extents_in_use)
17080 return -EIO;
17081
James Smart6fb120a2009-05-22 14:52:59 -040017082 /* The port is notified of the header region via a mailbox command. */
17083 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17084 if (!mboxq) {
17085 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17086 "2001 Unable to allocate memory for issuing "
17087 "SLI_CONFIG_SPECIAL mailbox command\n");
17088 return -ENOMEM;
17089 }
17090
17091 /* Post all rpi memory regions to the port. */
17092 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017093 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17094 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17095 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017096 sizeof(struct lpfc_sli4_cfg_mhdr),
17097 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017098
17099
17100 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017101 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17102 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017103 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17104 hdr_tmpl, rpi_page->page_count);
17105
James Smart6fb120a2009-05-22 14:52:59 -040017106 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17107 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017108 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017109 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17110 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17111 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17112 if (rc != MBX_TIMEOUT)
17113 mempool_free(mboxq, phba->mbox_mem_pool);
17114 if (shdr_status || shdr_add_status || rc) {
17115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17116 "2514 POST_RPI_HDR mailbox failed with "
17117 "status x%x add_status x%x, mbx status x%x\n",
17118 shdr_status, shdr_add_status, rc);
17119 rc = -ENXIO;
17120 }
17121 return rc;
17122}
17123
17124/**
17125 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17126 * @phba: pointer to lpfc hba data structure.
17127 *
17128 * This routine is invoked to post rpi header templates to the
17129 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017130 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17131 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017132 *
17133 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017134 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017135 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17136 **/
17137int
17138lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17139{
James Smart6d368e52011-05-24 11:44:12 -040017140 unsigned long rpi;
17141 uint16_t max_rpi, rpi_limit;
17142 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017143 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017144 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017145
James Smart6fb120a2009-05-22 14:52:59 -040017146 /*
James Smart6d368e52011-05-24 11:44:12 -040017147 * Fetch the next logical rpi. Because this index is logical,
17148 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017149 */
James Smart4902b382013-10-10 12:20:35 -040017150 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017151 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17152 rpi_limit = phba->sli4_hba.next_rpi;
17153
James Smart6d368e52011-05-24 11:44:12 -040017154 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17155 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017156 rpi = LPFC_RPI_ALLOC_ERROR;
17157 else {
17158 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17159 phba->sli4_hba.max_cfg_param.rpi_used++;
17160 phba->sli4_hba.rpi_count++;
17161 }
James Smartbe6bb942015-04-07 15:07:22 -040017162 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17163 "0001 rpi:%x max:%x lim:%x\n",
17164 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017165
17166 /*
17167 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017168 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017169 */
17170 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17171 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017172 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017173 return rpi;
17174 }
17175
17176 /*
James Smart6d368e52011-05-24 11:44:12 -040017177 * RPI header postings are not required for SLI4 ports capable of
17178 * extents.
17179 */
17180 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017181 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017182 return rpi;
17183 }
17184
17185 /*
James Smart6fb120a2009-05-22 14:52:59 -040017186 * If the driver is running low on rpi resources, allocate another
17187 * page now. Note that the next_rpi value is used because
17188 * it represents how many are actually in use whereas max_rpi notes
17189 * how many are supported max by the device.
17190 */
James Smart6d368e52011-05-24 11:44:12 -040017191 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017192 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017193 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17194 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17195 if (!rpi_hdr) {
17196 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17197 "2002 Error Could not grow rpi "
17198 "count\n");
17199 } else {
James Smart6d368e52011-05-24 11:44:12 -040017200 lrpi = rpi_hdr->start_rpi;
17201 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017202 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17203 }
17204 }
17205
17206 return rpi;
17207}
17208
17209/**
17210 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17211 * @phba: pointer to lpfc hba data structure.
17212 *
17213 * This routine is invoked to release an rpi to the pool of
17214 * available rpis maintained by the driver.
17215 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017216static void
James Smartd7c47992010-06-08 18:31:54 -040017217__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17218{
17219 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17220 phba->sli4_hba.rpi_count--;
17221 phba->sli4_hba.max_cfg_param.rpi_used--;
17222 }
17223}
17224
17225/**
17226 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17227 * @phba: pointer to lpfc hba data structure.
17228 *
17229 * This routine is invoked to release an rpi to the pool of
17230 * available rpis maintained by the driver.
17231 **/
17232void
James Smart6fb120a2009-05-22 14:52:59 -040017233lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17234{
17235 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017236 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017237 spin_unlock_irq(&phba->hbalock);
17238}
17239
17240/**
17241 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17242 * @phba: pointer to lpfc hba data structure.
17243 *
17244 * This routine is invoked to remove the memory region that
17245 * provided rpi via a bitmask.
17246 **/
17247void
17248lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17249{
17250 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017251 kfree(phba->sli4_hba.rpi_ids);
17252 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017253}
17254
17255/**
17256 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17257 * @phba: pointer to lpfc hba data structure.
17258 *
17259 * This routine is invoked to remove the memory region that
17260 * provided rpi via a bitmask.
17261 **/
17262int
James Smart6b5151f2012-01-18 16:24:06 -050017263lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17264 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017265{
17266 LPFC_MBOXQ_t *mboxq;
17267 struct lpfc_hba *phba = ndlp->phba;
17268 int rc;
17269
17270 /* The port is notified of the header region via a mailbox command. */
17271 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17272 if (!mboxq)
17273 return -ENOMEM;
17274
17275 /* Post all rpi memory regions to the port. */
17276 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017277 if (cmpl) {
17278 mboxq->mbox_cmpl = cmpl;
17279 mboxq->context1 = arg;
17280 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017281 } else
17282 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017283 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017284 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17285 if (rc == MBX_NOT_FINISHED) {
17286 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17287 "2010 Resume RPI Mailbox failed "
17288 "status %d, mbxStatus x%x\n", rc,
17289 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17290 mempool_free(mboxq, phba->mbox_mem_pool);
17291 return -EIO;
17292 }
17293 return 0;
17294}
17295
17296/**
17297 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017298 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017299 *
James Smart76a95d72010-11-20 23:11:48 -050017300 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017301 *
17302 * Returns:
17303 * 0 success
17304 * -Evalue otherwise
17305 **/
17306int
James Smart76a95d72010-11-20 23:11:48 -050017307lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017308{
17309 LPFC_MBOXQ_t *mboxq;
17310 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017311 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017312 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017313 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017314 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17315 if (!mboxq)
17316 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017317 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017318 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017319 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017320 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017321 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017322 "2022 INIT VPI Mailbox failed "
17323 "status %d, mbxStatus x%x\n", rc,
17324 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017325 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017326 }
James Smart6a9c52c2009-10-02 15:16:51 -040017327 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017328 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017329
17330 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017331}
17332
17333/**
17334 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17335 * @phba: pointer to lpfc hba data structure.
17336 * @mboxq: Pointer to mailbox object.
17337 *
17338 * This routine is invoked to manually add a single FCF record. The caller
17339 * must pass a completely initialized FCF_Record. This routine takes
17340 * care of the nonembedded mailbox operations.
17341 **/
17342static void
17343lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17344{
17345 void *virt_addr;
17346 union lpfc_sli4_cfg_shdr *shdr;
17347 uint32_t shdr_status, shdr_add_status;
17348
17349 virt_addr = mboxq->sge_array->addr[0];
17350 /* The IOCTL status is embedded in the mailbox subheader. */
17351 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17352 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17353 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17354
17355 if ((shdr_status || shdr_add_status) &&
17356 (shdr_status != STATUS_FCF_IN_USE))
17357 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17358 "2558 ADD_FCF_RECORD mailbox failed with "
17359 "status x%x add_status x%x\n",
17360 shdr_status, shdr_add_status);
17361
17362 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17363}
17364
17365/**
17366 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17367 * @phba: pointer to lpfc hba data structure.
17368 * @fcf_record: pointer to the initialized fcf record to add.
17369 *
17370 * This routine is invoked to manually add a single FCF record. The caller
17371 * must pass a completely initialized FCF_Record. This routine takes
17372 * care of the nonembedded mailbox operations.
17373 **/
17374int
17375lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17376{
17377 int rc = 0;
17378 LPFC_MBOXQ_t *mboxq;
17379 uint8_t *bytep;
17380 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017381 struct lpfc_mbx_sge sge;
17382 uint32_t alloc_len, req_len;
17383 uint32_t fcfindex;
17384
17385 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17386 if (!mboxq) {
17387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17388 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17389 return -ENOMEM;
17390 }
17391
17392 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17393 sizeof(uint32_t);
17394
17395 /* Allocate DMA memory and set up the non-embedded mailbox command */
17396 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17397 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17398 req_len, LPFC_SLI4_MBX_NEMBED);
17399 if (alloc_len < req_len) {
17400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17401 "2523 Allocated DMA memory size (x%x) is "
17402 "less than the requested DMA memory "
17403 "size (x%x)\n", alloc_len, req_len);
17404 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17405 return -ENOMEM;
17406 }
17407
17408 /*
17409 * Get the first SGE entry from the non-embedded DMA memory. This
17410 * routine only uses a single SGE.
17411 */
17412 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040017413 virt_addr = mboxq->sge_array->addr[0];
17414 /*
17415 * Configure the FCF record for FCFI 0. This is the driver's
17416 * hardcoded default and gets used in nonFIP mode.
17417 */
17418 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17419 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17420 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17421
17422 /*
17423 * Copy the fcf_index and the FCF Record Data. The data starts after
17424 * the FCoE header plus word10. The data copy needs to be endian
17425 * correct.
17426 */
17427 bytep += sizeof(uint32_t);
17428 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17429 mboxq->vport = phba->pport;
17430 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17431 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17432 if (rc == MBX_NOT_FINISHED) {
17433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17434 "2515 ADD_FCF_RECORD mailbox failed with "
17435 "status 0x%x\n", rc);
17436 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17437 rc = -EIO;
17438 } else
17439 rc = 0;
17440
17441 return rc;
17442}
17443
17444/**
17445 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17446 * @phba: pointer to lpfc hba data structure.
17447 * @fcf_record: pointer to the fcf record to write the default data.
17448 * @fcf_index: FCF table entry index.
17449 *
17450 * This routine is invoked to build the driver's default FCF record. The
17451 * values used are hardcoded. This routine handles memory initialization.
17452 *
17453 **/
17454void
17455lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17456 struct fcf_record *fcf_record,
17457 uint16_t fcf_index)
17458{
17459 memset(fcf_record, 0, sizeof(struct fcf_record));
17460 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17461 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17462 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17463 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17464 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17465 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17466 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17467 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17468 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17469 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17470 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17471 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17472 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040017473 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040017474 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17475 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17476 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17477 /* Set the VLAN bit map */
17478 if (phba->valid_vlan) {
17479 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17480 = 1 << (phba->vlan_id % 8);
17481 }
17482}
17483
17484/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017485 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040017486 * @phba: pointer to lpfc hba data structure.
17487 * @fcf_index: FCF table entry offset.
17488 *
James Smart0c9ab6f2010-02-26 14:15:57 -050017489 * This routine is invoked to scan the entire FCF table by reading FCF
17490 * record and processing it one at a time starting from the @fcf_index
17491 * for initial FCF discovery or fast FCF failover rediscovery.
17492 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017493 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017494 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040017495 **/
17496int
James Smart0c9ab6f2010-02-26 14:15:57 -050017497lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040017498{
17499 int rc = 0, error;
17500 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040017501
James Smart32b97932009-07-19 10:01:21 -040017502 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050017503 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040017504 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17505 if (!mboxq) {
17506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17507 "2000 Failed to allocate mbox for "
17508 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040017509 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050017510 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017511 }
James Smartecfd03c2010-02-12 14:41:27 -050017512 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050017513 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050017514 if (rc) {
17515 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050017516 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017517 }
James Smartecfd03c2010-02-12 14:41:27 -050017518 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040017519 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050017520 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040017521
17522 spin_lock_irq(&phba->hbalock);
17523 phba->hba_flag |= FCF_TS_INPROG;
17524 spin_unlock_irq(&phba->hbalock);
17525
James Smart6fb120a2009-05-22 14:52:59 -040017526 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050017527 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040017528 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050017529 else {
James Smart38b92ef2010-08-04 16:11:39 -040017530 /* Reset eligible FCF count for new scan */
17531 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040017532 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017533 error = 0;
James Smart32b97932009-07-19 10:01:21 -040017534 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017535fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040017536 if (error) {
17537 if (mboxq)
17538 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040017539 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040017540 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040017541 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040017542 spin_unlock_irq(&phba->hbalock);
17543 }
James Smart6fb120a2009-05-22 14:52:59 -040017544 return error;
17545}
James Smarta0c87cb2009-07-19 10:01:10 -040017546
17547/**
James Smarta93ff372010-10-22 11:06:08 -040017548 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050017549 * @phba: pointer to lpfc hba data structure.
17550 * @fcf_index: FCF table entry offset.
17551 *
17552 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040017553 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050017554 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017555 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017556 * otherwise.
17557 **/
17558int
17559lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17560{
17561 int rc = 0, error;
17562 LPFC_MBOXQ_t *mboxq;
17563
17564 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17565 if (!mboxq) {
17566 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17567 "2763 Failed to allocate mbox for "
17568 "READ_FCF cmd\n");
17569 error = -ENOMEM;
17570 goto fail_fcf_read;
17571 }
17572 /* Construct the read FCF record mailbox command */
17573 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17574 if (rc) {
17575 error = -EINVAL;
17576 goto fail_fcf_read;
17577 }
17578 /* Issue the mailbox command asynchronously */
17579 mboxq->vport = phba->pport;
17580 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17581 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17582 if (rc == MBX_NOT_FINISHED)
17583 error = -EIO;
17584 else
17585 error = 0;
17586
17587fail_fcf_read:
17588 if (error && mboxq)
17589 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17590 return error;
17591}
17592
17593/**
17594 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17595 * @phba: pointer to lpfc hba data structure.
17596 * @fcf_index: FCF table entry offset.
17597 *
17598 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040017599 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050017600 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017601 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017602 * otherwise.
17603 **/
17604int
17605lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17606{
17607 int rc = 0, error;
17608 LPFC_MBOXQ_t *mboxq;
17609
17610 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17611 if (!mboxq) {
17612 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17613 "2758 Failed to allocate mbox for "
17614 "READ_FCF cmd\n");
17615 error = -ENOMEM;
17616 goto fail_fcf_read;
17617 }
17618 /* Construct the read FCF record mailbox command */
17619 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17620 if (rc) {
17621 error = -EINVAL;
17622 goto fail_fcf_read;
17623 }
17624 /* Issue the mailbox command asynchronously */
17625 mboxq->vport = phba->pport;
17626 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17627 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17628 if (rc == MBX_NOT_FINISHED)
17629 error = -EIO;
17630 else
17631 error = 0;
17632
17633fail_fcf_read:
17634 if (error && mboxq)
17635 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17636 return error;
17637}
17638
17639/**
James Smartf5cb5302015-12-16 18:11:52 -050017640 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040017641 * phba pointer to the lpfc_hba struct for this port.
17642 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17643 * routine when the rr_bmask is empty. The FCF indecies are put into the
17644 * rr_bmask based on their priority level. Starting from the highest priority
17645 * to the lowest. The most likely FCF candidate will be in the highest
17646 * priority group. When this routine is called it searches the fcf_pri list for
17647 * next lowest priority group and repopulates the rr_bmask with only those
17648 * fcf_indexes.
17649 * returns:
17650 * 1=success 0=failure
17651 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017652static int
James Smart7d791df2011-07-22 18:37:52 -040017653lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17654{
17655 uint16_t next_fcf_pri;
17656 uint16_t last_index;
17657 struct lpfc_fcf_pri *fcf_pri;
17658 int rc;
17659 int ret = 0;
17660
17661 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17662 LPFC_SLI4_FCF_TBL_INDX_MAX);
17663 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17664 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050017665
17666 /* Verify the priority list has 2 or more entries */
17667 spin_lock_irq(&phba->hbalock);
17668 if (list_empty(&phba->fcf.fcf_pri_list) ||
17669 list_is_singular(&phba->fcf.fcf_pri_list)) {
17670 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040017671 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17672 "3061 Last IDX %d\n", last_index);
17673 return 0; /* Empty rr list */
17674 }
James Smart25626692013-03-01 16:36:54 -050017675 spin_unlock_irq(&phba->hbalock);
17676
James Smart7d791df2011-07-22 18:37:52 -040017677 next_fcf_pri = 0;
17678 /*
17679 * Clear the rr_bmask and set all of the bits that are at this
17680 * priority.
17681 */
17682 memset(phba->fcf.fcf_rr_bmask, 0,
17683 sizeof(*phba->fcf.fcf_rr_bmask));
17684 spin_lock_irq(&phba->hbalock);
17685 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17686 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
17687 continue;
17688 /*
17689 * the 1st priority that has not FLOGI failed
17690 * will be the highest.
17691 */
17692 if (!next_fcf_pri)
17693 next_fcf_pri = fcf_pri->fcf_rec.priority;
17694 spin_unlock_irq(&phba->hbalock);
17695 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17696 rc = lpfc_sli4_fcf_rr_index_set(phba,
17697 fcf_pri->fcf_rec.fcf_index);
17698 if (rc)
17699 return 0;
17700 }
17701 spin_lock_irq(&phba->hbalock);
17702 }
17703 /*
17704 * if next_fcf_pri was not set above and the list is not empty then
17705 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070017706 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040017707 */
17708 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
17709 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17710 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
17711 /*
17712 * the 1st priority that has not FLOGI failed
17713 * will be the highest.
17714 */
17715 if (!next_fcf_pri)
17716 next_fcf_pri = fcf_pri->fcf_rec.priority;
17717 spin_unlock_irq(&phba->hbalock);
17718 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17719 rc = lpfc_sli4_fcf_rr_index_set(phba,
17720 fcf_pri->fcf_rec.fcf_index);
17721 if (rc)
17722 return 0;
17723 }
17724 spin_lock_irq(&phba->hbalock);
17725 }
17726 } else
17727 ret = 1;
17728 spin_unlock_irq(&phba->hbalock);
17729
17730 return ret;
17731}
17732/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017733 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17734 * @phba: pointer to lpfc hba data structure.
17735 *
17736 * This routine is to get the next eligible FCF record index in a round
17737 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040017738 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050017739 * shall be returned, otherwise, the next eligible FCF record's index
17740 * shall be returned.
17741 **/
17742uint16_t
17743lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17744{
17745 uint16_t next_fcf_index;
17746
James Smart421c6622013-01-03 15:44:16 -050017747initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040017748 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050017749 next_fcf_index = phba->fcf.current_rec.fcf_indx;
17750
James Smart7d791df2011-07-22 18:37:52 -040017751next_priority:
James Smart421c6622013-01-03 15:44:16 -050017752 /* Determine the next fcf index to check */
17753 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050017754 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17755 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040017756 next_fcf_index);
17757
James Smart0c9ab6f2010-02-26 14:15:57 -050017758 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040017759 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17760 /*
17761 * If we have wrapped then we need to clear the bits that
17762 * have been tested so that we can detect when we should
17763 * change the priority level.
17764 */
James Smart0c9ab6f2010-02-26 14:15:57 -050017765 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17766 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040017767 }
17768
James Smart0c9ab6f2010-02-26 14:15:57 -050017769
James Smart3804dc82010-07-14 15:31:37 -040017770 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040017771 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17772 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17773 /*
17774 * If next fcf index is not found check if there are lower
17775 * Priority level fcf's in the fcf_priority list.
17776 * Set up the rr_bmask with all of the avaiable fcf bits
17777 * at that level and continue the selection process.
17778 */
17779 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050017780 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040017781 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17782 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040017783 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
17784 return LPFC_FCOE_FCF_NEXT_NONE;
17785 else {
17786 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17787 "3063 Only FCF available idx %d, flag %x\n",
17788 next_fcf_index,
17789 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
17790 return next_fcf_index;
17791 }
James Smart3804dc82010-07-14 15:31:37 -040017792 }
17793
James Smart7d791df2011-07-22 18:37:52 -040017794 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
17795 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050017796 LPFC_FCF_FLOGI_FAILED) {
17797 if (list_is_singular(&phba->fcf.fcf_pri_list))
17798 return LPFC_FCOE_FCF_NEXT_NONE;
17799
James Smart7d791df2011-07-22 18:37:52 -040017800 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050017801 }
James Smart7d791df2011-07-22 18:37:52 -040017802
James Smart3804dc82010-07-14 15:31:37 -040017803 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017804 "2845 Get next roundrobin failover FCF (x%x)\n",
17805 next_fcf_index);
17806
James Smart0c9ab6f2010-02-26 14:15:57 -050017807 return next_fcf_index;
17808}
17809
17810/**
17811 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
17812 * @phba: pointer to lpfc hba data structure.
17813 *
17814 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017815 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017816 * does not go beyond the range of the driver allocated bmask dimension
17817 * before setting the bit.
17818 *
17819 * Returns 0 if the index bit successfully set, otherwise, it returns
17820 * -EINVAL.
17821 **/
17822int
17823lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
17824{
17825 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17826 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017827 "2610 FCF (x%x) reached driver's book "
17828 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017829 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17830 return -EINVAL;
17831 }
17832 /* Set the eligible FCF record index bmask */
17833 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17834
James Smart3804dc82010-07-14 15:31:37 -040017835 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017836 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040017837 "bmask\n", fcf_index);
17838
James Smart0c9ab6f2010-02-26 14:15:57 -050017839 return 0;
17840}
17841
17842/**
James Smart3804dc82010-07-14 15:31:37 -040017843 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050017844 * @phba: pointer to lpfc hba data structure.
17845 *
17846 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017847 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017848 * does not go beyond the range of the driver allocated bmask dimension
17849 * before clearing the bit.
17850 **/
17851void
17852lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
17853{
James Smart9a803a72013-09-06 12:17:56 -040017854 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050017855 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17856 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017857 "2762 FCF (x%x) reached driver's book "
17858 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017859 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17860 return;
17861 }
17862 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040017863 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040017864 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
17865 list) {
James Smart7d791df2011-07-22 18:37:52 -040017866 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
17867 list_del_init(&fcf_pri->list);
17868 break;
17869 }
17870 }
17871 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017872 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040017873
17874 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017875 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040017876 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050017877}
17878
17879/**
James Smartecfd03c2010-02-12 14:41:27 -050017880 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
17881 * @phba: pointer to lpfc hba data structure.
17882 *
17883 * This routine is the completion routine for the rediscover FCF table mailbox
17884 * command. If the mailbox command returned failure, it will try to stop the
17885 * FCF rediscover wait timer.
17886 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017887static void
James Smartecfd03c2010-02-12 14:41:27 -050017888lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
17889{
17890 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17891 uint32_t shdr_status, shdr_add_status;
17892
17893 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17894
17895 shdr_status = bf_get(lpfc_mbox_hdr_status,
17896 &redisc_fcf->header.cfg_shdr.response);
17897 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
17898 &redisc_fcf->header.cfg_shdr.response);
17899 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050017900 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050017901 "2746 Requesting for FCF rediscovery failed "
17902 "status x%x add_status x%x\n",
17903 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050017904 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050017905 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017906 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050017907 spin_unlock_irq(&phba->hbalock);
17908 /*
17909 * CVL event triggered FCF rediscover request failed,
17910 * last resort to re-try current registered FCF entry.
17911 */
17912 lpfc_retry_pport_discovery(phba);
17913 } else {
17914 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017915 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050017916 spin_unlock_irq(&phba->hbalock);
17917 /*
17918 * DEAD FCF event triggered FCF rediscover request
17919 * failed, last resort to fail over as a link down
17920 * to FCF registration.
17921 */
17922 lpfc_sli4_fcf_dead_failthrough(phba);
17923 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017924 } else {
17925 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017926 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050017927 /*
17928 * Start FCF rediscovery wait timer for pending FCF
17929 * before rescan FCF record table.
17930 */
17931 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050017932 }
James Smartecfd03c2010-02-12 14:41:27 -050017933
17934 mempool_free(mbox, phba->mbox_mem_pool);
17935}
17936
17937/**
James Smart3804dc82010-07-14 15:31:37 -040017938 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050017939 * @phba: pointer to lpfc hba data structure.
17940 *
17941 * This routine is invoked to request for rediscovery of the entire FCF table
17942 * by the port.
17943 **/
17944int
17945lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
17946{
17947 LPFC_MBOXQ_t *mbox;
17948 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17949 int rc, length;
17950
James Smart0c9ab6f2010-02-26 14:15:57 -050017951 /* Cancel retry delay timers to all vports before FCF rediscover */
17952 lpfc_cancel_all_vport_retry_delay_timer(phba);
17953
James Smartecfd03c2010-02-12 14:41:27 -050017954 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17955 if (!mbox) {
17956 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17957 "2745 Failed to allocate mbox for "
17958 "requesting FCF rediscover.\n");
17959 return -ENOMEM;
17960 }
17961
17962 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
17963 sizeof(struct lpfc_sli4_cfg_mhdr));
17964 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17965 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
17966 length, LPFC_SLI4_MBX_EMBED);
17967
17968 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17969 /* Set count to 0 for invalidating the entire FCF database */
17970 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
17971
17972 /* Issue the mailbox command asynchronously */
17973 mbox->vport = phba->pport;
17974 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
17975 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
17976
17977 if (rc == MBX_NOT_FINISHED) {
17978 mempool_free(mbox, phba->mbox_mem_pool);
17979 return -EIO;
17980 }
17981 return 0;
17982}
17983
17984/**
James Smartfc2b9892010-02-26 14:15:29 -050017985 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
17986 * @phba: pointer to lpfc hba data structure.
17987 *
17988 * This function is the failover routine as a last resort to the FCF DEAD
17989 * event when driver failed to perform fast FCF failover.
17990 **/
17991void
17992lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
17993{
17994 uint32_t link_state;
17995
17996 /*
17997 * Last resort as FCF DEAD event failover will treat this as
17998 * a link down, but save the link state because we don't want
17999 * it to be changed to Link Down unless it is already down.
18000 */
18001 link_state = phba->link_state;
18002 lpfc_linkdown(phba);
18003 phba->link_state = link_state;
18004
18005 /* Unregister FCF if no devices connected to it */
18006 lpfc_unregister_unused_fcf(phba);
18007}
18008
18009/**
James Smart026abb82011-12-13 13:20:45 -050018010 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018011 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018012 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018013 *
James Smart026abb82011-12-13 13:20:45 -050018014 * This function gets SLI3 port configure region 23 data through memory dump
18015 * mailbox command. When it successfully retrieves data, the size of the data
18016 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018017 **/
James Smart026abb82011-12-13 13:20:45 -050018018static uint32_t
18019lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018020{
18021 LPFC_MBOXQ_t *pmb = NULL;
18022 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018023 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018024 int rc;
18025
James Smart026abb82011-12-13 13:20:45 -050018026 if (!rgn23_data)
18027 return 0;
18028
James Smarta0c87cb2009-07-19 10:01:10 -040018029 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18030 if (!pmb) {
18031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018032 "2600 failed to allocate mailbox memory\n");
18033 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018034 }
18035 mb = &pmb->u.mb;
18036
James Smarta0c87cb2009-07-19 10:01:10 -040018037 do {
18038 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18039 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18040
18041 if (rc != MBX_SUCCESS) {
18042 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018043 "2601 failed to read config "
18044 "region 23, rc 0x%x Status 0x%x\n",
18045 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018046 mb->un.varDmp.word_cnt = 0;
18047 }
18048 /*
18049 * dump mem may return a zero when finished or we got a
18050 * mailbox error, either way we are done.
18051 */
18052 if (mb->un.varDmp.word_cnt == 0)
18053 break;
18054 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18055 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18056
18057 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018058 rgn23_data + offset,
18059 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018060 offset += mb->un.varDmp.word_cnt;
18061 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18062
James Smart026abb82011-12-13 13:20:45 -050018063 mempool_free(pmb, phba->mbox_mem_pool);
18064 return offset;
18065}
18066
18067/**
18068 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18069 * @phba: pointer to lpfc hba data structure.
18070 * @rgn23_data: pointer to configure region 23 data.
18071 *
18072 * This function gets SLI4 port configure region 23 data through memory dump
18073 * mailbox command. When it successfully retrieves data, the size of the data
18074 * will be returned, otherwise, 0 will be returned.
18075 **/
18076static uint32_t
18077lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18078{
18079 LPFC_MBOXQ_t *mboxq = NULL;
18080 struct lpfc_dmabuf *mp = NULL;
18081 struct lpfc_mqe *mqe;
18082 uint32_t data_length = 0;
18083 int rc;
18084
18085 if (!rgn23_data)
18086 return 0;
18087
18088 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18089 if (!mboxq) {
18090 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18091 "3105 failed to allocate mailbox memory\n");
18092 return 0;
18093 }
18094
18095 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18096 goto out;
18097 mqe = &mboxq->u.mqe;
18098 mp = (struct lpfc_dmabuf *) mboxq->context1;
18099 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18100 if (rc)
18101 goto out;
18102 data_length = mqe->un.mb_words[5];
18103 if (data_length == 0)
18104 goto out;
18105 if (data_length > DMP_RGN23_SIZE) {
18106 data_length = 0;
18107 goto out;
18108 }
18109 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18110out:
18111 mempool_free(mboxq, phba->mbox_mem_pool);
18112 if (mp) {
18113 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18114 kfree(mp);
18115 }
18116 return data_length;
18117}
18118
18119/**
18120 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18121 * @phba: pointer to lpfc hba data structure.
18122 *
18123 * This function read region 23 and parse TLV for port status to
18124 * decide if the user disaled the port. If the TLV indicates the
18125 * port is disabled, the hba_flag is set accordingly.
18126 **/
18127void
18128lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18129{
18130 uint8_t *rgn23_data = NULL;
18131 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18132 uint32_t offset = 0;
18133
18134 /* Get adapter Region 23 data */
18135 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18136 if (!rgn23_data)
18137 goto out;
18138
18139 if (phba->sli_rev < LPFC_SLI_REV4)
18140 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18141 else {
18142 if_type = bf_get(lpfc_sli_intf_if_type,
18143 &phba->sli4_hba.sli_intf);
18144 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18145 goto out;
18146 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18147 }
James Smarta0c87cb2009-07-19 10:01:10 -040018148
18149 if (!data_size)
18150 goto out;
18151
18152 /* Check the region signature first */
18153 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18154 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18155 "2619 Config region 23 has bad signature\n");
18156 goto out;
18157 }
18158 offset += 4;
18159
18160 /* Check the data structure version */
18161 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18163 "2620 Config region 23 has bad version\n");
18164 goto out;
18165 }
18166 offset += 4;
18167
18168 /* Parse TLV entries in the region */
18169 while (offset < data_size) {
18170 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18171 break;
18172 /*
18173 * If the TLV is not driver specific TLV or driver id is
18174 * not linux driver id, skip the record.
18175 */
18176 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18177 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18178 (rgn23_data[offset + 3] != 0)) {
18179 offset += rgn23_data[offset + 1] * 4 + 4;
18180 continue;
18181 }
18182
18183 /* Driver found a driver specific TLV in the config region */
18184 sub_tlv_len = rgn23_data[offset + 1] * 4;
18185 offset += 4;
18186 tlv_offset = 0;
18187
18188 /*
18189 * Search for configured port state sub-TLV.
18190 */
18191 while ((offset < data_size) &&
18192 (tlv_offset < sub_tlv_len)) {
18193 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18194 offset += 4;
18195 tlv_offset += 4;
18196 break;
18197 }
18198 if (rgn23_data[offset] != PORT_STE_TYPE) {
18199 offset += rgn23_data[offset + 1] * 4 + 4;
18200 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18201 continue;
18202 }
18203
18204 /* This HBA contains PORT_STE configured */
18205 if (!rgn23_data[offset + 2])
18206 phba->hba_flag |= LINK_DISABLED;
18207
18208 goto out;
18209 }
18210 }
James Smart026abb82011-12-13 13:20:45 -050018211
James Smarta0c87cb2009-07-19 10:01:10 -040018212out:
James Smarta0c87cb2009-07-19 10:01:10 -040018213 kfree(rgn23_data);
18214 return;
18215}
James Smart695a8142010-01-26 23:08:03 -050018216
18217/**
James Smart52d52442011-05-24 11:42:45 -040018218 * lpfc_wr_object - write an object to the firmware
18219 * @phba: HBA structure that indicates port to create a queue on.
18220 * @dmabuf_list: list of dmabufs to write to the port.
18221 * @size: the total byte value of the objects to write to the port.
18222 * @offset: the current offset to be used to start the transfer.
18223 *
18224 * This routine will create a wr_object mailbox command to send to the port.
18225 * the mailbox command will be constructed using the dma buffers described in
18226 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18227 * BDEs that the imbedded mailbox can support. The @offset variable will be
18228 * used to indicate the starting offset of the transfer and will also return
18229 * the offset after the write object mailbox has completed. @size is used to
18230 * determine the end of the object and whether the eof bit should be set.
18231 *
18232 * Return 0 is successful and offset will contain the the new offset to use
18233 * for the next write.
18234 * Return negative value for error cases.
18235 **/
18236int
18237lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18238 uint32_t size, uint32_t *offset)
18239{
18240 struct lpfc_mbx_wr_object *wr_object;
18241 LPFC_MBOXQ_t *mbox;
18242 int rc = 0, i = 0;
18243 uint32_t shdr_status, shdr_add_status;
18244 uint32_t mbox_tmo;
18245 union lpfc_sli4_cfg_shdr *shdr;
18246 struct lpfc_dmabuf *dmabuf;
18247 uint32_t written = 0;
18248
18249 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18250 if (!mbox)
18251 return -ENOMEM;
18252
18253 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18254 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18255 sizeof(struct lpfc_mbx_wr_object) -
18256 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18257
18258 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18259 wr_object->u.request.write_offset = *offset;
18260 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18261 wr_object->u.request.object_name[0] =
18262 cpu_to_le32(wr_object->u.request.object_name[0]);
18263 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18264 list_for_each_entry(dmabuf, dmabuf_list, list) {
18265 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18266 break;
18267 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18268 wr_object->u.request.bde[i].addrHigh =
18269 putPaddrHigh(dmabuf->phys);
18270 if (written + SLI4_PAGE_SIZE >= size) {
18271 wr_object->u.request.bde[i].tus.f.bdeSize =
18272 (size - written);
18273 written += (size - written);
18274 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18275 } else {
18276 wr_object->u.request.bde[i].tus.f.bdeSize =
18277 SLI4_PAGE_SIZE;
18278 written += SLI4_PAGE_SIZE;
18279 }
18280 i++;
18281 }
18282 wr_object->u.request.bde_count = i;
18283 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18284 if (!phba->sli4_hba.intr_enable)
18285 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18286 else {
James Smarta183a152011-10-10 21:32:43 -040018287 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018288 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18289 }
18290 /* The IOCTL status is embedded in the mailbox subheader. */
18291 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18292 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18293 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18294 if (rc != MBX_TIMEOUT)
18295 mempool_free(mbox, phba->mbox_mem_pool);
18296 if (shdr_status || shdr_add_status || rc) {
18297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18298 "3025 Write Object mailbox failed with "
18299 "status x%x add_status x%x, mbx status x%x\n",
18300 shdr_status, shdr_add_status, rc);
18301 rc = -ENXIO;
18302 } else
18303 *offset += wr_object->u.response.actual_write_length;
18304 return rc;
18305}
18306
18307/**
James Smart695a8142010-01-26 23:08:03 -050018308 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18309 * @vport: pointer to vport data structure.
18310 *
18311 * This function iterate through the mailboxq and clean up all REG_LOGIN
18312 * and REG_VPI mailbox commands associated with the vport. This function
18313 * is called when driver want to restart discovery of the vport due to
18314 * a Clear Virtual Link event.
18315 **/
18316void
18317lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18318{
18319 struct lpfc_hba *phba = vport->phba;
18320 LPFC_MBOXQ_t *mb, *nextmb;
18321 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018322 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018323 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018324 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018325 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018326 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018327
James Smartd439d282010-09-29 11:18:45 -040018328 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018329 spin_lock_irq(&phba->hbalock);
18330 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18331 if (mb->vport != vport)
18332 continue;
18333
18334 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18335 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18336 continue;
18337
James Smartd439d282010-09-29 11:18:45 -040018338 list_del(&mb->list);
18339 list_add_tail(&mb->list, &mbox_cmd_list);
18340 }
18341 /* Clean up active mailbox command with the vport */
18342 mb = phba->sli.mbox_active;
18343 if (mb && (mb->vport == vport)) {
18344 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18345 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18346 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18347 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18348 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18349 /* Put reference count for delayed processing */
18350 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18351 /* Unregister the RPI when mailbox complete */
18352 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18353 }
18354 }
James Smart63e801c2010-11-20 23:14:19 -050018355 /* Cleanup any mailbox completions which are not yet processed */
18356 do {
18357 restart_loop = 0;
18358 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18359 /*
18360 * If this mailox is already processed or it is
18361 * for another vport ignore it.
18362 */
18363 if ((mb->vport != vport) ||
18364 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18365 continue;
18366
18367 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18368 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18369 continue;
18370
18371 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18372 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18373 ndlp = (struct lpfc_nodelist *)mb->context2;
18374 /* Unregister the RPI when mailbox complete */
18375 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18376 restart_loop = 1;
18377 spin_unlock_irq(&phba->hbalock);
18378 spin_lock(shost->host_lock);
18379 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18380 spin_unlock(shost->host_lock);
18381 spin_lock_irq(&phba->hbalock);
18382 break;
18383 }
18384 }
18385 } while (restart_loop);
18386
James Smartd439d282010-09-29 11:18:45 -040018387 spin_unlock_irq(&phba->hbalock);
18388
18389 /* Release the cleaned-up mailbox commands */
18390 while (!list_empty(&mbox_cmd_list)) {
18391 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018392 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18393 mp = (struct lpfc_dmabuf *) (mb->context1);
18394 if (mp) {
18395 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18396 kfree(mp);
18397 }
James Smart78730cf2010-04-06 15:06:30 -040018398 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018399 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018400 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018401 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018402 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018403 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018404 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018405 }
James Smart695a8142010-01-26 23:08:03 -050018406 }
James Smart695a8142010-01-26 23:08:03 -050018407 mempool_free(mb, phba->mbox_mem_pool);
18408 }
James Smartd439d282010-09-29 11:18:45 -040018409
18410 /* Release the ndlp with the cleaned-up active mailbox command */
18411 if (act_mbx_ndlp) {
18412 spin_lock(shost->host_lock);
18413 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18414 spin_unlock(shost->host_lock);
18415 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050018416 }
James Smart695a8142010-01-26 23:08:03 -050018417}
18418
James Smart2a9bf3d2010-06-07 15:24:45 -040018419/**
18420 * lpfc_drain_txq - Drain the txq
18421 * @phba: Pointer to HBA context object.
18422 *
18423 * This function attempt to submit IOCBs on the txq
18424 * to the adapter. For SLI4 adapters, the txq contains
18425 * ELS IOCBs that have been deferred because the there
18426 * are no SGLs. This congestion can occur with large
18427 * vport counts during node discovery.
18428 **/
18429
18430uint32_t
18431lpfc_drain_txq(struct lpfc_hba *phba)
18432{
18433 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080018434 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090018435 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040018436 unsigned long iflags = 0;
18437 char *fail_msg = NULL;
18438 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080018439 union lpfc_wqe128 wqe128;
18440 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040018441 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040018442
James Smart895427b2017-02-12 13:52:30 -080018443 pring = lpfc_phba_elsring(phba);
18444
James Smart398d81c2013-05-31 17:04:19 -040018445 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050018446 list_for_each_entry(piocbq, &pring->txq, list) {
18447 txq_cnt++;
18448 }
18449
18450 if (txq_cnt > pring->txq_max)
18451 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018452
James Smart398d81c2013-05-31 17:04:19 -040018453 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018454
James Smart0e9bb8d2013-03-01 16:35:12 -050018455 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040018456 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018457
James Smart19ca7602010-11-20 23:11:55 -050018458 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040018459 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040018460 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040018461 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18462 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050018463 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040018464 break;
18465 }
James Smart895427b2017-02-12 13:52:30 -080018466 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040018467 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050018468 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040018469 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018470 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040018471 }
James Smart0e9bb8d2013-03-01 16:35:12 -050018472 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040018473
18474 /* The xri and iocb resources secured,
18475 * attempt to issue request
18476 */
James Smart6d368e52011-05-24 11:44:12 -040018477 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040018478 piocbq->sli4_xritag = sglq->sli4_xritag;
18479 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18480 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080018481 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018482 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080018483 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018484 fail_msg = " - Wq is full";
18485 else
18486 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18487
18488 if (fail_msg) {
18489 /* Failed means we can't issue and need to cancel */
18490 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18491 "2822 IOCB failed %s iotag 0x%x "
18492 "xri 0x%x\n",
18493 fail_msg,
18494 piocbq->iotag, piocbq->sli4_xritag);
18495 list_add_tail(&piocbq->list, &completions);
18496 }
James Smart398d81c2013-05-31 17:04:19 -040018497 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018498 }
18499
James Smart2a9bf3d2010-06-07 15:24:45 -040018500 /* Cancel all the IOCBs that cannot be issued */
18501 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18502 IOERR_SLI_ABORTED);
18503
James Smart0e9bb8d2013-03-01 16:35:12 -050018504 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018505}
James Smart895427b2017-02-12 13:52:30 -080018506
18507/**
18508 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18509 * @phba: Pointer to HBA context object.
18510 * @pwqe: Pointer to command WQE.
18511 * @sglq: Pointer to the scatter gather queue object.
18512 *
18513 * This routine converts the bpl or bde that is in the WQE
18514 * to a sgl list for the sli4 hardware. The physical address
18515 * of the bpl/bde is converted back to a virtual address.
18516 * If the WQE contains a BPL then the list of BDE's is
18517 * converted to sli4_sge's. If the WQE contains a single
18518 * BDE then it is converted to a single sli_sge.
18519 * The WQE is still in cpu endianness so the contents of
18520 * the bpl can be used without byte swapping.
18521 *
18522 * Returns valid XRI = Success, NO_XRI = Failure.
18523 */
18524static uint16_t
18525lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18526 struct lpfc_sglq *sglq)
18527{
18528 uint16_t xritag = NO_XRI;
18529 struct ulp_bde64 *bpl = NULL;
18530 struct ulp_bde64 bde;
18531 struct sli4_sge *sgl = NULL;
18532 struct lpfc_dmabuf *dmabuf;
18533 union lpfc_wqe *wqe;
18534 int numBdes = 0;
18535 int i = 0;
18536 uint32_t offset = 0; /* accumulated offset in the sg request list */
18537 int inbound = 0; /* number of sg reply entries inbound from firmware */
18538 uint32_t cmd;
18539
18540 if (!pwqeq || !sglq)
18541 return xritag;
18542
18543 sgl = (struct sli4_sge *)sglq->sgl;
18544 wqe = &pwqeq->wqe;
18545 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18546
18547 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18548 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18549 return sglq->sli4_xritag;
18550 numBdes = pwqeq->rsvd2;
18551 if (numBdes) {
18552 /* The addrHigh and addrLow fields within the WQE
18553 * have not been byteswapped yet so there is no
18554 * need to swap them back.
18555 */
18556 if (pwqeq->context3)
18557 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18558 else
18559 return xritag;
18560
18561 bpl = (struct ulp_bde64 *)dmabuf->virt;
18562 if (!bpl)
18563 return xritag;
18564
18565 for (i = 0; i < numBdes; i++) {
18566 /* Should already be byte swapped. */
18567 sgl->addr_hi = bpl->addrHigh;
18568 sgl->addr_lo = bpl->addrLow;
18569
18570 sgl->word2 = le32_to_cpu(sgl->word2);
18571 if ((i+1) == numBdes)
18572 bf_set(lpfc_sli4_sge_last, sgl, 1);
18573 else
18574 bf_set(lpfc_sli4_sge_last, sgl, 0);
18575 /* swap the size field back to the cpu so we
18576 * can assign it to the sgl.
18577 */
18578 bde.tus.w = le32_to_cpu(bpl->tus.w);
18579 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18580 /* The offsets in the sgl need to be accumulated
18581 * separately for the request and reply lists.
18582 * The request is always first, the reply follows.
18583 */
18584 switch (cmd) {
18585 case CMD_GEN_REQUEST64_WQE:
18586 /* add up the reply sg entries */
18587 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18588 inbound++;
18589 /* first inbound? reset the offset */
18590 if (inbound == 1)
18591 offset = 0;
18592 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18593 bf_set(lpfc_sli4_sge_type, sgl,
18594 LPFC_SGE_TYPE_DATA);
18595 offset += bde.tus.f.bdeSize;
18596 break;
18597 case CMD_FCP_TRSP64_WQE:
18598 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18599 bf_set(lpfc_sli4_sge_type, sgl,
18600 LPFC_SGE_TYPE_DATA);
18601 break;
18602 case CMD_FCP_TSEND64_WQE:
18603 case CMD_FCP_TRECEIVE64_WQE:
18604 bf_set(lpfc_sli4_sge_type, sgl,
18605 bpl->tus.f.bdeFlags);
18606 if (i < 3)
18607 offset = 0;
18608 else
18609 offset += bde.tus.f.bdeSize;
18610 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18611 break;
18612 }
18613 sgl->word2 = cpu_to_le32(sgl->word2);
18614 bpl++;
18615 sgl++;
18616 }
18617 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18618 /* The addrHigh and addrLow fields of the BDE have not
18619 * been byteswapped yet so they need to be swapped
18620 * before putting them in the sgl.
18621 */
18622 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18623 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18624 sgl->word2 = le32_to_cpu(sgl->word2);
18625 bf_set(lpfc_sli4_sge_last, sgl, 1);
18626 sgl->word2 = cpu_to_le32(sgl->word2);
18627 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18628 }
18629 return sglq->sli4_xritag;
18630}
18631
18632/**
18633 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18634 * @phba: Pointer to HBA context object.
18635 * @ring_number: Base sli ring number
18636 * @pwqe: Pointer to command WQE.
18637 **/
18638int
18639lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18640 struct lpfc_iocbq *pwqe)
18641{
18642 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080018643 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080018644 struct lpfc_queue *wq;
18645 struct lpfc_sglq *sglq;
18646 struct lpfc_sli_ring *pring;
18647 unsigned long iflags;
18648
18649 /* NVME_LS and NVME_LS ABTS requests. */
18650 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18651 pring = phba->sli4_hba.nvmels_wq->pring;
18652 spin_lock_irqsave(&pring->ring_lock, iflags);
18653 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18654 if (!sglq) {
18655 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18656 return WQE_BUSY;
18657 }
18658 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18659 pwqe->sli4_xritag = sglq->sli4_xritag;
18660 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18661 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18662 return WQE_ERROR;
18663 }
18664 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18665 pwqe->sli4_xritag);
18666 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
18667 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18668 return WQE_ERROR;
18669 }
18670 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18671 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18672 return 0;
18673 }
18674
18675 /* NVME_FCREQ and NVME_ABTS requests */
18676 if (pwqe->iocb_flag & LPFC_IO_NVME) {
18677 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18678 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18679
18680 spin_lock_irqsave(&pring->ring_lock, iflags);
18681 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18682 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18683 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18684 if (lpfc_sli4_wq_put(wq, wqe)) {
18685 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18686 return WQE_ERROR;
18687 }
18688 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18689 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18690 return 0;
18691 }
18692
James Smartf358dd02017-02-12 13:52:34 -080018693 /* NVMET requests */
18694 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
18695 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18696 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18697
18698 spin_lock_irqsave(&pring->ring_lock, iflags);
18699 ctxp = pwqe->context2;
18700 sglq = ctxp->rqb_buffer->sglq;
18701 if (pwqe->sli4_xritag == NO_XRI) {
18702 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18703 pwqe->sli4_xritag = sglq->sli4_xritag;
18704 }
18705 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18706 pwqe->sli4_xritag);
18707 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18708 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18709 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18710 if (lpfc_sli4_wq_put(wq, wqe)) {
18711 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18712 return WQE_ERROR;
18713 }
18714 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18715 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18716 return 0;
18717 }
James Smart895427b2017-02-12 13:52:30 -080018718 return WQE_ERROR;
18719}