blob: d6b184839bc2ff951233ee8fbcf477d6b133206f [file] [log] [blame]
James Smart43140ca2017-03-04 09:30:34 -08001
dea31012005-04-17 16:05:31 -05002/*******************************************************************
3 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04004 * Fibre Channel Host Bus Adapters. *
James Smartd080abe2017-02-12 13:52:39 -08005 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
6 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07007 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04008 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08009 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050011 * *
12 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040013 * modify it under the terms of version 2 of the GNU General *
14 * Public License as published by the Free Software Foundation. *
15 * This program is distributed in the hope that it will be useful. *
16 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
17 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
18 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
19 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
20 * TO BE LEGALLY INVALID. See the GNU General Public License for *
21 * more details, a copy of which can be found in the file COPYING *
22 * included with this package. *
dea31012005-04-17 16:05:31 -050023 *******************************************************************/
24
dea31012005-04-17 16:05:31 -050025#include <linux/blkdev.h>
26#include <linux/pci.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010030#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050031
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040032#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_cmnd.h>
34#include <scsi/scsi_device.h>
35#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040036#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040037#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040038#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050039
James Smart895427b2017-02-12 13:52:30 -080040#include <linux/nvme-fc-driver.h>
41
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_hw.h"
44#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040045#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040046#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080049#include "lpfc_scsi.h"
50#include "lpfc_nvme.h"
James Smartf358dd02017-02-12 13:52:34 -080051#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050052#include "lpfc_crtn.h"
53#include "lpfc_logmsg.h"
54#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050055#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040056#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070057#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050058
59/* There are only four IOCB completion types. */
60typedef enum _lpfc_iocb_type {
61 LPFC_UNKNOWN_IOCB,
62 LPFC_UNSOL_IOCB,
63 LPFC_SOL_IOCB,
64 LPFC_ABORT_IOCB
65} lpfc_iocb_type;
66
James Smart4f774512009-05-22 14:52:35 -040067
68/* Provide function prototypes local to this module. */
69static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
70 uint32_t);
71static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040072 uint8_t *, uint32_t *);
73static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
74 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040075static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
76 struct hbq_dmabuf *);
James Smartae9e28f2017-05-15 15:20:51 -070077static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
78 struct hbq_dmabuf *dmabuf);
James Smart895427b2017-02-12 13:52:30 -080079static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
James Smart05580562011-05-24 11:40:48 -040080 struct lpfc_cqe *);
James Smart895427b2017-02-12 13:52:30 -080081static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040082 int);
James Smartba20c852012-08-03 12:36:52 -040083static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
84 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040085static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
86static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart895427b2017-02-12 13:52:30 -080087static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
88 struct lpfc_sli_ring *pring,
89 struct lpfc_iocbq *cmdiocb);
James Smart05580562011-05-24 11:40:48 -040090
James Smart4f774512009-05-22 14:52:35 -040091static IOCB_t *
92lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
93{
94 return &iocbq->iocb;
95}
96
97/**
98 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
99 * @q: The Work Queue to operate on.
100 * @wqe: The work Queue Entry to put on the Work queue.
101 *
102 * This routine will copy the contents of @wqe to the next available entry on
103 * the @q. This function will then ring the Work Queue Doorbell to signal the
104 * HBA to start processing the Work Queue Entry. This function returns 0 if
105 * successful. If no entries are available on @q then this function will return
106 * -ENOMEM.
107 * The caller is expected to hold the hbalock when calling this routine.
108 **/
109static uint32_t
110lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
111{
James Smart2e90f4b2011-12-13 13:22:37 -0500112 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400113 struct lpfc_register doorbell;
114 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400115 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400116
James Smart2e90f4b2011-12-13 13:22:37 -0500117 /* sanity check on queue memory */
118 if (unlikely(!q))
119 return -ENOMEM;
120 temp_wqe = q->qe[q->host_index].wqe;
121
James Smart4f774512009-05-22 14:52:35 -0400122 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400123 idx = ((q->host_index + 1) % q->entry_count);
124 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400125 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400126 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400127 }
128 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400129 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500130 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400131 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500132 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
133 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400134 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart6b3b3bd2016-12-19 15:07:30 -0800135 /* ensure WQE bcopy flushed before doorbell write */
136 wmb();
James Smart4f774512009-05-22 14:52:35 -0400137
138 /* Update the host index before invoking device */
139 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400140
141 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400142
143 /* Ring Doorbell */
144 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500145 if (q->db_format == LPFC_DB_LIST_FORMAT) {
146 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
147 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
148 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
149 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
150 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
151 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
152 } else {
153 return -EINVAL;
154 }
155 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400156
157 return 0;
158}
159
160/**
161 * lpfc_sli4_wq_release - Updates internal hba index for WQ
162 * @q: The Work Queue to operate on.
163 * @index: The index to advance the hba index to.
164 *
165 * This routine will update the HBA index of a queue to reflect consumption of
166 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
167 * an entry the host calls this function to update the queue's internal
168 * pointers. This routine returns the number of entries that were consumed by
169 * the HBA.
170 **/
171static uint32_t
172lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
173{
174 uint32_t released = 0;
175
James Smart2e90f4b2011-12-13 13:22:37 -0500176 /* sanity check on queue memory */
177 if (unlikely(!q))
178 return 0;
179
James Smart4f774512009-05-22 14:52:35 -0400180 if (q->hba_index == index)
181 return 0;
182 do {
183 q->hba_index = ((q->hba_index + 1) % q->entry_count);
184 released++;
185 } while (q->hba_index != index);
186 return released;
187}
188
189/**
190 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
191 * @q: The Mailbox Queue to operate on.
192 * @wqe: The Mailbox Queue Entry to put on the Work queue.
193 *
194 * This routine will copy the contents of @mqe to the next available entry on
195 * the @q. This function will then ring the Work Queue Doorbell to signal the
196 * HBA to start processing the Work Queue Entry. This function returns 0 if
197 * successful. If no entries are available on @q then this function will return
198 * -ENOMEM.
199 * The caller is expected to hold the hbalock when calling this routine.
200 **/
201static uint32_t
202lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
203{
James Smart2e90f4b2011-12-13 13:22:37 -0500204 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400205 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400206
James Smart2e90f4b2011-12-13 13:22:37 -0500207 /* sanity check on queue memory */
208 if (unlikely(!q))
209 return -ENOMEM;
210 temp_mqe = q->qe[q->host_index].mqe;
211
James Smart4f774512009-05-22 14:52:35 -0400212 /* If the host has not yet processed the next entry then we are done */
213 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
214 return -ENOMEM;
215 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
216 /* Save off the mailbox pointer for completion */
217 q->phba->mbox = (MAILBOX_t *)temp_mqe;
218
219 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400220 q->host_index = ((q->host_index + 1) % q->entry_count);
221
222 /* Ring Doorbell */
223 doorbell.word0 = 0;
224 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
225 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
226 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400227 return 0;
228}
229
230/**
231 * lpfc_sli4_mq_release - Updates internal hba index for MQ
232 * @q: The Mailbox Queue to operate on.
233 *
234 * This routine will update the HBA index of a queue to reflect consumption of
235 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
236 * an entry the host calls this function to update the queue's internal
237 * pointers. This routine returns the number of entries that were consumed by
238 * the HBA.
239 **/
240static uint32_t
241lpfc_sli4_mq_release(struct lpfc_queue *q)
242{
James Smart2e90f4b2011-12-13 13:22:37 -0500243 /* sanity check on queue memory */
244 if (unlikely(!q))
245 return 0;
246
James Smart4f774512009-05-22 14:52:35 -0400247 /* Clear the mailbox pointer for completion */
248 q->phba->mbox = NULL;
249 q->hba_index = ((q->hba_index + 1) % q->entry_count);
250 return 1;
251}
252
253/**
254 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
255 * @q: The Event Queue to get the first valid EQE from
256 *
257 * This routine will get the first valid Event Queue Entry from @q, update
258 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
259 * the Queue (no more work to do), or the Queue is full of EQEs that have been
260 * processed, but not popped back to the HBA then this routine will return NULL.
261 **/
262static struct lpfc_eqe *
263lpfc_sli4_eq_get(struct lpfc_queue *q)
264{
James Smart2e90f4b2011-12-13 13:22:37 -0500265 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400266 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500267
268 /* sanity check on queue memory */
269 if (unlikely(!q))
270 return NULL;
271 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400272
273 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400274 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400275 return NULL;
276 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400277 idx = ((q->hba_index + 1) % q->entry_count);
278 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400279 return NULL;
280
James Smart027140e2012-08-03 12:35:44 -0400281 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400282
283 /*
284 * insert barrier for instruction interlock : data from the hardware
285 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800286 * upon. Speculative instructions were allowing a bcopy at the start
287 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
288 * after our return, to copy data before the valid bit check above
289 * was done. As such, some of the copied data was stale. The barrier
290 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400291 */
292 mb();
James Smart4f774512009-05-22 14:52:35 -0400293 return eqe;
294}
295
296/**
James Smartba20c852012-08-03 12:36:52 -0400297 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
298 * @q: The Event Queue to disable interrupts
299 *
300 **/
301static inline void
302lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
303{
304 struct lpfc_register doorbell;
305
306 doorbell.word0 = 0;
307 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
308 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
309 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
310 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
311 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
312 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
313}
314
315/**
James Smart4f774512009-05-22 14:52:35 -0400316 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
317 * @q: The Event Queue that the host has completed processing for.
318 * @arm: Indicates whether the host wants to arms this CQ.
319 *
320 * This routine will mark all Event Queue Entries on @q, from the last
321 * known completed entry to the last entry that was processed, as completed
322 * by clearing the valid bit for each completion queue entry. Then it will
323 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
324 * The internal host index in the @q will be updated by this routine to indicate
325 * that the host has finished processing the entries. The @arm parameter
326 * indicates that the queue should be rearmed when ringing the doorbell.
327 *
328 * This function will return the number of EQEs that were popped.
329 **/
330uint32_t
331lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
332{
333 uint32_t released = 0;
334 struct lpfc_eqe *temp_eqe;
335 struct lpfc_register doorbell;
336
James Smart2e90f4b2011-12-13 13:22:37 -0500337 /* sanity check on queue memory */
338 if (unlikely(!q))
339 return 0;
340
James Smart4f774512009-05-22 14:52:35 -0400341 /* while there are valid entries */
342 while (q->hba_index != q->host_index) {
343 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400344 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400345 released++;
346 q->host_index = ((q->host_index + 1) % q->entry_count);
347 }
348 if (unlikely(released == 0 && !arm))
349 return 0;
350
351 /* ring doorbell for number popped */
352 doorbell.word0 = 0;
353 if (arm) {
354 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
355 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
356 }
357 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
358 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500359 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
360 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
361 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400362 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500363 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
364 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
365 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400366 return released;
367}
368
369/**
370 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
371 * @q: The Completion Queue to get the first valid CQE from
372 *
373 * This routine will get the first valid Completion Queue Entry from @q, update
374 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
375 * the Queue (no more work to do), or the Queue is full of CQEs that have been
376 * processed, but not popped back to the HBA then this routine will return NULL.
377 **/
378static struct lpfc_cqe *
379lpfc_sli4_cq_get(struct lpfc_queue *q)
380{
381 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400382 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400383
James Smart2e90f4b2011-12-13 13:22:37 -0500384 /* sanity check on queue memory */
385 if (unlikely(!q))
386 return NULL;
387
James Smart4f774512009-05-22 14:52:35 -0400388 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400389 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400390 return NULL;
391 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400392 idx = ((q->hba_index + 1) % q->entry_count);
393 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400394 return NULL;
395
396 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400397 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400398
399 /*
400 * insert barrier for instruction interlock : data from the hardware
401 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800402 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
403 * instructions allowing action on content before valid bit checked,
404 * add barrier here as well. May not be needed as "content" is a
405 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400406 */
407 mb();
James Smart4f774512009-05-22 14:52:35 -0400408 return cqe;
409}
410
411/**
412 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
413 * @q: The Completion Queue that the host has completed processing for.
414 * @arm: Indicates whether the host wants to arms this CQ.
415 *
416 * This routine will mark all Completion queue entries on @q, from the last
417 * known completed entry to the last entry that was processed, as completed
418 * by clearing the valid bit for each completion queue entry. Then it will
419 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
420 * The internal host index in the @q will be updated by this routine to indicate
421 * that the host has finished processing the entries. The @arm parameter
422 * indicates that the queue should be rearmed when ringing the doorbell.
423 *
424 * This function will return the number of CQEs that were released.
425 **/
426uint32_t
427lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
428{
429 uint32_t released = 0;
430 struct lpfc_cqe *temp_qe;
431 struct lpfc_register doorbell;
432
James Smart2e90f4b2011-12-13 13:22:37 -0500433 /* sanity check on queue memory */
434 if (unlikely(!q))
435 return 0;
James Smart4f774512009-05-22 14:52:35 -0400436 /* while there are valid entries */
437 while (q->hba_index != q->host_index) {
438 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400439 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400440 released++;
441 q->host_index = ((q->host_index + 1) % q->entry_count);
442 }
443 if (unlikely(released == 0 && !arm))
444 return 0;
445
446 /* ring doorbell for number popped */
447 doorbell.word0 = 0;
448 if (arm)
449 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
450 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
451 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500452 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
453 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
454 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400455 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
456 return released;
457}
458
459/**
460 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
461 * @q: The Header Receive Queue to operate on.
462 * @wqe: The Receive Queue Entry to put on the Receive queue.
463 *
464 * This routine will copy the contents of @wqe to the next available entry on
465 * the @q. This function will then ring the Receive Queue Doorbell to signal the
466 * HBA to start processing the Receive Queue Entry. This function returns the
467 * index that the rqe was copied to if successful. If no entries are available
468 * on @q then this function will return -ENOMEM.
469 * The caller is expected to hold the hbalock when calling this routine.
470 **/
James Smart895427b2017-02-12 13:52:30 -0800471int
James Smart4f774512009-05-22 14:52:35 -0400472lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
473 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
474{
James Smart2e90f4b2011-12-13 13:22:37 -0500475 struct lpfc_rqe *temp_hrqe;
476 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400477 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500478 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400479
James Smart2e90f4b2011-12-13 13:22:37 -0500480 /* sanity check on queue memory */
481 if (unlikely(!hq) || unlikely(!dq))
482 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500483 put_index = hq->host_index;
James Smart61f3d4b2017-05-15 15:20:41 -0700484 temp_hrqe = hq->qe[put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500485 temp_drqe = dq->qe[dq->host_index].rqe;
486
James Smart4f774512009-05-22 14:52:35 -0400487 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
488 return -EINVAL;
James Smart61f3d4b2017-05-15 15:20:41 -0700489 if (put_index != dq->host_index)
James Smart4f774512009-05-22 14:52:35 -0400490 return -EINVAL;
491 /* If the host has not yet processed the next entry then we are done */
James Smart61f3d4b2017-05-15 15:20:41 -0700492 if (((put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400493 return -EBUSY;
494 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
495 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
496
497 /* Update the host index to point to the next slot */
James Smart61f3d4b2017-05-15 15:20:41 -0700498 hq->host_index = ((put_index + 1) % hq->entry_count);
James Smart4f774512009-05-22 14:52:35 -0400499 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700500 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400501
502 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400503 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400504 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500505 if (hq->db_format == LPFC_DB_RING_FORMAT) {
506 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
507 hq->entry_repost);
508 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
509 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
510 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
511 hq->entry_repost);
512 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
513 hq->host_index);
514 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
515 } else {
516 return -EINVAL;
517 }
518 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400519 }
520 return put_index;
521}
522
523/**
524 * lpfc_sli4_rq_release - Updates internal hba index for RQ
525 * @q: The Header Receive Queue to operate on.
526 *
527 * This routine will update the HBA index of a queue to reflect consumption of
528 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
529 * consumed an entry the host calls this function to update the queue's
530 * internal pointers. This routine returns the number of entries that were
531 * consumed by the HBA.
532 **/
533static uint32_t
534lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
535{
James Smart2e90f4b2011-12-13 13:22:37 -0500536 /* sanity check on queue memory */
537 if (unlikely(!hq) || unlikely(!dq))
538 return 0;
539
James Smart4f774512009-05-22 14:52:35 -0400540 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
541 return 0;
542 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
543 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
544 return 1;
545}
546
James Smarte59058c2008-08-24 21:49:00 -0400547/**
James Smart3621a712009-04-06 18:47:14 -0400548 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400549 * @phba: Pointer to HBA context object.
550 * @pring: Pointer to driver SLI ring object.
551 *
552 * This function returns pointer to next command iocb entry
553 * in the command ring. The caller must hold hbalock to prevent
554 * other threads consume the next command iocb.
555 * SLI-2/SLI-3 provide different sized iocbs.
556 **/
James Smarted957682007-06-17 19:56:37 -0500557static inline IOCB_t *
558lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
559{
James Smart7e56aa22012-08-03 12:35:34 -0400560 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
561 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500562}
563
James Smarte59058c2008-08-24 21:49:00 -0400564/**
James Smart3621a712009-04-06 18:47:14 -0400565 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400566 * @phba: Pointer to HBA context object.
567 * @pring: Pointer to driver SLI ring object.
568 *
569 * This function returns pointer to next response iocb entry
570 * in the response ring. The caller must hold hbalock to make sure
571 * that no other thread consume the next response iocb.
572 * SLI-2/SLI-3 provide different sized iocbs.
573 **/
James Smarted957682007-06-17 19:56:37 -0500574static inline IOCB_t *
575lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
576{
James Smart7e56aa22012-08-03 12:35:34 -0400577 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
578 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500579}
580
James Smarte59058c2008-08-24 21:49:00 -0400581/**
James Smart3621a712009-04-06 18:47:14 -0400582 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400583 * @phba: Pointer to HBA context object.
584 *
585 * This function is called with hbalock held. This function
586 * allocates a new driver iocb object from the iocb pool. If the
587 * allocation is successful, it returns pointer to the newly
588 * allocated iocb object else it returns NULL.
589 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400590struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500591__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400592{
593 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
594 struct lpfc_iocbq * iocbq = NULL;
595
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100596 lockdep_assert_held(&phba->hbalock);
597
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400598 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400599 if (iocbq)
600 phba->iocb_cnt++;
601 if (phba->iocb_cnt > phba->iocb_max)
602 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400603 return iocbq;
604}
605
James Smarte59058c2008-08-24 21:49:00 -0400606/**
James Smartda0436e2009-05-22 14:51:39 -0400607 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
608 * @phba: Pointer to HBA context object.
609 * @xritag: XRI value.
610 *
611 * This function clears the sglq pointer from the array of acive
612 * sglq's. The xritag that is passed in is used to index into the
613 * array. Before the xritag can be used it needs to be adjusted
614 * by subtracting the xribase.
615 *
616 * Returns sglq ponter = success, NULL = Failure.
617 **/
James Smart895427b2017-02-12 13:52:30 -0800618struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400619__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
620{
James Smartda0436e2009-05-22 14:51:39 -0400621 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400622
623 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
624 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400625 return sglq;
626}
627
628/**
629 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
630 * @phba: Pointer to HBA context object.
631 * @xritag: XRI value.
632 *
633 * This function returns the sglq pointer from the array of acive
634 * sglq's. The xritag that is passed in is used to index into the
635 * array. Before the xritag can be used it needs to be adjusted
636 * by subtracting the xribase.
637 *
638 * Returns sglq ponter = success, NULL = Failure.
639 **/
James Smart0f65ff62010-02-26 14:14:23 -0500640struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400641__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
642{
James Smartda0436e2009-05-22 14:51:39 -0400643 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400644
645 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400646 return sglq;
647}
648
649/**
James Smart1151e3e2011-02-16 12:39:35 -0500650 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500651 * @phba: Pointer to HBA context object.
652 * @xritag: xri used in this exchange.
653 * @rrq: The RRQ to be cleared.
654 *
James Smart19ca7602010-11-20 23:11:55 -0500655 **/
James Smart1151e3e2011-02-16 12:39:35 -0500656void
657lpfc_clr_rrq_active(struct lpfc_hba *phba,
658 uint16_t xritag,
659 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500660{
James Smart1151e3e2011-02-16 12:39:35 -0500661 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500662
James Smart1151e3e2011-02-16 12:39:35 -0500663 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
664 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500665
666 /* The target DID could have been swapped (cable swap)
667 * we should use the ndlp from the findnode if it is
668 * available.
669 */
James Smart1151e3e2011-02-16 12:39:35 -0500670 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500671 ndlp = rrq->ndlp;
672
James Smart1151e3e2011-02-16 12:39:35 -0500673 if (!ndlp)
674 goto out;
675
James Smartcff261f2013-12-17 20:29:47 -0500676 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500677 rrq->send_rrq = 0;
678 rrq->xritag = 0;
679 rrq->rrq_stop_time = 0;
680 }
James Smart1151e3e2011-02-16 12:39:35 -0500681out:
James Smart19ca7602010-11-20 23:11:55 -0500682 mempool_free(rrq, phba->rrq_pool);
683}
684
685/**
686 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
687 * @phba: Pointer to HBA context object.
688 *
689 * This function is called with hbalock held. This function
690 * Checks if stop_time (ratov from setting rrq active) has
691 * been reached, if it has and the send_rrq flag is set then
692 * it will call lpfc_send_rrq. If the send_rrq flag is not set
693 * then it will just call the routine to clear the rrq and
694 * free the rrq resource.
695 * The timer is set to the next rrq that is going to expire before
696 * leaving the routine.
697 *
698 **/
699void
700lpfc_handle_rrq_active(struct lpfc_hba *phba)
701{
702 struct lpfc_node_rrq *rrq;
703 struct lpfc_node_rrq *nextrrq;
704 unsigned long next_time;
705 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500706 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500707
708 spin_lock_irqsave(&phba->hbalock, iflags);
709 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400710 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500711 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500712 &phba->active_rrq_list, list) {
713 if (time_after(jiffies, rrq->rrq_stop_time))
714 list_move(&rrq->list, &send_rrq);
715 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500716 next_time = rrq->rrq_stop_time;
717 }
718 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500719 if ((!list_empty(&phba->active_rrq_list)) &&
720 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500721 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500722 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
723 list_del(&rrq->list);
724 if (!rrq->send_rrq)
725 /* this call will free the rrq */
726 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
727 else if (lpfc_send_rrq(phba, rrq)) {
728 /* if we send the rrq then the completion handler
729 * will clear the bit in the xribitmap.
730 */
731 lpfc_clr_rrq_active(phba, rrq->xritag,
732 rrq);
733 }
734 }
James Smart19ca7602010-11-20 23:11:55 -0500735}
736
737/**
738 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
739 * @vport: Pointer to vport context object.
740 * @xri: The xri used in the exchange.
741 * @did: The targets DID for this exchange.
742 *
743 * returns NULL = rrq not found in the phba->active_rrq_list.
744 * rrq = rrq for this xri and target.
745 **/
746struct lpfc_node_rrq *
747lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
748{
749 struct lpfc_hba *phba = vport->phba;
750 struct lpfc_node_rrq *rrq;
751 struct lpfc_node_rrq *nextrrq;
752 unsigned long iflags;
753
754 if (phba->sli_rev != LPFC_SLI_REV4)
755 return NULL;
756 spin_lock_irqsave(&phba->hbalock, iflags);
757 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
758 if (rrq->vport == vport && rrq->xritag == xri &&
759 rrq->nlp_DID == did){
760 list_del(&rrq->list);
761 spin_unlock_irqrestore(&phba->hbalock, iflags);
762 return rrq;
763 }
764 }
765 spin_unlock_irqrestore(&phba->hbalock, iflags);
766 return NULL;
767}
768
769/**
770 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
771 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500772 * @ndlp: Pointer to the lpfc_node_list structure.
773 * If ndlp is NULL Remove all active RRQs for this vport from the
774 * phba->active_rrq_list and clear the rrq.
775 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500776 **/
777void
James Smart1151e3e2011-02-16 12:39:35 -0500778lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500779
780{
781 struct lpfc_hba *phba = vport->phba;
782 struct lpfc_node_rrq *rrq;
783 struct lpfc_node_rrq *nextrrq;
784 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500785 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500786
787 if (phba->sli_rev != LPFC_SLI_REV4)
788 return;
James Smart1151e3e2011-02-16 12:39:35 -0500789 if (!ndlp) {
790 lpfc_sli4_vport_delete_els_xri_aborted(vport);
791 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500792 }
James Smart1151e3e2011-02-16 12:39:35 -0500793 spin_lock_irqsave(&phba->hbalock, iflags);
794 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
795 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
796 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500797 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500798
799 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
800 list_del(&rrq->list);
801 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
802 }
James Smart19ca7602010-11-20 23:11:55 -0500803}
804
805/**
James Smart1151e3e2011-02-16 12:39:35 -0500806 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500807 * @phba: Pointer to HBA context object.
808 * @ndlp: Targets nodelist pointer for this exchange.
809 * @xritag the xri in the bitmap to test.
810 *
811 * This function is called with hbalock held. This function
812 * returns 0 = rrq not active for this xri
813 * 1 = rrq is valid for this xri.
814 **/
James Smart1151e3e2011-02-16 12:39:35 -0500815int
816lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500817 uint16_t xritag)
818{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100819 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500820 if (!ndlp)
821 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500822 if (!ndlp->active_rrqs_xri_bitmap)
823 return 0;
824 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500825 return 1;
826 else
827 return 0;
828}
829
830/**
831 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
832 * @phba: Pointer to HBA context object.
833 * @ndlp: nodelist pointer for this target.
834 * @xritag: xri used in this exchange.
835 * @rxid: Remote Exchange ID.
836 * @send_rrq: Flag used to determine if we should send rrq els cmd.
837 *
838 * This function takes the hbalock.
839 * The active bit is always set in the active rrq xri_bitmap even
840 * if there is no slot avaiable for the other rrq information.
841 *
842 * returns 0 rrq actived for this xri
843 * < 0 No memory or invalid ndlp.
844 **/
845int
846lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500847 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500848{
James Smart19ca7602010-11-20 23:11:55 -0500849 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500850 struct lpfc_node_rrq *rrq;
851 int empty;
852
853 if (!ndlp)
854 return -EINVAL;
855
856 if (!phba->cfg_enable_rrq)
857 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500858
859 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500860 if (phba->pport->load_flag & FC_UNLOADING) {
861 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
862 goto out;
863 }
864
865 /*
866 * set the active bit even if there is no mem available.
867 */
868 if (NLP_CHK_FREE_REQ(ndlp))
869 goto out;
870
871 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
872 goto out;
873
James Smartcff261f2013-12-17 20:29:47 -0500874 if (!ndlp->active_rrqs_xri_bitmap)
875 goto out;
876
877 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500878 goto out;
879
James Smart19ca7602010-11-20 23:11:55 -0500880 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500881 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
882 if (!rrq) {
883 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
884 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
885 " DID:0x%x Send:%d\n",
886 xritag, rxid, ndlp->nlp_DID, send_rrq);
887 return -EINVAL;
888 }
James Smarte5771b42013-03-01 16:37:14 -0500889 if (phba->cfg_enable_rrq == 1)
890 rrq->send_rrq = send_rrq;
891 else
892 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500893 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400894 rrq->rrq_stop_time = jiffies +
895 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500896 rrq->ndlp = ndlp;
897 rrq->nlp_DID = ndlp->nlp_DID;
898 rrq->vport = ndlp->vport;
899 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500900 spin_lock_irqsave(&phba->hbalock, iflags);
901 empty = list_empty(&phba->active_rrq_list);
902 list_add_tail(&rrq->list, &phba->active_rrq_list);
903 phba->hba_flag |= HBA_RRQ_ACTIVE;
904 if (empty)
905 lpfc_worker_wake_up(phba);
906 spin_unlock_irqrestore(&phba->hbalock, iflags);
907 return 0;
908out:
909 spin_unlock_irqrestore(&phba->hbalock, iflags);
910 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
911 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
912 " DID:0x%x Send:%d\n",
913 xritag, rxid, ndlp->nlp_DID, send_rrq);
914 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500915}
916
917/**
James Smart895427b2017-02-12 13:52:30 -0800918 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -0400919 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500920 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400921 *
James Smartdafe8ce2014-09-03 12:56:40 -0400922 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400923 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400924 * list is not empty then it is successful, it returns pointer to the newly
925 * allocated sglq object else it returns NULL.
926 **/
927static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -0800928__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400929{
James Smart895427b2017-02-12 13:52:30 -0800930 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -0400931 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500932 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500933 struct lpfc_scsi_buf *lpfc_cmd;
934 struct lpfc_nodelist *ndlp;
935 int found = 0;
936
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100937 lockdep_assert_held(&phba->hbalock);
938
James Smart19ca7602010-11-20 23:11:55 -0500939 if (piocbq->iocb_flag & LPFC_IO_FCP) {
940 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
941 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500942 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400943 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500944 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400945 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
946 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
947 ndlp = NULL;
948 else
949 ndlp = piocbq->context_un.ndlp;
950 } else {
James Smart19ca7602010-11-20 23:11:55 -0500951 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400952 }
James Smart19ca7602010-11-20 23:11:55 -0500953
James Smart895427b2017-02-12 13:52:30 -0800954 spin_lock(&phba->sli4_hba.sgl_list_lock);
955 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500956 start_sglq = sglq;
957 while (!found) {
958 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -0800959 break;
James Smart895427b2017-02-12 13:52:30 -0800960 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
961 test_bit(sglq->sli4_lxritag,
962 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500963 /* This xri has an rrq outstanding for this DID.
964 * put it back in the list and get another xri.
965 */
James Smart895427b2017-02-12 13:52:30 -0800966 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -0500967 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -0800968 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -0500969 struct lpfc_sglq, list);
970 if (sglq == start_sglq) {
971 sglq = NULL;
972 break;
973 } else
974 continue;
975 }
976 sglq->ndlp = ndlp;
977 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400978 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500979 sglq->state = SGL_ALLOCATED;
980 }
James Smart895427b2017-02-12 13:52:30 -0800981 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400982 return sglq;
983}
984
985/**
James Smartf358dd02017-02-12 13:52:34 -0800986 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
987 * @phba: Pointer to HBA context object.
988 * @piocb: Pointer to the iocbq.
989 *
990 * This function is called with the sgl_list lock held. This function
991 * gets a new driver sglq object from the sglq list. If the
992 * list is not empty then it is successful, it returns pointer to the newly
993 * allocated sglq object else it returns NULL.
994 **/
995struct lpfc_sglq *
996__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
997{
998 struct list_head *lpfc_nvmet_sgl_list;
999 struct lpfc_sglq *sglq = NULL;
1000
1001 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1002
1003 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1004
1005 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1006 if (!sglq)
1007 return NULL;
1008 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1009 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001010 return sglq;
1011}
1012
James Smarte59058c2008-08-24 21:49:00 -04001013/**
James Smart3621a712009-04-06 18:47:14 -04001014 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001015 * @phba: Pointer to HBA context object.
1016 *
1017 * This function is called with no lock held. This function
1018 * allocates a new driver iocb object from the iocb pool. If the
1019 * allocation is successful, it returns pointer to the newly
1020 * allocated iocb object else it returns NULL.
1021 **/
James Smart2e0fef82007-06-17 19:56:36 -05001022struct lpfc_iocbq *
1023lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001024{
James Smart2e0fef82007-06-17 19:56:36 -05001025 struct lpfc_iocbq * iocbq = NULL;
1026 unsigned long iflags;
1027
1028 spin_lock_irqsave(&phba->hbalock, iflags);
1029 iocbq = __lpfc_sli_get_iocbq(phba);
1030 spin_unlock_irqrestore(&phba->hbalock, iflags);
1031 return iocbq;
1032}
1033
James Smarte59058c2008-08-24 21:49:00 -04001034/**
James Smart4f774512009-05-22 14:52:35 -04001035 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1036 * @phba: Pointer to HBA context object.
1037 * @iocbq: Pointer to driver iocb object.
1038 *
1039 * This function is called with hbalock held to release driver
1040 * iocb object to the iocb pool. The iotag in the iocb object
1041 * does not change for each use of the iocb object. This function
1042 * clears all other fields of the iocb object when it is freed.
1043 * The sqlq structure that holds the xritag and phys and virtual
1044 * mappings for the scatter gather list is retrieved from the
1045 * active array of sglq. The get of the sglq pointer also clears
1046 * the entry in the array. If the status of the IO indiactes that
1047 * this IO was aborted then the sglq entry it put on the
1048 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1049 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001050 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001051 **/
1052static void
1053__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1054{
1055 struct lpfc_sglq *sglq;
1056 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001057 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001058 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001059
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001060 lockdep_assert_held(&phba->hbalock);
1061
James Smart4f774512009-05-22 14:52:35 -04001062 if (iocbq->sli4_xritag == NO_XRI)
1063 sglq = NULL;
1064 else
James Smart6d368e52011-05-24 11:44:12 -04001065 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1066
James Smart0e9bb8d2013-03-01 16:35:12 -05001067
James Smart4f774512009-05-22 14:52:35 -04001068 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001069 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1070 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1071 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001072 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001073 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001074 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001075 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1076 spin_unlock_irqrestore(
1077 &phba->sli4_hba.sgl_list_lock, iflag);
1078 goto out;
1079 }
1080
James Smart895427b2017-02-12 13:52:30 -08001081 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001082 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1083 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001084 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1085 iflag);
James Smart341af102010-01-26 23:07:37 -05001086 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001087 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001088 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001089 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001090 } else {
James Smart895427b2017-02-12 13:52:30 -08001091 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1092 iflag);
James Smart4f774512009-05-22 14:52:35 -04001093 sglq->state = SGL_FREED;
1094 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001095 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001096 &phba->sli4_hba.lpfc_els_sgl_list);
1097 spin_unlock_irqrestore(
1098 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001099
1100 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001101 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001102 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001103 }
James Smart4f774512009-05-22 14:52:35 -04001104 }
1105
James Smartf358dd02017-02-12 13:52:34 -08001106out:
James Smart4f774512009-05-22 14:52:35 -04001107 /*
1108 * Clean all volatile data fields, preserve iotag and node struct.
1109 */
1110 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001111 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001112 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001113 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1114 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001115 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1116}
1117
James Smart2a9bf3d2010-06-07 15:24:45 -04001118
James Smart4f774512009-05-22 14:52:35 -04001119/**
James Smart3772a992009-05-22 14:50:54 -04001120 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1121 * @phba: Pointer to HBA context object.
1122 * @iocbq: Pointer to driver iocb object.
1123 *
1124 * This function is called with hbalock held to release driver
1125 * iocb object to the iocb pool. The iotag in the iocb object
1126 * does not change for each use of the iocb object. This function
1127 * clears all other fields of the iocb object when it is freed.
1128 **/
1129static void
1130__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1131{
1132 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1133
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001134 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001135
1136 /*
James Smart3772a992009-05-22 14:50:54 -04001137 * Clean all volatile data fields, preserve iotag and node struct.
1138 */
1139 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1140 iocbq->sli4_xritag = NO_XRI;
1141 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1142}
1143
1144/**
James Smart3621a712009-04-06 18:47:14 -04001145 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001146 * @phba: Pointer to HBA context object.
1147 * @iocbq: Pointer to driver iocb object.
1148 *
1149 * This function is called with hbalock held to release driver
1150 * iocb object to the iocb pool. The iotag in the iocb object
1151 * does not change for each use of the iocb object. This function
1152 * clears all other fields of the iocb object when it is freed.
1153 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001154static void
James Smart2e0fef82007-06-17 19:56:36 -05001155__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1156{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001157 lockdep_assert_held(&phba->hbalock);
1158
James Smart3772a992009-05-22 14:50:54 -04001159 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001160 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001161}
1162
James Smarte59058c2008-08-24 21:49:00 -04001163/**
James Smart3621a712009-04-06 18:47:14 -04001164 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001165 * @phba: Pointer to HBA context object.
1166 * @iocbq: Pointer to driver iocb object.
1167 *
1168 * This function is called with no lock held to release the iocb to
1169 * iocb pool.
1170 **/
James Smart2e0fef82007-06-17 19:56:36 -05001171void
1172lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1173{
1174 unsigned long iflags;
1175
1176 /*
1177 * Clean all volatile data fields, preserve iotag and node struct.
1178 */
1179 spin_lock_irqsave(&phba->hbalock, iflags);
1180 __lpfc_sli_release_iocbq(phba, iocbq);
1181 spin_unlock_irqrestore(&phba->hbalock, iflags);
1182}
1183
James Smarte59058c2008-08-24 21:49:00 -04001184/**
James Smarta257bf92009-04-06 18:48:10 -04001185 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1186 * @phba: Pointer to HBA context object.
1187 * @iocblist: List of IOCBs.
1188 * @ulpstatus: ULP status in IOCB command field.
1189 * @ulpWord4: ULP word-4 in IOCB command field.
1190 *
1191 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1192 * on the list by invoking the complete callback function associated with the
1193 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1194 * fields.
1195 **/
1196void
1197lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1198 uint32_t ulpstatus, uint32_t ulpWord4)
1199{
1200 struct lpfc_iocbq *piocb;
1201
1202 while (!list_empty(iocblist)) {
1203 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001204 if (!piocb->iocb_cmpl)
1205 lpfc_sli_release_iocbq(phba, piocb);
1206 else {
1207 piocb->iocb.ulpStatus = ulpstatus;
1208 piocb->iocb.un.ulpWord[4] = ulpWord4;
1209 (piocb->iocb_cmpl) (phba, piocb, piocb);
1210 }
1211 }
1212 return;
1213}
1214
1215/**
James Smart3621a712009-04-06 18:47:14 -04001216 * lpfc_sli_iocb_cmd_type - Get the iocb type
1217 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001218 *
1219 * This function is called by ring event handler function to get the iocb type.
1220 * This function translates the iocb command to an iocb command type used to
1221 * decide the final disposition of each completed IOCB.
1222 * The function returns
1223 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1224 * LPFC_SOL_IOCB if it is a solicited iocb completion
1225 * LPFC_ABORT_IOCB if it is an abort iocb
1226 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1227 *
1228 * The caller is not required to hold any lock.
1229 **/
dea31012005-04-17 16:05:31 -05001230static lpfc_iocb_type
1231lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1232{
1233 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1234
1235 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1236 return 0;
1237
1238 switch (iocb_cmnd) {
1239 case CMD_XMIT_SEQUENCE_CR:
1240 case CMD_XMIT_SEQUENCE_CX:
1241 case CMD_XMIT_BCAST_CN:
1242 case CMD_XMIT_BCAST_CX:
1243 case CMD_ELS_REQUEST_CR:
1244 case CMD_ELS_REQUEST_CX:
1245 case CMD_CREATE_XRI_CR:
1246 case CMD_CREATE_XRI_CX:
1247 case CMD_GET_RPI_CN:
1248 case CMD_XMIT_ELS_RSP_CX:
1249 case CMD_GET_RPI_CR:
1250 case CMD_FCP_IWRITE_CR:
1251 case CMD_FCP_IWRITE_CX:
1252 case CMD_FCP_IREAD_CR:
1253 case CMD_FCP_IREAD_CX:
1254 case CMD_FCP_ICMND_CR:
1255 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001256 case CMD_FCP_TSEND_CX:
1257 case CMD_FCP_TRSP_CX:
1258 case CMD_FCP_TRECEIVE_CX:
1259 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001260 case CMD_ADAPTER_MSG:
1261 case CMD_ADAPTER_DUMP:
1262 case CMD_XMIT_SEQUENCE64_CR:
1263 case CMD_XMIT_SEQUENCE64_CX:
1264 case CMD_XMIT_BCAST64_CN:
1265 case CMD_XMIT_BCAST64_CX:
1266 case CMD_ELS_REQUEST64_CR:
1267 case CMD_ELS_REQUEST64_CX:
1268 case CMD_FCP_IWRITE64_CR:
1269 case CMD_FCP_IWRITE64_CX:
1270 case CMD_FCP_IREAD64_CR:
1271 case CMD_FCP_IREAD64_CX:
1272 case CMD_FCP_ICMND64_CR:
1273 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001274 case CMD_FCP_TSEND64_CX:
1275 case CMD_FCP_TRSP64_CX:
1276 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001277 case CMD_GEN_REQUEST64_CR:
1278 case CMD_GEN_REQUEST64_CX:
1279 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001280 case DSSCMD_IWRITE64_CR:
1281 case DSSCMD_IWRITE64_CX:
1282 case DSSCMD_IREAD64_CR:
1283 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001284 type = LPFC_SOL_IOCB;
1285 break;
1286 case CMD_ABORT_XRI_CN:
1287 case CMD_ABORT_XRI_CX:
1288 case CMD_CLOSE_XRI_CN:
1289 case CMD_CLOSE_XRI_CX:
1290 case CMD_XRI_ABORTED_CX:
1291 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001292 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001293 type = LPFC_ABORT_IOCB;
1294 break;
1295 case CMD_RCV_SEQUENCE_CX:
1296 case CMD_RCV_ELS_REQ_CX:
1297 case CMD_RCV_SEQUENCE64_CX:
1298 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001299 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001300 case CMD_IOCB_RCV_SEQ64_CX:
1301 case CMD_IOCB_RCV_ELS64_CX:
1302 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001303 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001304 type = LPFC_UNSOL_IOCB;
1305 break;
James Smart3163f722008-02-08 18:50:25 -05001306 case CMD_IOCB_XMIT_MSEQ64_CR:
1307 case CMD_IOCB_XMIT_MSEQ64_CX:
1308 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1309 case CMD_IOCB_RCV_ELS_LIST64_CX:
1310 case CMD_IOCB_CLOSE_EXTENDED_CN:
1311 case CMD_IOCB_ABORT_EXTENDED_CN:
1312 case CMD_IOCB_RET_HBQE64_CN:
1313 case CMD_IOCB_FCP_IBIDIR64_CR:
1314 case CMD_IOCB_FCP_IBIDIR64_CX:
1315 case CMD_IOCB_FCP_ITASKMGT64_CX:
1316 case CMD_IOCB_LOGENTRY_CN:
1317 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1318 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001319 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001320 type = LPFC_UNKNOWN_IOCB;
1321 break;
dea31012005-04-17 16:05:31 -05001322 default:
1323 type = LPFC_UNKNOWN_IOCB;
1324 break;
1325 }
1326
1327 return type;
1328}
1329
James Smarte59058c2008-08-24 21:49:00 -04001330/**
James Smart3621a712009-04-06 18:47:14 -04001331 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001332 * @phba: Pointer to HBA context object.
1333 *
1334 * This function is called from SLI initialization code
1335 * to configure every ring of the HBA's SLI interface. The
1336 * caller is not required to hold any lock. This function issues
1337 * a config_ring mailbox command for each ring.
1338 * This function returns zero if successful else returns a negative
1339 * error code.
1340 **/
dea31012005-04-17 16:05:31 -05001341static int
James Smarted957682007-06-17 19:56:37 -05001342lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001343{
1344 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001345 LPFC_MBOXQ_t *pmb;
1346 MAILBOX_t *pmbox;
1347 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001348
James Smarted957682007-06-17 19:56:37 -05001349 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1350 if (!pmb)
1351 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001352 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001353 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001354 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001355 lpfc_config_ring(phba, i, pmb);
1356 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1357 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001358 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001359 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001360 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1361 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001362 rc, pmbox->mbxCommand,
1363 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001364 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001365 ret = -ENXIO;
1366 break;
dea31012005-04-17 16:05:31 -05001367 }
1368 }
James Smarted957682007-06-17 19:56:37 -05001369 mempool_free(pmb, phba->mbox_mem_pool);
1370 return ret;
dea31012005-04-17 16:05:31 -05001371}
1372
James Smarte59058c2008-08-24 21:49:00 -04001373/**
James Smart3621a712009-04-06 18:47:14 -04001374 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001375 * @phba: Pointer to HBA context object.
1376 * @pring: Pointer to driver SLI ring object.
1377 * @piocb: Pointer to the driver iocb object.
1378 *
1379 * This function is called with hbalock held. The function adds the
1380 * new iocb to txcmplq of the given ring. This function always returns
1381 * 0. If this function is called for ELS ring, this function checks if
1382 * there is a vport associated with the ELS command. This function also
1383 * starts els_tmofunc timer if this is an ELS command.
1384 **/
dea31012005-04-17 16:05:31 -05001385static int
James Smart2e0fef82007-06-17 19:56:36 -05001386lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1387 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001388{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001389 lockdep_assert_held(&phba->hbalock);
1390
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001391 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001392
dea31012005-04-17 16:05:31 -05001393 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001394 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001395
James Smart92d7f7b2007-06-17 19:56:38 -05001396 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1397 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001398 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1399 BUG_ON(!piocb->vport);
1400 if (!(piocb->vport->load_flag & FC_UNLOADING))
1401 mod_timer(&piocb->vport->els_tmofunc,
1402 jiffies +
1403 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1404 }
dea31012005-04-17 16:05:31 -05001405
James Smart2e0fef82007-06-17 19:56:36 -05001406 return 0;
dea31012005-04-17 16:05:31 -05001407}
1408
James Smarte59058c2008-08-24 21:49:00 -04001409/**
James Smart3621a712009-04-06 18:47:14 -04001410 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001411 * @phba: Pointer to HBA context object.
1412 * @pring: Pointer to driver SLI ring object.
1413 *
1414 * This function is called with hbalock held to get next
1415 * iocb in txq of the given ring. If there is any iocb in
1416 * the txq, the function returns first iocb in the list after
1417 * removing the iocb from the list, else it returns NULL.
1418 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001419struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001420lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001421{
dea31012005-04-17 16:05:31 -05001422 struct lpfc_iocbq *cmd_iocb;
1423
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001424 lockdep_assert_held(&phba->hbalock);
1425
James Smart858c9f62007-06-17 19:56:39 -05001426 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001427 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001428}
1429
James Smarte59058c2008-08-24 21:49:00 -04001430/**
James Smart3621a712009-04-06 18:47:14 -04001431 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001432 * @phba: Pointer to HBA context object.
1433 * @pring: Pointer to driver SLI ring object.
1434 *
1435 * This function is called with hbalock held and the caller must post the
1436 * iocb without releasing the lock. If the caller releases the lock,
1437 * iocb slot returned by the function is not guaranteed to be available.
1438 * The function returns pointer to the next available iocb slot if there
1439 * is available slot in the ring, else it returns NULL.
1440 * If the get index of the ring is ahead of the put index, the function
1441 * will post an error attention event to the worker thread to take the
1442 * HBA to offline state.
1443 **/
dea31012005-04-17 16:05:31 -05001444static IOCB_t *
1445lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1446{
James Smart34b02dc2008-08-24 21:49:55 -04001447 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001448 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001449
1450 lockdep_assert_held(&phba->hbalock);
1451
James Smart7e56aa22012-08-03 12:35:34 -04001452 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1453 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1454 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001455
James Smart7e56aa22012-08-03 12:35:34 -04001456 if (unlikely(pring->sli.sli3.local_getidx ==
1457 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001458
James Smart7e56aa22012-08-03 12:35:34 -04001459 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001460
James Smart7e56aa22012-08-03 12:35:34 -04001461 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001462 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001463 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001464 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001465 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001466 pring->sli.sli3.local_getidx,
1467 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001468
James Smart2e0fef82007-06-17 19:56:36 -05001469 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001470 /*
1471 * All error attention handlers are posted to
1472 * worker thread
1473 */
1474 phba->work_ha |= HA_ERATT;
1475 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001476
James Smart5e9d9b82008-06-14 22:52:53 -04001477 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001478
1479 return NULL;
1480 }
1481
James Smart7e56aa22012-08-03 12:35:34 -04001482 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001483 return NULL;
1484 }
1485
James Smarted957682007-06-17 19:56:37 -05001486 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001487}
1488
James Smarte59058c2008-08-24 21:49:00 -04001489/**
James Smart3621a712009-04-06 18:47:14 -04001490 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001491 * @phba: Pointer to HBA context object.
1492 * @iocbq: Pointer to driver iocb object.
1493 *
1494 * This function gets an iotag for the iocb. If there is no unused iotag and
1495 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1496 * array and assigns a new iotag.
1497 * The function returns the allocated iotag if successful, else returns zero.
1498 * Zero is not a valid iotag.
1499 * The caller is not required to hold any lock.
1500 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001501uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001502lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001503{
James Smart2e0fef82007-06-17 19:56:36 -05001504 struct lpfc_iocbq **new_arr;
1505 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001506 size_t new_len;
1507 struct lpfc_sli *psli = &phba->sli;
1508 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001509
James Smart2e0fef82007-06-17 19:56:36 -05001510 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001511 iotag = psli->last_iotag;
1512 if(++iotag < psli->iocbq_lookup_len) {
1513 psli->last_iotag = iotag;
1514 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001515 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001516 iocbq->iotag = iotag;
1517 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001518 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001519 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1520 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001521 spin_unlock_irq(&phba->hbalock);
1522 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001523 GFP_KERNEL);
1524 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001525 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001526 old_arr = psli->iocbq_lookup;
1527 if (new_len <= psli->iocbq_lookup_len) {
1528 /* highly unprobable case */
1529 kfree(new_arr);
1530 iotag = psli->last_iotag;
1531 if(++iotag < psli->iocbq_lookup_len) {
1532 psli->last_iotag = iotag;
1533 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001534 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001535 iocbq->iotag = iotag;
1536 return iotag;
1537 }
James Smart2e0fef82007-06-17 19:56:36 -05001538 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001539 return 0;
1540 }
1541 if (psli->iocbq_lookup)
1542 memcpy(new_arr, old_arr,
1543 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001544 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001545 psli->iocbq_lookup = new_arr;
1546 psli->iocbq_lookup_len = new_len;
1547 psli->last_iotag = iotag;
1548 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001549 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001550 iocbq->iotag = iotag;
1551 kfree(old_arr);
1552 return iotag;
1553 }
James Smart8f6d98d2006-08-01 07:34:00 -04001554 } else
James Smart2e0fef82007-06-17 19:56:36 -05001555 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001556
James Smartbc739052010-08-04 16:11:18 -04001557 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001558 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1559 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001560
James Bottomley604a3e32005-10-29 10:28:33 -05001561 return 0;
dea31012005-04-17 16:05:31 -05001562}
1563
James Smarte59058c2008-08-24 21:49:00 -04001564/**
James Smart3621a712009-04-06 18:47:14 -04001565 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001566 * @phba: Pointer to HBA context object.
1567 * @pring: Pointer to driver SLI ring object.
1568 * @iocb: Pointer to iocb slot in the ring.
1569 * @nextiocb: Pointer to driver iocb object which need to be
1570 * posted to firmware.
1571 *
1572 * This function is called with hbalock held to post a new iocb to
1573 * the firmware. This function copies the new iocb to ring iocb slot and
1574 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1575 * a completion call back for this iocb else the function will free the
1576 * iocb object.
1577 **/
dea31012005-04-17 16:05:31 -05001578static void
1579lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1580 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1581{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001582 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001583 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001584 * Set up an iotag
dea31012005-04-17 16:05:31 -05001585 */
James Bottomley604a3e32005-10-29 10:28:33 -05001586 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001587
James Smarte2a0a9d2008-12-04 22:40:02 -05001588
James Smarta58cbd52007-08-02 11:09:43 -04001589 if (pring->ringno == LPFC_ELS_RING) {
1590 lpfc_debugfs_slow_ring_trc(phba,
1591 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1592 *(((uint32_t *) &nextiocb->iocb) + 4),
1593 *(((uint32_t *) &nextiocb->iocb) + 6),
1594 *(((uint32_t *) &nextiocb->iocb) + 7));
1595 }
1596
dea31012005-04-17 16:05:31 -05001597 /*
1598 * Issue iocb command to adapter
1599 */
James Smart92d7f7b2007-06-17 19:56:38 -05001600 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001601 wmb();
1602 pring->stats.iocb_cmd++;
1603
1604 /*
1605 * If there is no completion routine to call, we can release the
1606 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1607 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1608 */
1609 if (nextiocb->iocb_cmpl)
1610 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001611 else
James Smart2e0fef82007-06-17 19:56:36 -05001612 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001613
1614 /*
1615 * Let the HBA know what IOCB slot will be the next one the
1616 * driver will put a command into.
1617 */
James Smart7e56aa22012-08-03 12:35:34 -04001618 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1619 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001620}
1621
James Smarte59058c2008-08-24 21:49:00 -04001622/**
James Smart3621a712009-04-06 18:47:14 -04001623 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001624 * @phba: Pointer to HBA context object.
1625 * @pring: Pointer to driver SLI ring object.
1626 *
1627 * The caller is not required to hold any lock for calling this function.
1628 * This function updates the chip attention bits for the ring to inform firmware
1629 * that there are pending work to be done for this ring and requests an
1630 * interrupt when there is space available in the ring. This function is
1631 * called when the driver is unable to post more iocbs to the ring due
1632 * to unavailability of space in the ring.
1633 **/
dea31012005-04-17 16:05:31 -05001634static void
James Smart2e0fef82007-06-17 19:56:36 -05001635lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001636{
1637 int ringno = pring->ringno;
1638
1639 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1640
1641 wmb();
1642
1643 /*
1644 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1645 * The HBA will tell us when an IOCB entry is available.
1646 */
1647 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1648 readl(phba->CAregaddr); /* flush */
1649
1650 pring->stats.iocb_cmd_full++;
1651}
1652
James Smarte59058c2008-08-24 21:49:00 -04001653/**
James Smart3621a712009-04-06 18:47:14 -04001654 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001655 * @phba: Pointer to HBA context object.
1656 * @pring: Pointer to driver SLI ring object.
1657 *
1658 * This function updates the chip attention register bit for the
1659 * given ring to inform HBA that there is more work to be done
1660 * in this ring. The caller is not required to hold any lock.
1661 **/
dea31012005-04-17 16:05:31 -05001662static void
James Smart2e0fef82007-06-17 19:56:36 -05001663lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001664{
1665 int ringno = pring->ringno;
1666
1667 /*
1668 * Tell the HBA that there is work to do in this ring.
1669 */
James Smart34b02dc2008-08-24 21:49:55 -04001670 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1671 wmb();
1672 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1673 readl(phba->CAregaddr); /* flush */
1674 }
dea31012005-04-17 16:05:31 -05001675}
1676
James Smarte59058c2008-08-24 21:49:00 -04001677/**
James Smart3621a712009-04-06 18:47:14 -04001678 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001679 * @phba: Pointer to HBA context object.
1680 * @pring: Pointer to driver SLI ring object.
1681 *
1682 * This function is called with hbalock held to post pending iocbs
1683 * in the txq to the firmware. This function is called when driver
1684 * detects space available in the ring.
1685 **/
dea31012005-04-17 16:05:31 -05001686static void
James Smart2e0fef82007-06-17 19:56:36 -05001687lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001688{
1689 IOCB_t *iocb;
1690 struct lpfc_iocbq *nextiocb;
1691
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001692 lockdep_assert_held(&phba->hbalock);
1693
dea31012005-04-17 16:05:31 -05001694 /*
1695 * Check to see if:
1696 * (a) there is anything on the txq to send
1697 * (b) link is up
1698 * (c) link attention events can be processed (fcp ring only)
1699 * (d) IOCB processing is not blocked by the outstanding mbox command.
1700 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001701
1702 if (lpfc_is_link_up(phba) &&
1703 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001704 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001705 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001706
1707 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1708 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1709 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1710
1711 if (iocb)
1712 lpfc_sli_update_ring(phba, pring);
1713 else
1714 lpfc_sli_update_full_ring(phba, pring);
1715 }
1716
1717 return;
1718}
1719
James Smarte59058c2008-08-24 21:49:00 -04001720/**
James Smart3621a712009-04-06 18:47:14 -04001721 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001722 * @phba: Pointer to HBA context object.
1723 * @hbqno: HBQ number.
1724 *
1725 * This function is called with hbalock held to get the next
1726 * available slot for the given HBQ. If there is free slot
1727 * available for the HBQ it will return pointer to the next available
1728 * HBQ entry else it will return NULL.
1729 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001730static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001731lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1732{
1733 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1734
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001735 lockdep_assert_held(&phba->hbalock);
1736
James Smarted957682007-06-17 19:56:37 -05001737 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1738 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1739 hbqp->next_hbqPutIdx = 0;
1740
1741 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001742 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001743 uint32_t getidx = le32_to_cpu(raw_index);
1744
1745 hbqp->local_hbqGetIdx = getidx;
1746
1747 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1748 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001749 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001750 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001751 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001752 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001753 hbqp->entry_count);
1754
1755 phba->link_state = LPFC_HBA_ERROR;
1756 return NULL;
1757 }
1758
1759 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1760 return NULL;
1761 }
1762
James Smart51ef4c22007-08-02 11:10:31 -04001763 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1764 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001765}
1766
James Smarte59058c2008-08-24 21:49:00 -04001767/**
James Smart3621a712009-04-06 18:47:14 -04001768 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001769 * @phba: Pointer to HBA context object.
1770 *
1771 * This function is called with no lock held to free all the
1772 * hbq buffers while uninitializing the SLI interface. It also
1773 * frees the HBQ buffers returned by the firmware but not yet
1774 * processed by the upper layers.
1775 **/
James Smarted957682007-06-17 19:56:37 -05001776void
1777lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1778{
James Smart92d7f7b2007-06-17 19:56:38 -05001779 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1780 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001781 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001782 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001783
James Smart51ef4c22007-08-02 11:10:31 -04001784 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001785 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001786 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001787 for (i = 0; i < hbq_count; ++i) {
1788 list_for_each_entry_safe(dmabuf, next_dmabuf,
1789 &phba->hbqs[i].hbq_buffer_list, list) {
1790 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1791 list_del(&hbq_buf->dbuf.list);
1792 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1793 }
James Smarta8adb832007-10-27 13:37:53 -04001794 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001795 }
James Smart3163f722008-02-08 18:50:25 -05001796
1797 /* Mark the HBQs not in use */
1798 phba->hbq_in_use = 0;
1799 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001800}
1801
James Smarte59058c2008-08-24 21:49:00 -04001802/**
James Smart3621a712009-04-06 18:47:14 -04001803 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001804 * @phba: Pointer to HBA context object.
1805 * @hbqno: HBQ number.
1806 * @hbq_buf: Pointer to HBQ buffer.
1807 *
1808 * This function is called with the hbalock held to post a
1809 * hbq buffer to the firmware. If the function finds an empty
1810 * slot in the HBQ, it will post the buffer. The function will return
1811 * pointer to the hbq entry if it successfully post the buffer
1812 * else it will return NULL.
1813 **/
James Smart3772a992009-05-22 14:50:54 -04001814static int
James Smarted957682007-06-17 19:56:37 -05001815lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001816 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001817{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001818 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001819 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1820}
1821
1822/**
1823 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1824 * @phba: Pointer to HBA context object.
1825 * @hbqno: HBQ number.
1826 * @hbq_buf: Pointer to HBQ buffer.
1827 *
1828 * This function is called with the hbalock held to post a hbq buffer to the
1829 * firmware. If the function finds an empty slot in the HBQ, it will post the
1830 * buffer and place it on the hbq_buffer_list. The function will return zero if
1831 * it successfully post the buffer else it will return an error.
1832 **/
1833static int
1834lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1835 struct hbq_dmabuf *hbq_buf)
1836{
James Smarted957682007-06-17 19:56:37 -05001837 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001838 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001839
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001840 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001841 /* Get next HBQ entry slot to use */
1842 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1843 if (hbqe) {
1844 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1845
James Smart92d7f7b2007-06-17 19:56:38 -05001846 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1847 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08001848 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05001849 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001850 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1851 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1852 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001853 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1854 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001855 /* flush */
James Smarted957682007-06-17 19:56:37 -05001856 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001857 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001858 return 0;
1859 } else
1860 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001861}
1862
James Smart4f774512009-05-22 14:52:35 -04001863/**
1864 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1865 * @phba: Pointer to HBA context object.
1866 * @hbqno: HBQ number.
1867 * @hbq_buf: Pointer to HBQ buffer.
1868 *
1869 * This function is called with the hbalock held to post an RQE to the SLI4
1870 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1871 * the hbq_buffer_list and return zero, otherwise it will return an error.
1872 **/
1873static int
1874lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1875 struct hbq_dmabuf *hbq_buf)
1876{
1877 int rc;
1878 struct lpfc_rqe hrqe;
1879 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08001880 struct lpfc_queue *hrq;
1881 struct lpfc_queue *drq;
1882
1883 if (hbqno != LPFC_ELS_HBQ)
1884 return 1;
1885 hrq = phba->sli4_hba.hdr_rq;
1886 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04001887
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001888 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001889 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1890 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1891 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1892 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08001893 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04001894 if (rc < 0)
1895 return rc;
James Smart895427b2017-02-12 13:52:30 -08001896 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04001897 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1898 return 0;
1899}
1900
James Smarte59058c2008-08-24 21:49:00 -04001901/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001902static struct lpfc_hbq_init lpfc_els_hbq = {
1903 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001904 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001905 .mask_count = 0,
1906 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001907 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001908 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001909 .init_count = 40,
1910 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001911};
James Smarted957682007-06-17 19:56:37 -05001912
James Smarte59058c2008-08-24 21:49:00 -04001913/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001914struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001915 &lpfc_els_hbq,
1916};
1917
James Smarte59058c2008-08-24 21:49:00 -04001918/**
James Smart3621a712009-04-06 18:47:14 -04001919 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001920 * @phba: Pointer to HBA context object.
1921 * @hbqno: HBQ number.
1922 * @count: Number of HBQ buffers to be posted.
1923 *
James Smartd7c255b2008-08-24 21:50:00 -04001924 * This function is called with no lock held to post more hbq buffers to the
1925 * given HBQ. The function returns the number of HBQ buffers successfully
1926 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001927 **/
James Smart311464e2007-08-02 11:10:37 -04001928static int
James Smart92d7f7b2007-06-17 19:56:38 -05001929lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1930{
James Smartd7c255b2008-08-24 21:50:00 -04001931 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001932 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001933 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001934 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001935 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001936 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001937
James Smartd7c255b2008-08-24 21:50:00 -04001938 if ((phba->hbqs[hbqno].buffer_count + count) >
1939 lpfc_hbq_defs[hbqno]->entry_count)
1940 count = lpfc_hbq_defs[hbqno]->entry_count -
1941 phba->hbqs[hbqno].buffer_count;
1942 if (!count)
1943 return 0;
1944 /* Allocate HBQ entries */
1945 for (i = 0; i < count; i++) {
1946 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1947 if (!hbq_buffer)
1948 break;
1949 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1950 }
James Smart3163f722008-02-08 18:50:25 -05001951 /* Check whether HBQ is still in use */
1952 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001953 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001954 goto err;
1955 while (!list_empty(&hbq_buf_list)) {
1956 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1957 dbuf.list);
1958 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1959 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001960 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001961 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001962 posted++;
1963 } else
James Smart51ef4c22007-08-02 11:10:31 -04001964 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001965 }
James Smart3163f722008-02-08 18:50:25 -05001966 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001967 return posted;
1968err:
1969 spin_unlock_irqrestore(&phba->hbalock, flags);
1970 while (!list_empty(&hbq_buf_list)) {
1971 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1972 dbuf.list);
1973 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1974 }
James Smart92d7f7b2007-06-17 19:56:38 -05001975 return 0;
James Smarted957682007-06-17 19:56:37 -05001976}
1977
James Smarte59058c2008-08-24 21:49:00 -04001978/**
James Smart3621a712009-04-06 18:47:14 -04001979 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001980 * @phba: Pointer to HBA context object.
1981 * @qno: HBQ number.
1982 *
1983 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001984 * is called with no lock held. The function returns the number of HBQ entries
1985 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001986 **/
James Smarted957682007-06-17 19:56:37 -05001987int
James Smart92d7f7b2007-06-17 19:56:38 -05001988lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001989{
James Smartdef9c7a2009-12-21 17:02:28 -05001990 if (phba->sli_rev == LPFC_SLI_REV4)
1991 return 0;
1992 else
1993 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1994 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001995}
1996
James Smarte59058c2008-08-24 21:49:00 -04001997/**
James Smart3621a712009-04-06 18:47:14 -04001998 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001999 * @phba: Pointer to HBA context object.
2000 * @qno: HBQ queue number.
2001 *
2002 * This function is called from SLI initialization code path with
2003 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002004 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002005 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002006static int
James Smart92d7f7b2007-06-17 19:56:38 -05002007lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002008{
James Smartdef9c7a2009-12-21 17:02:28 -05002009 if (phba->sli_rev == LPFC_SLI_REV4)
2010 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002011 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002012 else
2013 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2014 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002015}
2016
James Smarte59058c2008-08-24 21:49:00 -04002017/**
James Smart3772a992009-05-22 14:50:54 -04002018 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2019 * @phba: Pointer to HBA context object.
2020 * @hbqno: HBQ number.
2021 *
2022 * This function removes the first hbq buffer on an hbq list and returns a
2023 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2024 **/
2025static struct hbq_dmabuf *
2026lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2027{
2028 struct lpfc_dmabuf *d_buf;
2029
2030 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2031 if (!d_buf)
2032 return NULL;
2033 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2034}
2035
2036/**
James Smart2d7dbc42017-02-12 13:52:35 -08002037 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2038 * @phba: Pointer to HBA context object.
2039 * @hbqno: HBQ number.
2040 *
2041 * This function removes the first RQ buffer on an RQ buffer list and returns a
2042 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2043 **/
2044static struct rqb_dmabuf *
2045lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2046{
2047 struct lpfc_dmabuf *h_buf;
2048 struct lpfc_rqb *rqbp;
2049
2050 rqbp = hrq->rqbp;
2051 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2052 struct lpfc_dmabuf, list);
2053 if (!h_buf)
2054 return NULL;
2055 rqbp->buffer_count--;
2056 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2057}
2058
2059/**
James Smart3621a712009-04-06 18:47:14 -04002060 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002061 * @phba: Pointer to HBA context object.
2062 * @tag: Tag of the hbq buffer.
2063 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002064 * This function searches for the hbq buffer associated with the given tag in
2065 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2066 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002067 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002068static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002069lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2070{
James Smart92d7f7b2007-06-17 19:56:38 -05002071 struct lpfc_dmabuf *d_buf;
2072 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002073 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002074
James Smart51ef4c22007-08-02 11:10:31 -04002075 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002076 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002077 return NULL;
2078
James Smart3772a992009-05-22 14:50:54 -04002079 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002080 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002081 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002082 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002083 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002084 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002085 }
2086 }
James Smart3772a992009-05-22 14:50:54 -04002087 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002088 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002089 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002090 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002091 return NULL;
James Smarted957682007-06-17 19:56:37 -05002092}
2093
James Smarte59058c2008-08-24 21:49:00 -04002094/**
James Smart3621a712009-04-06 18:47:14 -04002095 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002096 * @phba: Pointer to HBA context object.
2097 * @hbq_buffer: Pointer to HBQ buffer.
2098 *
2099 * This function is called with hbalock. This function gives back
2100 * the hbq buffer to firmware. If the HBQ does not have space to
2101 * post the buffer, it will free the buffer.
2102 **/
James Smarted957682007-06-17 19:56:37 -05002103void
James Smart51ef4c22007-08-02 11:10:31 -04002104lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002105{
2106 uint32_t hbqno;
2107
James Smart51ef4c22007-08-02 11:10:31 -04002108 if (hbq_buffer) {
2109 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002110 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002111 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002112 }
2113}
2114
James Smarte59058c2008-08-24 21:49:00 -04002115/**
James Smart3621a712009-04-06 18:47:14 -04002116 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002117 * @mbxCommand: mailbox command code.
2118 *
2119 * This function is called by the mailbox event handler function to verify
2120 * that the completed mailbox command is a legitimate mailbox command. If the
2121 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2122 * and the mailbox event handler will take the HBA offline.
2123 **/
dea31012005-04-17 16:05:31 -05002124static int
2125lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2126{
2127 uint8_t ret;
2128
2129 switch (mbxCommand) {
2130 case MBX_LOAD_SM:
2131 case MBX_READ_NV:
2132 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002133 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002134 case MBX_RUN_BIU_DIAG:
2135 case MBX_INIT_LINK:
2136 case MBX_DOWN_LINK:
2137 case MBX_CONFIG_LINK:
2138 case MBX_CONFIG_RING:
2139 case MBX_RESET_RING:
2140 case MBX_READ_CONFIG:
2141 case MBX_READ_RCONFIG:
2142 case MBX_READ_SPARM:
2143 case MBX_READ_STATUS:
2144 case MBX_READ_RPI:
2145 case MBX_READ_XRI:
2146 case MBX_READ_REV:
2147 case MBX_READ_LNK_STAT:
2148 case MBX_REG_LOGIN:
2149 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002150 case MBX_CLEAR_LA:
2151 case MBX_DUMP_MEMORY:
2152 case MBX_DUMP_CONTEXT:
2153 case MBX_RUN_DIAGS:
2154 case MBX_RESTART:
2155 case MBX_UPDATE_CFG:
2156 case MBX_DOWN_LOAD:
2157 case MBX_DEL_LD_ENTRY:
2158 case MBX_RUN_PROGRAM:
2159 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002160 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002161 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002162 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002163 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002164 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002165 case MBX_LOAD_AREA:
2166 case MBX_RUN_BIU_DIAG64:
2167 case MBX_CONFIG_PORT:
2168 case MBX_READ_SPARM64:
2169 case MBX_READ_RPI64:
2170 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002171 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002172 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002173 case MBX_SET_DEBUG:
2174 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002175 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002176 case MBX_REG_VPI:
2177 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002178 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002179 case MBX_PORT_CAPABILITIES:
2180 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002181 case MBX_SLI4_CONFIG:
2182 case MBX_SLI4_REQ_FTRS:
2183 case MBX_REG_FCFI:
2184 case MBX_UNREG_FCFI:
2185 case MBX_REG_VFI:
2186 case MBX_UNREG_VFI:
2187 case MBX_INIT_VPI:
2188 case MBX_INIT_VFI:
2189 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002190 case MBX_READ_EVENT_LOG_STATUS:
2191 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002192 case MBX_SECURITY_MGMT:
2193 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002194 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002195 ret = mbxCommand;
2196 break;
2197 default:
2198 ret = MBX_SHUTDOWN;
2199 break;
2200 }
James Smart2e0fef82007-06-17 19:56:36 -05002201 return ret;
dea31012005-04-17 16:05:31 -05002202}
James Smarte59058c2008-08-24 21:49:00 -04002203
2204/**
James Smart3621a712009-04-06 18:47:14 -04002205 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002206 * @phba: Pointer to HBA context object.
2207 * @pmboxq: Pointer to mailbox command.
2208 *
2209 * This is completion handler function for mailbox commands issued from
2210 * lpfc_sli_issue_mbox_wait function. This function is called by the
2211 * mailbox event handler function with no lock held. This function
2212 * will wake up thread waiting on the wait queue pointed by context1
2213 * of the mailbox.
2214 **/
James Smart04c68492009-05-22 14:52:52 -04002215void
James Smart2e0fef82007-06-17 19:56:36 -05002216lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002217{
2218 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002219 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002220
2221 /*
2222 * If pdone_q is empty, the driver thread gave up waiting and
2223 * continued running.
2224 */
James Smart7054a602007-04-25 09:52:34 -04002225 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002226 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002227 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2228 if (pdone_q)
2229 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002230 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002231 return;
2232}
2233
James Smarte59058c2008-08-24 21:49:00 -04002234
2235/**
James Smart3621a712009-04-06 18:47:14 -04002236 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002237 * @phba: Pointer to HBA context object.
2238 * @pmb: Pointer to mailbox object.
2239 *
2240 * This function is the default mailbox completion handler. It
2241 * frees the memory resources associated with the completed mailbox
2242 * command. If the completed command is a REG_LOGIN mailbox command,
2243 * this function will issue a UREG_LOGIN to re-claim the RPI.
2244 **/
dea31012005-04-17 16:05:31 -05002245void
James Smart2e0fef82007-06-17 19:56:36 -05002246lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002247{
James Smartd439d282010-09-29 11:18:45 -04002248 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002249 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002250 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002251 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002252 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002253 int rc;
2254
dea31012005-04-17 16:05:31 -05002255 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002256
dea31012005-04-17 16:05:31 -05002257 if (mp) {
2258 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2259 kfree(mp);
2260 }
James Smart7054a602007-04-25 09:52:34 -04002261
2262 /*
2263 * If a REG_LOGIN succeeded after node is destroyed or node
2264 * is in re-discovery driver need to cleanup the RPI.
2265 */
James Smart2e0fef82007-06-17 19:56:36 -05002266 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002267 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2268 !pmb->u.mb.mbxStatus) {
2269 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002270 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002271 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002272 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002273 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002274 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2275 if (rc != MBX_NOT_FINISHED)
2276 return;
2277 }
2278
James Smart695a8142010-01-26 23:08:03 -05002279 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2280 !(phba->pport->load_flag & FC_UNLOADING) &&
2281 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002282 shost = lpfc_shost_from_vport(vport);
2283 spin_lock_irq(shost->host_lock);
2284 vport->vpi_state |= LPFC_VPI_REGISTERED;
2285 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2286 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002287 }
2288
James Smartd439d282010-09-29 11:18:45 -04002289 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2290 ndlp = (struct lpfc_nodelist *)pmb->context2;
2291 lpfc_nlp_put(ndlp);
2292 pmb->context2 = NULL;
2293 }
2294
James Smartdcf2a4e2010-09-29 11:18:53 -04002295 /* Check security permission status on INIT_LINK mailbox command */
2296 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2297 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2298 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2299 "2860 SLI authentication is required "
2300 "for INIT_LINK but has not done yet\n");
2301
James Smart04c68492009-05-22 14:52:52 -04002302 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2303 lpfc_sli4_mbox_cmd_free(phba, pmb);
2304 else
2305 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002306}
James Smartbe6bb942015-04-07 15:07:22 -04002307 /**
2308 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2309 * @phba: Pointer to HBA context object.
2310 * @pmb: Pointer to mailbox object.
2311 *
2312 * This function is the unreg rpi mailbox completion handler. It
2313 * frees the memory resources associated with the completed mailbox
2314 * command. An additional refrenece is put on the ndlp to prevent
2315 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2316 * the unreg mailbox command completes, this routine puts the
2317 * reference back.
2318 *
2319 **/
2320void
2321lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2322{
2323 struct lpfc_vport *vport = pmb->vport;
2324 struct lpfc_nodelist *ndlp;
2325
2326 ndlp = pmb->context1;
2327 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2328 if (phba->sli_rev == LPFC_SLI_REV4 &&
2329 (bf_get(lpfc_sli_intf_if_type,
2330 &phba->sli4_hba.sli_intf) ==
2331 LPFC_SLI_INTF_IF_TYPE_2)) {
2332 if (ndlp) {
2333 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2334 "0010 UNREG_LOGIN vpi:%x "
2335 "rpi:%x DID:%x map:%x %p\n",
2336 vport->vpi, ndlp->nlp_rpi,
2337 ndlp->nlp_DID,
2338 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002339 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002340 lpfc_nlp_put(ndlp);
2341 }
2342 }
2343 }
2344
2345 mempool_free(pmb, phba->mbox_mem_pool);
2346}
dea31012005-04-17 16:05:31 -05002347
James Smarte59058c2008-08-24 21:49:00 -04002348/**
James Smart3621a712009-04-06 18:47:14 -04002349 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002350 * @phba: Pointer to HBA context object.
2351 *
2352 * This function is called with no lock held. This function processes all
2353 * the completed mailbox commands and gives it to upper layers. The interrupt
2354 * service routine processes mailbox completion interrupt and adds completed
2355 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2356 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2357 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2358 * function returns the mailbox commands to the upper layer by calling the
2359 * completion handler function of each mailbox.
2360 **/
dea31012005-04-17 16:05:31 -05002361int
James Smart2e0fef82007-06-17 19:56:36 -05002362lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002363{
James Smart92d7f7b2007-06-17 19:56:38 -05002364 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002365 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002366 int rc;
2367 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002368
2369 phba->sli.slistat.mbox_event++;
2370
James Smart92d7f7b2007-06-17 19:56:38 -05002371 /* Get all completed mailboxe buffers into the cmplq */
2372 spin_lock_irq(&phba->hbalock);
2373 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2374 spin_unlock_irq(&phba->hbalock);
2375
dea31012005-04-17 16:05:31 -05002376 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002377 do {
2378 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2379 if (pmb == NULL)
2380 break;
2381
James Smart04c68492009-05-22 14:52:52 -04002382 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002383
James Smart858c9f62007-06-17 19:56:39 -05002384 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2385 if (pmb->vport) {
2386 lpfc_debugfs_disc_trc(pmb->vport,
2387 LPFC_DISC_TRC_MBOX_VPORT,
2388 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2389 (uint32_t)pmbox->mbxCommand,
2390 pmbox->un.varWords[0],
2391 pmbox->un.varWords[1]);
2392 }
2393 else {
2394 lpfc_debugfs_disc_trc(phba->pport,
2395 LPFC_DISC_TRC_MBOX,
2396 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2397 (uint32_t)pmbox->mbxCommand,
2398 pmbox->un.varWords[0],
2399 pmbox->un.varWords[1]);
2400 }
2401 }
2402
dea31012005-04-17 16:05:31 -05002403 /*
2404 * It is a fatal error if unknown mbox command completion.
2405 */
2406 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2407 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002408 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002409 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002410 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002411 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002412 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002413 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002414 lpfc_sli_config_mbox_subsys_get(phba,
2415 pmb),
2416 lpfc_sli_config_mbox_opcode_get(phba,
2417 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002418 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002419 phba->work_hs = HS_FFER3;
2420 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002421 continue;
dea31012005-04-17 16:05:31 -05002422 }
2423
dea31012005-04-17 16:05:31 -05002424 if (pmbox->mbxStatus) {
2425 phba->sli.slistat.mbox_stat_err++;
2426 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2427 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002428 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002429 LOG_MBOX | LOG_SLI,
2430 "(%d):0305 Mbox cmd cmpl "
2431 "error - RETRYing Data: x%x "
2432 "(x%x/x%x) x%x x%x x%x\n",
2433 pmb->vport ? pmb->vport->vpi : 0,
2434 pmbox->mbxCommand,
2435 lpfc_sli_config_mbox_subsys_get(phba,
2436 pmb),
2437 lpfc_sli_config_mbox_opcode_get(phba,
2438 pmb),
2439 pmbox->mbxStatus,
2440 pmbox->un.varWords[0],
2441 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002442 pmbox->mbxStatus = 0;
2443 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002444 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002445 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002446 continue;
dea31012005-04-17 16:05:31 -05002447 }
2448 }
2449
2450 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002451 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002452 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002453 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2454 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002455 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002456 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002457 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2458 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002459 pmb->mbox_cmpl,
2460 *((uint32_t *) pmbox),
2461 pmbox->un.varWords[0],
2462 pmbox->un.varWords[1],
2463 pmbox->un.varWords[2],
2464 pmbox->un.varWords[3],
2465 pmbox->un.varWords[4],
2466 pmbox->un.varWords[5],
2467 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002468 pmbox->un.varWords[7],
2469 pmbox->un.varWords[8],
2470 pmbox->un.varWords[9],
2471 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002472
James Smart92d7f7b2007-06-17 19:56:38 -05002473 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002474 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002475 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002476 return 0;
dea31012005-04-17 16:05:31 -05002477}
James Smart92d7f7b2007-06-17 19:56:38 -05002478
James Smarte59058c2008-08-24 21:49:00 -04002479/**
James Smart3621a712009-04-06 18:47:14 -04002480 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002481 * @phba: Pointer to HBA context object.
2482 * @pring: Pointer to driver SLI ring object.
2483 * @tag: buffer tag.
2484 *
2485 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2486 * is set in the tag the buffer is posted for a particular exchange,
2487 * the function will return the buffer without replacing the buffer.
2488 * If the buffer is for unsolicited ELS or CT traffic, this function
2489 * returns the buffer and also posts another buffer to the firmware.
2490 **/
James Smart76bb24e2007-10-27 13:38:00 -04002491static struct lpfc_dmabuf *
2492lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002493 struct lpfc_sli_ring *pring,
2494 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002495{
James Smart9f1e1b52008-12-04 22:39:40 -05002496 struct hbq_dmabuf *hbq_entry;
2497
James Smart76bb24e2007-10-27 13:38:00 -04002498 if (tag & QUE_BUFTAG_BIT)
2499 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002500 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2501 if (!hbq_entry)
2502 return NULL;
2503 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002504}
James Smart57127f12007-10-27 13:37:05 -04002505
James Smart3772a992009-05-22 14:50:54 -04002506/**
2507 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2508 * @phba: Pointer to HBA context object.
2509 * @pring: Pointer to driver SLI ring object.
2510 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2511 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2512 * @fch_type: the type for the first frame of the sequence.
2513 *
2514 * This function is called with no lock held. This function uses the r_ctl and
2515 * type of the received sequence to find the correct callback function to call
2516 * to process the sequence.
2517 **/
2518static int
2519lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2520 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2521 uint32_t fch_type)
2522{
2523 int i;
2524
James Smartf358dd02017-02-12 13:52:34 -08002525 switch (fch_type) {
2526 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002527 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002528 return 1;
2529 default:
2530 break;
2531 }
2532
James Smart3772a992009-05-22 14:50:54 -04002533 /* unSolicited Responses */
2534 if (pring->prt[0].profile) {
2535 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2536 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2537 saveq);
2538 return 1;
2539 }
2540 /* We must search, based on rctl / type
2541 for the right routine */
2542 for (i = 0; i < pring->num_mask; i++) {
2543 if ((pring->prt[i].rctl == fch_r_ctl) &&
2544 (pring->prt[i].type == fch_type)) {
2545 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2546 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2547 (phba, pring, saveq);
2548 return 1;
2549 }
2550 }
2551 return 0;
2552}
James Smarte59058c2008-08-24 21:49:00 -04002553
2554/**
James Smart3621a712009-04-06 18:47:14 -04002555 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002556 * @phba: Pointer to HBA context object.
2557 * @pring: Pointer to driver SLI ring object.
2558 * @saveq: Pointer to the unsolicited iocb.
2559 *
2560 * This function is called with no lock held by the ring event handler
2561 * when there is an unsolicited iocb posted to the response ring by the
2562 * firmware. This function gets the buffer associated with the iocbs
2563 * and calls the event handler for the ring. This function handles both
2564 * qring buffers and hbq buffers.
2565 * When the function returns 1 the caller can free the iocb object otherwise
2566 * upper layer functions will free the iocb objects.
2567 **/
dea31012005-04-17 16:05:31 -05002568static int
2569lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2570 struct lpfc_iocbq *saveq)
2571{
2572 IOCB_t * irsp;
2573 WORD5 * w5p;
2574 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002575 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002576 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002577
dea31012005-04-17 16:05:31 -05002578 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002579
2580 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2581 if (pring->lpfc_sli_rcv_async_status)
2582 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2583 else
2584 lpfc_printf_log(phba,
2585 KERN_WARNING,
2586 LOG_SLI,
2587 "0316 Ring %d handler: unexpected "
2588 "ASYNC_STATUS iocb received evt_code "
2589 "0x%x\n",
2590 pring->ringno,
2591 irsp->un.asyncstat.evt_code);
2592 return 1;
2593 }
2594
James Smart3163f722008-02-08 18:50:25 -05002595 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2596 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2597 if (irsp->ulpBdeCount > 0) {
2598 dmzbuf = lpfc_sli_get_buff(phba, pring,
2599 irsp->un.ulpWord[3]);
2600 lpfc_in_buf_free(phba, dmzbuf);
2601 }
2602
2603 if (irsp->ulpBdeCount > 1) {
2604 dmzbuf = lpfc_sli_get_buff(phba, pring,
2605 irsp->unsli3.sli3Words[3]);
2606 lpfc_in_buf_free(phba, dmzbuf);
2607 }
2608
2609 if (irsp->ulpBdeCount > 2) {
2610 dmzbuf = lpfc_sli_get_buff(phba, pring,
2611 irsp->unsli3.sli3Words[7]);
2612 lpfc_in_buf_free(phba, dmzbuf);
2613 }
2614
2615 return 1;
2616 }
2617
James Smart92d7f7b2007-06-17 19:56:38 -05002618 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002619 if (irsp->ulpBdeCount != 0) {
2620 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002621 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002622 if (!saveq->context2)
2623 lpfc_printf_log(phba,
2624 KERN_ERR,
2625 LOG_SLI,
2626 "0341 Ring %d Cannot find buffer for "
2627 "an unsolicited iocb. tag 0x%x\n",
2628 pring->ringno,
2629 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002630 }
2631 if (irsp->ulpBdeCount == 2) {
2632 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002633 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002634 if (!saveq->context3)
2635 lpfc_printf_log(phba,
2636 KERN_ERR,
2637 LOG_SLI,
2638 "0342 Ring %d Cannot find buffer for an"
2639 " unsolicited iocb. tag 0x%x\n",
2640 pring->ringno,
2641 irsp->unsli3.sli3Words[7]);
2642 }
2643 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002644 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002645 if (irsp->ulpBdeCount != 0) {
2646 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2647 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002648 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002649 lpfc_printf_log(phba,
2650 KERN_ERR,
2651 LOG_SLI,
2652 "0343 Ring %d Cannot find "
2653 "buffer for an unsolicited iocb"
2654 ". tag 0x%x\n", pring->ringno,
2655 irsp->un.ulpWord[3]);
2656 }
2657 if (irsp->ulpBdeCount == 2) {
2658 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2659 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002660 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002661 lpfc_printf_log(phba,
2662 KERN_ERR,
2663 LOG_SLI,
2664 "0344 Ring %d Cannot find "
2665 "buffer for an unsolicited "
2666 "iocb. tag 0x%x\n",
2667 pring->ringno,
2668 irsp->unsli3.sli3Words[7]);
2669 }
2670 }
James Smart92d7f7b2007-06-17 19:56:38 -05002671 }
James Smart9c2face2008-01-11 01:53:18 -05002672 if (irsp->ulpBdeCount != 0 &&
2673 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2674 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2675 int found = 0;
2676
2677 /* search continue save q for same XRI */
2678 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002679 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2680 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002681 list_add_tail(&saveq->list, &iocbq->list);
2682 found = 1;
2683 break;
2684 }
2685 }
2686 if (!found)
2687 list_add_tail(&saveq->clist,
2688 &pring->iocb_continue_saveq);
2689 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2690 list_del_init(&iocbq->clist);
2691 saveq = iocbq;
2692 irsp = &(saveq->iocb);
2693 } else
2694 return 0;
2695 }
2696 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2697 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2698 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002699 Rctl = FC_RCTL_ELS_REQ;
2700 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002701 } else {
2702 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2703 Rctl = w5p->hcsw.Rctl;
2704 Type = w5p->hcsw.Type;
2705
2706 /* Firmware Workaround */
2707 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2708 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2709 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002710 Rctl = FC_RCTL_ELS_REQ;
2711 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002712 w5p->hcsw.Rctl = Rctl;
2713 w5p->hcsw.Type = Type;
2714 }
2715 }
James Smart92d7f7b2007-06-17 19:56:38 -05002716
James Smart3772a992009-05-22 14:50:54 -04002717 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002718 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002719 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002720 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002721 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002722
James Smart92d7f7b2007-06-17 19:56:38 -05002723 return 1;
dea31012005-04-17 16:05:31 -05002724}
2725
James Smarte59058c2008-08-24 21:49:00 -04002726/**
James Smart3621a712009-04-06 18:47:14 -04002727 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002728 * @phba: Pointer to HBA context object.
2729 * @pring: Pointer to driver SLI ring object.
2730 * @prspiocb: Pointer to response iocb object.
2731 *
2732 * This function looks up the iocb_lookup table to get the command iocb
2733 * corresponding to the given response iocb using the iotag of the
2734 * response iocb. This function is called with the hbalock held.
2735 * This function returns the command iocb object if it finds the command
2736 * iocb else returns NULL.
2737 **/
dea31012005-04-17 16:05:31 -05002738static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002739lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2740 struct lpfc_sli_ring *pring,
2741 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002742{
dea31012005-04-17 16:05:31 -05002743 struct lpfc_iocbq *cmd_iocb = NULL;
2744 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002745 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002746
James Bottomley604a3e32005-10-29 10:28:33 -05002747 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002748
James Bottomley604a3e32005-10-29 10:28:33 -05002749 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2750 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002751 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002752 /* remove from txcmpl queue list */
2753 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002754 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002755 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002756 }
dea31012005-04-17 16:05:31 -05002757 }
2758
dea31012005-04-17 16:05:31 -05002759 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002760 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002761 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002762 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002763 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002764 return NULL;
2765}
2766
James Smarte59058c2008-08-24 21:49:00 -04002767/**
James Smart3772a992009-05-22 14:50:54 -04002768 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2769 * @phba: Pointer to HBA context object.
2770 * @pring: Pointer to driver SLI ring object.
2771 * @iotag: IOCB tag.
2772 *
2773 * This function looks up the iocb_lookup table to get the command iocb
2774 * corresponding to the given iotag. This function is called with the
2775 * hbalock held.
2776 * This function returns the command iocb object if it finds the command
2777 * iocb else returns NULL.
2778 **/
2779static struct lpfc_iocbq *
2780lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2781 struct lpfc_sli_ring *pring, uint16_t iotag)
2782{
James Smart895427b2017-02-12 13:52:30 -08002783 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002784
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002785 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002786 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2787 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002788 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2789 /* remove from txcmpl queue list */
2790 list_del_init(&cmd_iocb->list);
2791 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002792 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002793 }
James Smart3772a992009-05-22 14:50:54 -04002794 }
James Smart89533e92016-10-13 15:06:15 -07002795
James Smart3772a992009-05-22 14:50:54 -04002796 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002797 "0372 iotag x%x lookup error: max iotag (x%x) "
2798 "iocb_flag x%x\n",
2799 iotag, phba->sli.last_iotag,
2800 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002801 return NULL;
2802}
2803
2804/**
James Smart3621a712009-04-06 18:47:14 -04002805 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002806 * @phba: Pointer to HBA context object.
2807 * @pring: Pointer to driver SLI ring object.
2808 * @saveq: Pointer to the response iocb to be processed.
2809 *
2810 * This function is called by the ring event handler for non-fcp
2811 * rings when there is a new response iocb in the response ring.
2812 * The caller is not required to hold any locks. This function
2813 * gets the command iocb associated with the response iocb and
2814 * calls the completion handler for the command iocb. If there
2815 * is no completion handler, the function will free the resources
2816 * associated with command iocb. If the response iocb is for
2817 * an already aborted command iocb, the status of the completion
2818 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2819 * This function always returns 1.
2820 **/
dea31012005-04-17 16:05:31 -05002821static int
James Smart2e0fef82007-06-17 19:56:36 -05002822lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002823 struct lpfc_iocbq *saveq)
2824{
James Smart2e0fef82007-06-17 19:56:36 -05002825 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002826 int rc = 1;
2827 unsigned long iflag;
2828
2829 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002830 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002831 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002832 spin_unlock_irqrestore(&phba->hbalock, iflag);
2833
dea31012005-04-17 16:05:31 -05002834 if (cmdiocbp) {
2835 if (cmdiocbp->iocb_cmpl) {
2836 /*
James Smartea2151b2008-09-07 11:52:10 -04002837 * If an ELS command failed send an event to mgmt
2838 * application.
2839 */
2840 if (saveq->iocb.ulpStatus &&
2841 (pring->ringno == LPFC_ELS_RING) &&
2842 (cmdiocbp->iocb.ulpCommand ==
2843 CMD_ELS_REQUEST64_CR))
2844 lpfc_send_els_failure_event(phba,
2845 cmdiocbp, saveq);
2846
2847 /*
dea31012005-04-17 16:05:31 -05002848 * Post all ELS completions to the worker thread.
2849 * All other are passed to the completion callback.
2850 */
2851 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002852 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2853 (cmdiocbp->iocb_flag &
2854 LPFC_DRIVER_ABORTED)) {
2855 spin_lock_irqsave(&phba->hbalock,
2856 iflag);
James Smart07951072007-04-25 09:51:38 -04002857 cmdiocbp->iocb_flag &=
2858 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002859 spin_unlock_irqrestore(&phba->hbalock,
2860 iflag);
James Smart07951072007-04-25 09:51:38 -04002861 saveq->iocb.ulpStatus =
2862 IOSTAT_LOCAL_REJECT;
2863 saveq->iocb.un.ulpWord[4] =
2864 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002865
2866 /* Firmware could still be in progress
2867 * of DMAing payload, so don't free data
2868 * buffer till after a hbeat.
2869 */
James Smart341af102010-01-26 23:07:37 -05002870 spin_lock_irqsave(&phba->hbalock,
2871 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002872 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002873 spin_unlock_irqrestore(&phba->hbalock,
2874 iflag);
2875 }
James Smart0f65ff62010-02-26 14:14:23 -05002876 if (phba->sli_rev == LPFC_SLI_REV4) {
2877 if (saveq->iocb_flag &
2878 LPFC_EXCHANGE_BUSY) {
2879 /* Set cmdiocb flag for the
2880 * exchange busy so sgl (xri)
2881 * will not be released until
2882 * the abort xri is received
2883 * from hba.
2884 */
2885 spin_lock_irqsave(
2886 &phba->hbalock, iflag);
2887 cmdiocbp->iocb_flag |=
2888 LPFC_EXCHANGE_BUSY;
2889 spin_unlock_irqrestore(
2890 &phba->hbalock, iflag);
2891 }
2892 if (cmdiocbp->iocb_flag &
2893 LPFC_DRIVER_ABORTED) {
2894 /*
2895 * Clear LPFC_DRIVER_ABORTED
2896 * bit in case it was driver
2897 * initiated abort.
2898 */
2899 spin_lock_irqsave(
2900 &phba->hbalock, iflag);
2901 cmdiocbp->iocb_flag &=
2902 ~LPFC_DRIVER_ABORTED;
2903 spin_unlock_irqrestore(
2904 &phba->hbalock, iflag);
2905 cmdiocbp->iocb.ulpStatus =
2906 IOSTAT_LOCAL_REJECT;
2907 cmdiocbp->iocb.un.ulpWord[4] =
2908 IOERR_ABORT_REQUESTED;
2909 /*
2910 * For SLI4, irsiocb contains
2911 * NO_XRI in sli_xritag, it
2912 * shall not affect releasing
2913 * sgl (xri) process.
2914 */
2915 saveq->iocb.ulpStatus =
2916 IOSTAT_LOCAL_REJECT;
2917 saveq->iocb.un.ulpWord[4] =
2918 IOERR_SLI_ABORTED;
2919 spin_lock_irqsave(
2920 &phba->hbalock, iflag);
2921 saveq->iocb_flag |=
2922 LPFC_DELAY_MEM_FREE;
2923 spin_unlock_irqrestore(
2924 &phba->hbalock, iflag);
2925 }
James Smart07951072007-04-25 09:51:38 -04002926 }
dea31012005-04-17 16:05:31 -05002927 }
James Smart2e0fef82007-06-17 19:56:36 -05002928 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002929 } else
2930 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002931 } else {
2932 /*
2933 * Unknown initiating command based on the response iotag.
2934 * This could be the case on the ELS ring because of
2935 * lpfc_els_abort().
2936 */
2937 if (pring->ringno != LPFC_ELS_RING) {
2938 /*
2939 * Ring <ringno> handler: unexpected completion IoTag
2940 * <IoTag>
2941 */
James Smarta257bf92009-04-06 18:48:10 -04002942 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002943 "0322 Ring %d handler: "
2944 "unexpected completion IoTag x%x "
2945 "Data: x%x x%x x%x x%x\n",
2946 pring->ringno,
2947 saveq->iocb.ulpIoTag,
2948 saveq->iocb.ulpStatus,
2949 saveq->iocb.un.ulpWord[4],
2950 saveq->iocb.ulpCommand,
2951 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002952 }
2953 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002954
dea31012005-04-17 16:05:31 -05002955 return rc;
2956}
2957
James Smarte59058c2008-08-24 21:49:00 -04002958/**
James Smart3621a712009-04-06 18:47:14 -04002959 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002960 * @phba: Pointer to HBA context object.
2961 * @pring: Pointer to driver SLI ring object.
2962 *
2963 * This function is called from the iocb ring event handlers when
2964 * put pointer is ahead of the get pointer for a ring. This function signal
2965 * an error attention condition to the worker thread and the worker
2966 * thread will transition the HBA to offline state.
2967 **/
James Smart2e0fef82007-06-17 19:56:36 -05002968static void
2969lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002970{
James Smart34b02dc2008-08-24 21:49:55 -04002971 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002972 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002973 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002974 * rsp ring <portRspMax>
2975 */
2976 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002977 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002978 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002979 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002980 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002981
James Smart2e0fef82007-06-17 19:56:36 -05002982 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002983
2984 /*
2985 * All error attention handlers are posted to
2986 * worker thread
2987 */
2988 phba->work_ha |= HA_ERATT;
2989 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002990
James Smart5e9d9b82008-06-14 22:52:53 -04002991 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002992
2993 return;
2994}
2995
James Smarte59058c2008-08-24 21:49:00 -04002996/**
James Smart3621a712009-04-06 18:47:14 -04002997 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002998 * @ptr: Pointer to address of HBA context object.
2999 *
3000 * This function is invoked by the Error Attention polling timer when the
3001 * timer times out. It will check the SLI Error Attention register for
3002 * possible attention events. If so, it will post an Error Attention event
3003 * and wake up worker thread to process it. Otherwise, it will set up the
3004 * Error Attention polling timer for the next poll.
3005 **/
3006void lpfc_poll_eratt(unsigned long ptr)
3007{
3008 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003009 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003010 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003011
3012 phba = (struct lpfc_hba *)ptr;
3013
James Smartaa6fbb72012-08-03 12:36:03 -04003014 /* Here we will also keep track of interrupts per sec of the hba */
3015 sli_intr = phba->sli.slistat.sli_intr;
3016
3017 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3018 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3019 sli_intr);
3020 else
3021 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3022
James Smart65791f12016-07-06 12:35:56 -07003023 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3024 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003025 phba->sli.slistat.sli_ips = cnt;
3026
3027 phba->sli.slistat.sli_prev_intr = sli_intr;
3028
James Smart93996272008-08-24 21:50:30 -04003029 /* Check chip HA register for error event */
3030 eratt = lpfc_sli_check_eratt(phba);
3031
3032 if (eratt)
3033 /* Tell the worker thread there is work to do */
3034 lpfc_worker_wake_up(phba);
3035 else
3036 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003037 mod_timer(&phba->eratt_poll,
3038 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003039 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003040 return;
3041}
3042
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003043
James Smarte59058c2008-08-24 21:49:00 -04003044/**
James Smart3621a712009-04-06 18:47:14 -04003045 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003046 * @phba: Pointer to HBA context object.
3047 * @pring: Pointer to driver SLI ring object.
3048 * @mask: Host attention register mask for this ring.
3049 *
3050 * This function is called from the interrupt context when there is a ring
3051 * event for the fcp ring. The caller does not hold any lock.
3052 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003053 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003054 * LE bit set. The function will call the completion handler of the command iocb
3055 * if the response iocb indicates a completion for a command iocb or it is
3056 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3057 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003058 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003059 * to check it explicitly.
3060 */
3061int
James Smart2e0fef82007-06-17 19:56:36 -05003062lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3063 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003064{
James Smart34b02dc2008-08-24 21:49:55 -04003065 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003066 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003067 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003068 struct lpfc_iocbq *cmdiocbq = NULL;
3069 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003070 uint32_t status;
3071 uint32_t portRspPut, portRspMax;
3072 int rc = 1;
3073 lpfc_iocb_type type;
3074 unsigned long iflag;
3075 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003076
James Smart2e0fef82007-06-17 19:56:36 -05003077 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003078 pring->stats.iocb_event++;
3079
dea31012005-04-17 16:05:31 -05003080 /*
3081 * The next available response entry should never exceed the maximum
3082 * entries. If it does, treat it as an adapter hardware error.
3083 */
James Smart7e56aa22012-08-03 12:35:34 -04003084 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003085 portRspPut = le32_to_cpu(pgp->rspPutInx);
3086 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003087 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003088 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003089 return 1;
3090 }
James Smart45ed1192009-10-02 15:17:02 -04003091 if (phba->fcp_ring_in_use) {
3092 spin_unlock_irqrestore(&phba->hbalock, iflag);
3093 return 1;
3094 } else
3095 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003096
3097 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003098 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003099 /*
3100 * Fetch an entry off the ring and copy it into a local data
3101 * structure. The copy involves a byte-swap since the
3102 * network byte order and pci byte orders are different.
3103 */
James Smarted957682007-06-17 19:56:37 -05003104 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003105 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003106
James Smart7e56aa22012-08-03 12:35:34 -04003107 if (++pring->sli.sli3.rspidx >= portRspMax)
3108 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003109
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003110 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3111 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003112 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003113 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003114 irsp = &rspiocbq.iocb;
3115
dea31012005-04-17 16:05:31 -05003116 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3117 pring->stats.iocb_rsp++;
3118 rsp_cmpl++;
3119
3120 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003121 /*
3122 * If resource errors reported from HBA, reduce
3123 * queuedepths of the SCSI device.
3124 */
3125 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003126 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3127 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003128 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003129 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003130 spin_lock_irqsave(&phba->hbalock, iflag);
3131 }
3132
dea31012005-04-17 16:05:31 -05003133 /* Rsp ring <ringno> error: IOCB */
3134 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003135 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003136 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003137 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003138 irsp->un.ulpWord[0],
3139 irsp->un.ulpWord[1],
3140 irsp->un.ulpWord[2],
3141 irsp->un.ulpWord[3],
3142 irsp->un.ulpWord[4],
3143 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003144 *(uint32_t *)&irsp->un1,
3145 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003146 }
3147
3148 switch (type) {
3149 case LPFC_ABORT_IOCB:
3150 case LPFC_SOL_IOCB:
3151 /*
3152 * Idle exchange closed via ABTS from port. No iocb
3153 * resources need to be recovered.
3154 */
3155 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003156 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003157 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003158 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003159 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003160 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003161 break;
3162 }
3163
James Bottomley604a3e32005-10-29 10:28:33 -05003164 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3165 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003166 if (unlikely(!cmdiocbq))
3167 break;
3168 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3169 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3170 if (cmdiocbq->iocb_cmpl) {
3171 spin_unlock_irqrestore(&phba->hbalock, iflag);
3172 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3173 &rspiocbq);
3174 spin_lock_irqsave(&phba->hbalock, iflag);
3175 }
dea31012005-04-17 16:05:31 -05003176 break;
James Smarta4bc3372006-12-02 13:34:16 -05003177 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003178 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003179 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003180 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003181 break;
dea31012005-04-17 16:05:31 -05003182 default:
3183 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3184 char adaptermsg[LPFC_MAX_ADPTMSG];
3185 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3186 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3187 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003188 dev_warn(&((phba->pcidev)->dev),
3189 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003190 phba->brd_no, adaptermsg);
3191 } else {
3192 /* Unknown IOCB command */
3193 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003194 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003195 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003196 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003197 irsp->ulpStatus,
3198 irsp->ulpIoTag,
3199 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003200 }
3201 break;
3202 }
3203
3204 /*
3205 * The response IOCB has been processed. Update the ring
3206 * pointer in SLIM. If the port response put pointer has not
3207 * been updated, sync the pgp->rspPutInx and fetch the new port
3208 * response put pointer.
3209 */
James Smart7e56aa22012-08-03 12:35:34 -04003210 writel(pring->sli.sli3.rspidx,
3211 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003212
James Smart7e56aa22012-08-03 12:35:34 -04003213 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003214 portRspPut = le32_to_cpu(pgp->rspPutInx);
3215 }
3216
3217 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3218 pring->stats.iocb_rsp_full++;
3219 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3220 writel(status, phba->CAregaddr);
3221 readl(phba->CAregaddr);
3222 }
3223 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3224 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3225 pring->stats.iocb_cmd_empty++;
3226
3227 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003228 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003229 lpfc_sli_resume_iocb(phba, pring);
3230
3231 if ((pring->lpfc_sli_cmd_available))
3232 (pring->lpfc_sli_cmd_available) (phba, pring);
3233
3234 }
3235
James Smart45ed1192009-10-02 15:17:02 -04003236 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003237 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003238 return rc;
3239}
3240
James Smarte59058c2008-08-24 21:49:00 -04003241/**
James Smart3772a992009-05-22 14:50:54 -04003242 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3243 * @phba: Pointer to HBA context object.
3244 * @pring: Pointer to driver SLI ring object.
3245 * @rspiocbp: Pointer to driver response IOCB object.
3246 *
3247 * This function is called from the worker thread when there is a slow-path
3248 * response IOCB to process. This function chains all the response iocbs until
3249 * seeing the iocb with the LE bit set. The function will call
3250 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3251 * completion of a command iocb. The function will call the
3252 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3253 * The function frees the resources or calls the completion handler if this
3254 * iocb is an abort completion. The function returns NULL when the response
3255 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3256 * this function shall chain the iocb on to the iocb_continueq and return the
3257 * response iocb passed in.
3258 **/
3259static struct lpfc_iocbq *
3260lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3261 struct lpfc_iocbq *rspiocbp)
3262{
3263 struct lpfc_iocbq *saveq;
3264 struct lpfc_iocbq *cmdiocbp;
3265 struct lpfc_iocbq *next_iocb;
3266 IOCB_t *irsp = NULL;
3267 uint32_t free_saveq;
3268 uint8_t iocb_cmd_type;
3269 lpfc_iocb_type type;
3270 unsigned long iflag;
3271 int rc;
3272
3273 spin_lock_irqsave(&phba->hbalock, iflag);
3274 /* First add the response iocb to the countinueq list */
3275 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3276 pring->iocb_continueq_cnt++;
3277
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003278 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003279 irsp = &rspiocbp->iocb;
3280 if (irsp->ulpLe) {
3281 /*
3282 * By default, the driver expects to free all resources
3283 * associated with this iocb completion.
3284 */
3285 free_saveq = 1;
3286 saveq = list_get_first(&pring->iocb_continueq,
3287 struct lpfc_iocbq, list);
3288 irsp = &(saveq->iocb);
3289 list_del_init(&pring->iocb_continueq);
3290 pring->iocb_continueq_cnt = 0;
3291
3292 pring->stats.iocb_rsp++;
3293
3294 /*
3295 * If resource errors reported from HBA, reduce
3296 * queuedepths of the SCSI device.
3297 */
3298 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003299 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3300 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003301 spin_unlock_irqrestore(&phba->hbalock, iflag);
3302 phba->lpfc_rampdown_queue_depth(phba);
3303 spin_lock_irqsave(&phba->hbalock, iflag);
3304 }
3305
3306 if (irsp->ulpStatus) {
3307 /* Rsp ring <ringno> error: IOCB */
3308 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3309 "0328 Rsp Ring %d error: "
3310 "IOCB Data: "
3311 "x%x x%x x%x x%x "
3312 "x%x x%x x%x x%x "
3313 "x%x x%x x%x x%x "
3314 "x%x x%x x%x x%x\n",
3315 pring->ringno,
3316 irsp->un.ulpWord[0],
3317 irsp->un.ulpWord[1],
3318 irsp->un.ulpWord[2],
3319 irsp->un.ulpWord[3],
3320 irsp->un.ulpWord[4],
3321 irsp->un.ulpWord[5],
3322 *(((uint32_t *) irsp) + 6),
3323 *(((uint32_t *) irsp) + 7),
3324 *(((uint32_t *) irsp) + 8),
3325 *(((uint32_t *) irsp) + 9),
3326 *(((uint32_t *) irsp) + 10),
3327 *(((uint32_t *) irsp) + 11),
3328 *(((uint32_t *) irsp) + 12),
3329 *(((uint32_t *) irsp) + 13),
3330 *(((uint32_t *) irsp) + 14),
3331 *(((uint32_t *) irsp) + 15));
3332 }
3333
3334 /*
3335 * Fetch the IOCB command type and call the correct completion
3336 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3337 * get freed back to the lpfc_iocb_list by the discovery
3338 * kernel thread.
3339 */
3340 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3341 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3342 switch (type) {
3343 case LPFC_SOL_IOCB:
3344 spin_unlock_irqrestore(&phba->hbalock, iflag);
3345 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3346 spin_lock_irqsave(&phba->hbalock, iflag);
3347 break;
3348
3349 case LPFC_UNSOL_IOCB:
3350 spin_unlock_irqrestore(&phba->hbalock, iflag);
3351 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3352 spin_lock_irqsave(&phba->hbalock, iflag);
3353 if (!rc)
3354 free_saveq = 0;
3355 break;
3356
3357 case LPFC_ABORT_IOCB:
3358 cmdiocbp = NULL;
3359 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3360 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3361 saveq);
3362 if (cmdiocbp) {
3363 /* Call the specified completion routine */
3364 if (cmdiocbp->iocb_cmpl) {
3365 spin_unlock_irqrestore(&phba->hbalock,
3366 iflag);
3367 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3368 saveq);
3369 spin_lock_irqsave(&phba->hbalock,
3370 iflag);
3371 } else
3372 __lpfc_sli_release_iocbq(phba,
3373 cmdiocbp);
3374 }
3375 break;
3376
3377 case LPFC_UNKNOWN_IOCB:
3378 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3379 char adaptermsg[LPFC_MAX_ADPTMSG];
3380 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3381 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3382 MAX_MSG_DATA);
3383 dev_warn(&((phba->pcidev)->dev),
3384 "lpfc%d: %s\n",
3385 phba->brd_no, adaptermsg);
3386 } else {
3387 /* Unknown IOCB command */
3388 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3389 "0335 Unknown IOCB "
3390 "command Data: x%x "
3391 "x%x x%x x%x\n",
3392 irsp->ulpCommand,
3393 irsp->ulpStatus,
3394 irsp->ulpIoTag,
3395 irsp->ulpContext);
3396 }
3397 break;
3398 }
3399
3400 if (free_saveq) {
3401 list_for_each_entry_safe(rspiocbp, next_iocb,
3402 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003403 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003404 __lpfc_sli_release_iocbq(phba, rspiocbp);
3405 }
3406 __lpfc_sli_release_iocbq(phba, saveq);
3407 }
3408 rspiocbp = NULL;
3409 }
3410 spin_unlock_irqrestore(&phba->hbalock, iflag);
3411 return rspiocbp;
3412}
3413
3414/**
3415 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003416 * @phba: Pointer to HBA context object.
3417 * @pring: Pointer to driver SLI ring object.
3418 * @mask: Host attention register mask for this ring.
3419 *
James Smart3772a992009-05-22 14:50:54 -04003420 * This routine wraps the actual slow_ring event process routine from the
3421 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003422 **/
James Smart3772a992009-05-22 14:50:54 -04003423void
James Smart2e0fef82007-06-17 19:56:36 -05003424lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3425 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003426{
James Smart3772a992009-05-22 14:50:54 -04003427 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3428}
3429
3430/**
3431 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3432 * @phba: Pointer to HBA context object.
3433 * @pring: Pointer to driver SLI ring object.
3434 * @mask: Host attention register mask for this ring.
3435 *
3436 * This function is called from the worker thread when there is a ring event
3437 * for non-fcp rings. The caller does not hold any lock. The function will
3438 * remove each response iocb in the response ring and calls the handle
3439 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3440 **/
3441static void
3442lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3443 struct lpfc_sli_ring *pring, uint32_t mask)
3444{
James Smart34b02dc2008-08-24 21:49:55 -04003445 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003446 IOCB_t *entry;
3447 IOCB_t *irsp = NULL;
3448 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003449 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003450 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003451 uint32_t status;
dea31012005-04-17 16:05:31 -05003452
James Smart34b02dc2008-08-24 21:49:55 -04003453 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003454 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003455 pring->stats.iocb_event++;
3456
dea31012005-04-17 16:05:31 -05003457 /*
3458 * The next available response entry should never exceed the maximum
3459 * entries. If it does, treat it as an adapter hardware error.
3460 */
James Smart7e56aa22012-08-03 12:35:34 -04003461 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003462 portRspPut = le32_to_cpu(pgp->rspPutInx);
3463 if (portRspPut >= portRspMax) {
3464 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003465 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003466 * rsp ring <portRspMax>
3467 */
James Smarted957682007-06-17 19:56:37 -05003468 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003469 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003470 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003471 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003472
James Smart2e0fef82007-06-17 19:56:36 -05003473 phba->link_state = LPFC_HBA_ERROR;
3474 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003475
3476 phba->work_hs = HS_FFER3;
3477 lpfc_handle_eratt(phba);
3478
James Smart3772a992009-05-22 14:50:54 -04003479 return;
dea31012005-04-17 16:05:31 -05003480 }
3481
3482 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003483 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003484 /*
3485 * Build a completion list and call the appropriate handler.
3486 * The process is to get the next available response iocb, get
3487 * a free iocb from the list, copy the response data into the
3488 * free iocb, insert to the continuation list, and update the
3489 * next response index to slim. This process makes response
3490 * iocb's in the ring available to DMA as fast as possible but
3491 * pays a penalty for a copy operation. Since the iocb is
3492 * only 32 bytes, this penalty is considered small relative to
3493 * the PCI reads for register values and a slim write. When
3494 * the ulpLe field is set, the entire Command has been
3495 * received.
3496 */
James Smarted957682007-06-17 19:56:37 -05003497 entry = lpfc_resp_iocb(phba, pring);
3498
James Smart858c9f62007-06-17 19:56:39 -05003499 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003500 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003501 if (rspiocbp == NULL) {
3502 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003503 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003504 break;
3505 }
3506
James Smarted957682007-06-17 19:56:37 -05003507 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3508 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003509 irsp = &rspiocbp->iocb;
3510
James Smart7e56aa22012-08-03 12:35:34 -04003511 if (++pring->sli.sli3.rspidx >= portRspMax)
3512 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003513
James Smarta58cbd52007-08-02 11:09:43 -04003514 if (pring->ringno == LPFC_ELS_RING) {
3515 lpfc_debugfs_slow_ring_trc(phba,
3516 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3517 *(((uint32_t *) irsp) + 4),
3518 *(((uint32_t *) irsp) + 6),
3519 *(((uint32_t *) irsp) + 7));
3520 }
3521
James Smart7e56aa22012-08-03 12:35:34 -04003522 writel(pring->sli.sli3.rspidx,
3523 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003524
James Smart3772a992009-05-22 14:50:54 -04003525 spin_unlock_irqrestore(&phba->hbalock, iflag);
3526 /* Handle the response IOCB */
3527 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3528 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003529
3530 /*
3531 * If the port response put pointer has not been updated, sync
3532 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3533 * response put pointer.
3534 */
James Smart7e56aa22012-08-03 12:35:34 -04003535 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003536 portRspPut = le32_to_cpu(pgp->rspPutInx);
3537 }
James Smart7e56aa22012-08-03 12:35:34 -04003538 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003539
James Smart92d7f7b2007-06-17 19:56:38 -05003540 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003541 /* At least one response entry has been freed */
3542 pring->stats.iocb_rsp_full++;
3543 /* SET RxRE_RSP in Chip Att register */
3544 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3545 writel(status, phba->CAregaddr);
3546 readl(phba->CAregaddr); /* flush */
3547 }
3548 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3549 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3550 pring->stats.iocb_cmd_empty++;
3551
3552 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003553 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003554 lpfc_sli_resume_iocb(phba, pring);
3555
3556 if ((pring->lpfc_sli_cmd_available))
3557 (pring->lpfc_sli_cmd_available) (phba, pring);
3558
3559 }
3560
James Smart2e0fef82007-06-17 19:56:36 -05003561 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003562 return;
dea31012005-04-17 16:05:31 -05003563}
3564
James Smarte59058c2008-08-24 21:49:00 -04003565/**
James Smart4f774512009-05-22 14:52:35 -04003566 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3567 * @phba: Pointer to HBA context object.
3568 * @pring: Pointer to driver SLI ring object.
3569 * @mask: Host attention register mask for this ring.
3570 *
3571 * This function is called from the worker thread when there is a pending
3572 * ELS response iocb on the driver internal slow-path response iocb worker
3573 * queue. The caller does not hold any lock. The function will remove each
3574 * response iocb from the response worker queue and calls the handle
3575 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3576 **/
3577static void
3578lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3579 struct lpfc_sli_ring *pring, uint32_t mask)
3580{
3581 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003582 struct hbq_dmabuf *dmabuf;
3583 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003584 unsigned long iflag;
3585
James Smart45ed1192009-10-02 15:17:02 -04003586 spin_lock_irqsave(&phba->hbalock, iflag);
3587 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3588 spin_unlock_irqrestore(&phba->hbalock, iflag);
3589 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003590 /* Get the response iocb from the head of work queue */
3591 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003592 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003593 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003594 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003595
3596 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3597 case CQE_CODE_COMPL_WQE:
3598 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3599 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003600 /* Translate ELS WCQE to response IOCBQ */
3601 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3602 irspiocbq);
3603 if (irspiocbq)
3604 lpfc_sli_sp_handle_rspiocb(phba, pring,
3605 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003606 break;
3607 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003608 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003609 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3610 cq_event);
3611 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3612 break;
3613 default:
3614 break;
3615 }
James Smart4f774512009-05-22 14:52:35 -04003616 }
3617}
3618
3619/**
James Smart3621a712009-04-06 18:47:14 -04003620 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003621 * @phba: Pointer to HBA context object.
3622 * @pring: Pointer to driver SLI ring object.
3623 *
3624 * This function aborts all iocbs in the given ring and frees all the iocb
3625 * objects in txq. This function issues an abort iocb for all the iocb commands
3626 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3627 * the return of this function. The caller is not required to hold any locks.
3628 **/
James Smart2e0fef82007-06-17 19:56:36 -05003629void
dea31012005-04-17 16:05:31 -05003630lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3631{
James Smart2534ba72007-04-25 09:52:20 -04003632 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003633 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003634
James Smart92d7f7b2007-06-17 19:56:38 -05003635 if (pring->ringno == LPFC_ELS_RING) {
3636 lpfc_fabric_abort_hba(phba);
3637 }
3638
dea31012005-04-17 16:05:31 -05003639 /* Error everything on txq and txcmplq
3640 * First do the txq.
3641 */
James Smartdb55fba2014-04-04 13:52:02 -04003642 if (phba->sli_rev >= LPFC_SLI_REV4) {
3643 spin_lock_irq(&pring->ring_lock);
3644 list_splice_init(&pring->txq, &completions);
3645 pring->txq_cnt = 0;
3646 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003647
James Smartdb55fba2014-04-04 13:52:02 -04003648 spin_lock_irq(&phba->hbalock);
3649 /* Next issue ABTS for everything on the txcmplq */
3650 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3651 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3652 spin_unlock_irq(&phba->hbalock);
3653 } else {
3654 spin_lock_irq(&phba->hbalock);
3655 list_splice_init(&pring->txq, &completions);
3656 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003657
James Smartdb55fba2014-04-04 13:52:02 -04003658 /* Next issue ABTS for everything on the txcmplq */
3659 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3660 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3661 spin_unlock_irq(&phba->hbalock);
3662 }
James Smart2534ba72007-04-25 09:52:20 -04003663
James Smarta257bf92009-04-06 18:48:10 -04003664 /* Cancel all the IOCBs from the completions list */
3665 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3666 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003667}
3668
James Smarte59058c2008-08-24 21:49:00 -04003669/**
James Smart895427b2017-02-12 13:52:30 -08003670 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3671 * @phba: Pointer to HBA context object.
3672 * @pring: Pointer to driver SLI ring object.
3673 *
3674 * This function aborts all iocbs in the given ring and frees all the iocb
3675 * objects in txq. This function issues an abort iocb for all the iocb commands
3676 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3677 * the return of this function. The caller is not required to hold any locks.
3678 **/
3679void
3680lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3681{
3682 LIST_HEAD(completions);
3683 struct lpfc_iocbq *iocb, *next_iocb;
3684
3685 if (pring->ringno == LPFC_ELS_RING)
3686 lpfc_fabric_abort_hba(phba);
3687
3688 spin_lock_irq(&phba->hbalock);
3689 /* Next issue ABTS for everything on the txcmplq */
3690 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3691 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3692 spin_unlock_irq(&phba->hbalock);
3693}
3694
3695
3696/**
James Smartdb55fba2014-04-04 13:52:02 -04003697 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3698 * @phba: Pointer to HBA context object.
3699 * @pring: Pointer to driver SLI ring object.
3700 *
3701 * This function aborts all iocbs in FCP rings and frees all the iocb
3702 * objects in txq. This function issues an abort iocb for all the iocb commands
3703 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3704 * the return of this function. The caller is not required to hold any locks.
3705 **/
3706void
3707lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3708{
3709 struct lpfc_sli *psli = &phba->sli;
3710 struct lpfc_sli_ring *pring;
3711 uint32_t i;
3712
3713 /* Look on all the FCP Rings for the iotag */
3714 if (phba->sli_rev >= LPFC_SLI_REV4) {
3715 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003716 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003717 lpfc_sli_abort_iocb_ring(phba, pring);
3718 }
3719 } else {
James Smart895427b2017-02-12 13:52:30 -08003720 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003721 lpfc_sli_abort_iocb_ring(phba, pring);
3722 }
3723}
3724
James Smart895427b2017-02-12 13:52:30 -08003725/**
3726 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3727 * @phba: Pointer to HBA context object.
3728 *
3729 * This function aborts all wqes in NVME rings. This function issues an
3730 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3731 * the txcmplq is not guaranteed to complete before the return of this
3732 * function. The caller is not required to hold any locks.
3733 **/
3734void
3735lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3736{
3737 struct lpfc_sli_ring *pring;
3738 uint32_t i;
3739
3740 if (phba->sli_rev < LPFC_SLI_REV4)
3741 return;
3742
3743 /* Abort all IO on each NVME ring. */
3744 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3745 pring = phba->sli4_hba.nvme_wq[i]->pring;
3746 lpfc_sli_abort_wqe_ring(phba, pring);
3747 }
3748}
3749
James Smartdb55fba2014-04-04 13:52:02 -04003750
3751/**
James Smart3621a712009-04-06 18:47:14 -04003752 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003753 * @phba: Pointer to HBA context object.
3754 *
3755 * This function flushes all iocbs in the fcp ring and frees all the iocb
3756 * objects in txq and txcmplq. This function will not issue abort iocbs
3757 * for all the iocb commands in txcmplq, they will just be returned with
3758 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3759 * slot has been permanently disabled.
3760 **/
3761void
3762lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3763{
3764 LIST_HEAD(txq);
3765 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003766 struct lpfc_sli *psli = &phba->sli;
3767 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003768 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003769
3770 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003771 /* Indicate the I/O queues are flushed */
3772 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003773 spin_unlock_irq(&phba->hbalock);
3774
James Smartdb55fba2014-04-04 13:52:02 -04003775 /* Look on all the FCP Rings for the iotag */
3776 if (phba->sli_rev >= LPFC_SLI_REV4) {
3777 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003778 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003779
James Smartdb55fba2014-04-04 13:52:02 -04003780 spin_lock_irq(&pring->ring_lock);
3781 /* Retrieve everything on txq */
3782 list_splice_init(&pring->txq, &txq);
3783 /* Retrieve everything on the txcmplq */
3784 list_splice_init(&pring->txcmplq, &txcmplq);
3785 pring->txq_cnt = 0;
3786 pring->txcmplq_cnt = 0;
3787 spin_unlock_irq(&pring->ring_lock);
3788
3789 /* Flush the txq */
3790 lpfc_sli_cancel_iocbs(phba, &txq,
3791 IOSTAT_LOCAL_REJECT,
3792 IOERR_SLI_DOWN);
3793 /* Flush the txcmpq */
3794 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3795 IOSTAT_LOCAL_REJECT,
3796 IOERR_SLI_DOWN);
3797 }
3798 } else {
James Smart895427b2017-02-12 13:52:30 -08003799 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003800
3801 spin_lock_irq(&phba->hbalock);
3802 /* Retrieve everything on txq */
3803 list_splice_init(&pring->txq, &txq);
3804 /* Retrieve everything on the txcmplq */
3805 list_splice_init(&pring->txcmplq, &txcmplq);
3806 pring->txq_cnt = 0;
3807 pring->txcmplq_cnt = 0;
3808 spin_unlock_irq(&phba->hbalock);
3809
3810 /* Flush the txq */
3811 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3812 IOERR_SLI_DOWN);
3813 /* Flush the txcmpq */
3814 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3815 IOERR_SLI_DOWN);
3816 }
James Smarta8e497d2008-08-24 21:50:11 -04003817}
3818
3819/**
James Smart895427b2017-02-12 13:52:30 -08003820 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3821 * @phba: Pointer to HBA context object.
3822 *
3823 * This function flushes all wqes in the nvme rings and frees all resources
3824 * in the txcmplq. This function does not issue abort wqes for the IO
3825 * commands in txcmplq, they will just be returned with
3826 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3827 * slot has been permanently disabled.
3828 **/
3829void
3830lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3831{
3832 LIST_HEAD(txcmplq);
3833 struct lpfc_sli_ring *pring;
3834 uint32_t i;
3835
3836 if (phba->sli_rev < LPFC_SLI_REV4)
3837 return;
3838
3839 /* Hint to other driver operations that a flush is in progress. */
3840 spin_lock_irq(&phba->hbalock);
3841 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3842 spin_unlock_irq(&phba->hbalock);
3843
3844 /* Cycle through all NVME rings and complete each IO with
3845 * a local driver reason code. This is a flush so no
3846 * abort exchange to FW.
3847 */
3848 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3849 pring = phba->sli4_hba.nvme_wq[i]->pring;
3850
3851 /* Retrieve everything on the txcmplq */
3852 spin_lock_irq(&pring->ring_lock);
3853 list_splice_init(&pring->txcmplq, &txcmplq);
3854 pring->txcmplq_cnt = 0;
3855 spin_unlock_irq(&pring->ring_lock);
3856
3857 /* Flush the txcmpq &&&PAE */
3858 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3859 IOSTAT_LOCAL_REJECT,
3860 IOERR_SLI_DOWN);
3861 }
3862}
3863
3864/**
James Smart3772a992009-05-22 14:50:54 -04003865 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003866 * @phba: Pointer to HBA context object.
3867 * @mask: Bit mask to be checked.
3868 *
3869 * This function reads the host status register and compares
3870 * with the provided bit mask to check if HBA completed
3871 * the restart. This function will wait in a loop for the
3872 * HBA to complete restart. If the HBA does not restart within
3873 * 15 iterations, the function will reset the HBA again. The
3874 * function returns 1 when HBA fail to restart otherwise returns
3875 * zero.
3876 **/
James Smart3772a992009-05-22 14:50:54 -04003877static int
3878lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003879{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003880 uint32_t status;
3881 int i = 0;
3882 int retval = 0;
dea31012005-04-17 16:05:31 -05003883
Jamie Wellnitz41415862006-02-28 19:25:27 -05003884 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003885 if (lpfc_readl(phba->HSregaddr, &status))
3886 return 1;
dea31012005-04-17 16:05:31 -05003887
Jamie Wellnitz41415862006-02-28 19:25:27 -05003888 /*
3889 * Check status register every 100ms for 5 retries, then every
3890 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3891 * every 2.5 sec for 4.
3892 * Break our of the loop if errors occurred during init.
3893 */
3894 while (((status & mask) != mask) &&
3895 !(status & HS_FFERM) &&
3896 i++ < 20) {
dea31012005-04-17 16:05:31 -05003897
Jamie Wellnitz41415862006-02-28 19:25:27 -05003898 if (i <= 5)
3899 msleep(10);
3900 else if (i <= 10)
3901 msleep(500);
3902 else
3903 msleep(2500);
dea31012005-04-17 16:05:31 -05003904
Jamie Wellnitz41415862006-02-28 19:25:27 -05003905 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003906 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003907 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003908 lpfc_sli_brdrestart(phba);
3909 }
3910 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003911 if (lpfc_readl(phba->HSregaddr, &status)) {
3912 retval = 1;
3913 break;
3914 }
dea31012005-04-17 16:05:31 -05003915 }
dea31012005-04-17 16:05:31 -05003916
Jamie Wellnitz41415862006-02-28 19:25:27 -05003917 /* Check to see if any errors occurred during init */
3918 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003919 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3920 "2751 Adapter failed to restart, "
3921 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3922 status,
3923 readl(phba->MBslimaddr + 0xa8),
3924 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003925 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003926 retval = 1;
3927 }
dea31012005-04-17 16:05:31 -05003928
Jamie Wellnitz41415862006-02-28 19:25:27 -05003929 return retval;
dea31012005-04-17 16:05:31 -05003930}
3931
James Smartda0436e2009-05-22 14:51:39 -04003932/**
3933 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3934 * @phba: Pointer to HBA context object.
3935 * @mask: Bit mask to be checked.
3936 *
3937 * This function checks the host status register to check if HBA is
3938 * ready. This function will wait in a loop for the HBA to be ready
3939 * If the HBA is not ready , the function will will reset the HBA PCI
3940 * function again. The function returns 1 when HBA fail to be ready
3941 * otherwise returns zero.
3942 **/
3943static int
3944lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3945{
3946 uint32_t status;
3947 int retval = 0;
3948
3949 /* Read the HBA Host Status Register */
3950 status = lpfc_sli4_post_status_check(phba);
3951
3952 if (status) {
3953 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3954 lpfc_sli_brdrestart(phba);
3955 status = lpfc_sli4_post_status_check(phba);
3956 }
3957
3958 /* Check to see if any errors occurred during init */
3959 if (status) {
3960 phba->link_state = LPFC_HBA_ERROR;
3961 retval = 1;
3962 } else
3963 phba->sli4_hba.intr_enable = 0;
3964
3965 return retval;
3966}
3967
3968/**
3969 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3970 * @phba: Pointer to HBA context object.
3971 * @mask: Bit mask to be checked.
3972 *
3973 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3974 * from the API jump table function pointer from the lpfc_hba struct.
3975 **/
3976int
3977lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3978{
3979 return phba->lpfc_sli_brdready(phba, mask);
3980}
3981
James Smart92908312006-03-07 15:04:13 -05003982#define BARRIER_TEST_PATTERN (0xdeadbeef)
3983
James Smarte59058c2008-08-24 21:49:00 -04003984/**
James Smart3621a712009-04-06 18:47:14 -04003985 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003986 * @phba: Pointer to HBA context object.
3987 *
James Smart1b511972011-12-13 13:23:09 -05003988 * This function is called before resetting an HBA. This function is called
3989 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003990 **/
James Smart2e0fef82007-06-17 19:56:36 -05003991void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003992{
James Smart65a29c12006-07-06 15:50:50 -04003993 uint32_t __iomem *resp_buf;
3994 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003995 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003996 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003997 int i;
3998 uint8_t hdrtype;
3999
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004000 lockdep_assert_held(&phba->hbalock);
4001
James Smart92908312006-03-07 15:04:13 -05004002 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4003 if (hdrtype != 0x80 ||
4004 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4005 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4006 return;
4007
4008 /*
4009 * Tell the other part of the chip to suspend temporarily all
4010 * its DMA activity.
4011 */
James Smart65a29c12006-07-06 15:50:50 -04004012 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004013
4014 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004015 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4016 return;
James Smart92908312006-03-07 15:04:13 -05004017 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4018 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004019 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004020
James Smart9940b972011-03-11 16:06:12 -05004021 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4022 return;
4023 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004024 /* Clear Chip error bit */
4025 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004026 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004027 }
4028
4029 mbox = 0;
4030 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4031 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4032
4033 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004034 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004035 writel(mbox, mbox_buf);
4036
James Smart9940b972011-03-11 16:06:12 -05004037 for (i = 0; i < 50; i++) {
4038 if (lpfc_readl((resp_buf + 1), &resp_data))
4039 return;
4040 if (resp_data != ~(BARRIER_TEST_PATTERN))
4041 mdelay(1);
4042 else
4043 break;
4044 }
4045 resp_data = 0;
4046 if (lpfc_readl((resp_buf + 1), &resp_data))
4047 return;
4048 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004049 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004050 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004051 goto restore_hc;
4052 else
4053 goto clear_errat;
4054 }
4055
4056 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004057 resp_data = 0;
4058 for (i = 0; i < 500; i++) {
4059 if (lpfc_readl(resp_buf, &resp_data))
4060 return;
4061 if (resp_data != mbox)
4062 mdelay(1);
4063 else
4064 break;
4065 }
James Smart92908312006-03-07 15:04:13 -05004066
4067clear_errat:
4068
James Smart9940b972011-03-11 16:06:12 -05004069 while (++i < 500) {
4070 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4071 return;
4072 if (!(ha_copy & HA_ERATT))
4073 mdelay(1);
4074 else
4075 break;
4076 }
James Smart92908312006-03-07 15:04:13 -05004077
4078 if (readl(phba->HAregaddr) & HA_ERATT) {
4079 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004080 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004081 }
4082
4083restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004084 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004085 writel(hc_copy, phba->HCregaddr);
4086 readl(phba->HCregaddr); /* flush */
4087}
4088
James Smarte59058c2008-08-24 21:49:00 -04004089/**
James Smart3621a712009-04-06 18:47:14 -04004090 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004091 * @phba: Pointer to HBA context object.
4092 *
4093 * This function issues a kill_board mailbox command and waits for
4094 * the error attention interrupt. This function is called for stopping
4095 * the firmware processing. The caller is not required to hold any
4096 * locks. This function calls lpfc_hba_down_post function to free
4097 * any pending commands after the kill. The function will return 1 when it
4098 * fails to kill the board else will return 0.
4099 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004100int
James Smart2e0fef82007-06-17 19:56:36 -05004101lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004102{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004103 struct lpfc_sli *psli;
4104 LPFC_MBOXQ_t *pmb;
4105 uint32_t status;
4106 uint32_t ha_copy;
4107 int retval;
4108 int i = 0;
4109
4110 psli = &phba->sli;
4111
4112 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004113 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004114 "0329 Kill HBA Data: x%x x%x\n",
4115 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004116
James Smart98c9ea52007-10-27 13:37:33 -04004117 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4118 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004119 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004120
4121 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004122 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004123 if (lpfc_readl(phba->HCregaddr, &status)) {
4124 spin_unlock_irq(&phba->hbalock);
4125 mempool_free(pmb, phba->mbox_mem_pool);
4126 return 1;
4127 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004128 status &= ~HC_ERINT_ENA;
4129 writel(status, phba->HCregaddr);
4130 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004131 phba->link_flag |= LS_IGNORE_ERATT;
4132 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004133
4134 lpfc_kill_board(phba, pmb);
4135 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4136 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4137
4138 if (retval != MBX_SUCCESS) {
4139 if (retval != MBX_BUSY)
4140 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004141 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4142 "2752 KILL_BOARD command failed retval %d\n",
4143 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004144 spin_lock_irq(&phba->hbalock);
4145 phba->link_flag &= ~LS_IGNORE_ERATT;
4146 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004147 return 1;
4148 }
4149
James Smartf4b4c682009-05-22 14:53:12 -04004150 spin_lock_irq(&phba->hbalock);
4151 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4152 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004153
Jamie Wellnitz41415862006-02-28 19:25:27 -05004154 mempool_free(pmb, phba->mbox_mem_pool);
4155
4156 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4157 * attention every 100ms for 3 seconds. If we don't get ERATT after
4158 * 3 seconds we still set HBA_ERROR state because the status of the
4159 * board is now undefined.
4160 */
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 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4164 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004165 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4166 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004167 }
4168
4169 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004170 if (ha_copy & HA_ERATT) {
4171 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004172 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004173 }
James Smart2e0fef82007-06-17 19:56:36 -05004174 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004175 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004176 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004177 phba->link_flag &= ~LS_IGNORE_ERATT;
4178 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004179
Jamie Wellnitz41415862006-02-28 19:25:27 -05004180 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004181 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004182
James Smart2e0fef82007-06-17 19:56:36 -05004183 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004184}
4185
James Smarte59058c2008-08-24 21:49:00 -04004186/**
James Smart3772a992009-05-22 14:50:54 -04004187 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004188 * @phba: Pointer to HBA context object.
4189 *
4190 * This function resets the HBA by writing HC_INITFF to the control
4191 * register. After the HBA resets, this function resets all the iocb ring
4192 * indices. This function disables PCI layer parity checking during
4193 * the reset.
4194 * This function returns 0 always.
4195 * The caller is not required to hold any locks.
4196 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004197int
James Smart2e0fef82007-06-17 19:56:36 -05004198lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004199{
4200 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004201 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004202 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004203 int i;
dea31012005-04-17 16:05:31 -05004204
Jamie Wellnitz41415862006-02-28 19:25:27 -05004205 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004206
Jamie Wellnitz41415862006-02-28 19:25:27 -05004207 /* Reset HBA */
4208 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004209 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004210 (phba->pport) ? phba->pport->port_state : 0,
4211 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004212
4213 /* perform board reset */
4214 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004215 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004216 if (phba->pport) {
4217 phba->pport->fc_myDID = 0;
4218 phba->pport->fc_prevDID = 0;
4219 }
dea31012005-04-17 16:05:31 -05004220
Jamie Wellnitz41415862006-02-28 19:25:27 -05004221 /* Turn off parity checking and serr during the physical reset */
4222 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4223 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4224 (cfg_value &
4225 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4226
James Smart3772a992009-05-22 14:50:54 -04004227 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4228
Jamie Wellnitz41415862006-02-28 19:25:27 -05004229 /* Now toggle INITFF bit in the Host Control Register */
4230 writel(HC_INITFF, phba->HCregaddr);
4231 mdelay(1);
4232 readl(phba->HCregaddr); /* flush */
4233 writel(0, phba->HCregaddr);
4234 readl(phba->HCregaddr); /* flush */
4235
4236 /* Restore PCI cmd register */
4237 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004238
4239 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004240 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004241 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004242 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004243 pring->sli.sli3.rspidx = 0;
4244 pring->sli.sli3.next_cmdidx = 0;
4245 pring->sli.sli3.local_getidx = 0;
4246 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004247 pring->missbufcnt = 0;
4248 }
dea31012005-04-17 16:05:31 -05004249
James Smart2e0fef82007-06-17 19:56:36 -05004250 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004251 return 0;
4252}
4253
James Smarte59058c2008-08-24 21:49:00 -04004254/**
James Smartda0436e2009-05-22 14:51:39 -04004255 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4256 * @phba: Pointer to HBA context object.
4257 *
4258 * This function resets a SLI4 HBA. This function disables PCI layer parity
4259 * checking during resets the device. The caller is not required to hold
4260 * any locks.
4261 *
4262 * This function returns 0 always.
4263 **/
4264int
4265lpfc_sli4_brdreset(struct lpfc_hba *phba)
4266{
4267 struct lpfc_sli *psli = &phba->sli;
4268 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004269 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004270
4271 /* Reset HBA */
4272 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004273 "0295 Reset HBA Data: x%x x%x x%x\n",
4274 phba->pport->port_state, psli->sli_flag,
4275 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004276
4277 /* perform board reset */
4278 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004279 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004280 phba->pport->fc_myDID = 0;
4281 phba->pport->fc_prevDID = 0;
4282
James Smartda0436e2009-05-22 14:51:39 -04004283 spin_lock_irq(&phba->hbalock);
4284 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4285 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004286 spin_unlock_irq(&phba->hbalock);
4287
James Smart02936352014-04-04 13:52:12 -04004288 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4289 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4290 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4291 return rc;
4292 }
4293
James Smartda0436e2009-05-22 14:51:39 -04004294 /* Now physically reset the device */
4295 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4296 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004297
4298 /* Turn off parity checking and serr during the physical reset */
4299 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4300 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4301 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4302
James Smart88318812012-09-29 11:29:29 -04004303 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004304 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004305 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004306
James Smartbe858b62010-12-15 17:57:20 -05004307 /* Restore PCI cmd register */
4308 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4309
James Smart27b01b82012-05-09 21:19:44 -04004310 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004311}
4312
4313/**
4314 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004315 * @phba: Pointer to HBA context object.
4316 *
4317 * This function is called in the SLI initialization code path to
4318 * restart the HBA. The caller is not required to hold any lock.
4319 * This function writes MBX_RESTART mailbox command to the SLIM and
4320 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4321 * function to free any pending commands. The function enables
4322 * POST only during the first initialization. The function returns zero.
4323 * The function does not guarantee completion of MBX_RESTART mailbox
4324 * command before the return of this function.
4325 **/
James Smartda0436e2009-05-22 14:51:39 -04004326static int
4327lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004328{
4329 MAILBOX_t *mb;
4330 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004331 volatile uint32_t word0;
4332 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004333 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004334
James Smart2e0fef82007-06-17 19:56:36 -05004335 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004336
James Smart0d878412009-10-02 15:16:56 -04004337 /* Take PCIe device Advanced Error Reporting (AER) state */
4338 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4339
Jamie Wellnitz41415862006-02-28 19:25:27 -05004340 psli = &phba->sli;
4341
4342 /* Restart HBA */
4343 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004344 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004345 (phba->pport) ? phba->pport->port_state : 0,
4346 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004347
4348 word0 = 0;
4349 mb = (MAILBOX_t *) &word0;
4350 mb->mbxCommand = MBX_RESTART;
4351 mb->mbxHc = 1;
4352
James Smart92908312006-03-07 15:04:13 -05004353 lpfc_reset_barrier(phba);
4354
Jamie Wellnitz41415862006-02-28 19:25:27 -05004355 to_slim = phba->MBslimaddr;
4356 writel(*(uint32_t *) mb, to_slim);
4357 readl(to_slim); /* flush */
4358
4359 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004360 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004361 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004362 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004363 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004364 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004365 writel(*(uint32_t *) mb, to_slim);
4366 readl(to_slim); /* flush */
4367
4368 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004369 if (phba->pport)
4370 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004371 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004372 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004373 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004374
James Smart64ba8812006-08-02 15:24:34 -04004375 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4376 psli->stats_start = get_seconds();
4377
James Smarteaf15d52008-12-04 22:39:29 -05004378 /* Give the INITFF and Post time to settle. */
4379 mdelay(100);
dea31012005-04-17 16:05:31 -05004380
James Smart0d878412009-10-02 15:16:56 -04004381 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4382 if (hba_aer_enabled)
4383 pci_disable_pcie_error_reporting(phba->pcidev);
4384
Jamie Wellnitz41415862006-02-28 19:25:27 -05004385 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004386
4387 return 0;
4388}
4389
James Smarte59058c2008-08-24 21:49:00 -04004390/**
James Smartda0436e2009-05-22 14:51:39 -04004391 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4392 * @phba: Pointer to HBA context object.
4393 *
4394 * This function is called in the SLI initialization code path to restart
4395 * a SLI4 HBA. The caller is not required to hold any lock.
4396 * At the end of the function, it calls lpfc_hba_down_post function to
4397 * free any pending commands.
4398 **/
4399static int
4400lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4401{
4402 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004403 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004404 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004405
4406 /* Restart HBA */
4407 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4408 "0296 Restart HBA Data: x%x x%x\n",
4409 phba->pport->port_state, psli->sli_flag);
4410
James Smart75baf692010-06-08 18:31:21 -04004411 /* Take PCIe device Advanced Error Reporting (AER) state */
4412 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4413
James Smart27b01b82012-05-09 21:19:44 -04004414 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004415
4416 spin_lock_irq(&phba->hbalock);
4417 phba->pport->stopped = 0;
4418 phba->link_state = LPFC_INIT_START;
4419 phba->hba_flag = 0;
4420 spin_unlock_irq(&phba->hbalock);
4421
4422 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4423 psli->stats_start = get_seconds();
4424
James Smart75baf692010-06-08 18:31:21 -04004425 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4426 if (hba_aer_enabled)
4427 pci_disable_pcie_error_reporting(phba->pcidev);
4428
James Smartda0436e2009-05-22 14:51:39 -04004429 lpfc_hba_down_post(phba);
4430
James Smart27b01b82012-05-09 21:19:44 -04004431 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004432}
4433
4434/**
4435 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4436 * @phba: Pointer to HBA context object.
4437 *
4438 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4439 * API jump table function pointer from the lpfc_hba struct.
4440**/
4441int
4442lpfc_sli_brdrestart(struct lpfc_hba *phba)
4443{
4444 return phba->lpfc_sli_brdrestart(phba);
4445}
4446
4447/**
James Smart3621a712009-04-06 18:47:14 -04004448 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004449 * @phba: Pointer to HBA context object.
4450 *
4451 * This function is called after a HBA restart to wait for successful
4452 * restart of the HBA. Successful restart of the HBA is indicated by
4453 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4454 * iteration, the function will restart the HBA again. The function returns
4455 * zero if HBA successfully restarted else returns negative error code.
4456 **/
James Smart4492b732017-04-27 15:08:26 -07004457int
dea31012005-04-17 16:05:31 -05004458lpfc_sli_chipset_init(struct lpfc_hba *phba)
4459{
4460 uint32_t status, i = 0;
4461
4462 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004463 if (lpfc_readl(phba->HSregaddr, &status))
4464 return -EIO;
dea31012005-04-17 16:05:31 -05004465
4466 /* Check status register to see what current state is */
4467 i = 0;
4468 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4469
James Smartdcf2a4e2010-09-29 11:18:53 -04004470 /* Check every 10ms for 10 retries, then every 100ms for 90
4471 * retries, then every 1 sec for 50 retires for a total of
4472 * ~60 seconds before reset the board again and check every
4473 * 1 sec for 50 retries. The up to 60 seconds before the
4474 * board ready is required by the Falcon FIPS zeroization
4475 * complete, and any reset the board in between shall cause
4476 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004477 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004478 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004479 /* Adapter failed to init, timeout, status reg
4480 <status> */
James Smarted957682007-06-17 19:56:37 -05004481 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004482 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004483 "timeout, status reg x%x, "
4484 "FW Data: A8 x%x AC x%x\n", status,
4485 readl(phba->MBslimaddr + 0xa8),
4486 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004487 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004488 return -ETIMEDOUT;
4489 }
4490
4491 /* Check to see if any errors occurred during init */
4492 if (status & HS_FFERM) {
4493 /* ERROR: During chipset initialization */
4494 /* Adapter failed to init, chipset, status reg
4495 <status> */
James Smarted957682007-06-17 19:56:37 -05004496 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004497 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004498 "chipset, status reg x%x, "
4499 "FW Data: A8 x%x AC x%x\n", status,
4500 readl(phba->MBslimaddr + 0xa8),
4501 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004502 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004503 return -EIO;
4504 }
4505
James Smartdcf2a4e2010-09-29 11:18:53 -04004506 if (i <= 10)
dea31012005-04-17 16:05:31 -05004507 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004508 else if (i <= 100)
4509 msleep(100);
4510 else
4511 msleep(1000);
dea31012005-04-17 16:05:31 -05004512
James Smartdcf2a4e2010-09-29 11:18:53 -04004513 if (i == 150) {
4514 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004515 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004516 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004517 }
4518 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004519 if (lpfc_readl(phba->HSregaddr, &status))
4520 return -EIO;
dea31012005-04-17 16:05:31 -05004521 }
4522
4523 /* Check to see if any errors occurred during init */
4524 if (status & HS_FFERM) {
4525 /* ERROR: During chipset initialization */
4526 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004528 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004529 "status reg x%x, "
4530 "FW Data: A8 x%x AC x%x\n", status,
4531 readl(phba->MBslimaddr + 0xa8),
4532 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004533 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004534 return -EIO;
4535 }
4536
4537 /* Clear all interrupt enable conditions */
4538 writel(0, phba->HCregaddr);
4539 readl(phba->HCregaddr); /* flush */
4540
4541 /* setup host attn register */
4542 writel(0xffffffff, phba->HAregaddr);
4543 readl(phba->HAregaddr); /* flush */
4544 return 0;
4545}
4546
James Smarte59058c2008-08-24 21:49:00 -04004547/**
James Smart3621a712009-04-06 18:47:14 -04004548 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004549 *
4550 * This function calculates and returns the number of HBQs required to be
4551 * configured.
4552 **/
James Smart78b2d852007-08-02 11:10:21 -04004553int
James Smarted957682007-06-17 19:56:37 -05004554lpfc_sli_hbq_count(void)
4555{
James Smart92d7f7b2007-06-17 19:56:38 -05004556 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004557}
4558
James Smarte59058c2008-08-24 21:49:00 -04004559/**
James Smart3621a712009-04-06 18:47:14 -04004560 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004561 *
4562 * This function adds the number of hbq entries in every HBQ to get
4563 * the total number of hbq entries required for the HBA and returns
4564 * the total count.
4565 **/
James Smarted957682007-06-17 19:56:37 -05004566static int
4567lpfc_sli_hbq_entry_count(void)
4568{
4569 int hbq_count = lpfc_sli_hbq_count();
4570 int count = 0;
4571 int i;
4572
4573 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004574 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004575 return count;
4576}
4577
James Smarte59058c2008-08-24 21:49:00 -04004578/**
James Smart3621a712009-04-06 18:47:14 -04004579 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004580 *
4581 * This function calculates amount of memory required for all hbq entries
4582 * to be configured and returns the total memory required.
4583 **/
dea31012005-04-17 16:05:31 -05004584int
James Smarted957682007-06-17 19:56:37 -05004585lpfc_sli_hbq_size(void)
4586{
4587 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4588}
4589
James Smarte59058c2008-08-24 21:49:00 -04004590/**
James Smart3621a712009-04-06 18:47:14 -04004591 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004592 * @phba: Pointer to HBA context object.
4593 *
4594 * This function is called during the SLI initialization to configure
4595 * all the HBQs and post buffers to the HBQ. The caller is not
4596 * required to hold any locks. This function will return zero if successful
4597 * else it will return negative error code.
4598 **/
James Smarted957682007-06-17 19:56:37 -05004599static int
4600lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4601{
4602 int hbq_count = lpfc_sli_hbq_count();
4603 LPFC_MBOXQ_t *pmb;
4604 MAILBOX_t *pmbox;
4605 uint32_t hbqno;
4606 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004607
James Smart92d7f7b2007-06-17 19:56:38 -05004608 /* Get a Mailbox buffer to setup mailbox
4609 * commands for HBA initialization
4610 */
James Smarted957682007-06-17 19:56:37 -05004611 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4612
4613 if (!pmb)
4614 return -ENOMEM;
4615
James Smart04c68492009-05-22 14:52:52 -04004616 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004617
4618 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4619 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004620 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004621
4622 hbq_entry_index = 0;
4623 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4624 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4625 phba->hbqs[hbqno].hbqPutIdx = 0;
4626 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4627 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004628 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004629 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4630 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004631 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4632
4633 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4634 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4635 mbxStatus <status>, ring <num> */
4636
4637 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004638 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004639 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004640 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004641 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004642 pmbox->mbxStatus, hbqno);
4643
4644 phba->link_state = LPFC_HBA_ERROR;
4645 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004646 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004647 }
4648 }
4649 phba->hbq_count = hbq_count;
4650
James Smarted957682007-06-17 19:56:37 -05004651 mempool_free(pmb, phba->mbox_mem_pool);
4652
James Smart92d7f7b2007-06-17 19:56:38 -05004653 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004654 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4655 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004656 return 0;
4657}
4658
James Smarte59058c2008-08-24 21:49:00 -04004659/**
James Smart4f774512009-05-22 14:52:35 -04004660 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4661 * @phba: Pointer to HBA context object.
4662 *
4663 * This function is called during the SLI initialization to configure
4664 * all the HBQs and post buffers to the HBQ. The caller is not
4665 * required to hold any locks. This function will return zero if successful
4666 * else it will return negative error code.
4667 **/
4668static int
4669lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4670{
4671 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004672 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4673 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004674 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004675 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004676 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004677 return 0;
4678}
4679
4680/**
James Smart3621a712009-04-06 18:47:14 -04004681 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004682 * @phba: Pointer to HBA context object.
4683 * @sli_mode: sli mode - 2/3
4684 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004685 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004686 * to issue config_port mailbox command. This function restarts the
4687 * HBA firmware and issues a config_port mailbox command to configure
4688 * the SLI interface in the sli mode specified by sli_mode
4689 * variable. The caller is not required to hold any locks.
4690 * The function returns 0 if successful, else returns negative error
4691 * code.
4692 **/
James Smart93996272008-08-24 21:50:30 -04004693int
4694lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004695{
4696 LPFC_MBOXQ_t *pmb;
4697 uint32_t resetcount = 0, rc = 0, done = 0;
4698
4699 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4700 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004701 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004702 return -ENOMEM;
4703 }
4704
James Smarted957682007-06-17 19:56:37 -05004705 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004706 while (resetcount < 2 && !done) {
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);
James Smart92d7f7b2007-06-17 19:56:38 -05004710 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004711 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004712 rc = lpfc_sli_chipset_init(phba);
4713 if (rc)
4714 break;
4715
James Smart2e0fef82007-06-17 19:56:36 -05004716 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004717 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004718 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004719 resetcount++;
4720
James Smarted957682007-06-17 19:56:37 -05004721 /* Call pre CONFIG_PORT mailbox command initialization. A
4722 * value of 0 means the call was successful. Any other
4723 * nonzero value is a failure, but if ERESTART is returned,
4724 * the driver may reset the HBA and try again.
4725 */
dea31012005-04-17 16:05:31 -05004726 rc = lpfc_config_port_prep(phba);
4727 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004728 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004729 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004730 } else if (rc)
dea31012005-04-17 16:05:31 -05004731 break;
James Smart6d368e52011-05-24 11:44:12 -04004732
James Smart2e0fef82007-06-17 19:56:36 -05004733 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004734 lpfc_config_port(phba, pmb);
4735 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004736 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4737 LPFC_SLI3_HBQ_ENABLED |
4738 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004739 LPFC_SLI3_BG_ENABLED |
4740 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004741 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004743 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004744 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004745 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004746 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004747 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004748 spin_unlock_irq(&phba->hbalock);
4749 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004750 } else {
4751 /* Allow asynchronous mailbox command to go through */
4752 spin_lock_irq(&phba->hbalock);
4753 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4754 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004755 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004756
4757 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4758 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4759 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4760 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004761 }
dea31012005-04-17 16:05:31 -05004762 }
James Smarted957682007-06-17 19:56:37 -05004763 if (!done) {
4764 rc = -EINVAL;
4765 goto do_prep_failed;
4766 }
James Smart04c68492009-05-22 14:52:52 -04004767 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4768 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004769 rc = -ENXIO;
4770 goto do_prep_failed;
4771 }
James Smart04c68492009-05-22 14:52:52 -04004772 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004773 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004774 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4775 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4776 phba->max_vpi : phba->max_vports;
4777
James Smart34b02dc2008-08-24 21:49:55 -04004778 } else
4779 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004780 phba->fips_level = 0;
4781 phba->fips_spec_rev = 0;
4782 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004783 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004784 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4785 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4786 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4787 "2850 Security Crypto Active. FIPS x%d "
4788 "(Spec Rev: x%d)",
4789 phba->fips_level, phba->fips_spec_rev);
4790 }
4791 if (pmb->u.mb.un.varCfgPort.sec_err) {
4792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4793 "2856 Config Port Security Crypto "
4794 "Error: x%x ",
4795 pmb->u.mb.un.varCfgPort.sec_err);
4796 }
James Smart04c68492009-05-22 14:52:52 -04004797 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004798 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004799 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004800 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004801
4802 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4803 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004804
4805 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004806 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004807 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4808 else
4809 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4810 "0443 Adapter did not grant "
4811 "BlockGuard\n");
4812 }
James Smart34b02dc2008-08-24 21:49:55 -04004813 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004814 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004815 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004816 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004817 }
James Smart92d7f7b2007-06-17 19:56:38 -05004818do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004819 mempool_free(pmb, phba->mbox_mem_pool);
4820 return rc;
4821}
4822
James Smarte59058c2008-08-24 21:49:00 -04004823
4824/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08004825 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04004826 * @phba: Pointer to HBA context object.
4827 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004828 * This function is the main SLI initialization function. This function
4829 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04004830 * error attention handler code. Caller is not required to hold any
4831 * locks. This function issues config_port mailbox command to configure
4832 * the SLI, setup iocb rings and HBQ rings. In the end the function
4833 * calls the config_port_post function to issue init_link mailbox
4834 * command and to start the discovery. The function will return zero
4835 * if successful, else it will return negative error code.
4836 **/
James Smarted957682007-06-17 19:56:37 -05004837int
4838lpfc_sli_hba_setup(struct lpfc_hba *phba)
4839{
4840 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004841 int mode = 3, i;
4842 int longs;
James Smarted957682007-06-17 19:56:37 -05004843
James Smart12247e82016-07-06 12:36:09 -07004844 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004845 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004846 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004848 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004849 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004850 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004851 break;
4852 }
James Smarted957682007-06-17 19:56:37 -05004853 mode = 2;
4854 break;
4855 case 0:
4856 case 3:
4857 break;
4858 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004860 "1819 Unrecognized sli_mode parameter: %d.\n",
4861 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004862
4863 break;
4864 }
James Smartb5c53952016-03-31 14:12:30 -07004865 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004866
James Smart93996272008-08-24 21:50:30 -04004867 rc = lpfc_sli_config_port(phba, mode);
4868
James Smart12247e82016-07-06 12:36:09 -07004869 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004870 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004871 "1820 Unable to select SLI-3. "
4872 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004873 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004874 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004875 else if (rc && mode == 2)
4876 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004877 if (rc)
dea31012005-04-17 16:05:31 -05004878 goto lpfc_sli_hba_setup_error;
4879
James Smart0d878412009-10-02 15:16:56 -04004880 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4881 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4882 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4883 if (!rc) {
4884 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4885 "2709 This device supports "
4886 "Advanced Error Reporting (AER)\n");
4887 spin_lock_irq(&phba->hbalock);
4888 phba->hba_flag |= HBA_AER_ENABLED;
4889 spin_unlock_irq(&phba->hbalock);
4890 } else {
4891 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4892 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004893 "Advanced Error Reporting (AER): %d\n",
4894 rc);
James Smart0d878412009-10-02 15:16:56 -04004895 phba->cfg_aer_support = 0;
4896 }
4897 }
4898
James Smarted957682007-06-17 19:56:37 -05004899 if (phba->sli_rev == 3) {
4900 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4901 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004902 } else {
4903 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4904 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004905 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004906 }
4907
4908 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004909 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4910 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004911 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004912
4913 if (rc)
4914 goto lpfc_sli_hba_setup_error;
4915
James Smart6d368e52011-05-24 11:44:12 -04004916 /* Initialize VPIs. */
4917 if (phba->sli_rev == LPFC_SLI_REV3) {
4918 /*
4919 * The VPI bitmask and physical ID array are allocated
4920 * and initialized once only - at driver load. A port
4921 * reset doesn't need to reinitialize this memory.
4922 */
4923 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4924 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4925 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4926 GFP_KERNEL);
4927 if (!phba->vpi_bmask) {
4928 rc = -ENOMEM;
4929 goto lpfc_sli_hba_setup_error;
4930 }
4931
4932 phba->vpi_ids = kzalloc(
4933 (phba->max_vpi+1) * sizeof(uint16_t),
4934 GFP_KERNEL);
4935 if (!phba->vpi_ids) {
4936 kfree(phba->vpi_bmask);
4937 rc = -ENOMEM;
4938 goto lpfc_sli_hba_setup_error;
4939 }
4940 for (i = 0; i < phba->max_vpi; i++)
4941 phba->vpi_ids[i] = i;
4942 }
4943 }
4944
James Smart93996272008-08-24 21:50:30 -04004945 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004946 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4947 rc = lpfc_sli_hbq_setup(phba);
4948 if (rc)
4949 goto lpfc_sli_hba_setup_error;
4950 }
James Smart04c68492009-05-22 14:52:52 -04004951 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004952 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004953 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004954
4955 rc = lpfc_config_port_post(phba);
4956 if (rc)
4957 goto lpfc_sli_hba_setup_error;
4958
James Smarted957682007-06-17 19:56:37 -05004959 return rc;
4960
James Smart92d7f7b2007-06-17 19:56:38 -05004961lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004962 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004964 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004965 return rc;
4966}
4967
James Smartda0436e2009-05-22 14:51:39 -04004968/**
4969 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4970 * @phba: Pointer to HBA context object.
4971 * @mboxq: mailbox pointer.
4972 * This function issue a dump mailbox command to read config region
4973 * 23 and parse the records in the region and populate driver
4974 * data structure.
4975 **/
4976static int
James Smartff78d8f2011-12-13 13:21:35 -05004977lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004978{
James Smartff78d8f2011-12-13 13:21:35 -05004979 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004980 struct lpfc_dmabuf *mp;
4981 struct lpfc_mqe *mqe;
4982 uint32_t data_length;
4983 int rc;
4984
4985 /* Program the default value of vlan_id and fc_map */
4986 phba->valid_vlan = 0;
4987 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4988 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4989 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4990
James Smartff78d8f2011-12-13 13:21:35 -05004991 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4992 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004993 return -ENOMEM;
4994
James Smartff78d8f2011-12-13 13:21:35 -05004995 mqe = &mboxq->u.mqe;
4996 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4997 rc = -ENOMEM;
4998 goto out_free_mboxq;
4999 }
5000
James Smartda0436e2009-05-22 14:51:39 -04005001 mp = (struct lpfc_dmabuf *) mboxq->context1;
5002 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5003
5004 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5005 "(%d):2571 Mailbox cmd x%x Status x%x "
5006 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5007 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5008 "CQ: x%x x%x x%x x%x\n",
5009 mboxq->vport ? mboxq->vport->vpi : 0,
5010 bf_get(lpfc_mqe_command, mqe),
5011 bf_get(lpfc_mqe_status, mqe),
5012 mqe->un.mb_words[0], mqe->un.mb_words[1],
5013 mqe->un.mb_words[2], mqe->un.mb_words[3],
5014 mqe->un.mb_words[4], mqe->un.mb_words[5],
5015 mqe->un.mb_words[6], mqe->un.mb_words[7],
5016 mqe->un.mb_words[8], mqe->un.mb_words[9],
5017 mqe->un.mb_words[10], mqe->un.mb_words[11],
5018 mqe->un.mb_words[12], mqe->un.mb_words[13],
5019 mqe->un.mb_words[14], mqe->un.mb_words[15],
5020 mqe->un.mb_words[16], mqe->un.mb_words[50],
5021 mboxq->mcqe.word0,
5022 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5023 mboxq->mcqe.trailer);
5024
5025 if (rc) {
5026 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5027 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005028 rc = -EIO;
5029 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005030 }
5031 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005032 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005033 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5034 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005035 rc = -EIO;
5036 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005037 }
James Smartda0436e2009-05-22 14:51:39 -04005038
5039 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5040 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5041 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005042 rc = 0;
5043
5044out_free_mboxq:
5045 mempool_free(mboxq, phba->mbox_mem_pool);
5046 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005047}
5048
5049/**
5050 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5051 * @phba: pointer to lpfc hba data structure.
5052 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5053 * @vpd: pointer to the memory to hold resulting port vpd data.
5054 * @vpd_size: On input, the number of bytes allocated to @vpd.
5055 * On output, the number of data bytes in @vpd.
5056 *
5057 * This routine executes a READ_REV SLI4 mailbox command. In
5058 * addition, this routine gets the port vpd data.
5059 *
5060 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005061 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005062 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005063 **/
5064static int
5065lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5066 uint8_t *vpd, uint32_t *vpd_size)
5067{
5068 int rc = 0;
5069 uint32_t dma_size;
5070 struct lpfc_dmabuf *dmabuf;
5071 struct lpfc_mqe *mqe;
5072
5073 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5074 if (!dmabuf)
5075 return -ENOMEM;
5076
5077 /*
5078 * Get a DMA buffer for the vpd data resulting from the READ_REV
5079 * mailbox command.
5080 */
5081 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005082 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5083 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005084 if (!dmabuf->virt) {
5085 kfree(dmabuf);
5086 return -ENOMEM;
5087 }
James Smartda0436e2009-05-22 14:51:39 -04005088
5089 /*
5090 * The SLI4 implementation of READ_REV conflicts at word1,
5091 * bits 31:16 and SLI4 adds vpd functionality not present
5092 * in SLI3. This code corrects the conflicts.
5093 */
5094 lpfc_read_rev(phba, mboxq);
5095 mqe = &mboxq->u.mqe;
5096 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5097 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5098 mqe->un.read_rev.word1 &= 0x0000FFFF;
5099 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5100 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5101
5102 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5103 if (rc) {
5104 dma_free_coherent(&phba->pcidev->dev, dma_size,
5105 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005106 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005107 return -EIO;
5108 }
5109
James Smartda0436e2009-05-22 14:51:39 -04005110 /*
5111 * The available vpd length cannot be bigger than the
5112 * DMA buffer passed to the port. Catch the less than
5113 * case and update the caller's size.
5114 */
5115 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5116 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5117
James Smartd7c47992010-06-08 18:31:54 -04005118 memcpy(vpd, dmabuf->virt, *vpd_size);
5119
James Smartda0436e2009-05-22 14:51:39 -04005120 dma_free_coherent(&phba->pcidev->dev, dma_size,
5121 dmabuf->virt, dmabuf->phys);
5122 kfree(dmabuf);
5123 return 0;
5124}
5125
5126/**
James Smartcd1c8302011-10-10 21:33:25 -04005127 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5128 * @phba: pointer to lpfc hba data structure.
5129 *
5130 * This routine retrieves SLI4 device physical port name this PCI function
5131 * is attached to.
5132 *
5133 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005134 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005135 * otherwise - failed to retrieve physical port name
5136 **/
5137static int
5138lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5139{
5140 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005141 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5142 struct lpfc_controller_attribute *cntl_attr;
5143 struct lpfc_mbx_get_port_name *get_port_name;
5144 void *virtaddr = NULL;
5145 uint32_t alloclen, reqlen;
5146 uint32_t shdr_status, shdr_add_status;
5147 union lpfc_sli4_cfg_shdr *shdr;
5148 char cport_name = 0;
5149 int rc;
5150
5151 /* We assume nothing at this point */
5152 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5153 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5154
5155 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5156 if (!mboxq)
5157 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005158 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005159 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5160 lpfc_sli4_read_config(phba);
5161 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5162 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005163
5164 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5165 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5166 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5167 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5168 LPFC_SLI4_MBX_NEMBED);
5169 if (alloclen < reqlen) {
5170 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5171 "3084 Allocated DMA memory size (%d) is "
5172 "less than the requested DMA memory size "
5173 "(%d)\n", alloclen, reqlen);
5174 rc = -ENOMEM;
5175 goto out_free_mboxq;
5176 }
5177 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5178 virtaddr = mboxq->sge_array->addr[0];
5179 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5180 shdr = &mbx_cntl_attr->cfg_shdr;
5181 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5182 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5183 if (shdr_status || shdr_add_status || rc) {
5184 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5185 "3085 Mailbox x%x (x%x/x%x) failed, "
5186 "rc:x%x, status:x%x, add_status:x%x\n",
5187 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5188 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5189 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5190 rc, shdr_status, shdr_add_status);
5191 rc = -ENXIO;
5192 goto out_free_mboxq;
5193 }
5194 cntl_attr = &mbx_cntl_attr->cntl_attr;
5195 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5196 phba->sli4_hba.lnk_info.lnk_tp =
5197 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5198 phba->sli4_hba.lnk_info.lnk_no =
5199 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5200 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5201 "3086 lnk_type:%d, lnk_numb:%d\n",
5202 phba->sli4_hba.lnk_info.lnk_tp,
5203 phba->sli4_hba.lnk_info.lnk_no);
5204
5205retrieve_ppname:
5206 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5207 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5208 sizeof(struct lpfc_mbx_get_port_name) -
5209 sizeof(struct lpfc_sli4_cfg_mhdr),
5210 LPFC_SLI4_MBX_EMBED);
5211 get_port_name = &mboxq->u.mqe.un.get_port_name;
5212 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5213 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5214 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5215 phba->sli4_hba.lnk_info.lnk_tp);
5216 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5217 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5218 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5219 if (shdr_status || shdr_add_status || rc) {
5220 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5221 "3087 Mailbox x%x (x%x/x%x) failed: "
5222 "rc:x%x, status:x%x, add_status:x%x\n",
5223 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5224 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5225 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5226 rc, shdr_status, shdr_add_status);
5227 rc = -ENXIO;
5228 goto out_free_mboxq;
5229 }
5230 switch (phba->sli4_hba.lnk_info.lnk_no) {
5231 case LPFC_LINK_NUMBER_0:
5232 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5233 &get_port_name->u.response);
5234 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5235 break;
5236 case LPFC_LINK_NUMBER_1:
5237 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5238 &get_port_name->u.response);
5239 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5240 break;
5241 case LPFC_LINK_NUMBER_2:
5242 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5243 &get_port_name->u.response);
5244 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5245 break;
5246 case LPFC_LINK_NUMBER_3:
5247 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5248 &get_port_name->u.response);
5249 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5250 break;
5251 default:
5252 break;
5253 }
5254
5255 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5256 phba->Port[0] = cport_name;
5257 phba->Port[1] = '\0';
5258 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5259 "3091 SLI get port name: %s\n", phba->Port);
5260 }
5261
5262out_free_mboxq:
5263 if (rc != MBX_TIMEOUT) {
5264 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5265 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5266 else
5267 mempool_free(mboxq, phba->mbox_mem_pool);
5268 }
5269 return rc;
5270}
5271
5272/**
James Smartda0436e2009-05-22 14:51:39 -04005273 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5274 * @phba: pointer to lpfc hba data structure.
5275 *
5276 * This routine is called to explicitly arm the SLI4 device's completion and
5277 * event queues
5278 **/
5279static void
5280lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5281{
James Smart895427b2017-02-12 13:52:30 -08005282 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04005283
5284 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5285 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005286 if (phba->sli4_hba.nvmels_cq)
5287 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5288 LPFC_QUEUE_REARM);
5289
5290 if (phba->sli4_hba.fcp_cq)
5291 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5292 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5293 LPFC_QUEUE_REARM);
5294
5295 if (phba->sli4_hba.nvme_cq)
5296 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5297 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5298 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005299
James Smartf38fa0b2014-04-04 13:52:21 -04005300 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005301 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5302
James Smart895427b2017-02-12 13:52:30 -08005303 if (phba->sli4_hba.hba_eq)
5304 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5305 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5306 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005307
James Smart2d7dbc42017-02-12 13:52:35 -08005308 if (phba->nvmet_support) {
5309 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5310 lpfc_sli4_cq_release(
5311 phba->sli4_hba.nvmet_cqset[qidx],
5312 LPFC_QUEUE_REARM);
5313 }
James Smart2e90f4b2011-12-13 13:22:37 -05005314 }
James Smart1ba981f2014-02-20 09:56:45 -05005315
5316 if (phba->cfg_fof)
5317 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005318}
5319
5320/**
James Smart6d368e52011-05-24 11:44:12 -04005321 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5322 * @phba: Pointer to HBA context object.
5323 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005324 * @extnt_count: buffer to hold port available extent count.
5325 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005326 *
James Smartb76f2dc2011-07-22 18:37:42 -04005327 * This function calls the port and retrievs the number of available
5328 * extents and their size for a particular extent type.
5329 *
5330 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005331 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005332int
James Smart6d368e52011-05-24 11:44:12 -04005333lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5334 uint16_t *extnt_count, uint16_t *extnt_size)
5335{
5336 int rc = 0;
5337 uint32_t length;
5338 uint32_t mbox_tmo;
5339 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5340 LPFC_MBOXQ_t *mbox;
5341
5342 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5343 if (!mbox)
5344 return -ENOMEM;
5345
5346 /* Find out how many extents are available for this resource type */
5347 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5348 sizeof(struct lpfc_sli4_cfg_mhdr));
5349 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5350 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5351 length, LPFC_SLI4_MBX_EMBED);
5352
5353 /* Send an extents count of 0 - the GET doesn't use it. */
5354 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5355 LPFC_SLI4_MBX_EMBED);
5356 if (unlikely(rc)) {
5357 rc = -EIO;
5358 goto err_exit;
5359 }
5360
5361 if (!phba->sli4_hba.intr_enable)
5362 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5363 else {
James Smarta183a152011-10-10 21:32:43 -04005364 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005365 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5366 }
5367 if (unlikely(rc)) {
5368 rc = -EIO;
5369 goto err_exit;
5370 }
5371
5372 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5373 if (bf_get(lpfc_mbox_hdr_status,
5374 &rsrc_info->header.cfg_shdr.response)) {
5375 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5376 "2930 Failed to get resource extents "
5377 "Status 0x%x Add'l Status 0x%x\n",
5378 bf_get(lpfc_mbox_hdr_status,
5379 &rsrc_info->header.cfg_shdr.response),
5380 bf_get(lpfc_mbox_hdr_add_status,
5381 &rsrc_info->header.cfg_shdr.response));
5382 rc = -EIO;
5383 goto err_exit;
5384 }
5385
5386 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5387 &rsrc_info->u.rsp);
5388 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5389 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005390
5391 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5392 "3162 Retrieved extents type-%d from port: count:%d, "
5393 "size:%d\n", type, *extnt_count, *extnt_size);
5394
5395err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005396 mempool_free(mbox, phba->mbox_mem_pool);
5397 return rc;
5398}
5399
5400/**
5401 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5402 * @phba: Pointer to HBA context object.
5403 * @type: The extent type to check.
5404 *
5405 * This function reads the current available extents from the port and checks
5406 * if the extent count or extent size has changed since the last access.
5407 * Callers use this routine post port reset to understand if there is a
5408 * extent reprovisioning requirement.
5409 *
5410 * Returns:
5411 * -Error: error indicates problem.
5412 * 1: Extent count or size has changed.
5413 * 0: No changes.
5414 **/
5415static int
5416lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5417{
5418 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5419 uint16_t size_diff, rsrc_ext_size;
5420 int rc = 0;
5421 struct lpfc_rsrc_blks *rsrc_entry;
5422 struct list_head *rsrc_blk_list = NULL;
5423
5424 size_diff = 0;
5425 curr_ext_cnt = 0;
5426 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5427 &rsrc_ext_cnt,
5428 &rsrc_ext_size);
5429 if (unlikely(rc))
5430 return -EIO;
5431
5432 switch (type) {
5433 case LPFC_RSC_TYPE_FCOE_RPI:
5434 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5435 break;
5436 case LPFC_RSC_TYPE_FCOE_VPI:
5437 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5438 break;
5439 case LPFC_RSC_TYPE_FCOE_XRI:
5440 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5441 break;
5442 case LPFC_RSC_TYPE_FCOE_VFI:
5443 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5444 break;
5445 default:
5446 break;
5447 }
5448
5449 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5450 curr_ext_cnt++;
5451 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5452 size_diff++;
5453 }
5454
5455 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5456 rc = 1;
5457
5458 return rc;
5459}
5460
5461/**
5462 * lpfc_sli4_cfg_post_extnts -
5463 * @phba: Pointer to HBA context object.
5464 * @extnt_cnt - number of available extents.
5465 * @type - the extent type (rpi, xri, vfi, vpi).
5466 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5467 * @mbox - pointer to the caller's allocated mailbox structure.
5468 *
5469 * This function executes the extents allocation request. It also
5470 * takes care of the amount of memory needed to allocate or get the
5471 * allocated extents. It is the caller's responsibility to evaluate
5472 * the response.
5473 *
5474 * Returns:
5475 * -Error: Error value describes the condition found.
5476 * 0: if successful
5477 **/
5478static int
James Smart8a9d2e82012-05-09 21:16:12 -04005479lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005480 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5481{
5482 int rc = 0;
5483 uint32_t req_len;
5484 uint32_t emb_len;
5485 uint32_t alloc_len, mbox_tmo;
5486
5487 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005488 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005489
5490 /*
5491 * Calculate the size of an embedded mailbox. The uint32_t
5492 * accounts for extents-specific word.
5493 */
5494 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5495 sizeof(uint32_t);
5496
5497 /*
5498 * Presume the allocation and response will fit into an embedded
5499 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5500 */
5501 *emb = LPFC_SLI4_MBX_EMBED;
5502 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005503 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005504 sizeof(union lpfc_sli4_cfg_shdr) +
5505 sizeof(uint32_t);
5506 *emb = LPFC_SLI4_MBX_NEMBED;
5507 }
5508
5509 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5510 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5511 req_len, *emb);
5512 if (alloc_len < req_len) {
5513 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005514 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005515 "less than the requested DMA memory "
5516 "size (x%x)\n", alloc_len, req_len);
5517 return -ENOMEM;
5518 }
James Smart8a9d2e82012-05-09 21:16:12 -04005519 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005520 if (unlikely(rc))
5521 return -EIO;
5522
5523 if (!phba->sli4_hba.intr_enable)
5524 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5525 else {
James Smarta183a152011-10-10 21:32:43 -04005526 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005527 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5528 }
5529
5530 if (unlikely(rc))
5531 rc = -EIO;
5532 return rc;
5533}
5534
5535/**
5536 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5537 * @phba: Pointer to HBA context object.
5538 * @type: The resource extent type to allocate.
5539 *
5540 * This function allocates the number of elements for the specified
5541 * resource type.
5542 **/
5543static int
5544lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5545{
5546 bool emb = false;
5547 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5548 uint16_t rsrc_id, rsrc_start, j, k;
5549 uint16_t *ids;
5550 int i, rc;
5551 unsigned long longs;
5552 unsigned long *bmask;
5553 struct lpfc_rsrc_blks *rsrc_blks;
5554 LPFC_MBOXQ_t *mbox;
5555 uint32_t length;
5556 struct lpfc_id_range *id_array = NULL;
5557 void *virtaddr = NULL;
5558 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5559 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5560 struct list_head *ext_blk_list;
5561
5562 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5563 &rsrc_cnt,
5564 &rsrc_size);
5565 if (unlikely(rc))
5566 return -EIO;
5567
5568 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5569 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5570 "3009 No available Resource Extents "
5571 "for resource type 0x%x: Count: 0x%x, "
5572 "Size 0x%x\n", type, rsrc_cnt,
5573 rsrc_size);
5574 return -ENOMEM;
5575 }
5576
James Smart8a9d2e82012-05-09 21:16:12 -04005577 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5578 "2903 Post resource extents type-0x%x: "
5579 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005580
5581 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5582 if (!mbox)
5583 return -ENOMEM;
5584
James Smart8a9d2e82012-05-09 21:16:12 -04005585 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005586 if (unlikely(rc)) {
5587 rc = -EIO;
5588 goto err_exit;
5589 }
5590
5591 /*
5592 * Figure out where the response is located. Then get local pointers
5593 * to the response data. The port does not guarantee to respond to
5594 * all extents counts request so update the local variable with the
5595 * allocated count from the port.
5596 */
5597 if (emb == LPFC_SLI4_MBX_EMBED) {
5598 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5599 id_array = &rsrc_ext->u.rsp.id[0];
5600 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5601 } else {
5602 virtaddr = mbox->sge_array->addr[0];
5603 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5604 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5605 id_array = &n_rsrc->id;
5606 }
5607
5608 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5609 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5610
5611 /*
5612 * Based on the resource size and count, correct the base and max
5613 * resource values.
5614 */
5615 length = sizeof(struct lpfc_rsrc_blks);
5616 switch (type) {
5617 case LPFC_RSC_TYPE_FCOE_RPI:
5618 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5619 sizeof(unsigned long),
5620 GFP_KERNEL);
5621 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5622 rc = -ENOMEM;
5623 goto err_exit;
5624 }
5625 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5626 sizeof(uint16_t),
5627 GFP_KERNEL);
5628 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5629 kfree(phba->sli4_hba.rpi_bmask);
5630 rc = -ENOMEM;
5631 goto err_exit;
5632 }
5633
5634 /*
5635 * The next_rpi was initialized with the maximum available
5636 * count but the port may allocate a smaller number. Catch
5637 * that case and update the next_rpi.
5638 */
5639 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5640
5641 /* Initialize local ptrs for common extent processing later. */
5642 bmask = phba->sli4_hba.rpi_bmask;
5643 ids = phba->sli4_hba.rpi_ids;
5644 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5645 break;
5646 case LPFC_RSC_TYPE_FCOE_VPI:
5647 phba->vpi_bmask = kzalloc(longs *
5648 sizeof(unsigned long),
5649 GFP_KERNEL);
5650 if (unlikely(!phba->vpi_bmask)) {
5651 rc = -ENOMEM;
5652 goto err_exit;
5653 }
5654 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5655 sizeof(uint16_t),
5656 GFP_KERNEL);
5657 if (unlikely(!phba->vpi_ids)) {
5658 kfree(phba->vpi_bmask);
5659 rc = -ENOMEM;
5660 goto err_exit;
5661 }
5662
5663 /* Initialize local ptrs for common extent processing later. */
5664 bmask = phba->vpi_bmask;
5665 ids = phba->vpi_ids;
5666 ext_blk_list = &phba->lpfc_vpi_blk_list;
5667 break;
5668 case LPFC_RSC_TYPE_FCOE_XRI:
5669 phba->sli4_hba.xri_bmask = kzalloc(longs *
5670 sizeof(unsigned long),
5671 GFP_KERNEL);
5672 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5673 rc = -ENOMEM;
5674 goto err_exit;
5675 }
James Smart8a9d2e82012-05-09 21:16:12 -04005676 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005677 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5678 sizeof(uint16_t),
5679 GFP_KERNEL);
5680 if (unlikely(!phba->sli4_hba.xri_ids)) {
5681 kfree(phba->sli4_hba.xri_bmask);
5682 rc = -ENOMEM;
5683 goto err_exit;
5684 }
5685
5686 /* Initialize local ptrs for common extent processing later. */
5687 bmask = phba->sli4_hba.xri_bmask;
5688 ids = phba->sli4_hba.xri_ids;
5689 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5690 break;
5691 case LPFC_RSC_TYPE_FCOE_VFI:
5692 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5693 sizeof(unsigned long),
5694 GFP_KERNEL);
5695 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5696 rc = -ENOMEM;
5697 goto err_exit;
5698 }
5699 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5700 sizeof(uint16_t),
5701 GFP_KERNEL);
5702 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5703 kfree(phba->sli4_hba.vfi_bmask);
5704 rc = -ENOMEM;
5705 goto err_exit;
5706 }
5707
5708 /* Initialize local ptrs for common extent processing later. */
5709 bmask = phba->sli4_hba.vfi_bmask;
5710 ids = phba->sli4_hba.vfi_ids;
5711 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5712 break;
5713 default:
5714 /* Unsupported Opcode. Fail call. */
5715 id_array = NULL;
5716 bmask = NULL;
5717 ids = NULL;
5718 ext_blk_list = NULL;
5719 goto err_exit;
5720 }
5721
5722 /*
5723 * Complete initializing the extent configuration with the
5724 * allocated ids assigned to this function. The bitmask serves
5725 * as an index into the array and manages the available ids. The
5726 * array just stores the ids communicated to the port via the wqes.
5727 */
5728 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5729 if ((i % 2) == 0)
5730 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5731 &id_array[k]);
5732 else
5733 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5734 &id_array[k]);
5735
5736 rsrc_blks = kzalloc(length, GFP_KERNEL);
5737 if (unlikely(!rsrc_blks)) {
5738 rc = -ENOMEM;
5739 kfree(bmask);
5740 kfree(ids);
5741 goto err_exit;
5742 }
5743 rsrc_blks->rsrc_start = rsrc_id;
5744 rsrc_blks->rsrc_size = rsrc_size;
5745 list_add_tail(&rsrc_blks->list, ext_blk_list);
5746 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005747 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005748 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005749 lpfc_sli4_get_iocb_cnt(phba);
5750 phba->sli4_hba.nvme_xri_start =
5751 phba->sli4_hba.scsi_xri_start +
5752 phba->sli4_hba.scsi_xri_max;
5753 }
James Smart6d368e52011-05-24 11:44:12 -04005754
5755 while (rsrc_id < (rsrc_start + rsrc_size)) {
5756 ids[j] = rsrc_id;
5757 rsrc_id++;
5758 j++;
5759 }
5760 /* Entire word processed. Get next word.*/
5761 if ((i % 2) == 1)
5762 k++;
5763 }
5764 err_exit:
5765 lpfc_sli4_mbox_cmd_free(phba, mbox);
5766 return rc;
5767}
5768
James Smart895427b2017-02-12 13:52:30 -08005769
5770
James Smart6d368e52011-05-24 11:44:12 -04005771/**
5772 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5773 * @phba: Pointer to HBA context object.
5774 * @type: the extent's type.
5775 *
5776 * This function deallocates all extents of a particular resource type.
5777 * SLI4 does not allow for deallocating a particular extent range. It
5778 * is the caller's responsibility to release all kernel memory resources.
5779 **/
5780static int
5781lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5782{
5783 int rc;
5784 uint32_t length, mbox_tmo = 0;
5785 LPFC_MBOXQ_t *mbox;
5786 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5787 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5788
5789 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5790 if (!mbox)
5791 return -ENOMEM;
5792
5793 /*
5794 * This function sends an embedded mailbox because it only sends the
5795 * the resource type. All extents of this type are released by the
5796 * port.
5797 */
5798 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5799 sizeof(struct lpfc_sli4_cfg_mhdr));
5800 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5801 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5802 length, LPFC_SLI4_MBX_EMBED);
5803
5804 /* Send an extents count of 0 - the dealloc doesn't use it. */
5805 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5806 LPFC_SLI4_MBX_EMBED);
5807 if (unlikely(rc)) {
5808 rc = -EIO;
5809 goto out_free_mbox;
5810 }
5811 if (!phba->sli4_hba.intr_enable)
5812 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5813 else {
James Smarta183a152011-10-10 21:32:43 -04005814 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005815 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5816 }
5817 if (unlikely(rc)) {
5818 rc = -EIO;
5819 goto out_free_mbox;
5820 }
5821
5822 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5823 if (bf_get(lpfc_mbox_hdr_status,
5824 &dealloc_rsrc->header.cfg_shdr.response)) {
5825 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5826 "2919 Failed to release resource extents "
5827 "for type %d - Status 0x%x Add'l Status 0x%x. "
5828 "Resource memory not released.\n",
5829 type,
5830 bf_get(lpfc_mbox_hdr_status,
5831 &dealloc_rsrc->header.cfg_shdr.response),
5832 bf_get(lpfc_mbox_hdr_add_status,
5833 &dealloc_rsrc->header.cfg_shdr.response));
5834 rc = -EIO;
5835 goto out_free_mbox;
5836 }
5837
5838 /* Release kernel memory resources for the specific type. */
5839 switch (type) {
5840 case LPFC_RSC_TYPE_FCOE_VPI:
5841 kfree(phba->vpi_bmask);
5842 kfree(phba->vpi_ids);
5843 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5844 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5845 &phba->lpfc_vpi_blk_list, list) {
5846 list_del_init(&rsrc_blk->list);
5847 kfree(rsrc_blk);
5848 }
James Smart16a3a202013-04-17 20:14:38 -04005849 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005850 break;
5851 case LPFC_RSC_TYPE_FCOE_XRI:
5852 kfree(phba->sli4_hba.xri_bmask);
5853 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005854 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5855 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5856 list_del_init(&rsrc_blk->list);
5857 kfree(rsrc_blk);
5858 }
5859 break;
5860 case LPFC_RSC_TYPE_FCOE_VFI:
5861 kfree(phba->sli4_hba.vfi_bmask);
5862 kfree(phba->sli4_hba.vfi_ids);
5863 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5864 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5865 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5866 list_del_init(&rsrc_blk->list);
5867 kfree(rsrc_blk);
5868 }
5869 break;
5870 case LPFC_RSC_TYPE_FCOE_RPI:
5871 /* RPI bitmask and physical id array are cleaned up earlier. */
5872 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5873 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5874 list_del_init(&rsrc_blk->list);
5875 kfree(rsrc_blk);
5876 }
5877 break;
5878 default:
5879 break;
5880 }
5881
5882 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5883
5884 out_free_mbox:
5885 mempool_free(mbox, phba->mbox_mem_pool);
5886 return rc;
5887}
5888
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005889static void
James Smart7bdedb32016-07-06 12:36:00 -07005890lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5891 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005892{
James Smart65791f12016-07-06 12:35:56 -07005893 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005894
James Smart65791f12016-07-06 12:35:56 -07005895 len = sizeof(struct lpfc_mbx_set_feature) -
5896 sizeof(struct lpfc_sli4_cfg_mhdr);
5897 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5898 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5899 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005900
James Smart7bdedb32016-07-06 12:36:00 -07005901 switch (feature) {
5902 case LPFC_SET_UE_RECOVERY:
5903 bf_set(lpfc_mbx_set_feature_UER,
5904 &mbox->u.mqe.un.set_feature, 1);
5905 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5906 mbox->u.mqe.un.set_feature.param_len = 8;
5907 break;
5908 case LPFC_SET_MDS_DIAGS:
5909 bf_set(lpfc_mbx_set_feature_mds,
5910 &mbox->u.mqe.un.set_feature, 1);
5911 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07005912 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07005913 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5914 mbox->u.mqe.un.set_feature.param_len = 8;
5915 break;
James Smart65791f12016-07-06 12:35:56 -07005916 }
James Smart7bdedb32016-07-06 12:36:00 -07005917
5918 return;
James Smart65791f12016-07-06 12:35:56 -07005919}
5920
James Smart6d368e52011-05-24 11:44:12 -04005921/**
5922 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5923 * @phba: Pointer to HBA context object.
5924 *
5925 * This function allocates all SLI4 resource identifiers.
5926 **/
5927int
5928lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5929{
5930 int i, rc, error = 0;
5931 uint16_t count, base;
5932 unsigned long longs;
5933
James Smartff78d8f2011-12-13 13:21:35 -05005934 if (!phba->sli4_hba.rpi_hdrs_in_use)
5935 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005936 if (phba->sli4_hba.extents_in_use) {
5937 /*
5938 * The port supports resource extents. The XRI, VPI, VFI, RPI
5939 * resource extent count must be read and allocated before
5940 * provisioning the resource id arrays.
5941 */
5942 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5943 LPFC_IDX_RSRC_RDY) {
5944 /*
5945 * Extent-based resources are set - the driver could
5946 * be in a port reset. Figure out if any corrective
5947 * actions need to be taken.
5948 */
5949 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5950 LPFC_RSC_TYPE_FCOE_VFI);
5951 if (rc != 0)
5952 error++;
5953 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5954 LPFC_RSC_TYPE_FCOE_VPI);
5955 if (rc != 0)
5956 error++;
5957 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5958 LPFC_RSC_TYPE_FCOE_XRI);
5959 if (rc != 0)
5960 error++;
5961 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5962 LPFC_RSC_TYPE_FCOE_RPI);
5963 if (rc != 0)
5964 error++;
5965
5966 /*
5967 * It's possible that the number of resources
5968 * provided to this port instance changed between
5969 * resets. Detect this condition and reallocate
5970 * resources. Otherwise, there is no action.
5971 */
5972 if (error) {
5973 lpfc_printf_log(phba, KERN_INFO,
5974 LOG_MBOX | LOG_INIT,
5975 "2931 Detected extent resource "
5976 "change. Reallocating all "
5977 "extents.\n");
5978 rc = lpfc_sli4_dealloc_extent(phba,
5979 LPFC_RSC_TYPE_FCOE_VFI);
5980 rc = lpfc_sli4_dealloc_extent(phba,
5981 LPFC_RSC_TYPE_FCOE_VPI);
5982 rc = lpfc_sli4_dealloc_extent(phba,
5983 LPFC_RSC_TYPE_FCOE_XRI);
5984 rc = lpfc_sli4_dealloc_extent(phba,
5985 LPFC_RSC_TYPE_FCOE_RPI);
5986 } else
5987 return 0;
5988 }
5989
5990 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5991 if (unlikely(rc))
5992 goto err_exit;
5993
5994 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5995 if (unlikely(rc))
5996 goto err_exit;
5997
5998 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5999 if (unlikely(rc))
6000 goto err_exit;
6001
6002 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6003 if (unlikely(rc))
6004 goto err_exit;
6005 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6006 LPFC_IDX_RSRC_RDY);
6007 return rc;
6008 } else {
6009 /*
6010 * The port does not support resource extents. The XRI, VPI,
6011 * VFI, RPI resource ids were determined from READ_CONFIG.
6012 * Just allocate the bitmasks and provision the resource id
6013 * arrays. If a port reset is active, the resources don't
6014 * need any action - just exit.
6015 */
6016 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006017 LPFC_IDX_RSRC_RDY) {
6018 lpfc_sli4_dealloc_resource_identifiers(phba);
6019 lpfc_sli4_remove_rpis(phba);
6020 }
James Smart6d368e52011-05-24 11:44:12 -04006021 /* RPIs. */
6022 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006023 if (count <= 0) {
6024 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6025 "3279 Invalid provisioning of "
6026 "rpi:%d\n", count);
6027 rc = -EINVAL;
6028 goto err_exit;
6029 }
James Smart6d368e52011-05-24 11:44:12 -04006030 base = phba->sli4_hba.max_cfg_param.rpi_base;
6031 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6032 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6033 sizeof(unsigned long),
6034 GFP_KERNEL);
6035 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6036 rc = -ENOMEM;
6037 goto err_exit;
6038 }
6039 phba->sli4_hba.rpi_ids = kzalloc(count *
6040 sizeof(uint16_t),
6041 GFP_KERNEL);
6042 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6043 rc = -ENOMEM;
6044 goto free_rpi_bmask;
6045 }
6046
6047 for (i = 0; i < count; i++)
6048 phba->sli4_hba.rpi_ids[i] = base + i;
6049
6050 /* VPIs. */
6051 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006052 if (count <= 0) {
6053 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6054 "3280 Invalid provisioning of "
6055 "vpi:%d\n", count);
6056 rc = -EINVAL;
6057 goto free_rpi_ids;
6058 }
James Smart6d368e52011-05-24 11:44:12 -04006059 base = phba->sli4_hba.max_cfg_param.vpi_base;
6060 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6061 phba->vpi_bmask = kzalloc(longs *
6062 sizeof(unsigned long),
6063 GFP_KERNEL);
6064 if (unlikely(!phba->vpi_bmask)) {
6065 rc = -ENOMEM;
6066 goto free_rpi_ids;
6067 }
6068 phba->vpi_ids = kzalloc(count *
6069 sizeof(uint16_t),
6070 GFP_KERNEL);
6071 if (unlikely(!phba->vpi_ids)) {
6072 rc = -ENOMEM;
6073 goto free_vpi_bmask;
6074 }
6075
6076 for (i = 0; i < count; i++)
6077 phba->vpi_ids[i] = base + i;
6078
6079 /* XRIs. */
6080 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006081 if (count <= 0) {
6082 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6083 "3281 Invalid provisioning of "
6084 "xri:%d\n", count);
6085 rc = -EINVAL;
6086 goto free_vpi_ids;
6087 }
James Smart6d368e52011-05-24 11:44:12 -04006088 base = phba->sli4_hba.max_cfg_param.xri_base;
6089 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6090 phba->sli4_hba.xri_bmask = kzalloc(longs *
6091 sizeof(unsigned long),
6092 GFP_KERNEL);
6093 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6094 rc = -ENOMEM;
6095 goto free_vpi_ids;
6096 }
James Smart41899be2012-03-01 22:34:19 -05006097 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006098 phba->sli4_hba.xri_ids = kzalloc(count *
6099 sizeof(uint16_t),
6100 GFP_KERNEL);
6101 if (unlikely(!phba->sli4_hba.xri_ids)) {
6102 rc = -ENOMEM;
6103 goto free_xri_bmask;
6104 }
6105
6106 for (i = 0; i < count; i++)
6107 phba->sli4_hba.xri_ids[i] = base + i;
6108
6109 /* VFIs. */
6110 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006111 if (count <= 0) {
6112 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6113 "3282 Invalid provisioning of "
6114 "vfi:%d\n", count);
6115 rc = -EINVAL;
6116 goto free_xri_ids;
6117 }
James Smart6d368e52011-05-24 11:44:12 -04006118 base = phba->sli4_hba.max_cfg_param.vfi_base;
6119 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6120 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6121 sizeof(unsigned long),
6122 GFP_KERNEL);
6123 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6124 rc = -ENOMEM;
6125 goto free_xri_ids;
6126 }
6127 phba->sli4_hba.vfi_ids = kzalloc(count *
6128 sizeof(uint16_t),
6129 GFP_KERNEL);
6130 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6131 rc = -ENOMEM;
6132 goto free_vfi_bmask;
6133 }
6134
6135 for (i = 0; i < count; i++)
6136 phba->sli4_hba.vfi_ids[i] = base + i;
6137
6138 /*
6139 * Mark all resources ready. An HBA reset doesn't need
6140 * to reset the initialization.
6141 */
6142 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6143 LPFC_IDX_RSRC_RDY);
6144 return 0;
6145 }
6146
6147 free_vfi_bmask:
6148 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006149 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006150 free_xri_ids:
6151 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006152 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006153 free_xri_bmask:
6154 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006155 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006156 free_vpi_ids:
6157 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006158 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006159 free_vpi_bmask:
6160 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006161 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006162 free_rpi_ids:
6163 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006164 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006165 free_rpi_bmask:
6166 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006167 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006168 err_exit:
6169 return rc;
6170}
6171
6172/**
6173 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6174 * @phba: Pointer to HBA context object.
6175 *
6176 * This function allocates the number of elements for the specified
6177 * resource type.
6178 **/
6179int
6180lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6181{
6182 if (phba->sli4_hba.extents_in_use) {
6183 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6184 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6185 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6186 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6187 } else {
6188 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006189 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006190 kfree(phba->vpi_ids);
6191 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6192 kfree(phba->sli4_hba.xri_bmask);
6193 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006194 kfree(phba->sli4_hba.vfi_bmask);
6195 kfree(phba->sli4_hba.vfi_ids);
6196 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6197 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6198 }
6199
6200 return 0;
6201}
6202
6203/**
James Smartb76f2dc2011-07-22 18:37:42 -04006204 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6205 * @phba: Pointer to HBA context object.
6206 * @type: The resource extent type.
6207 * @extnt_count: buffer to hold port extent count response
6208 * @extnt_size: buffer to hold port extent size response.
6209 *
6210 * This function calls the port to read the host allocated extents
6211 * for a particular type.
6212 **/
6213int
6214lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6215 uint16_t *extnt_cnt, uint16_t *extnt_size)
6216{
6217 bool emb;
6218 int rc = 0;
6219 uint16_t curr_blks = 0;
6220 uint32_t req_len, emb_len;
6221 uint32_t alloc_len, mbox_tmo;
6222 struct list_head *blk_list_head;
6223 struct lpfc_rsrc_blks *rsrc_blk;
6224 LPFC_MBOXQ_t *mbox;
6225 void *virtaddr = NULL;
6226 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6227 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6228 union lpfc_sli4_cfg_shdr *shdr;
6229
6230 switch (type) {
6231 case LPFC_RSC_TYPE_FCOE_VPI:
6232 blk_list_head = &phba->lpfc_vpi_blk_list;
6233 break;
6234 case LPFC_RSC_TYPE_FCOE_XRI:
6235 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6236 break;
6237 case LPFC_RSC_TYPE_FCOE_VFI:
6238 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6239 break;
6240 case LPFC_RSC_TYPE_FCOE_RPI:
6241 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6242 break;
6243 default:
6244 return -EIO;
6245 }
6246
6247 /* Count the number of extents currently allocatd for this type. */
6248 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6249 if (curr_blks == 0) {
6250 /*
6251 * The GET_ALLOCATED mailbox does not return the size,
6252 * just the count. The size should be just the size
6253 * stored in the current allocated block and all sizes
6254 * for an extent type are the same so set the return
6255 * value now.
6256 */
6257 *extnt_size = rsrc_blk->rsrc_size;
6258 }
6259 curr_blks++;
6260 }
6261
James Smartb76f2dc2011-07-22 18:37:42 -04006262 /*
6263 * Calculate the size of an embedded mailbox. The uint32_t
6264 * accounts for extents-specific word.
6265 */
6266 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6267 sizeof(uint32_t);
6268
6269 /*
6270 * Presume the allocation and response will fit into an embedded
6271 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6272 */
6273 emb = LPFC_SLI4_MBX_EMBED;
6274 req_len = emb_len;
6275 if (req_len > emb_len) {
6276 req_len = curr_blks * sizeof(uint16_t) +
6277 sizeof(union lpfc_sli4_cfg_shdr) +
6278 sizeof(uint32_t);
6279 emb = LPFC_SLI4_MBX_NEMBED;
6280 }
6281
6282 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6283 if (!mbox)
6284 return -ENOMEM;
6285 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6286
6287 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6288 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6289 req_len, emb);
6290 if (alloc_len < req_len) {
6291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6292 "2983 Allocated DMA memory size (x%x) is "
6293 "less than the requested DMA memory "
6294 "size (x%x)\n", alloc_len, req_len);
6295 rc = -ENOMEM;
6296 goto err_exit;
6297 }
6298 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6299 if (unlikely(rc)) {
6300 rc = -EIO;
6301 goto err_exit;
6302 }
6303
6304 if (!phba->sli4_hba.intr_enable)
6305 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6306 else {
James Smarta183a152011-10-10 21:32:43 -04006307 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006308 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6309 }
6310
6311 if (unlikely(rc)) {
6312 rc = -EIO;
6313 goto err_exit;
6314 }
6315
6316 /*
6317 * Figure out where the response is located. Then get local pointers
6318 * to the response data. The port does not guarantee to respond to
6319 * all extents counts request so update the local variable with the
6320 * allocated count from the port.
6321 */
6322 if (emb == LPFC_SLI4_MBX_EMBED) {
6323 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6324 shdr = &rsrc_ext->header.cfg_shdr;
6325 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6326 } else {
6327 virtaddr = mbox->sge_array->addr[0];
6328 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6329 shdr = &n_rsrc->cfg_shdr;
6330 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6331 }
6332
6333 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6334 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6335 "2984 Failed to read allocated resources "
6336 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6337 type,
6338 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6339 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6340 rc = -EIO;
6341 goto err_exit;
6342 }
6343 err_exit:
6344 lpfc_sli4_mbox_cmd_free(phba, mbox);
6345 return rc;
6346}
6347
6348/**
James Smart0ef69962017-04-21 16:04:50 -07006349 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006350 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006351 * @pring: Pointer to driver SLI ring object.
6352 * @sgl_list: linked link of sgl buffers to post
6353 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006354 *
James Smart895427b2017-02-12 13:52:30 -08006355 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006356 * repost them to the port by using SGL block post. This is needed after a
6357 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006358 * of buffer sgls which contains contiguous xris and uses the non-embedded
6359 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006360 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6361 * mailbox command for posting.
6362 *
6363 * Returns: 0 = success, non-zero failure.
6364 **/
6365static int
James Smart895427b2017-02-12 13:52:30 -08006366lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6367 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006368{
6369 struct lpfc_sglq *sglq_entry = NULL;
6370 struct lpfc_sglq *sglq_entry_next = NULL;
6371 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006372 int status, total_cnt;
6373 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006374 int last_xritag = NO_XRI;
6375 LIST_HEAD(prep_sgl_list);
6376 LIST_HEAD(blck_sgl_list);
6377 LIST_HEAD(allc_sgl_list);
6378 LIST_HEAD(post_sgl_list);
6379 LIST_HEAD(free_sgl_list);
6380
James Smart38c20672013-03-01 16:37:44 -05006381 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006382 spin_lock(&phba->sli4_hba.sgl_list_lock);
6383 list_splice_init(sgl_list, &allc_sgl_list);
6384 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006385 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006386
James Smart895427b2017-02-12 13:52:30 -08006387 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006388 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6389 &allc_sgl_list, list) {
6390 list_del_init(&sglq_entry->list);
6391 block_cnt++;
6392 if ((last_xritag != NO_XRI) &&
6393 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6394 /* a hole in xri block, form a sgl posting block */
6395 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6396 post_cnt = block_cnt - 1;
6397 /* prepare list for next posting block */
6398 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6399 block_cnt = 1;
6400 } else {
6401 /* prepare list for next posting block */
6402 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6403 /* enough sgls for non-embed sgl mbox command */
6404 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6405 list_splice_init(&prep_sgl_list,
6406 &blck_sgl_list);
6407 post_cnt = block_cnt;
6408 block_cnt = 0;
6409 }
6410 }
6411 num_posted++;
6412
6413 /* keep track of last sgl's xritag */
6414 last_xritag = sglq_entry->sli4_xritag;
6415
James Smart895427b2017-02-12 13:52:30 -08006416 /* end of repost sgl list condition for buffers */
6417 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006418 if (post_cnt == 0) {
6419 list_splice_init(&prep_sgl_list,
6420 &blck_sgl_list);
6421 post_cnt = block_cnt;
6422 } else if (block_cnt == 1) {
6423 status = lpfc_sli4_post_sgl(phba,
6424 sglq_entry->phys, 0,
6425 sglq_entry->sli4_xritag);
6426 if (!status) {
6427 /* successful, put sgl to posted list */
6428 list_add_tail(&sglq_entry->list,
6429 &post_sgl_list);
6430 } else {
6431 /* Failure, put sgl to free list */
6432 lpfc_printf_log(phba, KERN_WARNING,
6433 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006434 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006435 "sgl, xritag:x%x\n",
6436 sglq_entry->sli4_xritag);
6437 list_add_tail(&sglq_entry->list,
6438 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006439 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006440 }
6441 }
6442 }
6443
6444 /* continue until a nembed page worth of sgls */
6445 if (post_cnt == 0)
6446 continue;
6447
James Smart895427b2017-02-12 13:52:30 -08006448 /* post the buffer list sgls as a block */
6449 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6450 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006451
6452 if (!status) {
6453 /* success, put sgl list to posted sgl list */
6454 list_splice_init(&blck_sgl_list, &post_sgl_list);
6455 } else {
6456 /* Failure, put sgl list to free sgl list */
6457 sglq_entry_first = list_first_entry(&blck_sgl_list,
6458 struct lpfc_sglq,
6459 list);
6460 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006461 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006462 "xritag:x%x-x%x\n",
6463 sglq_entry_first->sli4_xritag,
6464 (sglq_entry_first->sli4_xritag +
6465 post_cnt - 1));
6466 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006467 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006468 }
6469
6470 /* don't reset xirtag due to hole in xri block */
6471 if (block_cnt == 0)
6472 last_xritag = NO_XRI;
6473
James Smart895427b2017-02-12 13:52:30 -08006474 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006475 post_cnt = 0;
6476 }
6477
James Smart895427b2017-02-12 13:52:30 -08006478 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006479 lpfc_free_sgl_list(phba, &free_sgl_list);
6480
James Smart895427b2017-02-12 13:52:30 -08006481 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006482 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006483 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006484 spin_lock(&phba->sli4_hba.sgl_list_lock);
6485 list_splice_init(&post_sgl_list, sgl_list);
6486 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006487 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006488 } else {
6489 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006490 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006491 return -EIO;
6492 }
James Smart895427b2017-02-12 13:52:30 -08006493
6494 /* return the number of XRIs actually posted */
6495 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006496}
6497
James Smart61bda8f2016-10-13 15:06:05 -07006498void
6499lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6500{
6501 uint32_t len;
6502
6503 len = sizeof(struct lpfc_mbx_set_host_data) -
6504 sizeof(struct lpfc_sli4_cfg_mhdr);
6505 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6506 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6507 LPFC_SLI4_MBX_EMBED);
6508
6509 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006510 mbox->u.mqe.un.set_host_data.param_len =
6511 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006512 snprintf(mbox->u.mqe.un.set_host_data.data,
6513 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6514 "Linux %s v"LPFC_DRIVER_VERSION,
6515 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6516}
6517
James Smarta8cf5df2017-05-15 15:20:46 -07006518int
James Smart6c621a22017-05-15 15:20:45 -07006519lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006520 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006521{
6522 int rc, i;
6523 struct lpfc_rqe hrqe;
6524 struct lpfc_rqe drqe;
6525 struct lpfc_rqb *rqbp;
6526 struct rqb_dmabuf *rqb_buffer;
6527 LIST_HEAD(rqb_buf_list);
6528
6529 rqbp = hrq->rqbp;
6530 for (i = 0; i < count; i++) {
6531 /* IF RQ is already full, don't bother */
6532 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6533 break;
6534 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6535 if (!rqb_buffer)
6536 break;
6537 rqb_buffer->hrq = hrq;
6538 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006539 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006540 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6541 }
6542 while (!list_empty(&rqb_buf_list)) {
6543 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6544 hbuf.list);
6545
6546 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6547 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6548 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6549 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6550 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6551 if (rc < 0) {
6552 rqbp->rqb_free_buffer(phba, rqb_buffer);
6553 } else {
6554 list_add_tail(&rqb_buffer->hbuf.list,
6555 &rqbp->rqb_buffer_list);
6556 rqbp->buffer_count++;
6557 }
6558 }
6559 return 1;
6560}
6561
James Smart8a9d2e82012-05-09 21:16:12 -04006562/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006563 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006564 * @phba: Pointer to HBA context object.
6565 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006566 * This function is the main SLI4 device initialization PCI function. This
6567 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006568 * HBA error attention handler code. Caller is not required to hold any
6569 * locks.
6570 **/
6571int
6572lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6573{
James Smart6c621a22017-05-15 15:20:45 -07006574 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006575 LPFC_MBOXQ_t *mboxq;
6576 struct lpfc_mqe *mqe;
6577 uint8_t *vpd;
6578 uint32_t vpd_size;
6579 uint32_t ftr_rsp = 0;
6580 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6581 struct lpfc_vport *vport = phba->pport;
6582 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006583 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006584
6585 /* Perform a PCI function reset to start from clean */
6586 rc = lpfc_pci_function_reset(phba);
6587 if (unlikely(rc))
6588 return -ENODEV;
6589
6590 /* Check the HBA Host Status Register for readyness */
6591 rc = lpfc_sli4_post_status_check(phba);
6592 if (unlikely(rc))
6593 return -ENODEV;
6594 else {
6595 spin_lock_irq(&phba->hbalock);
6596 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6597 spin_unlock_irq(&phba->hbalock);
6598 }
6599
6600 /*
6601 * Allocate a single mailbox container for initializing the
6602 * port.
6603 */
6604 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6605 if (!mboxq)
6606 return -ENOMEM;
6607
James Smartda0436e2009-05-22 14:51:39 -04006608 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006609 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006610 vpd = kzalloc(vpd_size, GFP_KERNEL);
6611 if (!vpd) {
6612 rc = -ENOMEM;
6613 goto out_free_mbox;
6614 }
6615
6616 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006617 if (unlikely(rc)) {
6618 kfree(vpd);
6619 goto out_free_mbox;
6620 }
James Smart572709e2013-07-15 18:32:43 -04006621
James Smartda0436e2009-05-22 14:51:39 -04006622 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006623 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006624 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006625 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006626 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6627 } else {
James Smart76a95d72010-11-20 23:11:48 -05006628 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006629 }
James Smart45ed1192009-10-02 15:17:02 -04006630
6631 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6632 LPFC_DCBX_CEE_MODE)
6633 phba->hba_flag |= HBA_FIP_SUPPORT;
6634 else
6635 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6636
James Smart4f2e66c2012-05-09 21:17:07 -04006637 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6638
James Smartc31098c2011-04-16 11:03:33 -04006639 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006640 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6641 "0376 READ_REV Error. SLI Level %d "
6642 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006643 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006644 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006645 kfree(vpd);
6646 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006647 }
James Smartcd1c8302011-10-10 21:33:25 -04006648
6649 /*
James Smartff78d8f2011-12-13 13:21:35 -05006650 * Continue initialization with default values even if driver failed
6651 * to read FCoE param config regions, only read parameters if the
6652 * board is FCoE
6653 */
6654 if (phba->hba_flag & HBA_FCOE_MODE &&
6655 lpfc_sli4_read_fcoe_params(phba))
6656 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6657 "2570 Failed to read FCoE parameters\n");
6658
6659 /*
James Smartcd1c8302011-10-10 21:33:25 -04006660 * Retrieve sli4 device physical port name, failure of doing it
6661 * is considered as non-fatal.
6662 */
6663 rc = lpfc_sli4_retrieve_pport_name(phba);
6664 if (!rc)
6665 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6666 "3080 Successful retrieving SLI4 device "
6667 "physical port name: %s.\n", phba->Port);
6668
James Smartda0436e2009-05-22 14:51:39 -04006669 /*
6670 * Evaluate the read rev and vpd data. Populate the driver
6671 * state with the results. If this routine fails, the failure
6672 * is not fatal as the driver will use generic values.
6673 */
6674 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6675 if (unlikely(!rc)) {
6676 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6677 "0377 Error %d parsing vpd. "
6678 "Using defaults.\n", rc);
6679 rc = 0;
6680 }
James Smart76a95d72010-11-20 23:11:48 -05006681 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006682
James Smartf1126682009-06-10 17:22:44 -04006683 /* Save information as VPD data */
6684 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6685 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6686 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6687 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6688 &mqe->un.read_rev);
6689 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6690 &mqe->un.read_rev);
6691 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6692 &mqe->un.read_rev);
6693 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6694 &mqe->un.read_rev);
6695 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6696 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6697 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6698 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6699 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6700 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6701 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6702 "(%d):0380 READ_REV Status x%x "
6703 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6704 mboxq->vport ? mboxq->vport->vpi : 0,
6705 bf_get(lpfc_mqe_status, mqe),
6706 phba->vpd.rev.opFwName,
6707 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6708 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006709
James Smart572709e2013-07-15 18:32:43 -04006710 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6711 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6712 if (phba->pport->cfg_lun_queue_depth > rc) {
6713 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6714 "3362 LUN queue depth changed from %d to %d\n",
6715 phba->pport->cfg_lun_queue_depth, rc);
6716 phba->pport->cfg_lun_queue_depth = rc;
6717 }
6718
James Smart65791f12016-07-06 12:35:56 -07006719 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006720 LPFC_SLI_INTF_IF_TYPE_0) {
6721 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6722 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6723 if (rc == MBX_SUCCESS) {
6724 phba->hba_flag |= HBA_RECOVERABLE_UE;
6725 /* Set 1Sec interval to detect UE */
6726 phba->eratt_poll_interval = 1;
6727 phba->sli4_hba.ue_to_sr = bf_get(
6728 lpfc_mbx_set_feature_UESR,
6729 &mboxq->u.mqe.un.set_feature);
6730 phba->sli4_hba.ue_to_rp = bf_get(
6731 lpfc_mbx_set_feature_UERP,
6732 &mboxq->u.mqe.un.set_feature);
6733 }
6734 }
6735
6736 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6737 /* Enable MDS Diagnostics only if the SLI Port supports it */
6738 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6739 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6740 if (rc != MBX_SUCCESS)
6741 phba->mds_diags_support = 0;
6742 }
James Smart572709e2013-07-15 18:32:43 -04006743
James Smartda0436e2009-05-22 14:51:39 -04006744 /*
6745 * Discover the port's supported feature set and match it against the
6746 * hosts requests.
6747 */
6748 lpfc_request_features(phba, mboxq);
6749 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6750 if (unlikely(rc)) {
6751 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006752 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006753 }
6754
6755 /*
6756 * The port must support FCP initiator mode as this is the
6757 * only mode running in the host.
6758 */
6759 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6760 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6761 "0378 No support for fcpi mode.\n");
6762 ftr_rsp++;
6763 }
James Smartfedd3b72011-02-16 12:39:24 -05006764 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6765 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6766 else
6767 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006768 /*
6769 * If the port cannot support the host's requested features
6770 * then turn off the global config parameters to disable the
6771 * feature in the driver. This is not a fatal error.
6772 */
James Smartbf086112011-08-21 21:48:13 -04006773 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6774 if (phba->cfg_enable_bg) {
6775 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6776 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6777 else
6778 ftr_rsp++;
6779 }
James Smartda0436e2009-05-22 14:51:39 -04006780
6781 if (phba->max_vpi && phba->cfg_enable_npiv &&
6782 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6783 ftr_rsp++;
6784
6785 if (ftr_rsp) {
6786 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6787 "0379 Feature Mismatch Data: x%08x %08x "
6788 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6789 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6790 phba->cfg_enable_npiv, phba->max_vpi);
6791 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6792 phba->cfg_enable_bg = 0;
6793 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6794 phba->cfg_enable_npiv = 0;
6795 }
6796
6797 /* These SLI3 features are assumed in SLI4 */
6798 spin_lock_irq(&phba->hbalock);
6799 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6800 spin_unlock_irq(&phba->hbalock);
6801
James Smart6d368e52011-05-24 11:44:12 -04006802 /*
6803 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6804 * calls depends on these resources to complete port setup.
6805 */
6806 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6807 if (rc) {
6808 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6809 "2920 Failed to alloc Resource IDs "
6810 "rc = x%x\n", rc);
6811 goto out_free_mbox;
6812 }
6813
James Smart61bda8f2016-10-13 15:06:05 -07006814 lpfc_set_host_data(phba, mboxq);
6815
6816 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6817 if (rc) {
6818 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6819 "2134 Failed to set host os driver version %x",
6820 rc);
6821 }
6822
James Smartda0436e2009-05-22 14:51:39 -04006823 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006824 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6825 if (rc) {
6826 phba->link_state = LPFC_HBA_ERROR;
6827 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006828 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006829 }
6830
James Smartda0436e2009-05-22 14:51:39 -04006831 mboxq->vport = vport;
6832 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6833 mp = (struct lpfc_dmabuf *) mboxq->context1;
6834 if (rc == MBX_SUCCESS) {
6835 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6836 rc = 0;
6837 }
6838
6839 /*
6840 * This memory was allocated by the lpfc_read_sparam routine. Release
6841 * it to the mbuf pool.
6842 */
6843 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6844 kfree(mp);
6845 mboxq->context1 = NULL;
6846 if (unlikely(rc)) {
6847 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6848 "0382 READ_SPARAM command failed "
6849 "status %d, mbxStatus x%x\n",
6850 rc, bf_get(lpfc_mqe_status, mqe));
6851 phba->link_state = LPFC_HBA_ERROR;
6852 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006853 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006854 }
6855
James Smart05580562011-05-24 11:40:48 -04006856 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006857
6858 /* Update the fc_host data structures with new wwn. */
6859 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6860 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6861
James Smart895427b2017-02-12 13:52:30 -08006862 /* Create all the SLI4 queues */
6863 rc = lpfc_sli4_queue_create(phba);
6864 if (rc) {
6865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6866 "3089 Failed to allocate queues\n");
6867 rc = -ENODEV;
6868 goto out_free_mbox;
6869 }
6870 /* Set up all the queues to the device */
6871 rc = lpfc_sli4_queue_setup(phba);
6872 if (unlikely(rc)) {
6873 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6874 "0381 Error %d during queue setup.\n ", rc);
6875 goto out_stop_timers;
6876 }
6877 /* Initialize the driver internal SLI layer lists. */
6878 lpfc_sli4_setup(phba);
6879 lpfc_sli4_queue_init(phba);
6880
6881 /* update host els xri-sgl sizes and mappings */
6882 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006883 if (unlikely(rc)) {
6884 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6885 "1400 Failed to update xri-sgl size and "
6886 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08006887 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006888 }
6889
James Smart8a9d2e82012-05-09 21:16:12 -04006890 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08006891 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6892 phba->sli4_hba.els_xri_cnt);
6893 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04006894 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6895 "0582 Error %d during els sgl post "
6896 "operation\n", rc);
6897 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006898 goto out_destroy_queue;
6899 }
6900 phba->sli4_hba.els_xri_cnt = rc;
6901
James Smartf358dd02017-02-12 13:52:34 -08006902 if (phba->nvmet_support) {
6903 /* update host nvmet xri-sgl sizes and mappings */
6904 rc = lpfc_sli4_nvmet_sgl_update(phba);
6905 if (unlikely(rc)) {
6906 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6907 "6308 Failed to update nvmet-sgl size "
6908 "and mapping: %d\n", rc);
6909 goto out_destroy_queue;
6910 }
6911
6912 /* register the nvmet sgl pool to the port */
6913 rc = lpfc_sli4_repost_sgl_list(
6914 phba,
6915 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6916 phba->sli4_hba.nvmet_xri_cnt);
6917 if (unlikely(rc < 0)) {
6918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6919 "3117 Error %d during nvmet "
6920 "sgl post\n", rc);
6921 rc = -ENODEV;
6922 goto out_destroy_queue;
6923 }
6924 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07006925
6926 cnt = phba->cfg_iocb_cnt * 1024;
6927 /* We need 1 iocbq for every SGL, for IO processing */
6928 cnt += phba->sli4_hba.nvmet_xri_cnt;
6929 /* Initialize and populate the iocb list per host */
6930 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6931 "2821 initialize iocb list %d total %d\n",
6932 phba->cfg_iocb_cnt, cnt);
6933 rc = lpfc_init_iocb_list(phba, cnt);
6934 if (rc) {
6935 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6936 "1413 Failed to init iocb list.\n");
6937 goto out_destroy_queue;
6938 }
6939
James Smartd613b6a2017-02-12 13:52:37 -08006940 lpfc_nvmet_create_targetport(phba);
James Smartf358dd02017-02-12 13:52:34 -08006941 } else {
James Smart895427b2017-02-12 13:52:30 -08006942 /* update host scsi xri-sgl sizes and mappings */
6943 rc = lpfc_sli4_scsi_sgl_update(phba);
6944 if (unlikely(rc)) {
6945 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6946 "6309 Failed to update scsi-sgl size "
6947 "and mapping: %d\n", rc);
6948 goto out_destroy_queue;
6949 }
6950
6951 /* update host nvme xri-sgl sizes and mappings */
6952 rc = lpfc_sli4_nvme_sgl_update(phba);
6953 if (unlikely(rc)) {
6954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6955 "6082 Failed to update nvme-sgl size "
6956 "and mapping: %d\n", rc);
6957 goto out_destroy_queue;
6958 }
James Smart6c621a22017-05-15 15:20:45 -07006959
6960 cnt = phba->cfg_iocb_cnt * 1024;
6961 /* Initialize and populate the iocb list per host */
6962 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6963 "2820 initialize iocb list %d total %d\n",
6964 phba->cfg_iocb_cnt, cnt);
6965 rc = lpfc_init_iocb_list(phba, cnt);
6966 if (rc) {
6967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6968 "6301 Failed to init iocb list.\n");
6969 goto out_destroy_queue;
6970 }
James Smart8a9d2e82012-05-09 21:16:12 -04006971 }
6972
James Smart2d7dbc42017-02-12 13:52:35 -08006973 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08006974 /* Post initial buffers to all RQs created */
6975 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6976 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6977 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6978 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6979 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07006980 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08006981 rqbp->buffer_count = 0;
6982
James Smart2d7dbc42017-02-12 13:52:35 -08006983 lpfc_post_rq_buffer(
6984 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6985 phba->sli4_hba.nvmet_mrq_data[i],
James Smarta8cf5df2017-05-15 15:20:46 -07006986 LPFC_NVMET_RQE_DEF_COUNT, i);
James Smart2d7dbc42017-02-12 13:52:35 -08006987 }
6988 }
6989
James Smart895427b2017-02-12 13:52:30 -08006990 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6991 /* register the allocated scsi sgl pool to the port */
6992 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6993 if (unlikely(rc)) {
6994 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6995 "0383 Error %d during scsi sgl post "
6996 "operation\n", rc);
6997 /* Some Scsi buffers were moved to abort scsi list */
6998 /* A pci function reset will repost them */
6999 rc = -ENODEV;
7000 goto out_destroy_queue;
7001 }
James Smartda0436e2009-05-22 14:51:39 -04007002 }
7003
James Smart01649562017-02-12 13:52:32 -08007004 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7005 (phba->nvmet_support == 0)) {
7006
7007 /* register the allocated nvme sgl pool to the port */
7008 rc = lpfc_repost_nvme_sgl_list(phba);
7009 if (unlikely(rc)) {
7010 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7011 "6116 Error %d during nvme sgl post "
7012 "operation\n", rc);
7013 /* Some NVME buffers were moved to abort nvme list */
7014 /* A pci function reset will repost them */
7015 rc = -ENODEV;
7016 goto out_destroy_queue;
7017 }
James Smartda0436e2009-05-22 14:51:39 -04007018 }
7019
7020 /* Post the rpi header region to the device. */
7021 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7022 if (unlikely(rc)) {
7023 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7024 "0393 Error %d during rpi post operation\n",
7025 rc);
7026 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007027 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007028 }
James Smart97f2ecf2012-03-01 22:35:23 -05007029 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007030
James Smart895427b2017-02-12 13:52:30 -08007031 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007032 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007033 /*
7034 * The FC Port needs to register FCFI (index 0)
7035 */
7036 lpfc_reg_fcfi(phba, mboxq);
7037 mboxq->vport = phba->pport;
7038 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7039 if (rc != MBX_SUCCESS)
7040 goto out_unset_queue;
7041 rc = 0;
7042 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7043 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007044 } else {
7045 /* We are a NVME Target mode with MRQ > 1 */
7046
7047 /* First register the FCFI */
7048 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7049 mboxq->vport = phba->pport;
7050 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7051 if (rc != MBX_SUCCESS)
7052 goto out_unset_queue;
7053 rc = 0;
7054 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7055 &mboxq->u.mqe.un.reg_fcfi_mrq);
7056
7057 /* Next register the MRQs */
7058 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7059 mboxq->vport = phba->pport;
7060 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7061 if (rc != MBX_SUCCESS)
7062 goto out_unset_queue;
7063 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007064 }
7065 /* Check if the port is configured to be disabled */
7066 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007067 }
7068
7069 /* Arm the CQs and then EQs on device */
7070 lpfc_sli4_arm_cqeq_intr(phba);
7071
7072 /* Indicate device interrupt mode */
7073 phba->sli4_hba.intr_enable = 1;
7074
7075 /* Allow asynchronous mailbox command to go through */
7076 spin_lock_irq(&phba->hbalock);
7077 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7078 spin_unlock_irq(&phba->hbalock);
7079
7080 /* Post receive buffers to the device */
7081 lpfc_sli4_rb_setup(phba);
7082
James Smartfc2b9892010-02-26 14:15:29 -05007083 /* Reset HBA FCF states after HBA reset */
7084 phba->fcf.fcf_flag = 0;
7085 phba->fcf.current_rec.flag = 0;
7086
James Smartda0436e2009-05-22 14:51:39 -04007087 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007088 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007089 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007090
7091 /* Start heart beat timer */
7092 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007093 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007094 phba->hb_outstanding = 0;
7095 phba->last_completion_time = jiffies;
7096
7097 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007098 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007099 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007100
James Smart75baf692010-06-08 18:31:21 -04007101 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7102 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7103 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7104 if (!rc) {
7105 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7106 "2829 This device supports "
7107 "Advanced Error Reporting (AER)\n");
7108 spin_lock_irq(&phba->hbalock);
7109 phba->hba_flag |= HBA_AER_ENABLED;
7110 spin_unlock_irq(&phba->hbalock);
7111 } else {
7112 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7113 "2830 This device does not support "
7114 "Advanced Error Reporting (AER)\n");
7115 phba->cfg_aer_support = 0;
7116 }
James Smart0a96e972011-07-22 18:37:28 -04007117 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007118 }
7119
James Smartda0436e2009-05-22 14:51:39 -04007120 /*
7121 * The port is ready, set the host's link state to LINK_DOWN
7122 * in preparation for link interrupts.
7123 */
James Smartda0436e2009-05-22 14:51:39 -04007124 spin_lock_irq(&phba->hbalock);
7125 phba->link_state = LPFC_LINK_DOWN;
7126 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007127 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7128 (phba->hba_flag & LINK_DISABLED)) {
7129 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7130 "3103 Adapter Link is disabled.\n");
7131 lpfc_down_link(phba, mboxq);
7132 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7133 if (rc != MBX_SUCCESS) {
7134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7135 "3104 Adapter failed to issue "
7136 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7137 goto out_unset_queue;
7138 }
7139 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007140 /* don't perform init_link on SLI4 FC port loopback test */
7141 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7142 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7143 if (rc)
7144 goto out_unset_queue;
7145 }
James Smart5350d872011-10-10 21:33:49 -04007146 }
7147 mempool_free(mboxq, phba->mbox_mem_pool);
7148 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007149out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007150 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007151 lpfc_sli4_queue_unset(phba);
7152out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007153 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007154 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007155out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007156 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007157out_free_mbox:
7158 mempool_free(mboxq, phba->mbox_mem_pool);
7159 return rc;
7160}
James Smarte59058c2008-08-24 21:49:00 -04007161
7162/**
James Smart3621a712009-04-06 18:47:14 -04007163 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007164 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007165 *
James Smarte59058c2008-08-24 21:49:00 -04007166 * This is the callback function for mailbox timer. The mailbox
7167 * timer is armed when a new mailbox command is issued and the timer
7168 * is deleted when the mailbox complete. The function is called by
7169 * the kernel timer code when a mailbox does not complete within
7170 * expected time. This function wakes up the worker thread to
7171 * process the mailbox timeout and returns. All the processing is
7172 * done by the worker thread function lpfc_mbox_timeout_handler.
7173 **/
dea31012005-04-17 16:05:31 -05007174void
7175lpfc_mbox_timeout(unsigned long ptr)
7176{
James Smart92d7f7b2007-06-17 19:56:38 -05007177 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05007178 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007179 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007180
James Smart2e0fef82007-06-17 19:56:36 -05007181 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007182 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007183 if (!tmo_posted)
7184 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7185 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7186
James Smart5e9d9b82008-06-14 22:52:53 -04007187 if (!tmo_posted)
7188 lpfc_worker_wake_up(phba);
7189 return;
dea31012005-04-17 16:05:31 -05007190}
7191
James Smarte8d3c3b2013-10-10 12:21:30 -04007192/**
7193 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7194 * are pending
7195 * @phba: Pointer to HBA context object.
7196 *
7197 * This function checks if any mailbox completions are present on the mailbox
7198 * completion queue.
7199 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007200static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007201lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7202{
7203
7204 uint32_t idx;
7205 struct lpfc_queue *mcq;
7206 struct lpfc_mcqe *mcqe;
7207 bool pending_completions = false;
7208
7209 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7210 return false;
7211
7212 /* Check for completions on mailbox completion queue */
7213
7214 mcq = phba->sli4_hba.mbx_cq;
7215 idx = mcq->hba_index;
7216 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7217 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7218 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7219 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7220 pending_completions = true;
7221 break;
7222 }
7223 idx = (idx + 1) % mcq->entry_count;
7224 if (mcq->hba_index == idx)
7225 break;
7226 }
7227 return pending_completions;
7228
7229}
7230
7231/**
7232 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7233 * that were missed.
7234 * @phba: Pointer to HBA context object.
7235 *
7236 * For sli4, it is possible to miss an interrupt. As such mbox completions
7237 * maybe missed causing erroneous mailbox timeouts to occur. This function
7238 * checks to see if mbox completions are on the mailbox completion queue
7239 * and will process all the completions associated with the eq for the
7240 * mailbox completion queue.
7241 **/
7242bool
7243lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7244{
7245
7246 uint32_t eqidx;
7247 struct lpfc_queue *fpeq = NULL;
7248 struct lpfc_eqe *eqe;
7249 bool mbox_pending;
7250
7251 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7252 return false;
7253
7254 /* Find the eq associated with the mcq */
7255
7256 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007257 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007258 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7259 phba->sli4_hba.mbx_cq->assoc_qid) {
7260 fpeq = phba->sli4_hba.hba_eq[eqidx];
7261 break;
7262 }
7263 if (!fpeq)
7264 return false;
7265
7266 /* Turn off interrupts from this EQ */
7267
7268 lpfc_sli4_eq_clr_intr(fpeq);
7269
7270 /* Check to see if a mbox completion is pending */
7271
7272 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7273
7274 /*
7275 * If a mbox completion is pending, process all the events on EQ
7276 * associated with the mbox completion queue (this could include
7277 * mailbox commands, async events, els commands, receive queue data
7278 * and fcp commands)
7279 */
7280
7281 if (mbox_pending)
7282 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7283 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7284 fpeq->EQ_processed++;
7285 }
7286
7287 /* Always clear and re-arm the EQ */
7288
7289 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7290
7291 return mbox_pending;
7292
7293}
James Smarte59058c2008-08-24 21:49:00 -04007294
7295/**
James Smart3621a712009-04-06 18:47:14 -04007296 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007297 * @phba: Pointer to HBA context object.
7298 *
7299 * This function is called from worker thread when a mailbox command times out.
7300 * The caller is not required to hold any locks. This function will reset the
7301 * HBA and recover all the pending commands.
7302 **/
dea31012005-04-17 16:05:31 -05007303void
7304lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7305{
James Smart2e0fef82007-06-17 19:56:36 -05007306 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007307 MAILBOX_t *mb = NULL;
7308
James Smart1dcb58e2007-04-25 09:51:30 -04007309 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007310
James Smarte8d3c3b2013-10-10 12:21:30 -04007311 /* If the mailbox completed, process the completion and return */
7312 if (lpfc_sli4_process_missed_mbox_completions(phba))
7313 return;
7314
James Smarteb016562014-09-03 12:58:06 -04007315 if (pmbox != NULL)
7316 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007317 /* Check the pmbox pointer first. There is a race condition
7318 * between the mbox timeout handler getting executed in the
7319 * worklist and the mailbox actually completing. When this
7320 * race condition occurs, the mbox_active will be NULL.
7321 */
7322 spin_lock_irq(&phba->hbalock);
7323 if (pmbox == NULL) {
7324 lpfc_printf_log(phba, KERN_WARNING,
7325 LOG_MBOX | LOG_SLI,
7326 "0353 Active Mailbox cleared - mailbox timeout "
7327 "exiting\n");
7328 spin_unlock_irq(&phba->hbalock);
7329 return;
7330 }
7331
dea31012005-04-17 16:05:31 -05007332 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007333 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007334 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007335 mb->mbxCommand,
7336 phba->pport->port_state,
7337 phba->sli.sli_flag,
7338 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007339 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007340
James Smart1dcb58e2007-04-25 09:51:30 -04007341 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7342 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007343 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007344 */
James Smart2e0fef82007-06-17 19:56:36 -05007345 spin_lock_irq(&phba->pport->work_port_lock);
7346 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7347 spin_unlock_irq(&phba->pport->work_port_lock);
7348 spin_lock_irq(&phba->hbalock);
7349 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007350 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007351 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007352
James Smartdb55fba2014-04-04 13:52:02 -04007353 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007354
7355 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007356 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007357
7358 /* Reset the HBA device */
7359 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007360}
7361
James Smarte59058c2008-08-24 21:49:00 -04007362/**
James Smart3772a992009-05-22 14:50:54 -04007363 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007364 * @phba: Pointer to HBA context object.
7365 * @pmbox: Pointer to mailbox object.
7366 * @flag: Flag indicating how the mailbox need to be processed.
7367 *
7368 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007369 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7370 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007371 * The mailbox command can be submitted in polling mode, in which case
7372 * this function will wait in a polling loop for the completion of the
7373 * mailbox.
7374 * If the mailbox is submitted in no_wait mode (not polling) the
7375 * function will submit the command and returns immediately without waiting
7376 * for the mailbox completion. The no_wait is supported only when HBA
7377 * is in SLI2/SLI3 mode - interrupts are enabled.
7378 * The SLI interface allows only one mailbox pending at a time. If the
7379 * mailbox is issued in polling mode and there is already a mailbox
7380 * pending, then the function will return an error. If the mailbox is issued
7381 * in NO_WAIT mode and there is a mailbox pending already, the function
7382 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7383 * The sli layer owns the mailbox object until the completion of mailbox
7384 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7385 * return codes the caller owns the mailbox command after the return of
7386 * the function.
7387 **/
James Smart3772a992009-05-22 14:50:54 -04007388static int
7389lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7390 uint32_t flag)
dea31012005-04-17 16:05:31 -05007391{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007392 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007393 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007394 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007395 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007396 int i;
James Smart09372822008-01-11 01:52:54 -05007397 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007398 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007399 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007400 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007401 int processing_queue = 0;
7402
7403 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7404 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007405 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007406 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007407 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7408 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7409 return MBX_SUCCESS;
7410 }
James Smart58da1ff2008-04-07 10:15:56 -04007411 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007412 pmbox = lpfc_mbox_get(phba);
7413 if (!pmbox) {
7414 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7415 return MBX_SUCCESS;
7416 }
7417 }
dea31012005-04-17 16:05:31 -05007418
James Smarted957682007-06-17 19:56:37 -05007419 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007420 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007421 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007422 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007423 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007424 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007425 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007426 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007427 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007428 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007429 }
7430 }
7431
Linas Vepstas8d63f372007-02-14 14:28:36 -06007432 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007433 if (unlikely(pci_channel_offline(phba->pcidev))) {
7434 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7435 goto out_not_finished;
7436 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007437
James Smarta257bf92009-04-06 18:48:10 -04007438 /* If HBA has a deferred error attention, fail the iocb. */
7439 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7440 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7441 goto out_not_finished;
7442 }
7443
dea31012005-04-17 16:05:31 -05007444 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007445
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007446 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007447 status = MBX_SUCCESS;
7448
James Smart2e0fef82007-06-17 19:56:36 -05007449 if (phba->link_state == LPFC_HBA_ERROR) {
7450 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007451
7452 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007453 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7454 "(%d):0311 Mailbox command x%x cannot "
7455 "issue Data: x%x x%x\n",
7456 pmbox->vport ? pmbox->vport->vpi : 0,
7457 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007458 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007459 }
7460
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007461 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007462 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7463 !(hc_copy & HC_MBINT_ENA)) {
7464 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7465 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007466 "(%d):2528 Mailbox command x%x cannot "
7467 "issue Data: x%x x%x\n",
7468 pmbox->vport ? pmbox->vport->vpi : 0,
7469 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007470 goto out_not_finished;
7471 }
James Smart92908312006-03-07 15:04:13 -05007472 }
7473
dea31012005-04-17 16:05:31 -05007474 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7475 /* Polling for a mbox command when another one is already active
7476 * is not allowed in SLI. Also, the driver must have established
7477 * SLI2 mode to queue and process multiple mbox commands.
7478 */
7479
7480 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007481 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007482
7483 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007484 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7485 "(%d):2529 Mailbox command x%x "
7486 "cannot issue Data: x%x x%x\n",
7487 pmbox->vport ? pmbox->vport->vpi : 0,
7488 pmbox->u.mb.mbxCommand,
7489 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007490 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007491 }
7492
James Smart3772a992009-05-22 14:50:54 -04007493 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007494 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007495 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007496 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7497 "(%d):2530 Mailbox command x%x "
7498 "cannot issue Data: x%x x%x\n",
7499 pmbox->vport ? pmbox->vport->vpi : 0,
7500 pmbox->u.mb.mbxCommand,
7501 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007502 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007503 }
7504
dea31012005-04-17 16:05:31 -05007505 /* Another mailbox command is still being processed, queue this
7506 * command to be processed later.
7507 */
7508 lpfc_mbox_put(phba, pmbox);
7509
7510 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007511 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007512 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007513 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007514 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007515 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007516 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007517
7518 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007519 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007520
James Smart858c9f62007-06-17 19:56:39 -05007521 if (pmbox->vport) {
7522 lpfc_debugfs_disc_trc(pmbox->vport,
7523 LPFC_DISC_TRC_MBOX_VPORT,
7524 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007525 (uint32_t)mbx->mbxCommand,
7526 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007527 }
7528 else {
7529 lpfc_debugfs_disc_trc(phba->pport,
7530 LPFC_DISC_TRC_MBOX,
7531 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007532 (uint32_t)mbx->mbxCommand,
7533 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007534 }
7535
James Smart2e0fef82007-06-17 19:56:36 -05007536 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007537 }
7538
dea31012005-04-17 16:05:31 -05007539 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7540
7541 /* If we are not polling, we MUST be in SLI2 mode */
7542 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007543 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007544 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007545 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007546 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007547 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007548 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7549 "(%d):2531 Mailbox command x%x "
7550 "cannot issue Data: x%x x%x\n",
7551 pmbox->vport ? pmbox->vport->vpi : 0,
7552 pmbox->u.mb.mbxCommand,
7553 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007554 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007555 }
7556 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007557 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7558 1000);
7559 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007560 }
7561
7562 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007563 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007564 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007565 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007566 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007567 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007568 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007569
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007570 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007571 if (pmbox->vport) {
7572 lpfc_debugfs_disc_trc(pmbox->vport,
7573 LPFC_DISC_TRC_MBOX_VPORT,
7574 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007575 (uint32_t)mbx->mbxCommand,
7576 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007577 }
7578 else {
7579 lpfc_debugfs_disc_trc(phba->pport,
7580 LPFC_DISC_TRC_MBOX,
7581 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007582 (uint32_t)mbx->mbxCommand,
7583 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007584 }
7585 }
7586
dea31012005-04-17 16:05:31 -05007587 psli->slistat.mbox_cmd++;
7588 evtctr = psli->slistat.mbox_event;
7589
7590 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007591 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007592
James Smart3772a992009-05-22 14:50:54 -04007593 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007594 /* Populate mbox extension offset word. */
7595 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007596 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007597 = (uint8_t *)phba->mbox_ext
7598 - (uint8_t *)phba->mbox;
7599 }
7600
7601 /* Copy the mailbox extension data */
7602 if (pmbox->in_ext_byte_len && pmbox->context2) {
7603 lpfc_sli_pcimem_bcopy(pmbox->context2,
7604 (uint8_t *)phba->mbox_ext,
7605 pmbox->in_ext_byte_len);
7606 }
7607 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007608 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007609 } else {
James Smart7a470272010-03-15 11:25:20 -04007610 /* Populate mbox extension offset word. */
7611 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007612 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007613 = MAILBOX_HBA_EXT_OFFSET;
7614
7615 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007616 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007617 lpfc_memcpy_to_slim(phba->MBslimaddr +
7618 MAILBOX_HBA_EXT_OFFSET,
7619 pmbox->context2, pmbox->in_ext_byte_len);
7620
James Smart895427b2017-02-12 13:52:30 -08007621 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007622 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007623 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7624 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007625
7626 /* First copy mbox command data to HBA SLIM, skip past first
7627 word */
7628 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007629 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007630 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7631
7632 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007633 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007634 to_slim = phba->MBslimaddr;
7635 writel(ldata, to_slim);
7636 readl(to_slim); /* flush */
7637
James Smart895427b2017-02-12 13:52:30 -08007638 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007639 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007640 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007641 }
7642
7643 wmb();
dea31012005-04-17 16:05:31 -05007644
7645 switch (flag) {
7646 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007647 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007648 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007649 /* Interrupt board to do it */
7650 writel(CA_MBATT, phba->CAregaddr);
7651 readl(phba->CAregaddr); /* flush */
7652 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007653 break;
7654
7655 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007656 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007657 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007658 /* Interrupt board to do it */
7659 writel(CA_MBATT, phba->CAregaddr);
7660 readl(phba->CAregaddr); /* flush */
7661
James Smart3772a992009-05-22 14:50:54 -04007662 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007663 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007664 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007665 word0 = le32_to_cpu(word0);
7666 } else {
7667 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007668 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7669 spin_unlock_irqrestore(&phba->hbalock,
7670 drvr_flag);
7671 goto out_not_finished;
7672 }
dea31012005-04-17 16:05:31 -05007673 }
7674
7675 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007676 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7677 spin_unlock_irqrestore(&phba->hbalock,
7678 drvr_flag);
7679 goto out_not_finished;
7680 }
James Smarta183a152011-10-10 21:32:43 -04007681 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7682 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007683 i = 0;
dea31012005-04-17 16:05:31 -05007684 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007685 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7686 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007687 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007688 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007689 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007690 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007691 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007692 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007693 }
7694
7695 /* Check if we took a mbox interrupt while we were
7696 polling */
7697 if (((word0 & OWN_CHIP) != OWN_CHIP)
7698 && (evtctr != psli->slistat.mbox_event))
7699 break;
7700
James Smart09372822008-01-11 01:52:54 -05007701 if (i++ > 10) {
7702 spin_unlock_irqrestore(&phba->hbalock,
7703 drvr_flag);
7704 msleep(1);
7705 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7706 }
dea31012005-04-17 16:05:31 -05007707
James Smart3772a992009-05-22 14:50:54 -04007708 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007709 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007710 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007711 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007712 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007713 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007714 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007715 /* Check real SLIM for any errors */
7716 slimword0 = readl(phba->MBslimaddr);
7717 slimmb = (MAILBOX_t *) & slimword0;
7718 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7719 && slimmb->mbxStatus) {
7720 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007721 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007722 word0 = slimword0;
7723 }
7724 }
7725 } else {
7726 /* First copy command data */
7727 word0 = readl(phba->MBslimaddr);
7728 }
7729 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007730 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7731 spin_unlock_irqrestore(&phba->hbalock,
7732 drvr_flag);
7733 goto out_not_finished;
7734 }
dea31012005-04-17 16:05:31 -05007735 }
7736
James Smart3772a992009-05-22 14:50:54 -04007737 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007738 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007739 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7740 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007741 /* Copy the mailbox extension data */
7742 if (pmbox->out_ext_byte_len && pmbox->context2) {
7743 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7744 pmbox->context2,
7745 pmbox->out_ext_byte_len);
7746 }
dea31012005-04-17 16:05:31 -05007747 } else {
7748 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007749 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007750 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007751 /* Copy the mailbox extension data */
7752 if (pmbox->out_ext_byte_len && pmbox->context2) {
7753 lpfc_memcpy_from_slim(pmbox->context2,
7754 phba->MBslimaddr +
7755 MAILBOX_HBA_EXT_OFFSET,
7756 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007757 }
7758 }
7759
7760 writel(HA_MBATT, phba->HAregaddr);
7761 readl(phba->HAregaddr); /* flush */
7762
7763 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007764 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007765 }
7766
James Smart2e0fef82007-06-17 19:56:36 -05007767 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7768 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007769
7770out_not_finished:
7771 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007772 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007773 lpfc_mbox_cmpl_put(phba, pmbox);
7774 }
7775 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007776}
7777
James Smarte59058c2008-08-24 21:49:00 -04007778/**
James Smartf1126682009-06-10 17:22:44 -04007779 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7780 * @phba: Pointer to HBA context object.
7781 *
7782 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7783 * the driver internal pending mailbox queue. It will then try to wait out the
7784 * possible outstanding mailbox command before return.
7785 *
7786 * Returns:
7787 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7788 * the outstanding mailbox command timed out.
7789 **/
7790static int
7791lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7792{
7793 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007794 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007795 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007796
7797 /* Mark the asynchronous mailbox command posting as blocked */
7798 spin_lock_irq(&phba->hbalock);
7799 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007800 /* Determine how long we might wait for the active mailbox
7801 * command to be gracefully completed by firmware.
7802 */
James Smarta183a152011-10-10 21:32:43 -04007803 if (phba->sli.mbox_active)
7804 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7805 phba->sli.mbox_active) *
7806 1000) + jiffies;
7807 spin_unlock_irq(&phba->hbalock);
7808
James Smarte8d3c3b2013-10-10 12:21:30 -04007809 /* Make sure the mailbox is really active */
7810 if (timeout)
7811 lpfc_sli4_process_missed_mbox_completions(phba);
7812
James Smartf1126682009-06-10 17:22:44 -04007813 /* Wait for the outstnading mailbox command to complete */
7814 while (phba->sli.mbox_active) {
7815 /* Check active mailbox complete status every 2ms */
7816 msleep(2);
7817 if (time_after(jiffies, timeout)) {
7818 /* Timeout, marked the outstanding cmd not complete */
7819 rc = 1;
7820 break;
7821 }
7822 }
7823
7824 /* Can not cleanly block async mailbox command, fails it */
7825 if (rc) {
7826 spin_lock_irq(&phba->hbalock);
7827 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7828 spin_unlock_irq(&phba->hbalock);
7829 }
7830 return rc;
7831}
7832
7833/**
7834 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7835 * @phba: Pointer to HBA context object.
7836 *
7837 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7838 * commands from the driver internal pending mailbox queue. It makes sure
7839 * that there is no outstanding mailbox command before resuming posting
7840 * asynchronous mailbox commands. If, for any reason, there is outstanding
7841 * mailbox command, it will try to wait it out before resuming asynchronous
7842 * mailbox command posting.
7843 **/
7844static void
7845lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7846{
7847 struct lpfc_sli *psli = &phba->sli;
7848
7849 spin_lock_irq(&phba->hbalock);
7850 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7851 /* Asynchronous mailbox posting is not blocked, do nothing */
7852 spin_unlock_irq(&phba->hbalock);
7853 return;
7854 }
7855
7856 /* Outstanding synchronous mailbox command is guaranteed to be done,
7857 * successful or timeout, after timing-out the outstanding mailbox
7858 * command shall always be removed, so just unblock posting async
7859 * mailbox command and resume
7860 */
7861 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7862 spin_unlock_irq(&phba->hbalock);
7863
7864 /* wake up worker thread to post asynchronlous mailbox command */
7865 lpfc_worker_wake_up(phba);
7866}
7867
7868/**
James Smart2d843ed2012-09-29 11:29:06 -04007869 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7870 * @phba: Pointer to HBA context object.
7871 * @mboxq: Pointer to mailbox object.
7872 *
7873 * The function waits for the bootstrap mailbox register ready bit from
7874 * port for twice the regular mailbox command timeout value.
7875 *
7876 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7877 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7878 **/
7879static int
7880lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7881{
7882 uint32_t db_ready;
7883 unsigned long timeout;
7884 struct lpfc_register bmbx_reg;
7885
7886 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7887 * 1000) + jiffies;
7888
7889 do {
7890 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7891 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7892 if (!db_ready)
7893 msleep(2);
7894
7895 if (time_after(jiffies, timeout))
7896 return MBXERR_ERROR;
7897 } while (!db_ready);
7898
7899 return 0;
7900}
7901
7902/**
James Smartda0436e2009-05-22 14:51:39 -04007903 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7904 * @phba: Pointer to HBA context object.
7905 * @mboxq: Pointer to mailbox object.
7906 *
7907 * The function posts a mailbox to the port. The mailbox is expected
7908 * to be comletely filled in and ready for the port to operate on it.
7909 * This routine executes a synchronous completion operation on the
7910 * mailbox by polling for its completion.
7911 *
7912 * The caller must not be holding any locks when calling this routine.
7913 *
7914 * Returns:
7915 * MBX_SUCCESS - mailbox posted successfully
7916 * Any of the MBX error values.
7917 **/
7918static int
7919lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7920{
7921 int rc = MBX_SUCCESS;
7922 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007923 uint32_t mcqe_status;
7924 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007925 struct lpfc_sli *psli = &phba->sli;
7926 struct lpfc_mqe *mb = &mboxq->u.mqe;
7927 struct lpfc_bmbx_create *mbox_rgn;
7928 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007929
7930 /*
7931 * Only one mailbox can be active to the bootstrap mailbox region
7932 * at a time and there is no queueing provided.
7933 */
7934 spin_lock_irqsave(&phba->hbalock, iflag);
7935 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7936 spin_unlock_irqrestore(&phba->hbalock, iflag);
7937 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007938 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007939 "cannot issue Data: x%x x%x\n",
7940 mboxq->vport ? mboxq->vport->vpi : 0,
7941 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007942 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7943 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007944 psli->sli_flag, MBX_POLL);
7945 return MBXERR_ERROR;
7946 }
7947 /* The server grabs the token and owns it until release */
7948 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7949 phba->sli.mbox_active = mboxq;
7950 spin_unlock_irqrestore(&phba->hbalock, iflag);
7951
James Smart2d843ed2012-09-29 11:29:06 -04007952 /* wait for bootstrap mbox register for readyness */
7953 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7954 if (rc)
7955 goto exit;
7956
James Smartda0436e2009-05-22 14:51:39 -04007957 /*
7958 * Initialize the bootstrap memory region to avoid stale data areas
7959 * in the mailbox post. Then copy the caller's mailbox contents to
7960 * the bmbx mailbox region.
7961 */
7962 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7963 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7964 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7965 sizeof(struct lpfc_mqe));
7966
7967 /* Post the high mailbox dma address to the port and wait for ready. */
7968 dma_address = &phba->sli4_hba.bmbx.dma_address;
7969 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7970
James Smart2d843ed2012-09-29 11:29:06 -04007971 /* wait for bootstrap mbox register for hi-address write done */
7972 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7973 if (rc)
7974 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007975
7976 /* Post the low mailbox dma address to the port. */
7977 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007978
James Smart2d843ed2012-09-29 11:29:06 -04007979 /* wait for bootstrap mbox register for low address write done */
7980 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7981 if (rc)
7982 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007983
7984 /*
7985 * Read the CQ to ensure the mailbox has completed.
7986 * If so, update the mailbox status so that the upper layers
7987 * can complete the request normally.
7988 */
7989 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7990 sizeof(struct lpfc_mqe));
7991 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7992 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7993 sizeof(struct lpfc_mcqe));
7994 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007995 /*
7996 * When the CQE status indicates a failure and the mailbox status
7997 * indicates success then copy the CQE status into the mailbox status
7998 * (and prefix it with x4000).
7999 */
James Smartda0436e2009-05-22 14:51:39 -04008000 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008001 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8002 bf_set(lpfc_mqe_status, mb,
8003 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008004 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008005 } else
8006 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008007
8008 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008009 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008010 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8011 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008012 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8013 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8014 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008015 bf_get(lpfc_mqe_status, mb),
8016 mb->un.mb_words[0], mb->un.mb_words[1],
8017 mb->un.mb_words[2], mb->un.mb_words[3],
8018 mb->un.mb_words[4], mb->un.mb_words[5],
8019 mb->un.mb_words[6], mb->un.mb_words[7],
8020 mb->un.mb_words[8], mb->un.mb_words[9],
8021 mb->un.mb_words[10], mb->un.mb_words[11],
8022 mb->un.mb_words[12], mboxq->mcqe.word0,
8023 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8024 mboxq->mcqe.trailer);
8025exit:
8026 /* We are holding the token, no needed for lock when release */
8027 spin_lock_irqsave(&phba->hbalock, iflag);
8028 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8029 phba->sli.mbox_active = NULL;
8030 spin_unlock_irqrestore(&phba->hbalock, iflag);
8031 return rc;
8032}
8033
8034/**
8035 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8036 * @phba: Pointer to HBA context object.
8037 * @pmbox: Pointer to mailbox object.
8038 * @flag: Flag indicating how the mailbox need to be processed.
8039 *
8040 * This function is called by discovery code and HBA management code to submit
8041 * a mailbox command to firmware with SLI-4 interface spec.
8042 *
8043 * Return codes the caller owns the mailbox command after the return of the
8044 * function.
8045 **/
8046static int
8047lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8048 uint32_t flag)
8049{
8050 struct lpfc_sli *psli = &phba->sli;
8051 unsigned long iflags;
8052 int rc;
8053
James Smartb76f2dc2011-07-22 18:37:42 -04008054 /* dump from issue mailbox command if setup */
8055 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8056
James Smart8fa38512009-07-19 10:01:03 -04008057 rc = lpfc_mbox_dev_check(phba);
8058 if (unlikely(rc)) {
8059 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008060 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008061 "cannot issue Data: x%x x%x\n",
8062 mboxq->vport ? mboxq->vport->vpi : 0,
8063 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008064 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8065 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008066 psli->sli_flag, flag);
8067 goto out_not_finished;
8068 }
8069
James Smartda0436e2009-05-22 14:51:39 -04008070 /* Detect polling mode and jump to a handler */
8071 if (!phba->sli4_hba.intr_enable) {
8072 if (flag == MBX_POLL)
8073 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8074 else
8075 rc = -EIO;
8076 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008077 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008078 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008079 "(x%x/x%x) failure: "
8080 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8081 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008082 mboxq->vport ? mboxq->vport->vpi : 0,
8083 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008084 lpfc_sli_config_mbox_subsys_get(phba,
8085 mboxq),
8086 lpfc_sli_config_mbox_opcode_get(phba,
8087 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008088 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8089 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8090 bf_get(lpfc_mcqe_ext_status,
8091 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008092 psli->sli_flag, flag);
8093 return rc;
8094 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008095 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8096 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04008097 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04008098 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008099 mboxq->vport ? mboxq->vport->vpi : 0,
8100 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008101 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8102 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008103 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008104 /* Try to block the asynchronous mailbox posting */
8105 rc = lpfc_sli4_async_mbox_block(phba);
8106 if (!rc) {
8107 /* Successfully blocked, now issue sync mbox cmd */
8108 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8109 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008110 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008111 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008112 "(%d):2597 Sync Mailbox command "
8113 "x%x (x%x/x%x) failure: "
8114 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8115 "Data: x%x x%x\n,",
8116 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008117 mboxq->u.mb.mbxCommand,
8118 lpfc_sli_config_mbox_subsys_get(phba,
8119 mboxq),
8120 lpfc_sli_config_mbox_opcode_get(phba,
8121 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008122 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8123 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8124 bf_get(lpfc_mcqe_ext_status,
8125 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008126 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008127 /* Unblock the async mailbox posting afterward */
8128 lpfc_sli4_async_mbox_unblock(phba);
8129 }
8130 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008131 }
8132
8133 /* Now, interrupt mode asynchrous mailbox command */
8134 rc = lpfc_mbox_cmd_check(phba, mboxq);
8135 if (rc) {
8136 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008137 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008138 "cannot issue Data: x%x x%x\n",
8139 mboxq->vport ? mboxq->vport->vpi : 0,
8140 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008141 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8142 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008143 psli->sli_flag, flag);
8144 goto out_not_finished;
8145 }
James Smartda0436e2009-05-22 14:51:39 -04008146
8147 /* Put the mailbox command to the driver internal FIFO */
8148 psli->slistat.mbox_busy++;
8149 spin_lock_irqsave(&phba->hbalock, iflags);
8150 lpfc_mbox_put(phba, mboxq);
8151 spin_unlock_irqrestore(&phba->hbalock, iflags);
8152 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8153 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008154 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008155 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8156 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008157 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8158 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008159 phba->pport->port_state,
8160 psli->sli_flag, MBX_NOWAIT);
8161 /* Wake up worker thread to transport mailbox command from head */
8162 lpfc_worker_wake_up(phba);
8163
8164 return MBX_BUSY;
8165
8166out_not_finished:
8167 return MBX_NOT_FINISHED;
8168}
8169
8170/**
8171 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8172 * @phba: Pointer to HBA context object.
8173 *
8174 * This function is called by worker thread to send a mailbox command to
8175 * SLI4 HBA firmware.
8176 *
8177 **/
8178int
8179lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8180{
8181 struct lpfc_sli *psli = &phba->sli;
8182 LPFC_MBOXQ_t *mboxq;
8183 int rc = MBX_SUCCESS;
8184 unsigned long iflags;
8185 struct lpfc_mqe *mqe;
8186 uint32_t mbx_cmnd;
8187
8188 /* Check interrupt mode before post async mailbox command */
8189 if (unlikely(!phba->sli4_hba.intr_enable))
8190 return MBX_NOT_FINISHED;
8191
8192 /* Check for mailbox command service token */
8193 spin_lock_irqsave(&phba->hbalock, iflags);
8194 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8195 spin_unlock_irqrestore(&phba->hbalock, iflags);
8196 return MBX_NOT_FINISHED;
8197 }
8198 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8199 spin_unlock_irqrestore(&phba->hbalock, iflags);
8200 return MBX_NOT_FINISHED;
8201 }
8202 if (unlikely(phba->sli.mbox_active)) {
8203 spin_unlock_irqrestore(&phba->hbalock, iflags);
8204 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8205 "0384 There is pending active mailbox cmd\n");
8206 return MBX_NOT_FINISHED;
8207 }
8208 /* Take the mailbox command service token */
8209 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8210
8211 /* Get the next mailbox command from head of queue */
8212 mboxq = lpfc_mbox_get(phba);
8213
8214 /* If no more mailbox command waiting for post, we're done */
8215 if (!mboxq) {
8216 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8217 spin_unlock_irqrestore(&phba->hbalock, iflags);
8218 return MBX_SUCCESS;
8219 }
8220 phba->sli.mbox_active = mboxq;
8221 spin_unlock_irqrestore(&phba->hbalock, iflags);
8222
8223 /* Check device readiness for posting mailbox command */
8224 rc = lpfc_mbox_dev_check(phba);
8225 if (unlikely(rc))
8226 /* Driver clean routine will clean up pending mailbox */
8227 goto out_not_finished;
8228
8229 /* Prepare the mbox command to be posted */
8230 mqe = &mboxq->u.mqe;
8231 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8232
8233 /* Start timer for the mbox_tmo and log some mailbox post messages */
8234 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008235 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008236
8237 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008238 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008239 "x%x x%x\n",
8240 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008241 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8242 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008243 phba->pport->port_state, psli->sli_flag);
8244
8245 if (mbx_cmnd != MBX_HEARTBEAT) {
8246 if (mboxq->vport) {
8247 lpfc_debugfs_disc_trc(mboxq->vport,
8248 LPFC_DISC_TRC_MBOX_VPORT,
8249 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8250 mbx_cmnd, mqe->un.mb_words[0],
8251 mqe->un.mb_words[1]);
8252 } else {
8253 lpfc_debugfs_disc_trc(phba->pport,
8254 LPFC_DISC_TRC_MBOX,
8255 "MBOX Send: cmd:x%x mb:x%x x%x",
8256 mbx_cmnd, mqe->un.mb_words[0],
8257 mqe->un.mb_words[1]);
8258 }
8259 }
8260 psli->slistat.mbox_cmd++;
8261
8262 /* Post the mailbox command to the port */
8263 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8264 if (rc != MBX_SUCCESS) {
8265 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008266 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008267 "cannot issue Data: x%x x%x\n",
8268 mboxq->vport ? mboxq->vport->vpi : 0,
8269 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008270 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8271 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008272 psli->sli_flag, MBX_NOWAIT);
8273 goto out_not_finished;
8274 }
8275
8276 return rc;
8277
8278out_not_finished:
8279 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008280 if (phba->sli.mbox_active) {
8281 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8282 __lpfc_mbox_cmpl_put(phba, mboxq);
8283 /* Release the token */
8284 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8285 phba->sli.mbox_active = NULL;
8286 }
James Smartda0436e2009-05-22 14:51:39 -04008287 spin_unlock_irqrestore(&phba->hbalock, iflags);
8288
8289 return MBX_NOT_FINISHED;
8290}
8291
8292/**
8293 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8294 * @phba: Pointer to HBA context object.
8295 * @pmbox: Pointer to mailbox object.
8296 * @flag: Flag indicating how the mailbox need to be processed.
8297 *
8298 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8299 * the API jump table function pointer from the lpfc_hba struct.
8300 *
8301 * Return codes the caller owns the mailbox command after the return of the
8302 * function.
8303 **/
8304int
8305lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8306{
8307 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8308}
8309
8310/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008311 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008312 * @phba: The hba struct for which this call is being executed.
8313 * @dev_grp: The HBA PCI-Device group number.
8314 *
8315 * This routine sets up the mbox interface API function jump table in @phba
8316 * struct.
8317 * Returns: 0 - success, -ENODEV - failure.
8318 **/
8319int
8320lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8321{
8322
8323 switch (dev_grp) {
8324 case LPFC_PCI_DEV_LP:
8325 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8326 phba->lpfc_sli_handle_slow_ring_event =
8327 lpfc_sli_handle_slow_ring_event_s3;
8328 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8329 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8330 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8331 break;
8332 case LPFC_PCI_DEV_OC:
8333 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8334 phba->lpfc_sli_handle_slow_ring_event =
8335 lpfc_sli_handle_slow_ring_event_s4;
8336 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8337 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8338 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8339 break;
8340 default:
8341 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8342 "1420 Invalid HBA PCI-device group: 0x%x\n",
8343 dev_grp);
8344 return -ENODEV;
8345 break;
8346 }
8347 return 0;
8348}
8349
8350/**
James Smart3621a712009-04-06 18:47:14 -04008351 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008352 * @phba: Pointer to HBA context object.
8353 * @pring: Pointer to driver SLI ring object.
8354 * @piocb: Pointer to address of newly added command iocb.
8355 *
8356 * This function is called with hbalock held to add a command
8357 * iocb to the txq when SLI layer cannot submit the command iocb
8358 * to the ring.
8359 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008360void
James Smart92d7f7b2007-06-17 19:56:38 -05008361__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008362 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008363{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008364 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008365 /* Insert the caller's iocb in the txq tail for later processing. */
8366 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008367}
8368
James Smarte59058c2008-08-24 21:49:00 -04008369/**
James Smart3621a712009-04-06 18:47:14 -04008370 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008371 * @phba: Pointer to HBA context object.
8372 * @pring: Pointer to driver SLI ring object.
8373 * @piocb: Pointer to address of newly added command iocb.
8374 *
8375 * This function is called with hbalock held before a new
8376 * iocb is submitted to the firmware. This function checks
8377 * txq to flush the iocbs in txq to Firmware before
8378 * submitting new iocbs to the Firmware.
8379 * If there are iocbs in the txq which need to be submitted
8380 * to firmware, lpfc_sli_next_iocb returns the first element
8381 * of the txq after dequeuing it from txq.
8382 * If there is no iocb in the txq then the function will return
8383 * *piocb and *piocb is set to NULL. Caller needs to check
8384 * *piocb to find if there are more commands in the txq.
8385 **/
dea31012005-04-17 16:05:31 -05008386static struct lpfc_iocbq *
8387lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008388 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008389{
8390 struct lpfc_iocbq * nextiocb;
8391
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008392 lockdep_assert_held(&phba->hbalock);
8393
dea31012005-04-17 16:05:31 -05008394 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8395 if (!nextiocb) {
8396 nextiocb = *piocb;
8397 *piocb = NULL;
8398 }
8399
8400 return nextiocb;
8401}
8402
James Smarte59058c2008-08-24 21:49:00 -04008403/**
James Smart3772a992009-05-22 14:50:54 -04008404 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008405 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008406 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008407 * @piocb: Pointer to command iocb.
8408 * @flag: Flag indicating if this command can be put into txq.
8409 *
James Smart3772a992009-05-22 14:50:54 -04008410 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8411 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8412 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8413 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8414 * this function allows only iocbs for posting buffers. This function finds
8415 * next available slot in the command ring and posts the command to the
8416 * available slot and writes the port attention register to request HBA start
8417 * processing new iocb. If there is no slot available in the ring and
8418 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8419 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008420 *
James Smart3772a992009-05-22 14:50:54 -04008421 * This function is called with hbalock held. The function will return success
8422 * after it successfully submit the iocb to firmware or after adding to the
8423 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008424 **/
James Smart98c9ea52007-10-27 13:37:33 -04008425static int
James Smart3772a992009-05-22 14:50:54 -04008426__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008427 struct lpfc_iocbq *piocb, uint32_t flag)
8428{
8429 struct lpfc_iocbq *nextiocb;
8430 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008431 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008432
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008433 lockdep_assert_held(&phba->hbalock);
8434
James Smart92d7f7b2007-06-17 19:56:38 -05008435 if (piocb->iocb_cmpl && (!piocb->vport) &&
8436 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8437 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8438 lpfc_printf_log(phba, KERN_ERR,
8439 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008440 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008441 piocb->iocb.ulpCommand);
8442 dump_stack();
8443 return IOCB_ERROR;
8444 }
8445
8446
Linas Vepstas8d63f372007-02-14 14:28:36 -06008447 /* If the PCI channel is in offline state, do not post iocbs. */
8448 if (unlikely(pci_channel_offline(phba->pcidev)))
8449 return IOCB_ERROR;
8450
James Smarta257bf92009-04-06 18:48:10 -04008451 /* If HBA has a deferred error attention, fail the iocb. */
8452 if (unlikely(phba->hba_flag & DEFER_ERATT))
8453 return IOCB_ERROR;
8454
dea31012005-04-17 16:05:31 -05008455 /*
8456 * We should never get an IOCB if we are in a < LINK_DOWN state
8457 */
James Smart2e0fef82007-06-17 19:56:36 -05008458 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008459 return IOCB_ERROR;
8460
8461 /*
8462 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008463 * outstanding event.
dea31012005-04-17 16:05:31 -05008464 */
James Smart0b727fe2007-10-27 13:37:25 -04008465 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008466 goto iocb_busy;
8467
James Smart2e0fef82007-06-17 19:56:36 -05008468 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008469 /*
James Smart2680eea2007-04-25 09:52:55 -04008470 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008471 * can be issued if the link is not up.
8472 */
8473 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008474 case CMD_GEN_REQUEST64_CR:
8475 case CMD_GEN_REQUEST64_CX:
8476 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8477 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008478 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008479 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8480 MENLO_TRANSPORT_TYPE))
8481
8482 goto iocb_busy;
8483 break;
dea31012005-04-17 16:05:31 -05008484 case CMD_QUE_RING_BUF_CN:
8485 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008486 /*
8487 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8488 * completion, iocb_cmpl MUST be 0.
8489 */
8490 if (piocb->iocb_cmpl)
8491 piocb->iocb_cmpl = NULL;
8492 /*FALLTHROUGH*/
8493 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008494 case CMD_CLOSE_XRI_CN:
8495 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008496 break;
8497 default:
8498 goto iocb_busy;
8499 }
8500
8501 /*
8502 * For FCP commands, we must be in a state where we can process link
8503 * attention events.
8504 */
James Smart895427b2017-02-12 13:52:30 -08008505 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008506 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008507 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008508 }
dea31012005-04-17 16:05:31 -05008509
dea31012005-04-17 16:05:31 -05008510 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8511 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8512 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8513
8514 if (iocb)
8515 lpfc_sli_update_ring(phba, pring);
8516 else
8517 lpfc_sli_update_full_ring(phba, pring);
8518
8519 if (!piocb)
8520 return IOCB_SUCCESS;
8521
8522 goto out_busy;
8523
8524 iocb_busy:
8525 pring->stats.iocb_cmd_delay++;
8526
8527 out_busy:
8528
8529 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008530 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008531 return IOCB_SUCCESS;
8532 }
8533
8534 return IOCB_BUSY;
8535}
8536
James Smart3772a992009-05-22 14:50:54 -04008537/**
James Smart4f774512009-05-22 14:52:35 -04008538 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8539 * @phba: Pointer to HBA context object.
8540 * @piocb: Pointer to command iocb.
8541 * @sglq: Pointer to the scatter gather queue object.
8542 *
8543 * This routine converts the bpl or bde that is in the IOCB
8544 * to a sgl list for the sli4 hardware. The physical address
8545 * of the bpl/bde is converted back to a virtual address.
8546 * If the IOCB contains a BPL then the list of BDE's is
8547 * converted to sli4_sge's. If the IOCB contains a single
8548 * BDE then it is converted to a single sli_sge.
8549 * The IOCB is still in cpu endianess so the contents of
8550 * the bpl can be used without byte swapping.
8551 *
8552 * Returns valid XRI = Success, NO_XRI = Failure.
8553**/
8554static uint16_t
8555lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8556 struct lpfc_sglq *sglq)
8557{
8558 uint16_t xritag = NO_XRI;
8559 struct ulp_bde64 *bpl = NULL;
8560 struct ulp_bde64 bde;
8561 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008562 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008563 IOCB_t *icmd;
8564 int numBdes = 0;
8565 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008566 uint32_t offset = 0; /* accumulated offset in the sg request list */
8567 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008568
8569 if (!piocbq || !sglq)
8570 return xritag;
8571
8572 sgl = (struct sli4_sge *)sglq->sgl;
8573 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008574 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8575 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008576 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8577 numBdes = icmd->un.genreq64.bdl.bdeSize /
8578 sizeof(struct ulp_bde64);
8579 /* The addrHigh and addrLow fields within the IOCB
8580 * have not been byteswapped yet so there is no
8581 * need to swap them back.
8582 */
James Smart1b511972011-12-13 13:23:09 -05008583 if (piocbq->context3)
8584 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8585 else
8586 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008587
James Smart1b511972011-12-13 13:23:09 -05008588 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008589 if (!bpl)
8590 return xritag;
8591
8592 for (i = 0; i < numBdes; i++) {
8593 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008594 sgl->addr_hi = bpl->addrHigh;
8595 sgl->addr_lo = bpl->addrLow;
8596
James Smart05580562011-05-24 11:40:48 -04008597 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008598 if ((i+1) == numBdes)
8599 bf_set(lpfc_sli4_sge_last, sgl, 1);
8600 else
8601 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008602 /* swap the size field back to the cpu so we
8603 * can assign it to the sgl.
8604 */
8605 bde.tus.w = le32_to_cpu(bpl->tus.w);
8606 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008607 /* The offsets in the sgl need to be accumulated
8608 * separately for the request and reply lists.
8609 * The request is always first, the reply follows.
8610 */
8611 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8612 /* add up the reply sg entries */
8613 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8614 inbound++;
8615 /* first inbound? reset the offset */
8616 if (inbound == 1)
8617 offset = 0;
8618 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008619 bf_set(lpfc_sli4_sge_type, sgl,
8620 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008621 offset += bde.tus.f.bdeSize;
8622 }
James Smart546fc852011-03-11 16:06:29 -05008623 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008624 bpl++;
8625 sgl++;
8626 }
8627 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8628 /* The addrHigh and addrLow fields of the BDE have not
8629 * been byteswapped yet so they need to be swapped
8630 * before putting them in the sgl.
8631 */
8632 sgl->addr_hi =
8633 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8634 sgl->addr_lo =
8635 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008636 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008637 bf_set(lpfc_sli4_sge_last, sgl, 1);
8638 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008639 sgl->sge_len =
8640 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008641 }
8642 return sglq->sli4_xritag;
8643}
8644
8645/**
James Smart4f774512009-05-22 14:52:35 -04008646 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8647 * @phba: Pointer to HBA context object.
8648 * @piocb: Pointer to command iocb.
8649 * @wqe: Pointer to the work queue entry.
8650 *
8651 * This routine converts the iocb command to its Work Queue Entry
8652 * equivalent. The wqe pointer should not have any fields set when
8653 * this routine is called because it will memcpy over them.
8654 * This routine does not set the CQ_ID or the WQEC bits in the
8655 * wqe.
8656 *
8657 * Returns: 0 = Success, IOCB_ERROR = Failure.
8658 **/
8659static int
8660lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8661 union lpfc_wqe *wqe)
8662{
James Smart5ffc2662009-11-18 15:39:44 -05008663 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008664 uint8_t ct = 0;
8665 uint32_t fip;
8666 uint32_t abort_tag;
8667 uint8_t command_type = ELS_COMMAND_NON_FIP;
8668 uint8_t cmnd;
8669 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008670 uint16_t abrt_iotag;
8671 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008672 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008673 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008674 int numBdes, i;
8675 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008676 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008677 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008678 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008679
James Smart45ed1192009-10-02 15:17:02 -04008680 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008681 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008682 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008683 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008684 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008685 command_type = ELS_COMMAND_FIP;
8686 else
8687 command_type = ELS_COMMAND_NON_FIP;
8688
James Smartb5c53952016-03-31 14:12:30 -07008689 if (phba->fcp_embed_io)
8690 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008691 /* Some of the fields are in the right position already */
8692 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008693 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8694 /* The ct field has moved so reset */
8695 wqe->generic.wqe_com.word7 = 0;
8696 wqe->generic.wqe_com.word10 = 0;
8697 }
James Smartb5c53952016-03-31 14:12:30 -07008698
8699 abort_tag = (uint32_t) iocbq->iotag;
8700 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008701 /* words0-2 bpl convert bde */
8702 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008703 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8704 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008705 bpl = (struct ulp_bde64 *)
8706 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8707 if (!bpl)
8708 return IOCB_ERROR;
8709
8710 /* Should already be byte swapped. */
8711 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8712 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8713 /* swap the size field back to the cpu so we
8714 * can assign it to the sgl.
8715 */
8716 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008717 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8718 total_len = 0;
8719 for (i = 0; i < numBdes; i++) {
8720 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8721 total_len += bde.tus.f.bdeSize;
8722 }
James Smart4f774512009-05-22 14:52:35 -04008723 } else
James Smart5ffc2662009-11-18 15:39:44 -05008724 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008725
8726 iocbq->iocb.ulpIoTag = iocbq->iotag;
8727 cmnd = iocbq->iocb.ulpCommand;
8728
8729 switch (iocbq->iocb.ulpCommand) {
8730 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008731 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8732 ndlp = iocbq->context_un.ndlp;
8733 else
8734 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008735 if (!iocbq->iocb.ulpLe) {
8736 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8737 "2007 Only Limited Edition cmd Format"
8738 " supported 0x%x\n",
8739 iocbq->iocb.ulpCommand);
8740 return IOCB_ERROR;
8741 }
James Smartff78d8f2011-12-13 13:21:35 -05008742
James Smart5ffc2662009-11-18 15:39:44 -05008743 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008744 /* Els_reguest64 has a TMO */
8745 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8746 iocbq->iocb.ulpTimeout);
8747 /* Need a VF for word 4 set the vf bit*/
8748 bf_set(els_req64_vf, &wqe->els_req, 0);
8749 /* And a VFID for word 12 */
8750 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008751 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008752 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8753 iocbq->iocb.ulpContext);
8754 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8755 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008756 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008757 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008758 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8759 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008760 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8761 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008762 if_type = bf_get(lpfc_sli_intf_if_type,
8763 &phba->sli4_hba.sli_intf);
8764 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008765 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008766 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008767 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008768 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008769 *pcmd == ELS_CMD_PLOGI)) {
8770 bf_set(els_req64_sp, &wqe->els_req, 1);
8771 bf_set(els_req64_sid, &wqe->els_req,
8772 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008773 if ((*pcmd == ELS_CMD_FLOGI) &&
8774 !(phba->fc_topology ==
8775 LPFC_TOPOLOGY_LOOP))
8776 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008777 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8778 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008779 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008780 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008781 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8782 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8783 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8784 }
James Smartc8685952009-11-18 15:39:16 -05008785 }
James Smart6d368e52011-05-24 11:44:12 -04008786 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8787 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008788 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8789 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8790 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8791 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8792 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8793 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008794 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008795 break;
James Smart5ffc2662009-11-18 15:39:44 -05008796 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008797 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8798 iocbq->iocb.un.ulpWord[3]);
8799 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008800 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008801 /* The entire sequence is transmitted for this IOCB */
8802 xmit_len = total_len;
8803 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008804 if (phba->link_flag & LS_LOOPBACK_MODE)
8805 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008806 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008807 /* word3 iocb=io_tag32 wqe=reserved */
8808 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008809 /* word4 relative_offset memcpy */
8810 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008811 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8812 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8813 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8814 LPFC_WQE_IOD_WRITE);
8815 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8816 LPFC_WQE_LENLOC_WORD12);
8817 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008818 wqe->xmit_sequence.xmit_len = xmit_len;
8819 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008820 break;
James Smart4f774512009-05-22 14:52:35 -04008821 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008822 /* word3 iocb=iotag32 wqe=seq_payload_len */
8823 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008824 /* word4 iocb=rsvd wqe=rsvd */
8825 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8826 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008827 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008828 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008829 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8830 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8831 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8832 LPFC_WQE_LENLOC_WORD3);
8833 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008834 break;
James Smart4f774512009-05-22 14:52:35 -04008835 case CMD_FCP_IWRITE64_CR:
8836 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008837 /* word3 iocb=iotag wqe=payload_offset_len */
8838 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008839 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8840 xmit_len + sizeof(struct fcp_rsp));
8841 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8842 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008843 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8844 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8845 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8846 iocbq->iocb.ulpFCP2Rcvy);
8847 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8848 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008849 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8850 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8851 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008852 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008853 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008854 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8855 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008856 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8857 if (iocbq->priority) {
8858 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8859 (iocbq->priority << 1));
8860 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008861 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8862 (phba->cfg_XLanePriority << 1));
8863 }
8864 }
James Smartb5c53952016-03-31 14:12:30 -07008865 /* Note, word 10 is already initialized to 0 */
8866
8867 if (phba->fcp_embed_io) {
8868 struct lpfc_scsi_buf *lpfc_cmd;
8869 struct sli4_sge *sgl;
8870 union lpfc_wqe128 *wqe128;
8871 struct fcp_cmnd *fcp_cmnd;
8872 uint32_t *ptr;
8873
8874 /* 128 byte wqe support here */
8875 wqe128 = (union lpfc_wqe128 *)wqe;
8876
8877 lpfc_cmd = iocbq->context1;
8878 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8879 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8880
8881 /* Word 0-2 - FCP_CMND */
8882 wqe128->generic.bde.tus.f.bdeFlags =
8883 BUFF_TYPE_BDE_IMMED;
8884 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8885 wqe128->generic.bde.addrHigh = 0;
8886 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8887
8888 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8889
8890 /* Word 22-29 FCP CMND Payload */
8891 ptr = &wqe128->words[22];
8892 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8893 }
James Smart7851fe22011-07-22 18:36:52 -04008894 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008895 case CMD_FCP_IREAD64_CR:
8896 /* word3 iocb=iotag wqe=payload_offset_len */
8897 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008898 bf_set(payload_offset_len, &wqe->fcp_iread,
8899 xmit_len + sizeof(struct fcp_rsp));
8900 bf_set(cmd_buff_len, &wqe->fcp_iread,
8901 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008902 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8903 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8904 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8905 iocbq->iocb.ulpFCP2Rcvy);
8906 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008907 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008908 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8909 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8910 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008911 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008912 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008913 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8914 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008915 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8916 if (iocbq->priority) {
8917 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8918 (iocbq->priority << 1));
8919 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008920 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8921 (phba->cfg_XLanePriority << 1));
8922 }
8923 }
James Smartb5c53952016-03-31 14:12:30 -07008924 /* Note, word 10 is already initialized to 0 */
8925
8926 if (phba->fcp_embed_io) {
8927 struct lpfc_scsi_buf *lpfc_cmd;
8928 struct sli4_sge *sgl;
8929 union lpfc_wqe128 *wqe128;
8930 struct fcp_cmnd *fcp_cmnd;
8931 uint32_t *ptr;
8932
8933 /* 128 byte wqe support here */
8934 wqe128 = (union lpfc_wqe128 *)wqe;
8935
8936 lpfc_cmd = iocbq->context1;
8937 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8938 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8939
8940 /* Word 0-2 - FCP_CMND */
8941 wqe128->generic.bde.tus.f.bdeFlags =
8942 BUFF_TYPE_BDE_IMMED;
8943 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8944 wqe128->generic.bde.addrHigh = 0;
8945 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8946
8947 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8948
8949 /* Word 22-29 FCP CMND Payload */
8950 ptr = &wqe128->words[22];
8951 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8952 }
James Smart7851fe22011-07-22 18:36:52 -04008953 break;
James Smartf1126682009-06-10 17:22:44 -04008954 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008955 /* word3 iocb=iotag wqe=payload_offset_len */
8956 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8957 bf_set(payload_offset_len, &wqe->fcp_icmd,
8958 xmit_len + sizeof(struct fcp_rsp));
8959 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8960 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008961 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008962 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008963 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008964 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8965 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8966 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8967 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8968 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008969 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8970 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008971 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8972 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008973 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8974 if (iocbq->priority) {
8975 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8976 (iocbq->priority << 1));
8977 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008978 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8979 (phba->cfg_XLanePriority << 1));
8980 }
8981 }
James Smartb5c53952016-03-31 14:12:30 -07008982 /* Note, word 10 is already initialized to 0 */
8983
8984 if (phba->fcp_embed_io) {
8985 struct lpfc_scsi_buf *lpfc_cmd;
8986 struct sli4_sge *sgl;
8987 union lpfc_wqe128 *wqe128;
8988 struct fcp_cmnd *fcp_cmnd;
8989 uint32_t *ptr;
8990
8991 /* 128 byte wqe support here */
8992 wqe128 = (union lpfc_wqe128 *)wqe;
8993
8994 lpfc_cmd = iocbq->context1;
8995 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8996 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8997
8998 /* Word 0-2 - FCP_CMND */
8999 wqe128->generic.bde.tus.f.bdeFlags =
9000 BUFF_TYPE_BDE_IMMED;
9001 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9002 wqe128->generic.bde.addrHigh = 0;
9003 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9004
9005 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9006
9007 /* Word 22-29 FCP CMND Payload */
9008 ptr = &wqe128->words[22];
9009 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9010 }
James Smart7851fe22011-07-22 18:36:52 -04009011 break;
James Smart4f774512009-05-22 14:52:35 -04009012 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009013 /* For this command calculate the xmit length of the
9014 * request bde.
9015 */
9016 xmit_len = 0;
9017 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9018 sizeof(struct ulp_bde64);
9019 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009020 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009021 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9022 break;
James Smart63e801c2010-11-20 23:14:19 -05009023 xmit_len += bde.tus.f.bdeSize;
9024 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009025 /* word3 iocb=IO_TAG wqe=request_payload_len */
9026 wqe->gen_req.request_payload_len = xmit_len;
9027 /* word4 iocb=parameter wqe=relative_offset memcpy */
9028 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009029 /* word6 context tag copied in memcpy */
9030 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9031 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9032 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9033 "2015 Invalid CT %x command 0x%x\n",
9034 ct, iocbq->iocb.ulpCommand);
9035 return IOCB_ERROR;
9036 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009037 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9038 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9039 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9040 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9041 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9042 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9043 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9044 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009045 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009046 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009047 break;
James Smart4f774512009-05-22 14:52:35 -04009048 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009049 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009050 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009051 /* word3 iocb=iotag32 wqe=response_payload_len */
9052 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009053 /* word4 */
9054 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009055 /* word5 iocb=rsvd wge=did */
9056 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009057 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9058
9059 if_type = bf_get(lpfc_sli_intf_if_type,
9060 &phba->sli4_hba.sli_intf);
9061 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9062 if (iocbq->vport->fc_flag & FC_PT2PT) {
9063 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9064 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9065 iocbq->vport->fc_myDID);
9066 if (iocbq->vport->fc_myDID == Fabric_DID) {
9067 bf_set(wqe_els_did,
9068 &wqe->xmit_els_rsp.wqe_dest, 0);
9069 }
9070 }
9071 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009072 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9073 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9074 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9075 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009076 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009077 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009078 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009079 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009080 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9081 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9082 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9083 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9084 LPFC_WQE_LENLOC_WORD3);
9085 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009086 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9087 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009088 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9089 iocbq->context2)->virt);
9090 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009091 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9092 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009093 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009094 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9095 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009096 phba->vpi_ids[phba->pport->vpi]);
9097 }
James Smart4f774512009-05-22 14:52:35 -04009098 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009099 break;
James Smart4f774512009-05-22 14:52:35 -04009100 case CMD_CLOSE_XRI_CN:
9101 case CMD_ABORT_XRI_CN:
9102 case CMD_ABORT_XRI_CX:
9103 /* words 0-2 memcpy should be 0 rserved */
9104 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009105 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9106 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9107 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9108 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9109 } else
9110 fip = 0;
9111
9112 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009113 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009114 * The link is down, or the command was ELS_FIP
9115 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009116 * on the wire.
9117 */
9118 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9119 else
9120 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9121 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009122 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9123 wqe->abort_cmd.rsrvd5 = 0;
9124 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009125 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9126 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009127 /*
9128 * The abort handler will send us CMD_ABORT_XRI_CN or
9129 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9130 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009131 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9132 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9133 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9134 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009135 cmnd = CMD_ABORT_XRI_CX;
9136 command_type = OTHER_COMMAND;
9137 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009138 break;
James Smart6669f9b2009-10-02 15:16:45 -04009139 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009140 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009141 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009142 * we re-construct this WQE here based on information in
9143 * iocbq from scratch.
9144 */
9145 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009146 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009147 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009148 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9149 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009150 LPFC_ABTS_UNSOL_INT) {
9151 /* ABTS sent by initiator to CT exchange, the
9152 * RX_ID field will be filled with the newly
9153 * allocated responder XRI.
9154 */
9155 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9156 iocbq->sli4_xritag);
9157 } else {
9158 /* ABTS sent by responder to CT exchange, the
9159 * RX_ID field will be filled with the responder
9160 * RX_ID from ABTS.
9161 */
9162 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009163 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009164 }
James Smart6669f9b2009-10-02 15:16:45 -04009165 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9166 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009167
9168 /* Use CT=VPI */
9169 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9170 ndlp->nlp_DID);
9171 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9172 iocbq->iocb.ulpContext);
9173 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009174 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009175 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009176 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9177 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9178 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009179 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9180 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009181 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9182 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9183 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9184 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9185 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9186 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9187 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9188 }
9189
James Smart7851fe22011-07-22 18:36:52 -04009190 break;
James Smartae9e28f2017-05-15 15:20:51 -07009191 case CMD_SEND_FRAME:
9192 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9193 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9194 return 0;
James Smart4f774512009-05-22 14:52:35 -04009195 case CMD_XRI_ABORTED_CX:
9196 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009197 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9198 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9199 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9200 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9201 default:
9202 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9203 "2014 Invalid command 0x%x\n",
9204 iocbq->iocb.ulpCommand);
9205 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009206 break;
James Smart4f774512009-05-22 14:52:35 -04009207 }
James Smart6d368e52011-05-24 11:44:12 -04009208
James Smart8012cc32012-10-31 14:44:49 -04009209 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9210 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9211 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9212 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9213 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9214 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9215 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9216 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009217 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9218 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9219 wqe->generic.wqe_com.abort_tag = abort_tag;
9220 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9221 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9222 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9223 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009224 return 0;
9225}
9226
9227/**
9228 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9229 * @phba: Pointer to HBA context object.
9230 * @ring_number: SLI ring number to issue iocb on.
9231 * @piocb: Pointer to command iocb.
9232 * @flag: Flag indicating if this command can be put into txq.
9233 *
9234 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9235 * an iocb command to an HBA with SLI-4 interface spec.
9236 *
9237 * This function is called with hbalock held. The function will return success
9238 * after it successfully submit the iocb to firmware or after adding to the
9239 * txq.
9240 **/
9241static int
9242__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9243 struct lpfc_iocbq *piocb, uint32_t flag)
9244{
9245 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009246 union lpfc_wqe *wqe;
9247 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009248 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009249 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009250
James Smart895427b2017-02-12 13:52:30 -08009251 /* Get the WQ */
9252 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9253 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9254 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9255 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9256 else
9257 wq = phba->sli4_hba.oas_wq;
9258 } else {
9259 wq = phba->sli4_hba.els_wq;
9260 }
9261
9262 /* Get corresponding ring */
9263 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009264
James Smartb5c53952016-03-31 14:12:30 -07009265 /*
9266 * The WQE can be either 64 or 128 bytes,
9267 * so allocate space on the stack assuming the largest.
9268 */
9269 wqe = (union lpfc_wqe *)&wqe128;
9270
James Smart895427b2017-02-12 13:52:30 -08009271 lockdep_assert_held(&phba->hbalock);
9272
James Smart4f774512009-05-22 14:52:35 -04009273 if (piocb->sli4_xritag == NO_XRI) {
9274 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009275 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009276 sglq = NULL;
9277 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009278 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009279 if (!(flag & SLI_IOCB_RET_IOCB)) {
9280 __lpfc_sli_ringtx_put(phba,
9281 pring, piocb);
9282 return IOCB_SUCCESS;
9283 } else {
9284 return IOCB_BUSY;
9285 }
9286 } else {
James Smart895427b2017-02-12 13:52:30 -08009287 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009288 if (!sglq) {
9289 if (!(flag & SLI_IOCB_RET_IOCB)) {
9290 __lpfc_sli_ringtx_put(phba,
9291 pring,
9292 piocb);
9293 return IOCB_SUCCESS;
9294 } else
9295 return IOCB_BUSY;
9296 }
9297 }
James Smart4f774512009-05-22 14:52:35 -04009298 }
James Smart2ea259e2017-02-12 13:52:27 -08009299 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009300 /* These IO's already have an XRI and a mapped sgl. */
9301 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009302 else {
James Smart6d368e52011-05-24 11:44:12 -04009303 /*
9304 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009305 * sglq is on the active list
9306 */
James Smartedccdc12013-01-03 15:43:45 -05009307 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009308 if (!sglq)
9309 return IOCB_ERROR;
9310 }
9311
9312 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009313 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009314 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009315 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009316 return IOCB_ERROR;
9317 }
9318
James Smartb5c53952016-03-31 14:12:30 -07009319 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009320 return IOCB_ERROR;
9321
James Smart895427b2017-02-12 13:52:30 -08009322 if (lpfc_sli4_wq_put(wq, wqe))
9323 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009324 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9325
9326 return 0;
9327}
9328
9329/**
James Smart3772a992009-05-22 14:50:54 -04009330 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9331 *
9332 * This routine wraps the actual lockless version for issusing IOCB function
9333 * pointer from the lpfc_hba struct.
9334 *
9335 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009336 * IOCB_ERROR - Error
9337 * IOCB_SUCCESS - Success
9338 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009339 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009340int
James Smart3772a992009-05-22 14:50:54 -04009341__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9342 struct lpfc_iocbq *piocb, uint32_t flag)
9343{
9344 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9345}
9346
9347/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009348 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009349 * @phba: The hba struct for which this call is being executed.
9350 * @dev_grp: The HBA PCI-Device group number.
9351 *
9352 * This routine sets up the SLI interface API function jump table in @phba
9353 * struct.
9354 * Returns: 0 - success, -ENODEV - failure.
9355 **/
9356int
9357lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9358{
9359
9360 switch (dev_grp) {
9361 case LPFC_PCI_DEV_LP:
9362 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9363 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9364 break;
James Smart4f774512009-05-22 14:52:35 -04009365 case LPFC_PCI_DEV_OC:
9366 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9367 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9368 break;
James Smart3772a992009-05-22 14:50:54 -04009369 default:
9370 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9371 "1419 Invalid HBA PCI-device group: 0x%x\n",
9372 dev_grp);
9373 return -ENODEV;
9374 break;
9375 }
9376 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9377 return 0;
9378}
James Smart92d7f7b2007-06-17 19:56:38 -05009379
James Smarta1efe162015-05-21 13:55:20 -04009380/**
James Smart895427b2017-02-12 13:52:30 -08009381 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009382 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009383 * @piocb: Pointer to command iocb.
9384 *
James Smart895427b2017-02-12 13:52:30 -08009385 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9386 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009387 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009388 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009389 */
James Smart895427b2017-02-12 13:52:30 -08009390struct lpfc_sli_ring *
9391lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009392{
James Smart895427b2017-02-12 13:52:30 -08009393 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009394 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009395 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009396 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009397 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009398 /*
James Smart895427b2017-02-12 13:52:30 -08009399 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009400 * be setup based on what work queue we used.
9401 */
9402 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
James Smart895427b2017-02-12 13:52:30 -08009403 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009404 lpfc_sli4_scmd_to_wqidx_distr(phba,
9405 piocb->context1);
James Smart895427b2017-02-12 13:52:30 -08009406 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009407 } else {
9408 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009409 return NULL;
9410 piocb->hba_wqidx = 0;
9411 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009412 }
James Smart895427b2017-02-12 13:52:30 -08009413 } else {
9414 if (unlikely(!phba->sli4_hba.els_wq))
9415 return NULL;
9416 piocb->hba_wqidx = 0;
9417 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009418 }
James Smart9bd2bff52014-09-03 12:57:30 -04009419}
9420
James Smarte59058c2008-08-24 21:49:00 -04009421/**
James Smart3621a712009-04-06 18:47:14 -04009422 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009423 * @phba: Pointer to HBA context object.
9424 * @pring: Pointer to driver SLI ring object.
9425 * @piocb: Pointer to command iocb.
9426 * @flag: Flag indicating if this command can be put into txq.
9427 *
9428 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9429 * function. This function gets the hbalock and calls
9430 * __lpfc_sli_issue_iocb function and will return the error returned
9431 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9432 * functions which do not hold hbalock.
9433 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009434int
James Smart3772a992009-05-22 14:50:54 -04009435lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009436 struct lpfc_iocbq *piocb, uint32_t flag)
9437{
James Smart895427b2017-02-12 13:52:30 -08009438 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009439 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009440 struct lpfc_queue *fpeq;
9441 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009442 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009443 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009444
James Smart7e56aa22012-08-03 12:35:34 -04009445 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009446 pring = lpfc_sli4_calc_ring(phba, piocb);
9447 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009448 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009449
James Smart9bd2bff52014-09-03 12:57:30 -04009450 spin_lock_irqsave(&pring->ring_lock, iflags);
9451 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9452 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009453
James Smart9bd2bff52014-09-03 12:57:30 -04009454 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009455 idx = piocb->hba_wqidx;
9456 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009457
James Smart895427b2017-02-12 13:52:30 -08009458 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009459
James Smart9bd2bff52014-09-03 12:57:30 -04009460 /* Get associated EQ with this index */
9461 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009462
James Smart9bd2bff52014-09-03 12:57:30 -04009463 /* Turn off interrupts from this EQ */
9464 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009465
James Smart9bd2bff52014-09-03 12:57:30 -04009466 /*
9467 * Process all the events on FCP EQ
9468 */
9469 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9470 lpfc_sli4_hba_handle_eqe(phba,
9471 eqe, idx);
9472 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009473 }
James Smartba20c852012-08-03 12:36:52 -04009474
James Smart9bd2bff52014-09-03 12:57:30 -04009475 /* Always clear and re-arm the EQ */
9476 lpfc_sli4_eq_release(fpeq,
9477 LPFC_QUEUE_REARM);
9478 }
James Smart895427b2017-02-12 13:52:30 -08009479 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009480 }
James Smart7e56aa22012-08-03 12:35:34 -04009481 } else {
9482 /* For now, SLI2/3 will still use hbalock */
9483 spin_lock_irqsave(&phba->hbalock, iflags);
9484 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9485 spin_unlock_irqrestore(&phba->hbalock, iflags);
9486 }
James Smart92d7f7b2007-06-17 19:56:38 -05009487 return rc;
9488}
9489
James Smarte59058c2008-08-24 21:49:00 -04009490/**
James Smart3621a712009-04-06 18:47:14 -04009491 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009492 * @phba: Pointer to HBA context object.
9493 *
9494 * This function is called while driver attaches with the
9495 * HBA to setup the extra ring. The extra ring is used
9496 * only when driver needs to support target mode functionality
9497 * or IP over FC functionalities.
9498 *
James Smart895427b2017-02-12 13:52:30 -08009499 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009500 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009501static int
9502lpfc_extra_ring_setup( struct lpfc_hba *phba)
9503{
9504 struct lpfc_sli *psli;
9505 struct lpfc_sli_ring *pring;
9506
9507 psli = &phba->sli;
9508
9509 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009510
9511 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009512 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009513 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9514 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9515 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9516 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009517
James Smarta4bc3372006-12-02 13:34:16 -05009518 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009519 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009520
James Smart7e56aa22012-08-03 12:35:34 -04009521 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9522 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9523 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9524 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009525
9526 /* Setup default profile for this ring */
9527 pring->iotag_max = 4096;
9528 pring->num_mask = 1;
9529 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009530 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9531 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009532 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9533 return 0;
9534}
9535
James Smartcb69f7d2011-12-13 13:21:57 -05009536/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9537 * @phba: Pointer to HBA context object.
9538 * @iocbq: Pointer to iocb object.
9539 *
9540 * The async_event handler calls this routine when it receives
9541 * an ASYNC_STATUS_CN event from the port. The port generates
9542 * this event when an Abort Sequence request to an rport fails
9543 * twice in succession. The abort could be originated by the
9544 * driver or by the port. The ABTS could have been for an ELS
9545 * or FCP IO. The port only generates this event when an ABTS
9546 * fails to complete after one retry.
9547 */
9548static void
9549lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9550 struct lpfc_iocbq *iocbq)
9551{
9552 struct lpfc_nodelist *ndlp = NULL;
9553 uint16_t rpi = 0, vpi = 0;
9554 struct lpfc_vport *vport = NULL;
9555
9556 /* The rpi in the ulpContext is vport-sensitive. */
9557 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9558 rpi = iocbq->iocb.ulpContext;
9559
9560 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9561 "3092 Port generated ABTS async event "
9562 "on vpi %d rpi %d status 0x%x\n",
9563 vpi, rpi, iocbq->iocb.ulpStatus);
9564
9565 vport = lpfc_find_vport_by_vpid(phba, vpi);
9566 if (!vport)
9567 goto err_exit;
9568 ndlp = lpfc_findnode_rpi(vport, rpi);
9569 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9570 goto err_exit;
9571
9572 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9573 lpfc_sli_abts_recover_port(vport, ndlp);
9574 return;
9575
9576 err_exit:
9577 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9578 "3095 Event Context not found, no "
9579 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9580 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9581 vpi, rpi);
9582}
9583
9584/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9585 * @phba: pointer to HBA context object.
9586 * @ndlp: nodelist pointer for the impacted rport.
9587 * @axri: pointer to the wcqe containing the failed exchange.
9588 *
9589 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9590 * port. The port generates this event when an abort exchange request to an
9591 * rport fails twice in succession with no reply. The abort could be originated
9592 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9593 */
9594void
9595lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9596 struct lpfc_nodelist *ndlp,
9597 struct sli4_wcqe_xri_aborted *axri)
9598{
9599 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009600 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009601
James Smart6b5151f2012-01-18 16:24:06 -05009602 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009603 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9604 "3115 Node Context not found, driver "
9605 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009606 return;
9607 }
9608
James Smartcb69f7d2011-12-13 13:21:57 -05009609 vport = ndlp->vport;
9610 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9611 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009612 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009613 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009614 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009615 bf_get(lpfc_wcqe_xa_status, axri),
9616 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009617
James Smart5c1db2a2012-03-01 22:34:36 -05009618 /*
9619 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9620 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9621 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9622 */
James Smarte3d2b802012-08-14 14:25:43 -04009623 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009624 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9625 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009626 lpfc_sli_abts_recover_port(vport, ndlp);
9627}
9628
James Smarte59058c2008-08-24 21:49:00 -04009629/**
James Smart3621a712009-04-06 18:47:14 -04009630 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009631 * @phba: Pointer to HBA context object.
9632 * @pring: Pointer to driver SLI ring object.
9633 * @iocbq: Pointer to iocb object.
9634 *
9635 * This function is called by the slow ring event handler
9636 * function when there is an ASYNC event iocb in the ring.
9637 * This function is called with no lock held.
9638 * Currently this function handles only temperature related
9639 * ASYNC events. The function decodes the temperature sensor
9640 * event message and posts events for the management applications.
9641 **/
James Smart98c9ea52007-10-27 13:37:33 -04009642static void
James Smart57127f12007-10-27 13:37:05 -04009643lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9644 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9645{
9646 IOCB_t *icmd;
9647 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009648 struct temp_event temp_event_data;
9649 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009650 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009651
9652 icmd = &iocbq->iocb;
9653 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009654
James Smartcb69f7d2011-12-13 13:21:57 -05009655 switch (evt_code) {
9656 case ASYNC_TEMP_WARN:
9657 case ASYNC_TEMP_SAFE:
9658 temp_event_data.data = (uint32_t) icmd->ulpContext;
9659 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9660 if (evt_code == ASYNC_TEMP_WARN) {
9661 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9662 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9663 "0347 Adapter is very hot, please take "
9664 "corrective action. temperature : %d Celsius\n",
9665 (uint32_t) icmd->ulpContext);
9666 } else {
9667 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9668 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9669 "0340 Adapter temperature is OK now. "
9670 "temperature : %d Celsius\n",
9671 (uint32_t) icmd->ulpContext);
9672 }
9673
9674 /* Send temperature change event to applications */
9675 shost = lpfc_shost_from_vport(phba->pport);
9676 fc_host_post_vendor_event(shost, fc_get_event_number(),
9677 sizeof(temp_event_data), (char *) &temp_event_data,
9678 LPFC_NL_VENDOR_ID);
9679 break;
9680 case ASYNC_STATUS_CN:
9681 lpfc_sli_abts_err_handler(phba, iocbq);
9682 break;
9683 default:
James Smarta257bf92009-04-06 18:48:10 -04009684 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009685 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009686 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009687 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009688 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9689 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9690 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9691 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009692 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009693 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9694 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9695 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9696 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9697
James Smartcb69f7d2011-12-13 13:21:57 -05009698 break;
James Smart57127f12007-10-27 13:37:05 -04009699 }
James Smart57127f12007-10-27 13:37:05 -04009700}
9701
9702
James Smarte59058c2008-08-24 21:49:00 -04009703/**
James Smart895427b2017-02-12 13:52:30 -08009704 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009705 * @phba: Pointer to HBA context object.
9706 *
9707 * lpfc_sli_setup sets up rings of the SLI interface with
9708 * number of iocbs per ring and iotags. This function is
9709 * called while driver attach to the HBA and before the
9710 * interrupts are enabled. So there is no need for locking.
9711 *
9712 * This function always returns 0.
9713 **/
dea31012005-04-17 16:05:31 -05009714int
James Smart895427b2017-02-12 13:52:30 -08009715lpfc_sli4_setup(struct lpfc_hba *phba)
9716{
9717 struct lpfc_sli_ring *pring;
9718
9719 pring = phba->sli4_hba.els_wq->pring;
9720 pring->num_mask = LPFC_MAX_RING_MASK;
9721 pring->prt[0].profile = 0; /* Mask 0 */
9722 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9723 pring->prt[0].type = FC_TYPE_ELS;
9724 pring->prt[0].lpfc_sli_rcv_unsol_event =
9725 lpfc_els_unsol_event;
9726 pring->prt[1].profile = 0; /* Mask 1 */
9727 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9728 pring->prt[1].type = FC_TYPE_ELS;
9729 pring->prt[1].lpfc_sli_rcv_unsol_event =
9730 lpfc_els_unsol_event;
9731 pring->prt[2].profile = 0; /* Mask 2 */
9732 /* NameServer Inquiry */
9733 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9734 /* NameServer */
9735 pring->prt[2].type = FC_TYPE_CT;
9736 pring->prt[2].lpfc_sli_rcv_unsol_event =
9737 lpfc_ct_unsol_event;
9738 pring->prt[3].profile = 0; /* Mask 3 */
9739 /* NameServer response */
9740 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9741 /* NameServer */
9742 pring->prt[3].type = FC_TYPE_CT;
9743 pring->prt[3].lpfc_sli_rcv_unsol_event =
9744 lpfc_ct_unsol_event;
9745 return 0;
9746}
9747
9748/**
9749 * lpfc_sli_setup - SLI ring setup function
9750 * @phba: Pointer to HBA context object.
9751 *
9752 * lpfc_sli_setup sets up rings of the SLI interface with
9753 * number of iocbs per ring and iotags. This function is
9754 * called while driver attach to the HBA and before the
9755 * interrupts are enabled. So there is no need for locking.
9756 *
9757 * This function always returns 0. SLI3 only.
9758 **/
9759int
dea31012005-04-17 16:05:31 -05009760lpfc_sli_setup(struct lpfc_hba *phba)
9761{
James Smarted957682007-06-17 19:56:37 -05009762 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009763 struct lpfc_sli *psli = &phba->sli;
9764 struct lpfc_sli_ring *pring;
9765
James Smart2a76a282012-08-03 12:35:54 -04009766 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009767 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009768
James Bottomley604a3e32005-10-29 10:28:33 -05009769 psli->iocbq_lookup = NULL;
9770 psli->iocbq_lookup_len = 0;
9771 psli->last_iotag = 0;
9772
dea31012005-04-17 16:05:31 -05009773 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009774 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009775 switch (i) {
9776 case LPFC_FCP_RING: /* ring 0 - FCP */
9777 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009778 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9779 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9780 pring->sli.sli3.numCiocb +=
9781 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9782 pring->sli.sli3.numRiocb +=
9783 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9784 pring->sli.sli3.numCiocb +=
9785 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9786 pring->sli.sli3.numRiocb +=
9787 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9788 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009789 SLI3_IOCB_CMD_SIZE :
9790 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009791 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009792 SLI3_IOCB_RSP_SIZE :
9793 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009794 pring->iotag_ctr = 0;
9795 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009796 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009797 pring->fast_iotag = pring->iotag_max;
9798 pring->num_mask = 0;
9799 break;
James Smarta4bc3372006-12-02 13:34:16 -05009800 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009801 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009802 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9803 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9804 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009805 SLI3_IOCB_CMD_SIZE :
9806 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009807 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009808 SLI3_IOCB_RSP_SIZE :
9809 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009810 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009811 pring->num_mask = 0;
9812 break;
9813 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9814 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009815 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9816 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9817 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009818 SLI3_IOCB_CMD_SIZE :
9819 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009820 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009821 SLI3_IOCB_RSP_SIZE :
9822 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009823 pring->fast_iotag = 0;
9824 pring->iotag_ctr = 0;
9825 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009826 pring->lpfc_sli_rcv_async_status =
9827 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009828 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009829 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009830 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9831 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009832 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009833 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009834 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009835 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9836 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009837 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009838 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009839 pring->prt[2].profile = 0; /* Mask 2 */
9840 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009841 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009842 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009843 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009844 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009845 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009846 pring->prt[3].profile = 0; /* Mask 3 */
9847 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009848 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009849 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009850 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009851 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009852 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009853 break;
9854 }
James Smart7e56aa22012-08-03 12:35:34 -04009855 totiocbsize += (pring->sli.sli3.numCiocb *
9856 pring->sli.sli3.sizeCiocb) +
9857 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009858 }
James Smarted957682007-06-17 19:56:37 -05009859 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009860 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009861 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9862 "SLI2 SLIM Data: x%x x%lx\n",
9863 phba->brd_no, totiocbsize,
9864 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009865 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009866 if (phba->cfg_multi_ring_support == 2)
9867 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009868
9869 return 0;
9870}
9871
James Smarte59058c2008-08-24 21:49:00 -04009872/**
James Smart895427b2017-02-12 13:52:30 -08009873 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009874 * @phba: Pointer to HBA context object.
9875 *
James Smart895427b2017-02-12 13:52:30 -08009876 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009877 * ring. This function also initializes ring indices of each ring.
9878 * This function is called during the initialization of the SLI
9879 * interface of an HBA.
9880 * This function is called with no lock held and always returns
9881 * 1.
9882 **/
James Smart895427b2017-02-12 13:52:30 -08009883void
9884lpfc_sli4_queue_init(struct lpfc_hba *phba)
9885{
9886 struct lpfc_sli *psli;
9887 struct lpfc_sli_ring *pring;
9888 int i;
9889
9890 psli = &phba->sli;
9891 spin_lock_irq(&phba->hbalock);
9892 INIT_LIST_HEAD(&psli->mboxq);
9893 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9894 /* Initialize list headers for txq and txcmplq as double linked lists */
9895 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9896 pring = phba->sli4_hba.fcp_wq[i]->pring;
9897 pring->flag = 0;
9898 pring->ringno = LPFC_FCP_RING;
9899 INIT_LIST_HEAD(&pring->txq);
9900 INIT_LIST_HEAD(&pring->txcmplq);
9901 INIT_LIST_HEAD(&pring->iocb_continueq);
9902 spin_lock_init(&pring->ring_lock);
9903 }
9904 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9905 pring = phba->sli4_hba.nvme_wq[i]->pring;
9906 pring->flag = 0;
9907 pring->ringno = LPFC_FCP_RING;
9908 INIT_LIST_HEAD(&pring->txq);
9909 INIT_LIST_HEAD(&pring->txcmplq);
9910 INIT_LIST_HEAD(&pring->iocb_continueq);
9911 spin_lock_init(&pring->ring_lock);
9912 }
9913 pring = phba->sli4_hba.els_wq->pring;
9914 pring->flag = 0;
9915 pring->ringno = LPFC_ELS_RING;
9916 INIT_LIST_HEAD(&pring->txq);
9917 INIT_LIST_HEAD(&pring->txcmplq);
9918 INIT_LIST_HEAD(&pring->iocb_continueq);
9919 spin_lock_init(&pring->ring_lock);
9920
9921 if (phba->cfg_nvme_io_channel) {
9922 pring = phba->sli4_hba.nvmels_wq->pring;
9923 pring->flag = 0;
9924 pring->ringno = LPFC_ELS_RING;
9925 INIT_LIST_HEAD(&pring->txq);
9926 INIT_LIST_HEAD(&pring->txcmplq);
9927 INIT_LIST_HEAD(&pring->iocb_continueq);
9928 spin_lock_init(&pring->ring_lock);
9929 }
9930
9931 if (phba->cfg_fof) {
9932 pring = phba->sli4_hba.oas_wq->pring;
9933 pring->flag = 0;
9934 pring->ringno = LPFC_FCP_RING;
9935 INIT_LIST_HEAD(&pring->txq);
9936 INIT_LIST_HEAD(&pring->txcmplq);
9937 INIT_LIST_HEAD(&pring->iocb_continueq);
9938 spin_lock_init(&pring->ring_lock);
9939 }
9940
9941 spin_unlock_irq(&phba->hbalock);
9942}
9943
9944/**
9945 * lpfc_sli_queue_init - Queue initialization function
9946 * @phba: Pointer to HBA context object.
9947 *
9948 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9949 * ring. This function also initializes ring indices of each ring.
9950 * This function is called during the initialization of the SLI
9951 * interface of an HBA.
9952 * This function is called with no lock held and always returns
9953 * 1.
9954 **/
9955void
9956lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009957{
9958 struct lpfc_sli *psli;
9959 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009960 int i;
dea31012005-04-17 16:05:31 -05009961
9962 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009963 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009964 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009965 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009966 /* Initialize list headers for txq and txcmplq as double linked lists */
9967 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009968 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009969 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009970 pring->sli.sli3.next_cmdidx = 0;
9971 pring->sli.sli3.local_getidx = 0;
9972 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009973 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009974 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009975 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -08009976 pring->flag = 0;
9977 INIT_LIST_HEAD(&pring->txq);
9978 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -04009979 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009980 }
James Smart2e0fef82007-06-17 19:56:36 -05009981 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009982}
9983
James Smarte59058c2008-08-24 21:49:00 -04009984/**
James Smart04c68492009-05-22 14:52:52 -04009985 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9986 * @phba: Pointer to HBA context object.
9987 *
9988 * This routine flushes the mailbox command subsystem. It will unconditionally
9989 * flush all the mailbox commands in the three possible stages in the mailbox
9990 * command sub-system: pending mailbox command queue; the outstanding mailbox
9991 * command; and completed mailbox command queue. It is caller's responsibility
9992 * to make sure that the driver is in the proper state to flush the mailbox
9993 * command sub-system. Namely, the posting of mailbox commands into the
9994 * pending mailbox command queue from the various clients must be stopped;
9995 * either the HBA is in a state that it will never works on the outstanding
9996 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9997 * mailbox command has been completed.
9998 **/
9999static void
10000lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10001{
10002 LIST_HEAD(completions);
10003 struct lpfc_sli *psli = &phba->sli;
10004 LPFC_MBOXQ_t *pmb;
10005 unsigned long iflag;
10006
10007 /* Flush all the mailbox commands in the mbox system */
10008 spin_lock_irqsave(&phba->hbalock, iflag);
10009 /* The pending mailbox command queue */
10010 list_splice_init(&phba->sli.mboxq, &completions);
10011 /* The outstanding active mailbox command */
10012 if (psli->mbox_active) {
10013 list_add_tail(&psli->mbox_active->list, &completions);
10014 psli->mbox_active = NULL;
10015 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10016 }
10017 /* The completed mailbox command queue */
10018 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10019 spin_unlock_irqrestore(&phba->hbalock, iflag);
10020
10021 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10022 while (!list_empty(&completions)) {
10023 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10024 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10025 if (pmb->mbox_cmpl)
10026 pmb->mbox_cmpl(phba, pmb);
10027 }
10028}
10029
10030/**
James Smart3621a712009-04-06 18:47:14 -040010031 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010032 * @vport: Pointer to virtual port object.
10033 *
10034 * lpfc_sli_host_down is called to clean up the resources
10035 * associated with a vport before destroying virtual
10036 * port data structures.
10037 * This function does following operations:
10038 * - Free discovery resources associated with this virtual
10039 * port.
10040 * - Free iocbs associated with this virtual port in
10041 * the txq.
10042 * - Send abort for all iocb commands associated with this
10043 * vport in txcmplq.
10044 *
10045 * This function is called with no lock held and always returns 1.
10046 **/
dea31012005-04-17 16:05:31 -050010047int
James Smart92d7f7b2007-06-17 19:56:38 -050010048lpfc_sli_host_down(struct lpfc_vport *vport)
10049{
James Smart858c9f62007-06-17 19:56:39 -050010050 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010051 struct lpfc_hba *phba = vport->phba;
10052 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010053 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010054 struct lpfc_sli_ring *pring;
10055 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010056 int i;
10057 unsigned long flags = 0;
10058 uint16_t prev_pring_flag;
10059
10060 lpfc_cleanup_discovery_resources(vport);
10061
10062 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010063
James Smart895427b2017-02-12 13:52:30 -080010064 /*
10065 * Error everything on the txq since these iocbs
10066 * have not been given to the FW yet.
10067 * Also issue ABTS for everything on the txcmplq
10068 */
10069 if (phba->sli_rev != LPFC_SLI_REV4) {
10070 for (i = 0; i < psli->num_rings; i++) {
10071 pring = &psli->sli3_ring[i];
10072 prev_pring_flag = pring->flag;
10073 /* Only slow rings */
10074 if (pring->ringno == LPFC_ELS_RING) {
10075 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10076 /* Set the lpfc data pending flag */
10077 set_bit(LPFC_DATA_READY, &phba->data_flags);
10078 }
10079 list_for_each_entry_safe(iocb, next_iocb,
10080 &pring->txq, list) {
10081 if (iocb->vport != vport)
10082 continue;
10083 list_move_tail(&iocb->list, &completions);
10084 }
10085 list_for_each_entry_safe(iocb, next_iocb,
10086 &pring->txcmplq, list) {
10087 if (iocb->vport != vport)
10088 continue;
10089 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10090 }
10091 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010092 }
James Smart895427b2017-02-12 13:52:30 -080010093 } else {
10094 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10095 pring = qp->pring;
10096 if (!pring)
10097 continue;
10098 if (pring == phba->sli4_hba.els_wq->pring) {
10099 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10100 /* Set the lpfc data pending flag */
10101 set_bit(LPFC_DATA_READY, &phba->data_flags);
10102 }
10103 prev_pring_flag = pring->flag;
10104 spin_lock_irq(&pring->ring_lock);
10105 list_for_each_entry_safe(iocb, next_iocb,
10106 &pring->txq, list) {
10107 if (iocb->vport != vport)
10108 continue;
10109 list_move_tail(&iocb->list, &completions);
10110 }
10111 spin_unlock_irq(&pring->ring_lock);
10112 list_for_each_entry_safe(iocb, next_iocb,
10113 &pring->txcmplq, list) {
10114 if (iocb->vport != vport)
10115 continue;
10116 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10117 }
10118 pring->flag = prev_pring_flag;
10119 }
James Smart92d7f7b2007-06-17 19:56:38 -050010120 }
James Smart92d7f7b2007-06-17 19:56:38 -050010121 spin_unlock_irqrestore(&phba->hbalock, flags);
10122
James Smarta257bf92009-04-06 18:48:10 -040010123 /* Cancel all the IOCBs from the completions list */
10124 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10125 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010126 return 1;
10127}
10128
James Smarte59058c2008-08-24 21:49:00 -040010129/**
James Smart3621a712009-04-06 18:47:14 -040010130 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010131 * @phba: Pointer to HBA context object.
10132 *
10133 * This function cleans up all iocb, buffers, mailbox commands
10134 * while shutting down the HBA. This function is called with no
10135 * lock held and always returns 1.
10136 * This function does the following to cleanup driver resources:
10137 * - Free discovery resources for each virtual port
10138 * - Cleanup any pending fabric iocbs
10139 * - Iterate through the iocb txq and free each entry
10140 * in the list.
10141 * - Free up any buffer posted to the HBA
10142 * - Free mailbox commands in the mailbox queue.
10143 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010144int
James Smart2e0fef82007-06-17 19:56:36 -050010145lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010146{
James Smart2534ba72007-04-25 09:52:20 -040010147 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010148 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010149 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010150 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010151 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010152 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010153 int i;
10154
10155 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010156 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010157
dea31012005-04-17 16:05:31 -050010158 lpfc_hba_down_prep(phba);
10159
James Smart92d7f7b2007-06-17 19:56:38 -050010160 lpfc_fabric_abort_hba(phba);
10161
James Smart2e0fef82007-06-17 19:56:36 -050010162 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010163
James Smart895427b2017-02-12 13:52:30 -080010164 /*
10165 * Error everything on the txq since these iocbs
10166 * have not been given to the FW yet.
10167 */
10168 if (phba->sli_rev != LPFC_SLI_REV4) {
10169 for (i = 0; i < psli->num_rings; i++) {
10170 pring = &psli->sli3_ring[i];
10171 /* Only slow rings */
10172 if (pring->ringno == LPFC_ELS_RING) {
10173 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10174 /* Set the lpfc data pending flag */
10175 set_bit(LPFC_DATA_READY, &phba->data_flags);
10176 }
10177 list_splice_init(&pring->txq, &completions);
10178 }
10179 } else {
10180 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10181 pring = qp->pring;
10182 if (!pring)
10183 continue;
10184 spin_lock_irq(&pring->ring_lock);
10185 list_splice_init(&pring->txq, &completions);
10186 spin_unlock_irq(&pring->ring_lock);
10187 if (pring == phba->sli4_hba.els_wq->pring) {
10188 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10189 /* Set the lpfc data pending flag */
10190 set_bit(LPFC_DATA_READY, &phba->data_flags);
10191 }
10192 }
dea31012005-04-17 16:05:31 -050010193 }
James Smart2e0fef82007-06-17 19:56:36 -050010194 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010195
James Smarta257bf92009-04-06 18:48:10 -040010196 /* Cancel all the IOCBs from the completions list */
10197 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10198 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010199
James Smart0ff10d42008-01-11 01:52:36 -050010200 spin_lock_irqsave(&phba->hbalock, flags);
10201 list_splice_init(&phba->elsbuf, &completions);
10202 phba->elsbuf_cnt = 0;
10203 phba->elsbuf_prev_cnt = 0;
10204 spin_unlock_irqrestore(&phba->hbalock, flags);
10205
10206 while (!list_empty(&completions)) {
10207 list_remove_head(&completions, buf_ptr,
10208 struct lpfc_dmabuf, list);
10209 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10210 kfree(buf_ptr);
10211 }
10212
dea31012005-04-17 16:05:31 -050010213 /* Return any active mbox cmds */
10214 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010215
James Smartda0436e2009-05-22 14:51:39 -040010216 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010217 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010218 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010219
James Smartda0436e2009-05-22 14:51:39 -040010220 return 1;
10221}
James Smart92d7f7b2007-06-17 19:56:38 -050010222
James Smartda0436e2009-05-22 14:51:39 -040010223/**
James Smart3621a712009-04-06 18:47:14 -040010224 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010225 * @srcp: Source memory pointer.
10226 * @destp: Destination memory pointer.
10227 * @cnt: Number of words required to be copied.
10228 *
10229 * This function is used for copying data between driver memory
10230 * and the SLI memory. This function also changes the endianness
10231 * of each word if native endianness is different from SLI
10232 * endianness. This function can be called with or without
10233 * lock.
10234 **/
dea31012005-04-17 16:05:31 -050010235void
10236lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10237{
10238 uint32_t *src = srcp;
10239 uint32_t *dest = destp;
10240 uint32_t ldata;
10241 int i;
10242
10243 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10244 ldata = *src;
10245 ldata = le32_to_cpu(ldata);
10246 *dest = ldata;
10247 src++;
10248 dest++;
10249 }
10250}
10251
James Smarte59058c2008-08-24 21:49:00 -040010252
10253/**
James Smarta0c87cb2009-07-19 10:01:10 -040010254 * lpfc_sli_bemem_bcopy - SLI memory copy function
10255 * @srcp: Source memory pointer.
10256 * @destp: Destination memory pointer.
10257 * @cnt: Number of words required to be copied.
10258 *
10259 * This function is used for copying data between a data structure
10260 * with big endian representation to local endianness.
10261 * This function can be called with or without lock.
10262 **/
10263void
10264lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10265{
10266 uint32_t *src = srcp;
10267 uint32_t *dest = destp;
10268 uint32_t ldata;
10269 int i;
10270
10271 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10272 ldata = *src;
10273 ldata = be32_to_cpu(ldata);
10274 *dest = ldata;
10275 src++;
10276 dest++;
10277 }
10278}
10279
10280/**
James Smart3621a712009-04-06 18:47:14 -040010281 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010282 * @phba: Pointer to HBA context object.
10283 * @pring: Pointer to driver SLI ring object.
10284 * @mp: Pointer to driver buffer object.
10285 *
10286 * This function is called with no lock held.
10287 * It always return zero after adding the buffer to the postbufq
10288 * buffer list.
10289 **/
dea31012005-04-17 16:05:31 -050010290int
James Smart2e0fef82007-06-17 19:56:36 -050010291lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10292 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010293{
10294 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10295 later */
James Smart2e0fef82007-06-17 19:56:36 -050010296 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010297 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010298 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010299 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010300 return 0;
10301}
10302
James Smarte59058c2008-08-24 21:49:00 -040010303/**
James Smart3621a712009-04-06 18:47:14 -040010304 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010305 * @phba: Pointer to HBA context object.
10306 *
10307 * When HBQ is enabled, buffers are searched based on tags. This function
10308 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10309 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10310 * does not conflict with tags of buffer posted for unsolicited events.
10311 * The function returns the allocated tag. The function is called with
10312 * no locks held.
10313 **/
James Smart76bb24e2007-10-27 13:38:00 -040010314uint32_t
10315lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10316{
10317 spin_lock_irq(&phba->hbalock);
10318 phba->buffer_tag_count++;
10319 /*
10320 * Always set the QUE_BUFTAG_BIT to distiguish between
10321 * a tag assigned by HBQ.
10322 */
10323 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10324 spin_unlock_irq(&phba->hbalock);
10325 return phba->buffer_tag_count;
10326}
10327
James Smarte59058c2008-08-24 21:49:00 -040010328/**
James Smart3621a712009-04-06 18:47:14 -040010329 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010330 * @phba: Pointer to HBA context object.
10331 * @pring: Pointer to driver SLI ring object.
10332 * @tag: Buffer tag.
10333 *
10334 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10335 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10336 * iocb is posted to the response ring with the tag of the buffer.
10337 * This function searches the pring->postbufq list using the tag
10338 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10339 * iocb. If the buffer is found then lpfc_dmabuf object of the
10340 * buffer is returned to the caller else NULL is returned.
10341 * This function is called with no lock held.
10342 **/
James Smart76bb24e2007-10-27 13:38:00 -040010343struct lpfc_dmabuf *
10344lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10345 uint32_t tag)
10346{
10347 struct lpfc_dmabuf *mp, *next_mp;
10348 struct list_head *slp = &pring->postbufq;
10349
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010350 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010351 spin_lock_irq(&phba->hbalock);
10352 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10353 if (mp->buffer_tag == tag) {
10354 list_del_init(&mp->list);
10355 pring->postbufq_cnt--;
10356 spin_unlock_irq(&phba->hbalock);
10357 return mp;
10358 }
10359 }
10360
10361 spin_unlock_irq(&phba->hbalock);
10362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010363 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010364 "ring %d Data x%lx x%p x%p x%x\n",
10365 pring->ringno, (unsigned long) tag,
10366 slp->next, slp->prev, pring->postbufq_cnt);
10367
10368 return NULL;
10369}
dea31012005-04-17 16:05:31 -050010370
James Smarte59058c2008-08-24 21:49:00 -040010371/**
James Smart3621a712009-04-06 18:47:14 -040010372 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010373 * @phba: Pointer to HBA context object.
10374 * @pring: Pointer to driver SLI ring object.
10375 * @phys: DMA address of the buffer.
10376 *
10377 * This function searches the buffer list using the dma_address
10378 * of unsolicited event to find the driver's lpfc_dmabuf object
10379 * corresponding to the dma_address. The function returns the
10380 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10381 * This function is called by the ct and els unsolicited event
10382 * handlers to get the buffer associated with the unsolicited
10383 * event.
10384 *
10385 * This function is called with no lock held.
10386 **/
dea31012005-04-17 16:05:31 -050010387struct lpfc_dmabuf *
10388lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10389 dma_addr_t phys)
10390{
10391 struct lpfc_dmabuf *mp, *next_mp;
10392 struct list_head *slp = &pring->postbufq;
10393
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010394 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010395 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010396 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10397 if (mp->phys == phys) {
10398 list_del_init(&mp->list);
10399 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010400 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010401 return mp;
10402 }
10403 }
10404
James Smart2e0fef82007-06-17 19:56:36 -050010405 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010407 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010408 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010409 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010410 slp->next, slp->prev, pring->postbufq_cnt);
10411 return NULL;
10412}
10413
James Smarte59058c2008-08-24 21:49:00 -040010414/**
James Smart3621a712009-04-06 18:47:14 -040010415 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010416 * @phba: Pointer to HBA context object.
10417 * @cmdiocb: Pointer to driver command iocb object.
10418 * @rspiocb: Pointer to driver response iocb object.
10419 *
10420 * This function is the completion handler for the abort iocbs for
10421 * ELS commands. This function is called from the ELS ring event
10422 * handler with no lock held. This function frees memory resources
10423 * associated with the abort iocb.
10424 **/
dea31012005-04-17 16:05:31 -050010425static void
James Smart2e0fef82007-06-17 19:56:36 -050010426lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10427 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010428{
James Smart2e0fef82007-06-17 19:56:36 -050010429 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010430 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010431 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010432
10433 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010434
10435 /*
10436 * Assume that the port already completed and returned, or
10437 * will return the iocb. Just Log the message.
10438 */
James Smart2680eea2007-04-25 09:52:55 -040010439 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10440 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10441
James Smart2e0fef82007-06-17 19:56:36 -050010442 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010443 if (phba->sli_rev < LPFC_SLI_REV4) {
10444 if (abort_iotag != 0 &&
10445 abort_iotag <= phba->sli.last_iotag)
10446 abort_iocb =
10447 phba->sli.iocbq_lookup[abort_iotag];
10448 } else
10449 /* For sli4 the abort_tag is the XRI,
10450 * so the abort routine puts the iotag of the iocb
10451 * being aborted in the context field of the abort
10452 * IOCB.
10453 */
10454 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010455
James Smart2a9bf3d2010-06-07 15:24:45 -040010456 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10457 "0327 Cannot abort els iocb %p "
10458 "with tag %x context %x, abort status %x, "
10459 "abort code %x\n",
10460 abort_iocb, abort_iotag, abort_context,
10461 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010462
James Smartff78d8f2011-12-13 13:21:35 -050010463 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010464 }
James Bottomley604a3e32005-10-29 10:28:33 -050010465 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010466 return;
10467}
10468
James Smarte59058c2008-08-24 21:49:00 -040010469/**
James Smart3621a712009-04-06 18:47:14 -040010470 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010471 * @phba: Pointer to HBA context object.
10472 * @cmdiocb: Pointer to driver command iocb object.
10473 * @rspiocb: Pointer to driver response iocb object.
10474 *
10475 * The function is called from SLI ring event handler with no
10476 * lock held. This function is the completion handler for ELS commands
10477 * which are aborted. The function frees memory resources used for
10478 * the aborted ELS commands.
10479 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010480static void
10481lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10482 struct lpfc_iocbq *rspiocb)
10483{
10484 IOCB_t *irsp = &rspiocb->iocb;
10485
10486 /* ELS cmd tag <ulpIoTag> completes */
10487 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010488 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010489 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010490 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010491 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010492 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10493 lpfc_ct_free_iocb(phba, cmdiocb);
10494 else
10495 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010496 return;
10497}
10498
James Smarte59058c2008-08-24 21:49:00 -040010499/**
James Smart5af5eee2010-10-22 11:06:38 -040010500 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010501 * @phba: Pointer to HBA context object.
10502 * @pring: Pointer to driver SLI ring object.
10503 * @cmdiocb: Pointer to driver command iocb object.
10504 *
James Smart5af5eee2010-10-22 11:06:38 -040010505 * This function issues an abort iocb for the provided command iocb down to
10506 * the port. Other than the case the outstanding command iocb is an abort
10507 * request, this function issues abort out unconditionally. This function is
10508 * called with hbalock held. The function returns 0 when it fails due to
10509 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010510 **/
James Smart5af5eee2010-10-22 11:06:38 -040010511static int
10512lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010513 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010514{
James Smart2e0fef82007-06-17 19:56:36 -050010515 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010516 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010517 IOCB_t *icmd = NULL;
10518 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010519 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010520 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010521
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010522 lockdep_assert_held(&phba->hbalock);
10523
James Smart92d7f7b2007-06-17 19:56:38 -050010524 /*
10525 * There are certain command types we don't want to abort. And we
10526 * don't want to abort commands that are already in the process of
10527 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010528 */
10529 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010530 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010531 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10532 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010533 return 0;
10534
dea31012005-04-17 16:05:31 -050010535 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010536 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010537 if (abtsiocbp == NULL)
10538 return 0;
dea31012005-04-17 16:05:31 -050010539
James Smart07951072007-04-25 09:51:38 -040010540 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010541 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010542 */
10543 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10544
dea31012005-04-17 16:05:31 -050010545 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010546 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10547 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010548 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010549 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010550 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10551 }
James Smartda0436e2009-05-22 14:51:39 -040010552 else
10553 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010554 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010555 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010556
James Smart5ffc2662009-11-18 15:39:44 -050010557 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010558 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010559 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10560 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010561 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10562 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010563
James Smart2e0fef82007-06-17 19:56:36 -050010564 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010565 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10566 else
10567 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10568
10569 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010570 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010571
James Smarte8b62012007-08-02 11:10:09 -040010572 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10573 "0339 Abort xri x%x, original iotag x%x, "
10574 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010575 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010576 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010577 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010578
10579 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010580 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10581 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010582 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010583 /* Note: both hbalock and ring_lock need to be set here */
10584 spin_lock_irqsave(&pring->ring_lock, iflags);
10585 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10586 abtsiocbp, 0);
10587 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10588 } else {
10589 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10590 abtsiocbp, 0);
10591 }
James Smart07951072007-04-25 09:51:38 -040010592
James Smartd7c255b2008-08-24 21:50:00 -040010593 if (retval)
10594 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010595
10596 /*
10597 * Caller to this routine should check for IOCB_ERROR
10598 * and handle it properly. This routine no longer removes
10599 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10600 */
10601 return retval;
10602}
10603
10604/**
10605 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10606 * @phba: Pointer to HBA context object.
10607 * @pring: Pointer to driver SLI ring object.
10608 * @cmdiocb: Pointer to driver command iocb object.
10609 *
10610 * This function issues an abort iocb for the provided command iocb. In case
10611 * of unloading, the abort iocb will not be issued to commands on the ELS
10612 * ring. Instead, the callback function shall be changed to those commands
10613 * so that nothing happens when them finishes. This function is called with
10614 * hbalock held. The function returns 0 when the command iocb is an abort
10615 * request.
10616 **/
10617int
10618lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10619 struct lpfc_iocbq *cmdiocb)
10620{
10621 struct lpfc_vport *vport = cmdiocb->vport;
10622 int retval = IOCB_ERROR;
10623 IOCB_t *icmd = NULL;
10624
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010625 lockdep_assert_held(&phba->hbalock);
10626
James Smart5af5eee2010-10-22 11:06:38 -040010627 /*
10628 * There are certain command types we don't want to abort. And we
10629 * don't want to abort commands that are already in the process of
10630 * being aborted.
10631 */
10632 icmd = &cmdiocb->iocb;
10633 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10634 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10635 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10636 return 0;
10637
10638 /*
10639 * If we're unloading, don't abort iocb on the ELS ring, but change
10640 * the callback so that nothing happens when it finishes.
10641 */
10642 if ((vport->load_flag & FC_UNLOADING) &&
10643 (pring->ringno == LPFC_ELS_RING)) {
10644 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10645 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10646 else
10647 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10648 goto abort_iotag_exit;
10649 }
10650
10651 /* Now, we try to issue the abort to the cmdiocb out */
10652 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10653
James Smart07951072007-04-25 09:51:38 -040010654abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010655 /*
10656 * Caller to this routine should check for IOCB_ERROR
10657 * and handle it properly. This routine no longer removes
10658 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010659 */
James Smart2e0fef82007-06-17 19:56:36 -050010660 return retval;
dea31012005-04-17 16:05:31 -050010661}
10662
James Smarte59058c2008-08-24 21:49:00 -040010663/**
James Smart895427b2017-02-12 13:52:30 -080010664 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10665 * @phba: Pointer to HBA context object.
10666 * @pring: Pointer to driver SLI ring object.
10667 * @cmdiocb: Pointer to driver command iocb object.
10668 *
10669 * This function issues an abort iocb for the provided command iocb down to
10670 * the port. Other than the case the outstanding command iocb is an abort
10671 * request, this function issues abort out unconditionally. This function is
10672 * called with hbalock held. The function returns 0 when it fails due to
10673 * memory allocation failure or when the command iocb is an abort request.
10674 **/
10675static int
10676lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10677 struct lpfc_iocbq *cmdiocb)
10678{
10679 struct lpfc_vport *vport = cmdiocb->vport;
10680 struct lpfc_iocbq *abtsiocbp;
10681 union lpfc_wqe *abts_wqe;
10682 int retval;
10683
10684 /*
10685 * There are certain command types we don't want to abort. And we
10686 * don't want to abort commands that are already in the process of
10687 * being aborted.
10688 */
10689 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10690 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10691 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10692 return 0;
10693
10694 /* issue ABTS for this io based on iotag */
10695 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10696 if (abtsiocbp == NULL)
10697 return 0;
10698
10699 /* This signals the response to set the correct status
10700 * before calling the completion handler
10701 */
10702 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10703
10704 /* Complete prepping the abort wqe and issue to the FW. */
10705 abts_wqe = &abtsiocbp->wqe;
10706 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10707 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10708
10709 /* Explicitly set reserved fields to zero.*/
10710 abts_wqe->abort_cmd.rsrvd4 = 0;
10711 abts_wqe->abort_cmd.rsrvd5 = 0;
10712
10713 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10714 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10715 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10716
10717 /* word 7 */
10718 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10719 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10720 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10721 cmdiocb->iocb.ulpClass);
10722
10723 /* word 8 - tell the FW to abort the IO associated with this
10724 * outstanding exchange ID.
10725 */
10726 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10727
10728 /* word 9 - this is the iotag for the abts_wqe completion. */
10729 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10730 abtsiocbp->iotag);
10731
10732 /* word 10 */
10733 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10734 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10735 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10736
10737 /* word 11 */
10738 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10739 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10740 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10741
10742 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10743 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10744 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010745 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010746 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10747 if (retval == IOCB_ERROR) {
10748 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10749 "6147 Failed abts issue_wqe with status x%x "
10750 "for oxid x%x\n",
10751 retval, cmdiocb->sli4_xritag);
10752 lpfc_sli_release_iocbq(phba, abtsiocbp);
10753 return retval;
10754 }
10755
10756 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10757 "6148 Drv Abort NVME Request Issued for "
10758 "ox_id x%x on reqtag x%x\n",
10759 cmdiocb->sli4_xritag,
10760 abtsiocbp->iotag);
10761
10762 return retval;
10763}
10764
10765/**
James Smart5af5eee2010-10-22 11:06:38 -040010766 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10767 * @phba: pointer to lpfc HBA data structure.
10768 *
10769 * This routine will abort all pending and outstanding iocbs to an HBA.
10770 **/
10771void
10772lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10773{
10774 struct lpfc_sli *psli = &phba->sli;
10775 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010776 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010777 int i;
10778
James Smart895427b2017-02-12 13:52:30 -080010779 if (phba->sli_rev != LPFC_SLI_REV4) {
10780 for (i = 0; i < psli->num_rings; i++) {
10781 pring = &psli->sli3_ring[i];
10782 lpfc_sli_abort_iocb_ring(phba, pring);
10783 }
10784 return;
10785 }
10786 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10787 pring = qp->pring;
10788 if (!pring)
10789 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010790 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010791 }
10792}
10793
10794/**
James Smart3621a712009-04-06 18:47:14 -040010795 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010796 * @iocbq: Pointer to driver iocb object.
10797 * @vport: Pointer to driver virtual port object.
10798 * @tgt_id: SCSI ID of the target.
10799 * @lun_id: LUN ID of the scsi device.
10800 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10801 *
James Smart3621a712009-04-06 18:47:14 -040010802 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010803 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10804 * 0 if the filtering criteria is met for the given iocb and will return
10805 * 1 if the filtering criteria is not met.
10806 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10807 * given iocb is for the SCSI device specified by vport, tgt_id and
10808 * lun_id parameter.
10809 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10810 * given iocb is for the SCSI target specified by vport and tgt_id
10811 * parameters.
10812 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10813 * given iocb is for the SCSI host associated with the given vport.
10814 * This function is called with no locks held.
10815 **/
dea31012005-04-17 16:05:31 -050010816static int
James Smart51ef4c22007-08-02 11:10:31 -040010817lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10818 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010819 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010820{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010821 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010822 int rc = 1;
10823
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010824 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10825 return rc;
10826
James Smart51ef4c22007-08-02 11:10:31 -040010827 if (iocbq->vport != vport)
10828 return rc;
10829
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010830 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010831
James Smart495a7142008-06-14 22:52:59 -040010832 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010833 return rc;
10834
10835 switch (ctx_cmd) {
10836 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010837 if ((lpfc_cmd->rdata->pnode) &&
10838 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10839 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010840 rc = 0;
10841 break;
10842 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010843 if ((lpfc_cmd->rdata->pnode) &&
10844 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010845 rc = 0;
10846 break;
dea31012005-04-17 16:05:31 -050010847 case LPFC_CTX_HOST:
10848 rc = 0;
10849 break;
10850 default:
10851 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010852 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010853 break;
10854 }
10855
10856 return rc;
10857}
10858
James Smarte59058c2008-08-24 21:49:00 -040010859/**
James Smart3621a712009-04-06 18:47:14 -040010860 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010861 * @vport: Pointer to virtual port.
10862 * @tgt_id: SCSI ID of the target.
10863 * @lun_id: LUN ID of the scsi device.
10864 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10865 *
10866 * This function returns number of FCP commands pending for the vport.
10867 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10868 * commands pending on the vport associated with SCSI device specified
10869 * by tgt_id and lun_id parameters.
10870 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10871 * commands pending on the vport associated with SCSI target specified
10872 * by tgt_id parameter.
10873 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10874 * commands pending on the vport.
10875 * This function returns the number of iocbs which satisfy the filter.
10876 * This function is called without any lock held.
10877 **/
dea31012005-04-17 16:05:31 -050010878int
James Smart51ef4c22007-08-02 11:10:31 -040010879lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10880 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010881{
James Smart51ef4c22007-08-02 11:10:31 -040010882 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010883 struct lpfc_iocbq *iocbq;
10884 int sum, i;
dea31012005-04-17 16:05:31 -050010885
Johannes Thumshirn31979002016-07-18 16:06:03 +020010886 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010887 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10888 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010889
James Smart51ef4c22007-08-02 11:10:31 -040010890 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10891 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010892 sum++;
dea31012005-04-17 16:05:31 -050010893 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010894 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010895
dea31012005-04-17 16:05:31 -050010896 return sum;
10897}
10898
James Smarte59058c2008-08-24 21:49:00 -040010899/**
James Smart3621a712009-04-06 18:47:14 -040010900 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010901 * @phba: Pointer to HBA context object
10902 * @cmdiocb: Pointer to command iocb object.
10903 * @rspiocb: Pointer to response iocb object.
10904 *
10905 * This function is called when an aborted FCP iocb completes. This
10906 * function is called by the ring event handler with no lock held.
10907 * This function frees the iocb.
10908 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010909void
James Smart2e0fef82007-06-17 19:56:36 -050010910lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10911 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010912{
James Smartcb69f7d2011-12-13 13:21:57 -050010913 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010914 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010915 "original iotag x%x, abort cmd iotag x%x "
10916 "status 0x%x, reason 0x%x\n",
10917 cmdiocb->iocb.un.acxri.abortContextTag,
10918 cmdiocb->iocb.un.acxri.abortIoTag,
10919 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10920 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010921 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010922 return;
10923}
10924
James Smarte59058c2008-08-24 21:49:00 -040010925/**
James Smart3621a712009-04-06 18:47:14 -040010926 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010927 * @vport: Pointer to virtual port.
10928 * @pring: Pointer to driver SLI ring object.
10929 * @tgt_id: SCSI ID of the target.
10930 * @lun_id: LUN ID of the scsi device.
10931 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10932 *
10933 * This function sends an abort command for every SCSI command
10934 * associated with the given virtual port pending on the ring
10935 * filtered by lpfc_sli_validate_fcp_iocb function.
10936 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10937 * FCP iocbs associated with lun specified by tgt_id and lun_id
10938 * parameters
10939 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10940 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10941 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10942 * FCP iocbs associated with virtual port.
10943 * This function returns number of iocbs it failed to abort.
10944 * This function is called with no locks held.
10945 **/
dea31012005-04-17 16:05:31 -050010946int
James Smart51ef4c22007-08-02 11:10:31 -040010947lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10948 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010949{
James Smart51ef4c22007-08-02 11:10:31 -040010950 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010951 struct lpfc_iocbq *iocbq;
10952 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010953 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010954 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010955 int i;
dea31012005-04-17 16:05:31 -050010956
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010957 for (i = 1; i <= phba->sli.last_iotag; i++) {
10958 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010959
James Smart51ef4c22007-08-02 11:10:31 -040010960 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010961 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010962 continue;
10963
James Smartafbd8d82013-09-06 12:22:13 -040010964 /*
10965 * If the iocbq is already being aborted, don't take a second
10966 * action, but do count it.
10967 */
10968 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10969 continue;
10970
dea31012005-04-17 16:05:31 -050010971 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010972 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010973 if (abtsiocb == NULL) {
10974 errcnt++;
10975 continue;
10976 }
dea31012005-04-17 16:05:31 -050010977
James Smartafbd8d82013-09-06 12:22:13 -040010978 /* indicate the IO is being aborted by the driver. */
10979 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10980
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010981 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010982 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10983 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010984 if (phba->sli_rev == LPFC_SLI_REV4)
10985 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10986 else
10987 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010988 abtsiocb->iocb.ulpLe = 1;
10989 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010990 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010991
James Smart5ffc2662009-11-18 15:39:44 -050010992 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010993 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010994 if (iocbq->iocb_flag & LPFC_IO_FCP)
10995 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010996 if (iocbq->iocb_flag & LPFC_IO_FOF)
10997 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010998
James Smart2e0fef82007-06-17 19:56:36 -050010999 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011000 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11001 else
11002 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11003
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011004 /* Setup callback routine and issue the command. */
11005 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040011006 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11007 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011008 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011009 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011010 errcnt++;
11011 continue;
11012 }
11013 }
11014
11015 return errcnt;
11016}
11017
James Smarte59058c2008-08-24 21:49:00 -040011018/**
James Smart98912dda2014-04-04 13:52:31 -040011019 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11020 * @vport: Pointer to virtual port.
11021 * @pring: Pointer to driver SLI ring object.
11022 * @tgt_id: SCSI ID of the target.
11023 * @lun_id: LUN ID of the scsi device.
11024 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11025 *
11026 * This function sends an abort command for every SCSI command
11027 * associated with the given virtual port pending on the ring
11028 * filtered by lpfc_sli_validate_fcp_iocb function.
11029 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11030 * FCP iocbs associated with lun specified by tgt_id and lun_id
11031 * parameters
11032 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11033 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11034 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11035 * FCP iocbs associated with virtual port.
11036 * This function returns number of iocbs it aborted .
11037 * This function is called with no locks held right after a taskmgmt
11038 * command is sent.
11039 **/
11040int
11041lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11042 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11043{
11044 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011045 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011046 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011047 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011048 struct lpfc_iocbq *iocbq;
11049 IOCB_t *icmd;
11050 int sum, i, ret_val;
11051 unsigned long iflags;
11052 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011053
11054 spin_lock_irq(&phba->hbalock);
11055
11056 /* all I/Os are in process of being flushed */
11057 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11058 spin_unlock_irq(&phba->hbalock);
11059 return 0;
11060 }
11061 sum = 0;
11062
11063 for (i = 1; i <= phba->sli.last_iotag; i++) {
11064 iocbq = phba->sli.iocbq_lookup[i];
11065
11066 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11067 cmd) != 0)
11068 continue;
11069
11070 /*
11071 * If the iocbq is already being aborted, don't take a second
11072 * action, but do count it.
11073 */
11074 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11075 continue;
11076
11077 /* issue ABTS for this IOCB based on iotag */
11078 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11079 if (abtsiocbq == NULL)
11080 continue;
11081
11082 icmd = &iocbq->iocb;
11083 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11084 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11085 if (phba->sli_rev == LPFC_SLI_REV4)
11086 abtsiocbq->iocb.un.acxri.abortIoTag =
11087 iocbq->sli4_xritag;
11088 else
11089 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11090 abtsiocbq->iocb.ulpLe = 1;
11091 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11092 abtsiocbq->vport = vport;
11093
11094 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011095 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011096 if (iocbq->iocb_flag & LPFC_IO_FCP)
11097 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011098 if (iocbq->iocb_flag & LPFC_IO_FOF)
11099 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011100
James Smart8c50d252014-09-03 12:58:16 -040011101 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11102 ndlp = lpfc_cmd->rdata->pnode;
11103
11104 if (lpfc_is_link_up(phba) &&
11105 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011106 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11107 else
11108 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11109
11110 /* Setup callback routine and issue the command. */
11111 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11112
11113 /*
11114 * Indicate the IO is being aborted by the driver and set
11115 * the caller's flag into the aborted IO.
11116 */
11117 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11118
11119 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011120 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11121 if (pring_s4 == NULL)
11122 continue;
James Smart98912dda2014-04-04 13:52:31 -040011123 /* Note: both hbalock and ring_lock must be set here */
11124 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11125 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11126 abtsiocbq, 0);
11127 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11128 } else {
11129 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11130 abtsiocbq, 0);
11131 }
11132
11133
11134 if (ret_val == IOCB_ERROR)
11135 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11136 else
11137 sum++;
11138 }
11139 spin_unlock_irq(&phba->hbalock);
11140 return sum;
11141}
11142
11143/**
James Smart3621a712009-04-06 18:47:14 -040011144 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011145 * @phba: Pointer to HBA context object.
11146 * @cmdiocbq: Pointer to command iocb.
11147 * @rspiocbq: Pointer to response iocb.
11148 *
11149 * This function is the completion handler for iocbs issued using
11150 * lpfc_sli_issue_iocb_wait function. This function is called by the
11151 * ring event handler function without any lock held. This function
11152 * can be called from both worker thread context and interrupt
11153 * context. This function also can be called from other thread which
11154 * cleans up the SLI layer objects.
11155 * This function copy the contents of the response iocb to the
11156 * response iocb memory object provided by the caller of
11157 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11158 * sleeps for the iocb completion.
11159 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011160static void
11161lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11162 struct lpfc_iocbq *cmdiocbq,
11163 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011164{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011165 wait_queue_head_t *pdone_q;
11166 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011167 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011168
James Smart2e0fef82007-06-17 19:56:36 -050011169 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011170 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11171
11172 /*
11173 * A time out has occurred for the iocb. If a time out
11174 * completion handler has been supplied, call it. Otherwise,
11175 * just free the iocbq.
11176 */
11177
11178 spin_unlock_irqrestore(&phba->hbalock, iflags);
11179 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11180 cmdiocbq->wait_iocb_cmpl = NULL;
11181 if (cmdiocbq->iocb_cmpl)
11182 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11183 else
11184 lpfc_sli_release_iocbq(phba, cmdiocbq);
11185 return;
11186 }
11187
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011188 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11189 if (cmdiocbq->context2 && rspiocbq)
11190 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11191 &rspiocbq->iocb, sizeof(IOCB_t));
11192
James Smart0f65ff62010-02-26 14:14:23 -050011193 /* Set the exchange busy flag for task management commands */
11194 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11195 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11196 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11197 cur_iocbq);
11198 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11199 }
11200
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011201 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011202 if (pdone_q)
11203 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011204 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011205 return;
11206}
11207
James Smarte59058c2008-08-24 21:49:00 -040011208/**
James Smartd11e31d2009-06-10 17:23:06 -040011209 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11210 * @phba: Pointer to HBA context object..
11211 * @piocbq: Pointer to command iocb.
11212 * @flag: Flag to test.
11213 *
11214 * This routine grabs the hbalock and then test the iocb_flag to
11215 * see if the passed in flag is set.
11216 * Returns:
11217 * 1 if flag is set.
11218 * 0 if flag is not set.
11219 **/
11220static int
11221lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11222 struct lpfc_iocbq *piocbq, uint32_t flag)
11223{
11224 unsigned long iflags;
11225 int ret;
11226
11227 spin_lock_irqsave(&phba->hbalock, iflags);
11228 ret = piocbq->iocb_flag & flag;
11229 spin_unlock_irqrestore(&phba->hbalock, iflags);
11230 return ret;
11231
11232}
11233
11234/**
James Smart3621a712009-04-06 18:47:14 -040011235 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011236 * @phba: Pointer to HBA context object..
11237 * @pring: Pointer to sli ring.
11238 * @piocb: Pointer to command iocb.
11239 * @prspiocbq: Pointer to response iocb.
11240 * @timeout: Timeout in number of seconds.
11241 *
11242 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011243 * iocb to complete. The iocb_cmpl field of the shall be used
11244 * to handle iocbs which time out. If the field is NULL, the
11245 * function shall free the iocbq structure. If more clean up is
11246 * needed, the caller is expected to provide a completion function
11247 * that will provide the needed clean up. If the iocb command is
11248 * not completed within timeout seconds, the function will either
11249 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11250 * completion function set in the iocb_cmpl field and then return
11251 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11252 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011253 * The function waits for the iocb completion using an
11254 * non-interruptible wait.
11255 * This function will sleep while waiting for iocb completion.
11256 * So, this function should not be called from any context which
11257 * does not allow sleeping. Due to the same reason, this function
11258 * cannot be called with interrupt disabled.
11259 * This function assumes that the iocb completions occur while
11260 * this function sleep. So, this function cannot be called from
11261 * the thread which process iocb completion for this ring.
11262 * This function clears the iocb_flag of the iocb object before
11263 * issuing the iocb and the iocb completion handler sets this
11264 * flag and wakes this thread when the iocb completes.
11265 * The contents of the response iocb will be copied to prspiocbq
11266 * by the completion handler when the command completes.
11267 * This function returns IOCB_SUCCESS when success.
11268 * This function is called with no lock held.
11269 **/
dea31012005-04-17 16:05:31 -050011270int
James Smart2e0fef82007-06-17 19:56:36 -050011271lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011272 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011273 struct lpfc_iocbq *piocb,
11274 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011275 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011276{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011277 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011278 long timeleft, timeout_req = 0;
11279 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011280 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011281 struct lpfc_iocbq *iocb;
11282 int txq_cnt = 0;
11283 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011284 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011285 unsigned long iflags;
11286 bool iocb_completed = true;
11287
James Smart895427b2017-02-12 13:52:30 -080011288 if (phba->sli_rev >= LPFC_SLI_REV4)
11289 pring = lpfc_sli4_calc_ring(phba, piocb);
11290 else
11291 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011292 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011293 * If the caller has provided a response iocbq buffer, then context2
11294 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011295 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011296 if (prspiocbq) {
11297 if (piocb->context2)
11298 return IOCB_ERROR;
11299 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011300 }
11301
James Smart5a0916b2013-07-15 18:31:42 -040011302 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011303 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11304 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011305 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011306
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011307 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011308 if (lpfc_readl(phba->HCregaddr, &creg_val))
11309 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011310 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11311 writel(creg_val, phba->HCregaddr);
11312 readl(phba->HCregaddr); /* flush */
11313 }
11314
James Smart2a9bf3d2010-06-07 15:24:45 -040011315 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11316 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011317 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011318 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011319 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011320 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011321 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011322 spin_lock_irqsave(&phba->hbalock, iflags);
11323 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011324
James Smart5a0916b2013-07-15 18:31:42 -040011325 /*
11326 * IOCB timed out. Inform the wake iocb wait
11327 * completion function and set local status
11328 */
11329
11330 iocb_completed = false;
11331 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11332 }
11333 spin_unlock_irqrestore(&phba->hbalock, iflags);
11334 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011335 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011336 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011337 /* Note: we are not indicating if the IOCB has a success
11338 * status or not - that's for the caller to check.
11339 * IOCB_SUCCESS means just that the command was sent and
11340 * completed. Not that it completed successfully.
11341 * */
James Smart7054a602007-04-25 09:52:34 -040011342 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011343 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011344 "0338 IOCB wait timeout error - no "
11345 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011346 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011347 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011348 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011349 "0330 IOCB wake NOT set, "
11350 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011351 timeout, (timeleft / jiffies));
11352 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011353 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011354 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011355 if (phba->cfg_log_verbose & LOG_SLI) {
11356 list_for_each_entry(iocb, &pring->txq, list) {
11357 txq_cnt++;
11358 }
11359 list_for_each_entry(iocb, &pring->txcmplq, list) {
11360 txcmplq_cnt++;
11361 }
11362 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11363 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11364 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11365 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011366 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011367 } else {
11368 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011369 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011370 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011371 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011372 }
11373
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011374 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011375 if (lpfc_readl(phba->HCregaddr, &creg_val))
11376 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011377 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11378 writel(creg_val, phba->HCregaddr);
11379 readl(phba->HCregaddr); /* flush */
11380 }
11381
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011382 if (prspiocbq)
11383 piocb->context2 = NULL;
11384
11385 piocb->context_un.wait_queue = NULL;
11386 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011387 return retval;
11388}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011389
James Smarte59058c2008-08-24 21:49:00 -040011390/**
James Smart3621a712009-04-06 18:47:14 -040011391 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011392 * @phba: Pointer to HBA context object.
11393 * @pmboxq: Pointer to driver mailbox object.
11394 * @timeout: Timeout in number of seconds.
11395 *
11396 * This function issues the mailbox to firmware and waits for the
11397 * mailbox command to complete. If the mailbox command is not
11398 * completed within timeout seconds, it returns MBX_TIMEOUT.
11399 * The function waits for the mailbox completion using an
11400 * interruptible wait. If the thread is woken up due to a
11401 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11402 * should not free the mailbox resources, if this function returns
11403 * MBX_TIMEOUT.
11404 * This function will sleep while waiting for mailbox completion.
11405 * So, this function should not be called from any context which
11406 * does not allow sleeping. Due to the same reason, this function
11407 * cannot be called with interrupt disabled.
11408 * This function assumes that the mailbox completion occurs while
11409 * this function sleep. So, this function cannot be called from
11410 * the worker thread which processes mailbox completion.
11411 * This function is called in the context of HBA management
11412 * applications.
11413 * This function returns MBX_SUCCESS when successful.
11414 * This function is called with no lock held.
11415 **/
dea31012005-04-17 16:05:31 -050011416int
James Smart2e0fef82007-06-17 19:56:36 -050011417lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011418 uint32_t timeout)
11419{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011420 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011421 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011422 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011423 unsigned long flag;
dea31012005-04-17 16:05:31 -050011424
James Smartb230b8a2013-05-31 17:05:27 -040011425 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011426 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011427 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011428
James Smart495a7142008-06-14 22:52:59 -040011429 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011430 /* setup wake call as IOCB callback */
11431 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11432 /* setup context field to pass wait_queue pointer to wake function */
11433 pmboxq->context1 = &done_q;
11434
dea31012005-04-17 16:05:31 -050011435 /* now issue the command */
11436 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011437 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011438 wait_event_interruptible_timeout(done_q,
11439 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011440 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011441
James Smart858c9f62007-06-17 19:56:39 -050011442 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011443 /* restore the possible extended buffer for free resource */
11444 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011445 /*
11446 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11447 * else do not free the resources.
11448 */
James Smartd7c47992010-06-08 18:31:54 -040011449 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011450 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011451 } else {
James Smart7054a602007-04-25 09:52:34 -040011452 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011453 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11454 }
11455 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011456 } else {
11457 /* restore the possible extended buffer for free resource */
11458 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011459 }
11460
dea31012005-04-17 16:05:31 -050011461 return retval;
11462}
11463
James Smarte59058c2008-08-24 21:49:00 -040011464/**
James Smart3772a992009-05-22 14:50:54 -040011465 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011466 * @phba: Pointer to HBA context.
11467 *
James Smart3772a992009-05-22 14:50:54 -040011468 * This function is called to shutdown the driver's mailbox sub-system.
11469 * It first marks the mailbox sub-system is in a block state to prevent
11470 * the asynchronous mailbox command from issued off the pending mailbox
11471 * command queue. If the mailbox command sub-system shutdown is due to
11472 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11473 * the mailbox sub-system flush routine to forcefully bring down the
11474 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11475 * as with offline or HBA function reset), this routine will wait for the
11476 * outstanding mailbox command to complete before invoking the mailbox
11477 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011478 **/
James Smart3772a992009-05-22 14:50:54 -040011479void
James Smart618a5232012-06-12 13:54:36 -040011480lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011481{
James Smart3772a992009-05-22 14:50:54 -040011482 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011483 unsigned long timeout;
11484
James Smart618a5232012-06-12 13:54:36 -040011485 if (mbx_action == LPFC_MBX_NO_WAIT) {
11486 /* delay 100ms for port state */
11487 msleep(100);
11488 lpfc_sli_mbox_sys_flush(phba);
11489 return;
11490 }
James Smarta183a152011-10-10 21:32:43 -040011491 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011492
James Smart3772a992009-05-22 14:50:54 -040011493 spin_lock_irq(&phba->hbalock);
11494 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011495
11496 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011497 /* Determine how long we might wait for the active mailbox
11498 * command to be gracefully completed by firmware.
11499 */
James Smarta183a152011-10-10 21:32:43 -040011500 if (phba->sli.mbox_active)
11501 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11502 phba->sli.mbox_active) *
11503 1000) + jiffies;
11504 spin_unlock_irq(&phba->hbalock);
11505
James Smart3772a992009-05-22 14:50:54 -040011506 while (phba->sli.mbox_active) {
11507 /* Check active mailbox complete status every 2ms */
11508 msleep(2);
11509 if (time_after(jiffies, timeout))
11510 /* Timeout, let the mailbox flush routine to
11511 * forcefully release active mailbox command
11512 */
11513 break;
11514 }
James Smartd7069f02012-03-01 22:36:29 -050011515 } else
11516 spin_unlock_irq(&phba->hbalock);
11517
James Smart3772a992009-05-22 14:50:54 -040011518 lpfc_sli_mbox_sys_flush(phba);
11519}
11520
11521/**
11522 * lpfc_sli_eratt_read - read sli-3 error attention events
11523 * @phba: Pointer to HBA context.
11524 *
11525 * This function is called to read the SLI3 device error attention registers
11526 * for possible error attention events. The caller must hold the hostlock
11527 * with spin_lock_irq().
11528 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011529 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011530 * Register and returns 0 otherwise.
11531 **/
11532static int
11533lpfc_sli_eratt_read(struct lpfc_hba *phba)
11534{
James Smarted957682007-06-17 19:56:37 -050011535 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011536
James Smart3772a992009-05-22 14:50:54 -040011537 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011538 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11539 goto unplug_err;
11540
James Smart3772a992009-05-22 14:50:54 -040011541 if (ha_copy & HA_ERATT) {
11542 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011543 if (lpfc_sli_read_hs(phba))
11544 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011545
James Smart3772a992009-05-22 14:50:54 -040011546 /* Check if there is a deferred error condition is active */
11547 if ((HS_FFER1 & phba->work_hs) &&
11548 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011549 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011550 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011551 /* Clear all interrupt enable conditions */
11552 writel(0, phba->HCregaddr);
11553 readl(phba->HCregaddr);
11554 }
11555
11556 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011557 phba->work_ha |= HA_ERATT;
11558 /* Indicate polling handles this ERATT */
11559 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011560 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011561 }
James Smart3772a992009-05-22 14:50:54 -040011562 return 0;
James Smart9940b972011-03-11 16:06:12 -050011563
11564unplug_err:
11565 /* Set the driver HS work bitmap */
11566 phba->work_hs |= UNPLUG_ERR;
11567 /* Set the driver HA work bitmap */
11568 phba->work_ha |= HA_ERATT;
11569 /* Indicate polling handles this ERATT */
11570 phba->hba_flag |= HBA_ERATT_HANDLED;
11571 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011572}
11573
James Smarte59058c2008-08-24 21:49:00 -040011574/**
James Smartda0436e2009-05-22 14:51:39 -040011575 * lpfc_sli4_eratt_read - read sli-4 error attention events
11576 * @phba: Pointer to HBA context.
11577 *
11578 * This function is called to read the SLI4 device error attention registers
11579 * for possible error attention events. The caller must hold the hostlock
11580 * with spin_lock_irq().
11581 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011582 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011583 * Register and returns 0 otherwise.
11584 **/
11585static int
11586lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11587{
11588 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011589 uint32_t if_type, portsmphr;
11590 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011591
James Smart2fcee4b2010-12-15 17:57:46 -050011592 /*
11593 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011594 * registers for error attention. This can be changed later.
11595 */
James Smart2fcee4b2010-12-15 17:57:46 -050011596 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11597 switch (if_type) {
11598 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011599 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11600 &uerr_sta_lo) ||
11601 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11602 &uerr_sta_hi)) {
11603 phba->work_hs |= UNPLUG_ERR;
11604 phba->work_ha |= HA_ERATT;
11605 phba->hba_flag |= HBA_ERATT_HANDLED;
11606 return 1;
11607 }
James Smart2fcee4b2010-12-15 17:57:46 -050011608 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11609 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11610 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11611 "1423 HBA Unrecoverable error: "
11612 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11613 "ue_mask_lo_reg=0x%x, "
11614 "ue_mask_hi_reg=0x%x\n",
11615 uerr_sta_lo, uerr_sta_hi,
11616 phba->sli4_hba.ue_mask_lo,
11617 phba->sli4_hba.ue_mask_hi);
11618 phba->work_status[0] = uerr_sta_lo;
11619 phba->work_status[1] = uerr_sta_hi;
11620 phba->work_ha |= HA_ERATT;
11621 phba->hba_flag |= HBA_ERATT_HANDLED;
11622 return 1;
11623 }
11624 break;
11625 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011626 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11627 &portstat_reg.word0) ||
11628 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11629 &portsmphr)){
11630 phba->work_hs |= UNPLUG_ERR;
11631 phba->work_ha |= HA_ERATT;
11632 phba->hba_flag |= HBA_ERATT_HANDLED;
11633 return 1;
11634 }
James Smart2fcee4b2010-12-15 17:57:46 -050011635 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11636 phba->work_status[0] =
11637 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11638 phba->work_status[1] =
11639 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011641 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011642 "port status reg 0x%x, "
11643 "port smphr reg 0x%x, "
11644 "error 1=0x%x, error 2=0x%x\n",
11645 portstat_reg.word0,
11646 portsmphr,
11647 phba->work_status[0],
11648 phba->work_status[1]);
11649 phba->work_ha |= HA_ERATT;
11650 phba->hba_flag |= HBA_ERATT_HANDLED;
11651 return 1;
11652 }
11653 break;
11654 case LPFC_SLI_INTF_IF_TYPE_1:
11655 default:
James Smarta747c9c2009-11-18 15:41:10 -050011656 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011657 "2886 HBA Error Attention on unsupported "
11658 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011659 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011660 }
James Smart2fcee4b2010-12-15 17:57:46 -050011661
James Smartda0436e2009-05-22 14:51:39 -040011662 return 0;
11663}
11664
11665/**
James Smart3621a712009-04-06 18:47:14 -040011666 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011667 * @phba: Pointer to HBA context.
11668 *
James Smart3772a992009-05-22 14:50:54 -040011669 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011670 * error attention register bit for error attention events.
11671 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011672 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011673 * Register and returns 0 otherwise.
11674 **/
11675int
11676lpfc_sli_check_eratt(struct lpfc_hba *phba)
11677{
11678 uint32_t ha_copy;
11679
11680 /* If somebody is waiting to handle an eratt, don't process it
11681 * here. The brdkill function will do this.
11682 */
11683 if (phba->link_flag & LS_IGNORE_ERATT)
11684 return 0;
11685
11686 /* Check if interrupt handler handles this ERATT */
11687 spin_lock_irq(&phba->hbalock);
11688 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11689 /* Interrupt handler has handled ERATT */
11690 spin_unlock_irq(&phba->hbalock);
11691 return 0;
11692 }
11693
James Smarta257bf92009-04-06 18:48:10 -040011694 /*
11695 * If there is deferred error attention, do not check for error
11696 * attention
11697 */
11698 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11699 spin_unlock_irq(&phba->hbalock);
11700 return 0;
11701 }
11702
James Smart3772a992009-05-22 14:50:54 -040011703 /* If PCI channel is offline, don't process it */
11704 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011705 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011706 return 0;
11707 }
11708
11709 switch (phba->sli_rev) {
11710 case LPFC_SLI_REV2:
11711 case LPFC_SLI_REV3:
11712 /* Read chip Host Attention (HA) register */
11713 ha_copy = lpfc_sli_eratt_read(phba);
11714 break;
James Smartda0436e2009-05-22 14:51:39 -040011715 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011716 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011717 ha_copy = lpfc_sli4_eratt_read(phba);
11718 break;
James Smart3772a992009-05-22 14:50:54 -040011719 default:
11720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11721 "0299 Invalid SLI revision (%d)\n",
11722 phba->sli_rev);
11723 ha_copy = 0;
11724 break;
James Smart93996272008-08-24 21:50:30 -040011725 }
11726 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011727
11728 return ha_copy;
11729}
11730
11731/**
11732 * lpfc_intr_state_check - Check device state for interrupt handling
11733 * @phba: Pointer to HBA context.
11734 *
11735 * This inline routine checks whether a device or its PCI slot is in a state
11736 * that the interrupt should be handled.
11737 *
11738 * This function returns 0 if the device or the PCI slot is in a state that
11739 * interrupt should be handled, otherwise -EIO.
11740 */
11741static inline int
11742lpfc_intr_state_check(struct lpfc_hba *phba)
11743{
11744 /* If the pci channel is offline, ignore all the interrupts */
11745 if (unlikely(pci_channel_offline(phba->pcidev)))
11746 return -EIO;
11747
11748 /* Update device level interrupt statistics */
11749 phba->sli.slistat.sli_intr++;
11750
11751 /* Ignore all interrupts during initialization. */
11752 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11753 return -EIO;
11754
James Smart93996272008-08-24 21:50:30 -040011755 return 0;
11756}
11757
11758/**
James Smart3772a992009-05-22 14:50:54 -040011759 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011760 * @irq: Interrupt number.
11761 * @dev_id: The device context pointer.
11762 *
James Smart93996272008-08-24 21:50:30 -040011763 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011764 * service routine when device with SLI-3 interface spec is enabled with
11765 * MSI-X multi-message interrupt mode and there are slow-path events in
11766 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11767 * interrupt mode, this function is called as part of the device-level
11768 * interrupt handler. When the PCI slot is in error recovery or the HBA
11769 * is undergoing initialization, the interrupt handler will not process
11770 * the interrupt. The link attention and ELS ring attention events are
11771 * handled by the worker thread. The interrupt handler signals the worker
11772 * thread and returns for these events. This function is called without
11773 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011774 * structures.
11775 *
11776 * This function returns IRQ_HANDLED when interrupt is handled else it
11777 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011778 **/
dea31012005-04-17 16:05:31 -050011779irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011780lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011781{
James Smart2e0fef82007-06-17 19:56:36 -050011782 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011783 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011784 uint32_t work_ha_copy;
11785 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011786 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011787 uint32_t control;
11788
James Smart92d7f7b2007-06-17 19:56:38 -050011789 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011790 struct lpfc_vport *vport;
11791 struct lpfc_nodelist *ndlp;
11792 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011793 LPFC_MBOXQ_t *pmb;
11794 int rc;
11795
dea31012005-04-17 16:05:31 -050011796 /*
11797 * Get the driver's phba structure from the dev_id and
11798 * assume the HBA is not interrupting.
11799 */
James Smart93996272008-08-24 21:50:30 -040011800 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011801
11802 if (unlikely(!phba))
11803 return IRQ_NONE;
11804
dea31012005-04-17 16:05:31 -050011805 /*
James Smart93996272008-08-24 21:50:30 -040011806 * Stuff needs to be attented to when this function is invoked as an
11807 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011808 */
James Smart93996272008-08-24 21:50:30 -040011809 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011810 /* Check device state for handling interrupt */
11811 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011812 return IRQ_NONE;
11813 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011814 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011815 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11816 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011817 /* If somebody is waiting to handle an eratt don't process it
11818 * here. The brdkill function will do this.
11819 */
11820 if (phba->link_flag & LS_IGNORE_ERATT)
11821 ha_copy &= ~HA_ERATT;
11822 /* Check the need for handling ERATT in interrupt handler */
11823 if (ha_copy & HA_ERATT) {
11824 if (phba->hba_flag & HBA_ERATT_HANDLED)
11825 /* ERATT polling has handled ERATT */
11826 ha_copy &= ~HA_ERATT;
11827 else
11828 /* Indicate interrupt handler handles ERATT */
11829 phba->hba_flag |= HBA_ERATT_HANDLED;
11830 }
James Smarta257bf92009-04-06 18:48:10 -040011831
11832 /*
11833 * If there is deferred error attention, do not check for any
11834 * interrupt.
11835 */
11836 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011837 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011838 return IRQ_NONE;
11839 }
11840
James Smart93996272008-08-24 21:50:30 -040011841 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011842 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11843 goto unplug_error;
11844
James Smarta747c9c2009-11-18 15:41:10 -050011845 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11846 HC_LAINT_ENA | HC_ERINT_ENA),
11847 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011848 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11849 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011850 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011851 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011852 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011853 } else
11854 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011855
dea31012005-04-17 16:05:31 -050011856 work_ha_copy = ha_copy & phba->work_ha_mask;
11857
James Smart93996272008-08-24 21:50:30 -040011858 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011859 if (work_ha_copy & HA_LATT) {
11860 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11861 /*
11862 * Turn off Link Attention interrupts
11863 * until CLEAR_LA done
11864 */
James Smart5b75da22008-12-04 22:39:35 -050011865 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011866 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011867 if (lpfc_readl(phba->HCregaddr, &control))
11868 goto unplug_error;
dea31012005-04-17 16:05:31 -050011869 control &= ~HC_LAINT_ENA;
11870 writel(control, phba->HCregaddr);
11871 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011872 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011873 }
11874 else
11875 work_ha_copy &= ~HA_LATT;
11876 }
11877
James Smart93996272008-08-24 21:50:30 -040011878 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011879 /*
11880 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11881 * the only slow ring.
11882 */
11883 status = (work_ha_copy &
11884 (HA_RXMASK << (4*LPFC_ELS_RING)));
11885 status >>= (4*LPFC_ELS_RING);
11886 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011887 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011888 if (lpfc_readl(phba->HCregaddr, &control))
11889 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011890
11891 lpfc_debugfs_slow_ring_trc(phba,
11892 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11893 control, status,
11894 (uint32_t)phba->sli.slistat.sli_intr);
11895
James Smart858c9f62007-06-17 19:56:39 -050011896 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011897 lpfc_debugfs_slow_ring_trc(phba,
11898 "ISR Disable ring:"
11899 "pwork:x%x hawork:x%x wait:x%x",
11900 phba->work_ha, work_ha_copy,
11901 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011902 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011903
James Smart858c9f62007-06-17 19:56:39 -050011904 control &=
11905 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011906 writel(control, phba->HCregaddr);
11907 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011908 }
James Smarta58cbd52007-08-02 11:09:43 -040011909 else {
11910 lpfc_debugfs_slow_ring_trc(phba,
11911 "ISR slow ring: pwork:"
11912 "x%x hawork:x%x wait:x%x",
11913 phba->work_ha, work_ha_copy,
11914 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011915 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011916 }
James Smart5b75da22008-12-04 22:39:35 -050011917 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011918 }
11919 }
James Smart5b75da22008-12-04 22:39:35 -050011920 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011921 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011922 if (lpfc_sli_read_hs(phba))
11923 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011924 /*
11925 * Check if there is a deferred error condition
11926 * is active
11927 */
11928 if ((HS_FFER1 & phba->work_hs) &&
11929 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011930 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11931 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011932 phba->hba_flag |= DEFER_ERATT;
11933 /* Clear all interrupt enable conditions */
11934 writel(0, phba->HCregaddr);
11935 readl(phba->HCregaddr);
11936 }
11937 }
11938
James Smart93996272008-08-24 21:50:30 -040011939 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011940 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011941 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011942 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011943 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011944
11945 /* First check out the status word */
11946 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11947 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011948 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011949 /*
11950 * Stray Mailbox Interrupt, mbxCommand <cmd>
11951 * mbxStatus <status>
11952 */
James Smart09372822008-01-11 01:52:54 -050011953 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011954 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011955 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011956 "Interrupt mbxCommand x%x "
11957 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011958 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011959 pmbox->mbxCommand,
11960 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011961 /* clear mailbox attention bit */
11962 work_ha_copy &= ~HA_MBATT;
11963 } else {
James Smart97eab632008-04-07 10:16:05 -040011964 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011965 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011966 phba->last_completion_time = jiffies;
11967 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011968 if (pmb->mbox_cmpl) {
11969 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11970 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011971 if (pmb->out_ext_byte_len &&
11972 pmb->context2)
11973 lpfc_sli_pcimem_bcopy(
11974 phba->mbox_ext,
11975 pmb->context2,
11976 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011977 }
James Smart09372822008-01-11 01:52:54 -050011978 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11979 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11980
11981 lpfc_debugfs_disc_trc(vport,
11982 LPFC_DISC_TRC_MBOX_VPORT,
11983 "MBOX dflt rpi: : "
11984 "status:x%x rpi:x%x",
11985 (uint32_t)pmbox->mbxStatus,
11986 pmbox->un.varWords[0], 0);
11987
11988 if (!pmbox->mbxStatus) {
11989 mp = (struct lpfc_dmabuf *)
11990 (pmb->context1);
11991 ndlp = (struct lpfc_nodelist *)
11992 pmb->context2;
11993
11994 /* Reg_LOGIN of dflt RPI was
11995 * successful. new lets get
11996 * rid of the RPI using the
11997 * same mbox buffer.
11998 */
11999 lpfc_unreg_login(phba,
12000 vport->vpi,
12001 pmbox->un.varWords[0],
12002 pmb);
12003 pmb->mbox_cmpl =
12004 lpfc_mbx_cmpl_dflt_rpi;
12005 pmb->context1 = mp;
12006 pmb->context2 = ndlp;
12007 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012008 rc = lpfc_sli_issue_mbox(phba,
12009 pmb,
12010 MBX_NOWAIT);
12011 if (rc != MBX_BUSY)
12012 lpfc_printf_log(phba,
12013 KERN_ERR,
12014 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012015 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012016 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012017 if (rc != MBX_NOT_FINISHED)
12018 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012019 }
12020 }
James Smart5b75da22008-12-04 22:39:35 -050012021 spin_lock_irqsave(
12022 &phba->pport->work_port_lock,
12023 iflag);
James Smart09372822008-01-11 01:52:54 -050012024 phba->pport->work_port_events &=
12025 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012026 spin_unlock_irqrestore(
12027 &phba->pport->work_port_lock,
12028 iflag);
James Smart09372822008-01-11 01:52:54 -050012029 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012030 }
James Smart97eab632008-04-07 10:16:05 -040012031 } else
James Smart5b75da22008-12-04 22:39:35 -050012032 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012033
James Smart92d7f7b2007-06-17 19:56:38 -050012034 if ((work_ha_copy & HA_MBATT) &&
12035 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012036send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012037 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012038 do {
12039 rc = lpfc_sli_issue_mbox(phba, NULL,
12040 MBX_NOWAIT);
12041 } while (rc == MBX_NOT_FINISHED);
12042 if (rc != MBX_SUCCESS)
12043 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12044 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012045 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012046 }
12047
James Smart5b75da22008-12-04 22:39:35 -050012048 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012049 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012050 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012051 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012052 }
James Smart93996272008-08-24 21:50:30 -040012053 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012054unplug_error:
12055 spin_unlock_irqrestore(&phba->hbalock, iflag);
12056 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012057
James Smart3772a992009-05-22 14:50:54 -040012058} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012059
12060/**
James Smart3772a992009-05-22 14:50:54 -040012061 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012062 * @irq: Interrupt number.
12063 * @dev_id: The device context pointer.
12064 *
12065 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012066 * service routine when device with SLI-3 interface spec is enabled with
12067 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12068 * ring event in the HBA. However, when the device is enabled with either
12069 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12070 * device-level interrupt handler. When the PCI slot is in error recovery
12071 * or the HBA is undergoing initialization, the interrupt handler will not
12072 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12073 * the intrrupt context. This function is called without any lock held.
12074 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012075 *
12076 * This function returns IRQ_HANDLED when interrupt is handled else it
12077 * returns IRQ_NONE.
12078 **/
12079irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012080lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012081{
12082 struct lpfc_hba *phba;
12083 uint32_t ha_copy;
12084 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012085 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012086 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012087
12088 /* Get the driver's phba structure from the dev_id and
12089 * assume the HBA is not interrupting.
12090 */
12091 phba = (struct lpfc_hba *) dev_id;
12092
12093 if (unlikely(!phba))
12094 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012095
12096 /*
James Smart93996272008-08-24 21:50:30 -040012097 * Stuff needs to be attented to when this function is invoked as an
12098 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012099 */
James Smart93996272008-08-24 21:50:30 -040012100 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012101 /* Check device state for handling interrupt */
12102 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012103 return IRQ_NONE;
12104 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012105 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12106 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012107 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012108 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012109 /*
12110 * If there is deferred error attention, do not check for
12111 * any interrupt.
12112 */
12113 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012114 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012115 return IRQ_NONE;
12116 }
James Smart93996272008-08-24 21:50:30 -040012117 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12118 phba->HAregaddr);
12119 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012120 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012121 } else
12122 ha_copy = phba->ha_copy;
12123
12124 /*
12125 * Process all events on FCP ring. Take the optimized path for FCP IO.
12126 */
12127 ha_copy &= ~(phba->work_ha_mask);
12128
12129 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012130 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012131 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012132 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012133 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012134
12135 if (phba->cfg_multi_ring_support == 2) {
12136 /*
James Smart93996272008-08-24 21:50:30 -040012137 * Process all events on extra ring. Take the optimized path
12138 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012139 */
James Smart93996272008-08-24 21:50:30 -040012140 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012141 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012142 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012143 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012144 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012145 status);
12146 }
12147 }
dea31012005-04-17 16:05:31 -050012148 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012149} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012150
James Smart93996272008-08-24 21:50:30 -040012151/**
James Smart3772a992009-05-22 14:50:54 -040012152 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012153 * @irq: Interrupt number.
12154 * @dev_id: The device context pointer.
12155 *
James Smart3772a992009-05-22 14:50:54 -040012156 * This function is the HBA device-level interrupt handler to device with
12157 * SLI-3 interface spec, called from the PCI layer when either MSI or
12158 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12159 * requires driver attention. This function invokes the slow-path interrupt
12160 * attention handling function and fast-path interrupt attention handling
12161 * function in turn to process the relevant HBA attention events. This
12162 * function is called without any lock held. It gets the hbalock to access
12163 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012164 *
12165 * This function returns IRQ_HANDLED when interrupt is handled, else it
12166 * returns IRQ_NONE.
12167 **/
12168irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012169lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012170{
12171 struct lpfc_hba *phba;
12172 irqreturn_t sp_irq_rc, fp_irq_rc;
12173 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012174 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012175
12176 /*
12177 * Get the driver's phba structure from the dev_id and
12178 * assume the HBA is not interrupting.
12179 */
12180 phba = (struct lpfc_hba *) dev_id;
12181
12182 if (unlikely(!phba))
12183 return IRQ_NONE;
12184
James Smart3772a992009-05-22 14:50:54 -040012185 /* Check device state for handling interrupt */
12186 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012187 return IRQ_NONE;
12188
12189 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012190 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12191 spin_unlock(&phba->hbalock);
12192 return IRQ_HANDLED;
12193 }
12194
James Smart93996272008-08-24 21:50:30 -040012195 if (unlikely(!phba->ha_copy)) {
12196 spin_unlock(&phba->hbalock);
12197 return IRQ_NONE;
12198 } else if (phba->ha_copy & HA_ERATT) {
12199 if (phba->hba_flag & HBA_ERATT_HANDLED)
12200 /* ERATT polling has handled ERATT */
12201 phba->ha_copy &= ~HA_ERATT;
12202 else
12203 /* Indicate interrupt handler handles ERATT */
12204 phba->hba_flag |= HBA_ERATT_HANDLED;
12205 }
12206
James Smarta257bf92009-04-06 18:48:10 -040012207 /*
12208 * If there is deferred error attention, do not check for any interrupt.
12209 */
12210 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012211 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012212 return IRQ_NONE;
12213 }
12214
James Smart93996272008-08-24 21:50:30 -040012215 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012216 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12217 spin_unlock(&phba->hbalock);
12218 return IRQ_HANDLED;
12219 }
James Smarta747c9c2009-11-18 15:41:10 -050012220 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12221 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12222 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012223 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012224 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012225 readl(phba->HAregaddr); /* flush */
12226 spin_unlock(&phba->hbalock);
12227
12228 /*
12229 * Invokes slow-path host attention interrupt handling as appropriate.
12230 */
12231
12232 /* status of events with mailbox and link attention */
12233 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12234
12235 /* status of events with ELS ring */
12236 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12237 status2 >>= (4*LPFC_ELS_RING);
12238
12239 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012240 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012241 else
12242 sp_irq_rc = IRQ_NONE;
12243
12244 /*
12245 * Invoke fast-path host attention interrupt handling as appropriate.
12246 */
12247
12248 /* status of events with FCP ring */
12249 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12250 status1 >>= (4*LPFC_FCP_RING);
12251
12252 /* status of events with extra ring */
12253 if (phba->cfg_multi_ring_support == 2) {
12254 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12255 status2 >>= (4*LPFC_EXTRA_RING);
12256 } else
12257 status2 = 0;
12258
12259 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012260 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012261 else
12262 fp_irq_rc = IRQ_NONE;
12263
12264 /* Return device-level interrupt handling status */
12265 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012266} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012267
12268/**
12269 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12270 * @phba: pointer to lpfc hba data structure.
12271 *
12272 * This routine is invoked by the worker thread to process all the pending
12273 * SLI4 FCP abort XRI events.
12274 **/
12275void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12276{
12277 struct lpfc_cq_event *cq_event;
12278
12279 /* First, declare the fcp xri abort event has been handled */
12280 spin_lock_irq(&phba->hbalock);
12281 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12282 spin_unlock_irq(&phba->hbalock);
12283 /* Now, handle all the fcp xri abort events */
12284 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12285 /* Get the first event from the head of the event queue */
12286 spin_lock_irq(&phba->hbalock);
12287 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12288 cq_event, struct lpfc_cq_event, list);
12289 spin_unlock_irq(&phba->hbalock);
12290 /* Notify aborted XRI for FCP work queue */
12291 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12292 /* Free the event processed back to the free pool */
12293 lpfc_sli4_cq_event_release(phba, cq_event);
12294 }
12295}
12296
12297/**
James Smart318083a2017-03-04 09:30:30 -080012298 * lpfc_sli4_nvme_xri_abort_event_proc - Process nvme xri abort event
12299 * @phba: pointer to lpfc hba data structure.
12300 *
12301 * This routine is invoked by the worker thread to process all the pending
12302 * SLI4 NVME abort XRI events.
12303 **/
12304void lpfc_sli4_nvme_xri_abort_event_proc(struct lpfc_hba *phba)
12305{
12306 struct lpfc_cq_event *cq_event;
12307
12308 /* First, declare the fcp xri abort event has been handled */
12309 spin_lock_irq(&phba->hbalock);
12310 phba->hba_flag &= ~NVME_XRI_ABORT_EVENT;
12311 spin_unlock_irq(&phba->hbalock);
12312 /* Now, handle all the fcp xri abort events */
12313 while (!list_empty(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue)) {
12314 /* Get the first event from the head of the event queue */
12315 spin_lock_irq(&phba->hbalock);
12316 list_remove_head(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
12317 cq_event, struct lpfc_cq_event, list);
12318 spin_unlock_irq(&phba->hbalock);
12319 /* Notify aborted XRI for NVME work queue */
12320 if (phba->nvmet_support) {
12321 lpfc_sli4_nvmet_xri_aborted(phba,
12322 &cq_event->cqe.wcqe_axri);
12323 } else {
12324 lpfc_sli4_nvme_xri_aborted(phba,
12325 &cq_event->cqe.wcqe_axri);
12326 }
12327 /* Free the event processed back to the free pool */
12328 lpfc_sli4_cq_event_release(phba, cq_event);
12329 }
12330}
12331
12332/**
James Smart4f774512009-05-22 14:52:35 -040012333 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12334 * @phba: pointer to lpfc hba data structure.
12335 *
12336 * This routine is invoked by the worker thread to process all the pending
12337 * SLI4 els abort xri events.
12338 **/
12339void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12340{
12341 struct lpfc_cq_event *cq_event;
12342
12343 /* First, declare the els xri abort event has been handled */
12344 spin_lock_irq(&phba->hbalock);
12345 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12346 spin_unlock_irq(&phba->hbalock);
12347 /* Now, handle all the els xri abort events */
12348 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12349 /* Get the first event from the head of the event queue */
12350 spin_lock_irq(&phba->hbalock);
12351 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12352 cq_event, struct lpfc_cq_event, list);
12353 spin_unlock_irq(&phba->hbalock);
12354 /* Notify aborted XRI for ELS work queue */
12355 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12356 /* Free the event processed back to the free pool */
12357 lpfc_sli4_cq_event_release(phba, cq_event);
12358 }
12359}
12360
James Smart341af102010-01-26 23:07:37 -050012361/**
12362 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12363 * @phba: pointer to lpfc hba data structure
12364 * @pIocbIn: pointer to the rspiocbq
12365 * @pIocbOut: pointer to the cmdiocbq
12366 * @wcqe: pointer to the complete wcqe
12367 *
12368 * This routine transfers the fields of a command iocbq to a response iocbq
12369 * by copying all the IOCB fields from command iocbq and transferring the
12370 * completion status information from the complete wcqe.
12371 **/
James Smart4f774512009-05-22 14:52:35 -040012372static void
James Smart341af102010-01-26 23:07:37 -050012373lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12374 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012375 struct lpfc_iocbq *pIocbOut,
12376 struct lpfc_wcqe_complete *wcqe)
12377{
James Smartaf227412013-10-10 12:23:10 -040012378 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012379 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012380 uint32_t status, max_response;
12381 struct lpfc_dmabuf *dmabuf;
12382 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012383 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12384
12385 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12386 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012387 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012388 status = bf_get(lpfc_wcqe_c_status, wcqe);
12389 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012390 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12391 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12392 pIocbIn->iocb.un.fcpi.fcpi_parm =
12393 pIocbOut->iocb.un.fcpi.fcpi_parm -
12394 wcqe->total_data_placed;
12395 else
12396 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012397 else {
James Smart4f774512009-05-22 14:52:35 -040012398 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012399 switch (pIocbOut->iocb.ulpCommand) {
12400 case CMD_ELS_REQUEST64_CR:
12401 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12402 bpl = (struct ulp_bde64 *)dmabuf->virt;
12403 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12404 max_response = bde.tus.f.bdeSize;
12405 break;
12406 case CMD_GEN_REQUEST64_CR:
12407 max_response = 0;
12408 if (!pIocbOut->context3)
12409 break;
12410 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12411 sizeof(struct ulp_bde64);
12412 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12413 bpl = (struct ulp_bde64 *)dmabuf->virt;
12414 for (i = 0; i < numBdes; i++) {
12415 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12416 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12417 max_response += bde.tus.f.bdeSize;
12418 }
12419 break;
12420 default:
12421 max_response = wcqe->total_data_placed;
12422 break;
12423 }
12424 if (max_response < wcqe->total_data_placed)
12425 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12426 else
12427 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12428 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012429 }
James Smart341af102010-01-26 23:07:37 -050012430
James Smartacd68592012-01-18 16:25:09 -050012431 /* Convert BG errors for completion status */
12432 if (status == CQE_STATUS_DI_ERROR) {
12433 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12434
12435 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12436 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12437 else
12438 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12439
12440 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12441 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12442 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12443 BGS_GUARD_ERR_MASK;
12444 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12445 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12446 BGS_APPTAG_ERR_MASK;
12447 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12448 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12449 BGS_REFTAG_ERR_MASK;
12450
12451 /* Check to see if there was any good data before the error */
12452 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12453 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12454 BGS_HI_WATER_MARK_PRESENT_MASK;
12455 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12456 wcqe->total_data_placed;
12457 }
12458
12459 /*
12460 * Set ALL the error bits to indicate we don't know what
12461 * type of error it is.
12462 */
12463 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12464 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12465 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12466 BGS_GUARD_ERR_MASK);
12467 }
12468
James Smart341af102010-01-26 23:07:37 -050012469 /* Pick up HBA exchange busy condition */
12470 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12471 spin_lock_irqsave(&phba->hbalock, iflags);
12472 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12473 spin_unlock_irqrestore(&phba->hbalock, iflags);
12474 }
James Smart4f774512009-05-22 14:52:35 -040012475}
12476
12477/**
James Smart45ed1192009-10-02 15:17:02 -040012478 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12479 * @phba: Pointer to HBA context object.
12480 * @wcqe: Pointer to work-queue completion queue entry.
12481 *
12482 * This routine handles an ELS work-queue completion event and construct
12483 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12484 * discovery engine to handle.
12485 *
12486 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12487 **/
12488static struct lpfc_iocbq *
12489lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12490 struct lpfc_iocbq *irspiocbq)
12491{
James Smart895427b2017-02-12 13:52:30 -080012492 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012493 struct lpfc_iocbq *cmdiocbq;
12494 struct lpfc_wcqe_complete *wcqe;
12495 unsigned long iflags;
12496
James Smart895427b2017-02-12 13:52:30 -080012497 pring = lpfc_phba_elsring(phba);
12498
James Smart45ed1192009-10-02 15:17:02 -040012499 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012500 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012501 pring->stats.iocb_event++;
12502 /* Look up the ELS command IOCB and create pseudo response IOCB */
12503 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12504 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart89533e92016-10-13 15:06:15 -070012505 /* Put the iocb back on the txcmplq */
12506 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
James Smart7e56aa22012-08-03 12:35:34 -040012507 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012508
12509 if (unlikely(!cmdiocbq)) {
12510 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12511 "0386 ELS complete with no corresponding "
12512 "cmdiocb: iotag (%d)\n",
12513 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12514 lpfc_sli_release_iocbq(phba, irspiocbq);
12515 return NULL;
12516 }
12517
12518 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012519 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012520
12521 return irspiocbq;
12522}
12523
12524/**
James Smart04c68492009-05-22 14:52:52 -040012525 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12526 * @phba: Pointer to HBA context object.
12527 * @cqe: Pointer to mailbox completion queue entry.
12528 *
12529 * This routine process a mailbox completion queue entry with asynchrous
12530 * event.
12531 *
12532 * Return: true if work posted to worker thread, otherwise false.
12533 **/
12534static bool
12535lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12536{
12537 struct lpfc_cq_event *cq_event;
12538 unsigned long iflags;
12539
12540 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12541 "0392 Async Event: word0:x%x, word1:x%x, "
12542 "word2:x%x, word3:x%x\n", mcqe->word0,
12543 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12544
12545 /* Allocate a new internal CQ_EVENT entry */
12546 cq_event = lpfc_sli4_cq_event_alloc(phba);
12547 if (!cq_event) {
12548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12549 "0394 Failed to allocate CQ_EVENT entry\n");
12550 return false;
12551 }
12552
12553 /* Move the CQE into an asynchronous event entry */
12554 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12555 spin_lock_irqsave(&phba->hbalock, iflags);
12556 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12557 /* Set the async event flag */
12558 phba->hba_flag |= ASYNC_EVENT;
12559 spin_unlock_irqrestore(&phba->hbalock, iflags);
12560
12561 return true;
12562}
12563
12564/**
12565 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12566 * @phba: Pointer to HBA context object.
12567 * @cqe: Pointer to mailbox completion queue entry.
12568 *
12569 * This routine process a mailbox completion queue entry with mailbox
12570 * completion event.
12571 *
12572 * Return: true if work posted to worker thread, otherwise false.
12573 **/
12574static bool
12575lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12576{
12577 uint32_t mcqe_status;
12578 MAILBOX_t *mbox, *pmbox;
12579 struct lpfc_mqe *mqe;
12580 struct lpfc_vport *vport;
12581 struct lpfc_nodelist *ndlp;
12582 struct lpfc_dmabuf *mp;
12583 unsigned long iflags;
12584 LPFC_MBOXQ_t *pmb;
12585 bool workposted = false;
12586 int rc;
12587
12588 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12589 if (!bf_get(lpfc_trailer_completed, mcqe))
12590 goto out_no_mqe_complete;
12591
12592 /* Get the reference to the active mbox command */
12593 spin_lock_irqsave(&phba->hbalock, iflags);
12594 pmb = phba->sli.mbox_active;
12595 if (unlikely(!pmb)) {
12596 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12597 "1832 No pending MBOX command to handle\n");
12598 spin_unlock_irqrestore(&phba->hbalock, iflags);
12599 goto out_no_mqe_complete;
12600 }
12601 spin_unlock_irqrestore(&phba->hbalock, iflags);
12602 mqe = &pmb->u.mqe;
12603 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12604 mbox = phba->mbox;
12605 vport = pmb->vport;
12606
12607 /* Reset heartbeat timer */
12608 phba->last_completion_time = jiffies;
12609 del_timer(&phba->sli.mbox_tmo);
12610
12611 /* Move mbox data to caller's mailbox region, do endian swapping */
12612 if (pmb->mbox_cmpl && mbox)
12613 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012614
James Smart73d91e52011-10-10 21:32:10 -040012615 /*
12616 * For mcqe errors, conditionally move a modified error code to
12617 * the mbox so that the error will not be missed.
12618 */
12619 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12620 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12621 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12622 bf_set(lpfc_mqe_status, mqe,
12623 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12624 }
James Smart04c68492009-05-22 14:52:52 -040012625 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12626 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12627 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12628 "MBOX dflt rpi: status:x%x rpi:x%x",
12629 mcqe_status,
12630 pmbox->un.varWords[0], 0);
12631 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12632 mp = (struct lpfc_dmabuf *)(pmb->context1);
12633 ndlp = (struct lpfc_nodelist *)pmb->context2;
12634 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12635 * RID of the PPI using the same mbox buffer.
12636 */
12637 lpfc_unreg_login(phba, vport->vpi,
12638 pmbox->un.varWords[0], pmb);
12639 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12640 pmb->context1 = mp;
12641 pmb->context2 = ndlp;
12642 pmb->vport = vport;
12643 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12644 if (rc != MBX_BUSY)
12645 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12646 LOG_SLI, "0385 rc should "
12647 "have been MBX_BUSY\n");
12648 if (rc != MBX_NOT_FINISHED)
12649 goto send_current_mbox;
12650 }
12651 }
12652 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12653 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12654 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12655
12656 /* There is mailbox completion work to do */
12657 spin_lock_irqsave(&phba->hbalock, iflags);
12658 __lpfc_mbox_cmpl_put(phba, pmb);
12659 phba->work_ha |= HA_MBATT;
12660 spin_unlock_irqrestore(&phba->hbalock, iflags);
12661 workposted = true;
12662
12663send_current_mbox:
12664 spin_lock_irqsave(&phba->hbalock, iflags);
12665 /* Release the mailbox command posting token */
12666 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12667 /* Setting active mailbox pointer need to be in sync to flag clear */
12668 phba->sli.mbox_active = NULL;
12669 spin_unlock_irqrestore(&phba->hbalock, iflags);
12670 /* Wake up worker thread to post the next pending mailbox command */
12671 lpfc_worker_wake_up(phba);
12672out_no_mqe_complete:
12673 if (bf_get(lpfc_trailer_consumed, mcqe))
12674 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12675 return workposted;
12676}
12677
12678/**
12679 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12680 * @phba: Pointer to HBA context object.
12681 * @cqe: Pointer to mailbox completion queue entry.
12682 *
12683 * This routine process a mailbox completion queue entry, it invokes the
12684 * proper mailbox complete handling or asynchrous event handling routine
12685 * according to the MCQE's async bit.
12686 *
12687 * Return: true if work posted to worker thread, otherwise false.
12688 **/
12689static bool
12690lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12691{
12692 struct lpfc_mcqe mcqe;
12693 bool workposted;
12694
12695 /* Copy the mailbox MCQE and convert endian order as needed */
12696 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12697
12698 /* Invoke the proper event handling routine */
12699 if (!bf_get(lpfc_trailer_async, &mcqe))
12700 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12701 else
12702 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12703 return workposted;
12704}
12705
12706/**
James Smart4f774512009-05-22 14:52:35 -040012707 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12708 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012709 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012710 * @wcqe: Pointer to work-queue completion queue entry.
12711 *
12712 * This routine handles an ELS work-queue completion event.
12713 *
12714 * Return: true if work posted to worker thread, otherwise false.
12715 **/
12716static bool
James Smart2a76a282012-08-03 12:35:54 -040012717lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012718 struct lpfc_wcqe_complete *wcqe)
12719{
James Smart4f774512009-05-22 14:52:35 -040012720 struct lpfc_iocbq *irspiocbq;
12721 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012722 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012723 int txq_cnt = 0;
12724 int txcmplq_cnt = 0;
12725 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012726
James Smart45ed1192009-10-02 15:17:02 -040012727 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012728 irspiocbq = lpfc_sli_get_iocbq(phba);
12729 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012730 if (!list_empty(&pring->txq))
12731 txq_cnt++;
12732 if (!list_empty(&pring->txcmplq))
12733 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012734 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012735 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12736 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012737 txq_cnt, phba->iocb_cnt,
12738 fcp_txcmplq_cnt,
12739 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012740 return false;
James Smart4f774512009-05-22 14:52:35 -040012741 }
James Smart4f774512009-05-22 14:52:35 -040012742
James Smart45ed1192009-10-02 15:17:02 -040012743 /* Save off the slow-path queue event for work thread to process */
12744 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012745 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012746 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012747 &phba->sli4_hba.sp_queue_event);
12748 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012749 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012750
James Smart45ed1192009-10-02 15:17:02 -040012751 return true;
James Smart4f774512009-05-22 14:52:35 -040012752}
12753
12754/**
12755 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12756 * @phba: Pointer to HBA context object.
12757 * @wcqe: Pointer to work-queue completion queue entry.
12758 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012759 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012760 * proper WQ release routine to the slow-path WQ.
12761 **/
12762static void
12763lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12764 struct lpfc_wcqe_release *wcqe)
12765{
James Smart2e90f4b2011-12-13 13:22:37 -050012766 /* sanity check on queue memory */
12767 if (unlikely(!phba->sli4_hba.els_wq))
12768 return;
James Smart4f774512009-05-22 14:52:35 -040012769 /* Check for the slow-path ELS work queue */
12770 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12771 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12772 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12773 else
12774 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12775 "2579 Slow-path wqe consume event carries "
12776 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12777 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12778 phba->sli4_hba.els_wq->queue_id);
12779}
12780
12781/**
12782 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12783 * @phba: Pointer to HBA context object.
12784 * @cq: Pointer to a WQ completion queue.
12785 * @wcqe: Pointer to work-queue completion queue entry.
12786 *
12787 * This routine handles an XRI abort event.
12788 *
12789 * Return: true if work posted to worker thread, otherwise false.
12790 **/
12791static bool
12792lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12793 struct lpfc_queue *cq,
12794 struct sli4_wcqe_xri_aborted *wcqe)
12795{
12796 bool workposted = false;
12797 struct lpfc_cq_event *cq_event;
12798 unsigned long iflags;
12799
12800 /* Allocate a new internal CQ_EVENT entry */
12801 cq_event = lpfc_sli4_cq_event_alloc(phba);
12802 if (!cq_event) {
12803 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12804 "0602 Failed to allocate CQ_EVENT entry\n");
12805 return false;
12806 }
12807
12808 /* Move the CQE into the proper xri abort event list */
12809 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12810 switch (cq->subtype) {
12811 case LPFC_FCP:
12812 spin_lock_irqsave(&phba->hbalock, iflags);
12813 list_add_tail(&cq_event->list,
12814 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12815 /* Set the fcp xri abort event flag */
12816 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12817 spin_unlock_irqrestore(&phba->hbalock, iflags);
12818 workposted = true;
12819 break;
12820 case LPFC_ELS:
12821 spin_lock_irqsave(&phba->hbalock, iflags);
12822 list_add_tail(&cq_event->list,
12823 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12824 /* Set the els xri abort event flag */
12825 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12826 spin_unlock_irqrestore(&phba->hbalock, iflags);
12827 workposted = true;
12828 break;
James Smart318083a2017-03-04 09:30:30 -080012829 case LPFC_NVME:
12830 spin_lock_irqsave(&phba->hbalock, iflags);
12831 list_add_tail(&cq_event->list,
12832 &phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
12833 /* Set the nvme xri abort event flag */
12834 phba->hba_flag |= NVME_XRI_ABORT_EVENT;
12835 spin_unlock_irqrestore(&phba->hbalock, iflags);
12836 workposted = true;
12837 break;
James Smart4f774512009-05-22 14:52:35 -040012838 default:
12839 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080012840 "0603 Invalid CQ subtype %d: "
12841 "%08x %08x %08x %08x\n",
12842 cq->subtype, wcqe->word0, wcqe->parameter,
12843 wcqe->word2, wcqe->word3);
12844 lpfc_sli4_cq_event_release(phba, cq_event);
James Smart4f774512009-05-22 14:52:35 -040012845 workposted = false;
12846 break;
12847 }
12848 return workposted;
12849}
12850
12851/**
James Smart4d9ab992009-10-02 15:16:39 -040012852 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012853 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012854 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012855 *
James Smart4d9ab992009-10-02 15:16:39 -040012856 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012857 *
12858 * Return: true if work posted to worker thread, otherwise false.
12859 **/
12860static bool
James Smart4d9ab992009-10-02 15:16:39 -040012861lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12862{
12863 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012864 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012865 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12866 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070012867 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040012868 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012869 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012870 unsigned long iflags;
12871
James Smart2e90f4b2011-12-13 13:22:37 -050012872 /* sanity check on queue memory */
12873 if (unlikely(!hrq) || unlikely(!drq))
12874 return workposted;
12875
James Smart7851fe22011-07-22 18:36:52 -040012876 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12877 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12878 else
12879 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12880 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012881 goto out;
12882
12883 status = bf_get(lpfc_rcqe_status, rcqe);
12884 switch (status) {
12885 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12886 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12887 "2537 Receive Frame Truncated!!\n");
12888 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012889 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012890 spin_lock_irqsave(&phba->hbalock, iflags);
12891 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12892 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012893 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012894 spin_unlock_irqrestore(&phba->hbalock, iflags);
12895 goto out;
12896 }
James Smartb84daac2012-08-03 12:35:13 -040012897 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070012898 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040012899 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012900
12901 /* If a NVME LS event (type 0x28), treat it as Fast path */
12902 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12903
James Smart4d9ab992009-10-02 15:16:39 -040012904 /* save off the frame for the word thread to process */
12905 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012906 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012907 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012908 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012909 spin_unlock_irqrestore(&phba->hbalock, iflags);
12910 workposted = true;
12911 break;
James Smart4d9ab992009-10-02 15:16:39 -040012912 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070012913 if (phba->nvmet_support) {
12914 tgtp = phba->targetport->private;
12915 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
12916 "6402 RQE Error x%x, posted %d err_cnt "
12917 "%d: %x %x %x\n",
12918 status, hrq->RQ_buf_posted,
12919 hrq->RQ_no_posted_buf,
12920 atomic_read(&tgtp->rcv_fcp_cmd_in),
12921 atomic_read(&tgtp->rcv_fcp_cmd_out),
12922 atomic_read(&tgtp->xmt_fcp_release));
12923 }
12924 /* fallthrough */
12925
12926 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040012927 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012928 /* Post more buffers if possible */
12929 spin_lock_irqsave(&phba->hbalock, iflags);
12930 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12931 spin_unlock_irqrestore(&phba->hbalock, iflags);
12932 workposted = true;
12933 break;
12934 }
12935out:
12936 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012937}
12938
12939/**
12940 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12941 * @phba: Pointer to HBA context object.
12942 * @cq: Pointer to the completion queue.
12943 * @wcqe: Pointer to a completion queue entry.
12944 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012945 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012946 * entry.
12947 *
12948 * Return: true if work posted to worker thread, otherwise false.
12949 **/
12950static bool
12951lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012952 struct lpfc_cqe *cqe)
12953{
James Smart45ed1192009-10-02 15:17:02 -040012954 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012955 bool workposted = false;
12956
12957 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012958 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012959
12960 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012961 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012962 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012963 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012964 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012965 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012966 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012967 break;
12968 case CQE_CODE_RELEASE_WQE:
12969 /* Process the WQ release event */
12970 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012971 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012972 break;
12973 case CQE_CODE_XRI_ABORTED:
12974 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012975 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012976 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012977 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012978 break;
James Smart4d9ab992009-10-02 15:16:39 -040012979 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012980 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012981 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012982 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012983 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012984 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012985 break;
James Smart4f774512009-05-22 14:52:35 -040012986 default:
12987 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12988 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012989 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012990 break;
12991 }
12992 return workposted;
12993}
12994
12995/**
James Smart4f774512009-05-22 14:52:35 -040012996 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12997 * @phba: Pointer to HBA context object.
12998 * @eqe: Pointer to fast-path event queue entry.
12999 *
13000 * This routine process a event queue entry from the slow-path event queue.
13001 * It will check the MajorCode and MinorCode to determine this is for a
13002 * completion event on a completion queue, if not, an error shall be logged
13003 * and just return. Otherwise, it will get to the corresponding completion
13004 * queue and process all the entries on that completion queue, rearm the
13005 * completion queue, and then return.
13006 *
13007 **/
13008static void
James Smart67d12732012-08-03 12:36:13 -040013009lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13010 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013011{
James Smart67d12732012-08-03 12:36:13 -040013012 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013013 struct lpfc_cqe *cqe;
13014 bool workposted = false;
13015 int ecount = 0;
13016 uint16_t cqid;
13017
James Smart4f774512009-05-22 14:52:35 -040013018 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013019 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013020
James Smart4f774512009-05-22 14:52:35 -040013021 list_for_each_entry(childq, &speq->child_list, list) {
13022 if (childq->queue_id == cqid) {
13023 cq = childq;
13024 break;
13025 }
13026 }
13027 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013028 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13030 "0365 Slow-path CQ identifier "
13031 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040013032 return;
13033 }
13034
James Smart895427b2017-02-12 13:52:30 -080013035 /* Save EQ associated with this CQ */
13036 cq->assoc_qp = speq;
13037
James Smart4f774512009-05-22 14:52:35 -040013038 /* Process all the entries to the CQ */
13039 switch (cq->type) {
13040 case LPFC_MCQ:
13041 while ((cqe = lpfc_sli4_cq_get(cq))) {
13042 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013043 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013044 break;
James Smartb84daac2012-08-03 12:35:13 -040013045 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013046 }
13047 break;
13048 case LPFC_WCQ:
13049 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013050 if ((cq->subtype == LPFC_FCP) ||
13051 (cq->subtype == LPFC_NVME))
13052 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013053 cqe);
13054 else
13055 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13056 cqe);
James Smart73d91e52011-10-10 21:32:10 -040013057 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013058 break;
James Smart4f774512009-05-22 14:52:35 -040013059 }
James Smartb84daac2012-08-03 12:35:13 -040013060
13061 /* Track the max number of CQEs processed in 1 EQ */
13062 if (ecount > cq->CQ_max_cqe)
13063 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040013064 break;
13065 default:
13066 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13067 "0370 Invalid completion queue type (%d)\n",
13068 cq->type);
13069 return;
13070 }
13071
13072 /* Catch the no cq entry condition, log an error */
13073 if (unlikely(ecount == 0))
13074 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13075 "0371 No entry from the CQ: identifier "
13076 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13077
13078 /* In any case, flash and re-arm the RCQ */
13079 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13080
13081 /* wake up worker thread if there are works to be done */
13082 if (workposted)
13083 lpfc_worker_wake_up(phba);
13084}
13085
13086/**
13087 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013088 * @phba: Pointer to HBA context object.
13089 * @cq: Pointer to associated CQ
13090 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013091 *
13092 * This routine process a fast-path work queue completion entry from fast-path
13093 * event queue for FCP command response completion.
13094 **/
13095static void
James Smart2a76a282012-08-03 12:35:54 -040013096lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013097 struct lpfc_wcqe_complete *wcqe)
13098{
James Smart2a76a282012-08-03 12:35:54 -040013099 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013100 struct lpfc_iocbq *cmdiocbq;
13101 struct lpfc_iocbq irspiocbq;
13102 unsigned long iflags;
13103
James Smart4f774512009-05-22 14:52:35 -040013104 /* Check for response status */
13105 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13106 /* If resource errors reported from HBA, reduce queue
13107 * depth of the SCSI device.
13108 */
James Smarte3d2b802012-08-14 14:25:43 -040013109 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13110 IOSTAT_LOCAL_REJECT)) &&
13111 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13112 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013113 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013114
James Smart4f774512009-05-22 14:52:35 -040013115 /* Log the error status */
13116 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13117 "0373 FCP complete error: status=x%x, "
13118 "hw_status=x%x, total_data_specified=%d, "
13119 "parameter=x%x, word3=x%x\n",
13120 bf_get(lpfc_wcqe_c_status, wcqe),
13121 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13122 wcqe->total_data_placed, wcqe->parameter,
13123 wcqe->word3);
13124 }
13125
13126 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013127 spin_lock_irqsave(&pring->ring_lock, iflags);
13128 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013129 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13130 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013131 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013132 if (unlikely(!cmdiocbq)) {
13133 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13134 "0374 FCP complete with no corresponding "
13135 "cmdiocb: iotag (%d)\n",
13136 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13137 return;
13138 }
James Smart895427b2017-02-12 13:52:30 -080013139
13140 if (cq->assoc_qp)
13141 cmdiocbq->isr_timestamp =
13142 cq->assoc_qp->isr_timestamp;
13143
13144 if (cmdiocbq->iocb_cmpl == NULL) {
13145 if (cmdiocbq->wqe_cmpl) {
13146 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13147 spin_lock_irqsave(&phba->hbalock, iflags);
13148 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13149 spin_unlock_irqrestore(&phba->hbalock, iflags);
13150 }
13151
13152 /* Pass the cmd_iocb and the wcqe to the upper layer */
13153 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13154 return;
13155 }
James Smart4f774512009-05-22 14:52:35 -040013156 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13157 "0375 FCP cmdiocb not callback function "
13158 "iotag: (%d)\n",
13159 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13160 return;
13161 }
13162
13163 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013164 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013165
James Smart0f65ff62010-02-26 14:14:23 -050013166 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13167 spin_lock_irqsave(&phba->hbalock, iflags);
13168 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13169 spin_unlock_irqrestore(&phba->hbalock, iflags);
13170 }
13171
James Smart4f774512009-05-22 14:52:35 -040013172 /* Pass the cmd_iocb and the rsp state to the upper layer */
13173 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13174}
13175
13176/**
13177 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13178 * @phba: Pointer to HBA context object.
13179 * @cq: Pointer to completion queue.
13180 * @wcqe: Pointer to work-queue completion queue entry.
13181 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013182 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013183 * proper WQ release routine to the slow-path WQ.
13184 **/
13185static void
13186lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13187 struct lpfc_wcqe_release *wcqe)
13188{
13189 struct lpfc_queue *childwq;
13190 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013191 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013192
13193 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013194 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013195 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013196 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013197 lpfc_sli4_wq_release(childwq,
13198 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13199 wqid_matched = true;
13200 break;
13201 }
13202 }
13203 /* Report warning log message if no match found */
13204 if (wqid_matched != true)
13205 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13206 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013207 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013208}
13209
13210/**
James Smart2d7dbc42017-02-12 13:52:35 -080013211 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13212 * @phba: Pointer to HBA context object.
13213 * @rcqe: Pointer to receive-queue completion queue entry.
13214 *
13215 * This routine process a receive-queue completion queue entry.
13216 *
13217 * Return: true if work posted to worker thread, otherwise false.
13218 **/
13219static bool
13220lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13221 struct lpfc_rcqe *rcqe)
13222{
13223 bool workposted = false;
13224 struct lpfc_queue *hrq;
13225 struct lpfc_queue *drq;
13226 struct rqb_dmabuf *dma_buf;
13227 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013228 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013229 uint32_t status, rq_id;
13230 unsigned long iflags;
13231 uint32_t fctl, idx;
13232
13233 if ((phba->nvmet_support == 0) ||
13234 (phba->sli4_hba.nvmet_cqset == NULL))
13235 return workposted;
13236
13237 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13238 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13239 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13240
13241 /* sanity check on queue memory */
13242 if (unlikely(!hrq) || unlikely(!drq))
13243 return workposted;
13244
13245 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13246 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13247 else
13248 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13249
13250 if ((phba->nvmet_support == 0) ||
13251 (rq_id != hrq->queue_id))
13252 return workposted;
13253
13254 status = bf_get(lpfc_rcqe_status, rcqe);
13255 switch (status) {
13256 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13257 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13258 "6126 Receive Frame Truncated!!\n");
James Smart2d7dbc42017-02-12 13:52:35 -080013259 case FC_STATUS_RQ_SUCCESS:
13260 lpfc_sli4_rq_release(hrq, drq);
13261 spin_lock_irqsave(&phba->hbalock, iflags);
13262 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13263 if (!dma_buf) {
13264 hrq->RQ_no_buf_found++;
13265 spin_unlock_irqrestore(&phba->hbalock, iflags);
13266 goto out;
13267 }
13268 spin_unlock_irqrestore(&phba->hbalock, iflags);
13269 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013270 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013271 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13272
13273 /* Just some basic sanity checks on FCP Command frame */
13274 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13275 fc_hdr->fh_f_ctl[1] << 8 |
13276 fc_hdr->fh_f_ctl[2]);
13277 if (((fctl &
13278 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13279 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13280 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13281 goto drop;
13282
13283 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13284 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013285 lpfc_nvmet_unsol_fcp_event(
13286 phba, phba->sli4_hba.els_wq->pring, dma_buf,
13287 cq->assoc_qp->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013288 return false;
13289 }
13290drop:
13291 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13292 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013293 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013294 if (phba->nvmet_support) {
13295 tgtp = phba->targetport->private;
13296 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13297 "6401 RQE Error x%x, posted %d err_cnt "
13298 "%d: %x %x %x\n",
13299 status, hrq->RQ_buf_posted,
13300 hrq->RQ_no_posted_buf,
13301 atomic_read(&tgtp->rcv_fcp_cmd_in),
13302 atomic_read(&tgtp->rcv_fcp_cmd_out),
13303 atomic_read(&tgtp->xmt_fcp_release));
13304 }
13305 /* fallthrough */
13306
13307 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013308 hrq->RQ_no_posted_buf++;
13309 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013310 break;
13311 }
13312out:
13313 return workposted;
13314}
13315
13316/**
James Smart895427b2017-02-12 13:52:30 -080013317 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013318 * @cq: Pointer to the completion queue.
13319 * @eqe: Pointer to fast-path completion queue entry.
13320 *
13321 * This routine process a fast-path work queue completion entry from fast-path
13322 * event queue for FCP command response completion.
13323 **/
13324static int
James Smart895427b2017-02-12 13:52:30 -080013325lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013326 struct lpfc_cqe *cqe)
13327{
13328 struct lpfc_wcqe_release wcqe;
13329 bool workposted = false;
13330
13331 /* Copy the work queue CQE and convert endian order if needed */
13332 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13333
13334 /* Check and process for different type of WCQE and dispatch */
13335 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13336 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013337 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013338 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013339 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013340 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013341 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13342 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13343 (struct lpfc_wcqe_complete *)&wcqe);
13344 if (cq->subtype == LPFC_NVME_LS)
13345 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013346 (struct lpfc_wcqe_complete *)&wcqe);
13347 break;
13348 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013349 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013350 /* Process the WQ release event */
13351 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13352 (struct lpfc_wcqe_release *)&wcqe);
13353 break;
13354 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013355 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013356 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013357 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013358 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13359 (struct sli4_wcqe_xri_aborted *)&wcqe);
13360 break;
James Smart895427b2017-02-12 13:52:30 -080013361 case CQE_CODE_RECEIVE_V1:
13362 case CQE_CODE_RECEIVE:
13363 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013364 if (cq->subtype == LPFC_NVMET) {
13365 workposted = lpfc_sli4_nvmet_handle_rcqe(
13366 phba, cq, (struct lpfc_rcqe *)&wcqe);
13367 }
James Smart895427b2017-02-12 13:52:30 -080013368 break;
James Smart4f774512009-05-22 14:52:35 -040013369 default:
13370 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013371 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013372 bf_get(lpfc_wcqe_c_code, &wcqe));
13373 break;
13374 }
13375 return workposted;
13376}
13377
13378/**
James Smart67d12732012-08-03 12:36:13 -040013379 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013380 * @phba: Pointer to HBA context object.
13381 * @eqe: Pointer to fast-path event queue entry.
13382 *
13383 * This routine process a event queue entry from the fast-path event queue.
13384 * It will check the MajorCode and MinorCode to determine this is for a
13385 * completion event on a completion queue, if not, an error shall be logged
13386 * and just return. Otherwise, it will get to the corresponding completion
13387 * queue and process all the entries on the completion queue, rearm the
13388 * completion queue, and then return.
13389 **/
13390static void
James Smart67d12732012-08-03 12:36:13 -040013391lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13392 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013393{
James Smart895427b2017-02-12 13:52:30 -080013394 struct lpfc_queue *cq = NULL;
James Smart4f774512009-05-22 14:52:35 -040013395 struct lpfc_cqe *cqe;
13396 bool workposted = false;
James Smart2d7dbc42017-02-12 13:52:35 -080013397 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013398 int ecount = 0;
13399
James Smartcb5172e2010-03-15 11:25:07 -040013400 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013402 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013403 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013404 bf_get_le32(lpfc_eqe_major_code, eqe),
13405 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040013406 return;
13407 }
13408
James Smart67d12732012-08-03 12:36:13 -040013409 /* Get the reference to the corresponding CQ */
13410 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13411
James Smart2d7dbc42017-02-12 13:52:35 -080013412 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13413 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13414 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13415 /* Process NVMET unsol rcv */
13416 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13417 goto process_cq;
13418 }
13419 }
13420
James Smart895427b2017-02-12 13:52:30 -080013421 if (phba->sli4_hba.nvme_cq_map &&
13422 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013423 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013424 cq = phba->sli4_hba.nvme_cq[qidx];
13425 goto process_cq;
13426 }
13427
13428 if (phba->sli4_hba.fcp_cq_map &&
13429 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13430 /* Process FCP command completion */
13431 cq = phba->sli4_hba.fcp_cq[qidx];
13432 goto process_cq;
13433 }
13434
13435 if (phba->sli4_hba.nvmels_cq &&
13436 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13437 /* Process NVME unsol rcv */
13438 cq = phba->sli4_hba.nvmels_cq;
13439 }
13440
13441 /* Otherwise this is a Slow path event */
13442 if (cq == NULL) {
13443 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013444 return;
13445 }
13446
James Smart895427b2017-02-12 13:52:30 -080013447process_cq:
James Smart4f774512009-05-22 14:52:35 -040013448 if (unlikely(cqid != cq->queue_id)) {
13449 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13450 "0368 Miss-matched fast-path completion "
13451 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13452 cqid, cq->queue_id);
13453 return;
13454 }
13455
James Smart895427b2017-02-12 13:52:30 -080013456 /* Save EQ associated with this CQ */
13457 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13458
James Smart4f774512009-05-22 14:52:35 -040013459 /* Process all the entries to the CQ */
13460 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013461 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013462 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013463 break;
James Smart4f774512009-05-22 14:52:35 -040013464 }
13465
James Smartb84daac2012-08-03 12:35:13 -040013466 /* Track the max number of CQEs processed in 1 EQ */
13467 if (ecount > cq->CQ_max_cqe)
13468 cq->CQ_max_cqe = ecount;
13469
James Smart4f774512009-05-22 14:52:35 -040013470 /* Catch the no cq entry condition */
13471 if (unlikely(ecount == 0))
13472 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13473 "0369 No entry from fast-path completion "
13474 "queue fcpcqid=%d\n", cq->queue_id);
13475
13476 /* In any case, flash and re-arm the CQ */
13477 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13478
13479 /* wake up worker thread if there are works to be done */
13480 if (workposted)
13481 lpfc_worker_wake_up(phba);
13482}
13483
13484static void
13485lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13486{
13487 struct lpfc_eqe *eqe;
13488
13489 /* walk all the EQ entries and drop on the floor */
13490 while ((eqe = lpfc_sli4_eq_get(eq)))
13491 ;
13492
13493 /* Clear and re-arm the EQ */
13494 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13495}
13496
James Smart1ba981f2014-02-20 09:56:45 -050013497
13498/**
13499 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13500 * entry
13501 * @phba: Pointer to HBA context object.
13502 * @eqe: Pointer to fast-path event queue entry.
13503 *
13504 * This routine process a event queue entry from the Flash Optimized Fabric
13505 * event queue. It will check the MajorCode and MinorCode to determine this
13506 * is for a completion event on a completion queue, if not, an error shall be
13507 * logged and just return. Otherwise, it will get to the corresponding
13508 * completion queue and process all the entries on the completion queue, rearm
13509 * the completion queue, and then return.
13510 **/
13511static void
13512lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13513{
13514 struct lpfc_queue *cq;
13515 struct lpfc_cqe *cqe;
13516 bool workposted = false;
13517 uint16_t cqid;
13518 int ecount = 0;
13519
13520 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13521 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13522 "9147 Not a valid completion "
13523 "event: majorcode=x%x, minorcode=x%x\n",
13524 bf_get_le32(lpfc_eqe_major_code, eqe),
13525 bf_get_le32(lpfc_eqe_minor_code, eqe));
13526 return;
13527 }
13528
13529 /* Get the reference to the corresponding CQ */
13530 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13531
13532 /* Next check for OAS */
13533 cq = phba->sli4_hba.oas_cq;
13534 if (unlikely(!cq)) {
13535 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13536 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13537 "9148 OAS completion queue "
13538 "does not exist\n");
13539 return;
13540 }
13541
13542 if (unlikely(cqid != cq->queue_id)) {
13543 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13544 "9149 Miss-matched fast-path compl "
13545 "queue id: eqcqid=%d, fcpcqid=%d\n",
13546 cqid, cq->queue_id);
13547 return;
13548 }
13549
13550 /* Process all the entries to the OAS CQ */
13551 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013552 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart1ba981f2014-02-20 09:56:45 -050013553 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013554 break;
James Smart1ba981f2014-02-20 09:56:45 -050013555 }
13556
13557 /* Track the max number of CQEs processed in 1 EQ */
13558 if (ecount > cq->CQ_max_cqe)
13559 cq->CQ_max_cqe = ecount;
13560
13561 /* Catch the no cq entry condition */
13562 if (unlikely(ecount == 0))
13563 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13564 "9153 No entry from fast-path completion "
13565 "queue fcpcqid=%d\n", cq->queue_id);
13566
13567 /* In any case, flash and re-arm the CQ */
13568 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13569
13570 /* wake up worker thread if there are works to be done */
13571 if (workposted)
13572 lpfc_worker_wake_up(phba);
13573}
13574
13575/**
13576 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13577 * @irq: Interrupt number.
13578 * @dev_id: The device context pointer.
13579 *
13580 * This function is directly called from the PCI layer as an interrupt
13581 * service routine when device with SLI-4 interface spec is enabled with
13582 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13583 * IOCB ring event in the HBA. However, when the device is enabled with either
13584 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13585 * device-level interrupt handler. When the PCI slot is in error recovery
13586 * or the HBA is undergoing initialization, the interrupt handler will not
13587 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13588 * the intrrupt context. This function is called without any lock held.
13589 * It gets the hbalock to access and update SLI data structures. Note that,
13590 * the EQ to CQ are one-to-one map such that the EQ index is
13591 * equal to that of CQ index.
13592 *
13593 * This function returns IRQ_HANDLED when interrupt is handled else it
13594 * returns IRQ_NONE.
13595 **/
13596irqreturn_t
13597lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13598{
13599 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013600 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013601 struct lpfc_queue *eq;
13602 struct lpfc_eqe *eqe;
13603 unsigned long iflag;
13604 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013605
13606 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013607 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13608 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013609
13610 if (unlikely(!phba))
13611 return IRQ_NONE;
13612
13613 /* Get to the EQ struct associated with this vector */
13614 eq = phba->sli4_hba.fof_eq;
13615 if (unlikely(!eq))
13616 return IRQ_NONE;
13617
13618 /* Check device state for handling interrupt */
13619 if (unlikely(lpfc_intr_state_check(phba))) {
13620 eq->EQ_badstate++;
13621 /* Check again for link_state with lock held */
13622 spin_lock_irqsave(&phba->hbalock, iflag);
13623 if (phba->link_state < LPFC_LINK_DOWN)
13624 /* Flush, clear interrupt, and rearm the EQ */
13625 lpfc_sli4_eq_flush(phba, eq);
13626 spin_unlock_irqrestore(&phba->hbalock, iflag);
13627 return IRQ_NONE;
13628 }
13629
13630 /*
13631 * Process all the event on FCP fast-path EQ
13632 */
13633 while ((eqe = lpfc_sli4_eq_get(eq))) {
13634 lpfc_sli4_fof_handle_eqe(phba, eqe);
13635 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013636 break;
James Smart1ba981f2014-02-20 09:56:45 -050013637 eq->EQ_processed++;
13638 }
13639
13640 /* Track the max number of EQEs processed in 1 intr */
13641 if (ecount > eq->EQ_max_eqe)
13642 eq->EQ_max_eqe = ecount;
13643
13644
13645 if (unlikely(ecount == 0)) {
13646 eq->EQ_no_entry++;
13647
13648 if (phba->intr_type == MSIX)
13649 /* MSI-X treated interrupt served as no EQ share INT */
13650 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13651 "9145 MSI-X interrupt with no EQE\n");
13652 else {
13653 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13654 "9146 ISR interrupt with no EQE\n");
13655 /* Non MSI-X treated on interrupt as EQ share INT */
13656 return IRQ_NONE;
13657 }
13658 }
13659 /* Always clear and re-arm the fast-path EQ */
13660 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13661 return IRQ_HANDLED;
13662}
13663
James Smart4f774512009-05-22 14:52:35 -040013664/**
James Smart67d12732012-08-03 12:36:13 -040013665 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013666 * @irq: Interrupt number.
13667 * @dev_id: The device context pointer.
13668 *
13669 * This function is directly called from the PCI layer as an interrupt
13670 * service routine when device with SLI-4 interface spec is enabled with
13671 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13672 * ring event in the HBA. However, when the device is enabled with either
13673 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13674 * device-level interrupt handler. When the PCI slot is in error recovery
13675 * or the HBA is undergoing initialization, the interrupt handler will not
13676 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13677 * the intrrupt context. This function is called without any lock held.
13678 * It gets the hbalock to access and update SLI data structures. Note that,
13679 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13680 * equal to that of FCP CQ index.
13681 *
James Smart67d12732012-08-03 12:36:13 -040013682 * The link attention and ELS ring attention events are handled
13683 * by the worker thread. The interrupt handler signals the worker thread
13684 * and returns for these events. This function is called without any lock
13685 * held. It gets the hbalock to access and update SLI data structures.
13686 *
James Smart4f774512009-05-22 14:52:35 -040013687 * This function returns IRQ_HANDLED when interrupt is handled else it
13688 * returns IRQ_NONE.
13689 **/
13690irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013691lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013692{
13693 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013694 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013695 struct lpfc_queue *fpeq;
13696 struct lpfc_eqe *eqe;
13697 unsigned long iflag;
13698 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013699 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013700
13701 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013702 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13703 phba = hba_eq_hdl->phba;
13704 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013705
13706 if (unlikely(!phba))
13707 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013708 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013709 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013710
13711 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013712 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013713 if (unlikely(!fpeq))
13714 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013715
James Smartbd2cdd52017-02-12 13:52:33 -080013716#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13717 if (phba->ktime_on)
13718 fpeq->isr_timestamp = ktime_get_ns();
13719#endif
13720
James Smartba20c852012-08-03 12:36:52 -040013721 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013722 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013723 lpfc_sli4_eq_clr_intr(fpeq);
13724 else {
James Smart895427b2017-02-12 13:52:30 -080013725 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013726 return IRQ_NONE;
13727 }
13728 }
13729
James Smart4f774512009-05-22 14:52:35 -040013730 /* Check device state for handling interrupt */
13731 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040013732 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040013733 /* Check again for link_state with lock held */
13734 spin_lock_irqsave(&phba->hbalock, iflag);
13735 if (phba->link_state < LPFC_LINK_DOWN)
13736 /* Flush, clear interrupt, and rearm the EQ */
13737 lpfc_sli4_eq_flush(phba, fpeq);
13738 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013739 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013740 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013741 return IRQ_NONE;
13742 }
13743
13744 /*
13745 * Process all the event on FCP fast-path EQ
13746 */
13747 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040013748 if (eqe == NULL)
13749 break;
13750
James Smart895427b2017-02-12 13:52:30 -080013751 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040013752 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013753 break;
James Smartb84daac2012-08-03 12:35:13 -040013754 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013755 }
13756
James Smartb84daac2012-08-03 12:35:13 -040013757 /* Track the max number of EQEs processed in 1 intr */
13758 if (ecount > fpeq->EQ_max_eqe)
13759 fpeq->EQ_max_eqe = ecount;
13760
James Smart4f774512009-05-22 14:52:35 -040013761 /* Always clear and re-arm the fast-path EQ */
13762 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13763
13764 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013765 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013766
13767 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013768 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013769 return IRQ_NONE;
13770 }
13771
James Smart4f774512009-05-22 14:52:35 -040013772 if (phba->intr_type == MSIX)
13773 /* MSI-X treated interrupt served as no EQ share INT */
13774 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13775 "0358 MSI-X interrupt with no EQE\n");
13776 else
13777 /* Non MSI-X treated on interrupt as EQ share INT */
13778 return IRQ_NONE;
13779 }
13780
James Smartba20c852012-08-03 12:36:52 -040013781 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013782 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13783
James Smart4f774512009-05-22 14:52:35 -040013784 return IRQ_HANDLED;
13785} /* lpfc_sli4_fp_intr_handler */
13786
13787/**
13788 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13789 * @irq: Interrupt number.
13790 * @dev_id: The device context pointer.
13791 *
13792 * This function is the device-level interrupt handler to device with SLI-4
13793 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13794 * interrupt mode is enabled and there is an event in the HBA which requires
13795 * driver attention. This function invokes the slow-path interrupt attention
13796 * handling function and fast-path interrupt attention handling function in
13797 * turn to process the relevant HBA attention events. This function is called
13798 * without any lock held. It gets the hbalock to access and update SLI data
13799 * structures.
13800 *
13801 * This function returns IRQ_HANDLED when interrupt is handled, else it
13802 * returns IRQ_NONE.
13803 **/
13804irqreturn_t
13805lpfc_sli4_intr_handler(int irq, void *dev_id)
13806{
13807 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013808 irqreturn_t hba_irq_rc;
13809 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013810 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013811
13812 /* Get the driver's phba structure from the dev_id */
13813 phba = (struct lpfc_hba *)dev_id;
13814
13815 if (unlikely(!phba))
13816 return IRQ_NONE;
13817
13818 /*
James Smart4f774512009-05-22 14:52:35 -040013819 * Invoke fast-path host attention interrupt handling as appropriate.
13820 */
James Smart895427b2017-02-12 13:52:30 -080013821 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013822 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013823 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013824 if (hba_irq_rc == IRQ_HANDLED)
13825 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013826 }
13827
James Smart1ba981f2014-02-20 09:56:45 -050013828 if (phba->cfg_fof) {
13829 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013830 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013831 if (hba_irq_rc == IRQ_HANDLED)
13832 hba_handled |= true;
13833 }
13834
James Smart67d12732012-08-03 12:36:13 -040013835 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013836} /* lpfc_sli4_intr_handler */
13837
13838/**
13839 * lpfc_sli4_queue_free - free a queue structure and associated memory
13840 * @queue: The queue structure to free.
13841 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013842 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013843 * the host resident queue. This function must be called after destroying the
13844 * queue on the HBA.
13845 **/
13846void
13847lpfc_sli4_queue_free(struct lpfc_queue *queue)
13848{
13849 struct lpfc_dmabuf *dmabuf;
13850
13851 if (!queue)
13852 return;
13853
13854 while (!list_empty(&queue->page_list)) {
13855 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13856 list);
James Smart49198b32010-04-06 15:04:33 -040013857 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040013858 dmabuf->virt, dmabuf->phys);
13859 kfree(dmabuf);
13860 }
James Smart895427b2017-02-12 13:52:30 -080013861 if (queue->rqbp) {
13862 lpfc_free_rq_buffer(queue->phba, queue);
13863 kfree(queue->rqbp);
13864 }
James Smartd1f525a2017-04-21 16:04:55 -070013865
13866 if (!list_empty(&queue->wq_list))
13867 list_del(&queue->wq_list);
13868
James Smart4f774512009-05-22 14:52:35 -040013869 kfree(queue);
13870 return;
13871}
13872
13873/**
13874 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13875 * @phba: The HBA that this queue is being created on.
13876 * @entry_size: The size of each queue entry for this queue.
13877 * @entry count: The number of entries that this queue will handle.
13878 *
13879 * This function allocates a queue structure and the DMAable memory used for
13880 * the host resident queue. This function must be called before creating the
13881 * queue on the HBA.
13882 **/
13883struct lpfc_queue *
13884lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13885 uint32_t entry_count)
13886{
13887 struct lpfc_queue *queue;
13888 struct lpfc_dmabuf *dmabuf;
13889 int x, total_qe_count;
13890 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013891 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013892
James Smartcb5172e2010-03-15 11:25:07 -040013893 if (!phba->sli4_hba.pc_sli4_params.supported)
13894 hw_page_size = SLI4_PAGE_SIZE;
13895
James Smart4f774512009-05-22 14:52:35 -040013896 queue = kzalloc(sizeof(struct lpfc_queue) +
13897 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13898 if (!queue)
13899 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013900 queue->page_count = (ALIGN(entry_size * entry_count,
13901 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013902
13903 /* If needed, Adjust page count to match the max the adapter supports */
13904 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13905 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13906
James Smart4f774512009-05-22 14:52:35 -040013907 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013908 INIT_LIST_HEAD(&queue->wq_list);
James Smart4f774512009-05-22 14:52:35 -040013909 INIT_LIST_HEAD(&queue->page_list);
13910 INIT_LIST_HEAD(&queue->child_list);
13911 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13912 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13913 if (!dmabuf)
13914 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013915 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13916 hw_page_size, &dmabuf->phys,
13917 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013918 if (!dmabuf->virt) {
13919 kfree(dmabuf);
13920 goto out_fail;
13921 }
13922 dmabuf->buffer_tag = x;
13923 list_add_tail(&dmabuf->list, &queue->page_list);
13924 /* initialize queue's entry array */
13925 dma_pointer = dmabuf->virt;
13926 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013927 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013928 total_qe_count++, dma_pointer += entry_size) {
13929 queue->qe[total_qe_count].address = dma_pointer;
13930 }
13931 }
13932 queue->entry_size = entry_size;
13933 queue->entry_count = entry_count;
13934 queue->phba = phba;
13935
James Smart64eb4dc2017-05-15 15:20:49 -070013936 /* entry_repost will be set during q creation */
13937
James Smart4f774512009-05-22 14:52:35 -040013938 return queue;
13939out_fail:
13940 lpfc_sli4_queue_free(queue);
13941 return NULL;
13942}
13943
13944/**
James Smart962bc512013-01-03 15:44:00 -050013945 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13946 * @phba: HBA structure that indicates port to create a queue on.
13947 * @pci_barset: PCI BAR set flag.
13948 *
13949 * This function shall perform iomap of the specified PCI BAR address to host
13950 * memory address if not already done so and return it. The returned host
13951 * memory address can be NULL.
13952 */
13953static void __iomem *
13954lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13955{
James Smart962bc512013-01-03 15:44:00 -050013956 if (!phba->pcidev)
13957 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013958
13959 switch (pci_barset) {
13960 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013961 return phba->pci_bar0_memmap_p;
13962 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013963 return phba->pci_bar2_memmap_p;
13964 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013965 return phba->pci_bar4_memmap_p;
13966 default:
13967 break;
13968 }
13969 return NULL;
13970}
13971
13972/**
James Smart895427b2017-02-12 13:52:30 -080013973 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040013974 * @phba: HBA structure that indicates port to create a queue on.
13975 * @startq: The starting FCP EQ to modify
13976 *
13977 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080013978 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
13979 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040013980 *
13981 * The @phba struct is used to send mailbox command to HBA. The @startq
13982 * is used to get the starting FCP EQ to change.
13983 * This function is asynchronous and will wait for the mailbox
13984 * command to finish before continuing.
13985 *
13986 * On success this function will return a zero. If unable to allocate enough
13987 * memory this function will return -ENOMEM. If the queue create mailbox command
13988 * fails this function will return -ENXIO.
13989 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013990int
James Smart895427b2017-02-12 13:52:30 -080013991lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013992{
13993 struct lpfc_mbx_modify_eq_delay *eq_delay;
13994 LPFC_MBOXQ_t *mbox;
13995 struct lpfc_queue *eq;
13996 int cnt, rc, length, status = 0;
13997 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013998 uint32_t result;
James Smart895427b2017-02-12 13:52:30 -080013999 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014000 union lpfc_sli4_cfg_shdr *shdr;
14001 uint16_t dmult;
14002
James Smart895427b2017-02-12 13:52:30 -080014003 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014004 return 0;
14005
14006 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14007 if (!mbox)
14008 return -ENOMEM;
14009 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14010 sizeof(struct lpfc_sli4_cfg_mhdr));
14011 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14012 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14013 length, LPFC_SLI4_MBX_EMBED);
14014 eq_delay = &mbox->u.mqe.un.eq_delay;
14015
14016 /* Calculate delay multiper from maximum interrupt per second */
James Smart895427b2017-02-12 13:52:30 -080014017 result = phba->cfg_fcp_imax / phba->io_channel_irqs;
14018 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014019 dmult = 0;
14020 else
14021 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040014022
14023 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014024 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14025 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014026 if (!eq)
14027 continue;
14028 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14029 eq_delay->u.request.eq[cnt].phase = 0;
14030 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14031 cnt++;
James Smart43140ca2017-03-04 09:30:34 -080014032 if (cnt >= LPFC_MAX_EQ_DELAY_EQID_CNT)
James Smart173edbb2012-06-12 13:54:50 -040014033 break;
14034 }
14035 eq_delay->u.request.num_eq = cnt;
14036
14037 mbox->vport = phba->pport;
14038 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14039 mbox->context1 = NULL;
14040 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14041 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14042 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14043 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14044 if (shdr_status || shdr_add_status || rc) {
14045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14046 "2512 MODIFY_EQ_DELAY mailbox failed with "
14047 "status x%x add_status x%x, mbx status x%x\n",
14048 shdr_status, shdr_add_status, rc);
14049 status = -ENXIO;
14050 }
14051 mempool_free(mbox, phba->mbox_mem_pool);
14052 return status;
14053}
14054
14055/**
James Smart4f774512009-05-22 14:52:35 -040014056 * lpfc_eq_create - Create an Event Queue on the HBA
14057 * @phba: HBA structure that indicates port to create a queue on.
14058 * @eq: The queue structure to use to create the event queue.
14059 * @imax: The maximum interrupt per second limit.
14060 *
14061 * This function creates an event queue, as detailed in @eq, on a port,
14062 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14063 *
14064 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14065 * is used to get the entry count and entry size that are necessary to
14066 * determine the number of pages to allocate and use for this queue. This
14067 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14068 * event queue. This function is asynchronous and will wait for the mailbox
14069 * command to finish before continuing.
14070 *
14071 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014072 * memory this function will return -ENOMEM. If the queue create mailbox command
14073 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014074 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014075int
James Smartee020062012-09-29 11:28:52 -040014076lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014077{
14078 struct lpfc_mbx_eq_create *eq_create;
14079 LPFC_MBOXQ_t *mbox;
14080 int rc, length, status = 0;
14081 struct lpfc_dmabuf *dmabuf;
14082 uint32_t shdr_status, shdr_add_status;
14083 union lpfc_sli4_cfg_shdr *shdr;
14084 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014085 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14086
James Smart2e90f4b2011-12-13 13:22:37 -050014087 /* sanity check on queue memory */
14088 if (!eq)
14089 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014090 if (!phba->sli4_hba.pc_sli4_params.supported)
14091 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014092
14093 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14094 if (!mbox)
14095 return -ENOMEM;
14096 length = (sizeof(struct lpfc_mbx_eq_create) -
14097 sizeof(struct lpfc_sli4_cfg_mhdr));
14098 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14099 LPFC_MBOX_OPCODE_EQ_CREATE,
14100 length, LPFC_SLI4_MBX_EMBED);
14101 eq_create = &mbox->u.mqe.un.eq_create;
14102 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14103 eq->page_count);
14104 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14105 LPFC_EQE_SIZE);
14106 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040014107 /* don't setup delay multiplier using EQ_CREATE */
14108 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014109 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14110 dmult);
14111 switch (eq->entry_count) {
14112 default:
14113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14114 "0360 Unsupported EQ count. (%d)\n",
14115 eq->entry_count);
14116 if (eq->entry_count < 256)
14117 return -EINVAL;
14118 /* otherwise default to smallest count (drop through) */
14119 case 256:
14120 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14121 LPFC_EQ_CNT_256);
14122 break;
14123 case 512:
14124 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14125 LPFC_EQ_CNT_512);
14126 break;
14127 case 1024:
14128 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14129 LPFC_EQ_CNT_1024);
14130 break;
14131 case 2048:
14132 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14133 LPFC_EQ_CNT_2048);
14134 break;
14135 case 4096:
14136 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14137 LPFC_EQ_CNT_4096);
14138 break;
14139 }
14140 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014141 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014142 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14143 putPaddrLow(dmabuf->phys);
14144 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14145 putPaddrHigh(dmabuf->phys);
14146 }
14147 mbox->vport = phba->pport;
14148 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14149 mbox->context1 = NULL;
14150 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14151 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14152 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14153 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14154 if (shdr_status || shdr_add_status || rc) {
14155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14156 "2500 EQ_CREATE mailbox failed with "
14157 "status x%x add_status x%x, mbx status x%x\n",
14158 shdr_status, shdr_add_status, rc);
14159 status = -ENXIO;
14160 }
14161 eq->type = LPFC_EQ;
14162 eq->subtype = LPFC_NONE;
14163 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14164 if (eq->queue_id == 0xFFFF)
14165 status = -ENXIO;
14166 eq->host_index = 0;
14167 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014168 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014169
James Smart8fa38512009-07-19 10:01:03 -040014170 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014171 return status;
14172}
14173
14174/**
14175 * lpfc_cq_create - Create a Completion Queue on the HBA
14176 * @phba: HBA structure that indicates port to create a queue on.
14177 * @cq: The queue structure to use to create the completion queue.
14178 * @eq: The event queue to bind this completion queue to.
14179 *
14180 * This function creates a completion queue, as detailed in @wq, on a port,
14181 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14182 *
14183 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14184 * is used to get the entry count and entry size that are necessary to
14185 * determine the number of pages to allocate and use for this queue. The @eq
14186 * is used to indicate which event queue to bind this completion queue to. This
14187 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14188 * completion queue. This function is asynchronous and will wait for the mailbox
14189 * command to finish before continuing.
14190 *
14191 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014192 * memory this function will return -ENOMEM. If the queue create mailbox command
14193 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014194 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014195int
James Smart4f774512009-05-22 14:52:35 -040014196lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14197 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14198{
14199 struct lpfc_mbx_cq_create *cq_create;
14200 struct lpfc_dmabuf *dmabuf;
14201 LPFC_MBOXQ_t *mbox;
14202 int rc, length, status = 0;
14203 uint32_t shdr_status, shdr_add_status;
14204 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014205 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14206
James Smart2e90f4b2011-12-13 13:22:37 -050014207 /* sanity check on queue memory */
14208 if (!cq || !eq)
14209 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014210 if (!phba->sli4_hba.pc_sli4_params.supported)
14211 hw_page_size = SLI4_PAGE_SIZE;
14212
James Smart4f774512009-05-22 14:52:35 -040014213 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14214 if (!mbox)
14215 return -ENOMEM;
14216 length = (sizeof(struct lpfc_mbx_cq_create) -
14217 sizeof(struct lpfc_sli4_cfg_mhdr));
14218 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14219 LPFC_MBOX_OPCODE_CQ_CREATE,
14220 length, LPFC_SLI4_MBX_EMBED);
14221 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014222 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014223 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14224 cq->page_count);
14225 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14226 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014227 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14228 phba->sli4_hba.pc_sli4_params.cqv);
14229 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040014230 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
14231 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014232 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14233 eq->queue_id);
14234 } else {
14235 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14236 eq->queue_id);
14237 }
James Smart4f774512009-05-22 14:52:35 -040014238 switch (cq->entry_count) {
14239 default:
14240 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014241 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014242 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014243 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014244 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014245 if (cq->entry_count < 256) {
14246 status = -EINVAL;
14247 goto out;
14248 }
James Smart4f774512009-05-22 14:52:35 -040014249 /* otherwise default to smallest count (drop through) */
14250 case 256:
14251 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14252 LPFC_CQ_CNT_256);
14253 break;
14254 case 512:
14255 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14256 LPFC_CQ_CNT_512);
14257 break;
14258 case 1024:
14259 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14260 LPFC_CQ_CNT_1024);
14261 break;
14262 }
14263 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014264 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014265 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14266 putPaddrLow(dmabuf->phys);
14267 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14268 putPaddrHigh(dmabuf->phys);
14269 }
14270 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14271
14272 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014273 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14274 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14275 if (shdr_status || shdr_add_status || rc) {
14276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14277 "2501 CQ_CREATE mailbox failed with "
14278 "status x%x add_status x%x, mbx status x%x\n",
14279 shdr_status, shdr_add_status, rc);
14280 status = -ENXIO;
14281 goto out;
14282 }
14283 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14284 if (cq->queue_id == 0xFFFF) {
14285 status = -ENXIO;
14286 goto out;
14287 }
14288 /* link the cq onto the parent eq child list */
14289 list_add_tail(&cq->list, &eq->child_list);
14290 /* Set up completion queue's type and subtype */
14291 cq->type = type;
14292 cq->subtype = subtype;
14293 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014294 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014295 cq->host_index = 0;
14296 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014297 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014298
James Smart8fa38512009-07-19 10:01:03 -040014299out:
14300 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014301 return status;
14302}
14303
14304/**
James Smart2d7dbc42017-02-12 13:52:35 -080014305 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14306 * @phba: HBA structure that indicates port to create a queue on.
14307 * @cqp: The queue structure array to use to create the completion queues.
14308 * @eqp: The event queue array to bind these completion queues to.
14309 *
14310 * This function creates a set of completion queue, s to support MRQ
14311 * as detailed in @cqp, on a port,
14312 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14313 *
14314 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14315 * is used to get the entry count and entry size that are necessary to
14316 * determine the number of pages to allocate and use for this queue. The @eq
14317 * is used to indicate which event queue to bind this completion queue to. This
14318 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14319 * completion queue. This function is asynchronous and will wait for the mailbox
14320 * command to finish before continuing.
14321 *
14322 * On success this function will return a zero. If unable to allocate enough
14323 * memory this function will return -ENOMEM. If the queue create mailbox command
14324 * fails this function will return -ENXIO.
14325 **/
14326int
14327lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14328 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14329{
14330 struct lpfc_queue *cq;
14331 struct lpfc_queue *eq;
14332 struct lpfc_mbx_cq_create_set *cq_set;
14333 struct lpfc_dmabuf *dmabuf;
14334 LPFC_MBOXQ_t *mbox;
14335 int rc, length, alloclen, status = 0;
14336 int cnt, idx, numcq, page_idx = 0;
14337 uint32_t shdr_status, shdr_add_status;
14338 union lpfc_sli4_cfg_shdr *shdr;
14339 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14340
14341 /* sanity check on queue memory */
14342 numcq = phba->cfg_nvmet_mrq;
14343 if (!cqp || !eqp || !numcq)
14344 return -ENODEV;
14345 if (!phba->sli4_hba.pc_sli4_params.supported)
14346 hw_page_size = SLI4_PAGE_SIZE;
14347
14348 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14349 if (!mbox)
14350 return -ENOMEM;
14351
14352 length = sizeof(struct lpfc_mbx_cq_create_set);
14353 length += ((numcq * cqp[0]->page_count) *
14354 sizeof(struct dma_address));
14355 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14356 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14357 LPFC_SLI4_MBX_NEMBED);
14358 if (alloclen < length) {
14359 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14360 "3098 Allocated DMA memory size (%d) is "
14361 "less than the requested DMA memory size "
14362 "(%d)\n", alloclen, length);
14363 status = -ENOMEM;
14364 goto out;
14365 }
14366 cq_set = mbox->sge_array->addr[0];
14367 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14368 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14369
14370 for (idx = 0; idx < numcq; idx++) {
14371 cq = cqp[idx];
14372 eq = eqp[idx];
14373 if (!cq || !eq) {
14374 status = -ENOMEM;
14375 goto out;
14376 }
14377
14378 switch (idx) {
14379 case 0:
14380 bf_set(lpfc_mbx_cq_create_set_page_size,
14381 &cq_set->u.request,
14382 (hw_page_size / SLI4_PAGE_SIZE));
14383 bf_set(lpfc_mbx_cq_create_set_num_pages,
14384 &cq_set->u.request, cq->page_count);
14385 bf_set(lpfc_mbx_cq_create_set_evt,
14386 &cq_set->u.request, 1);
14387 bf_set(lpfc_mbx_cq_create_set_valid,
14388 &cq_set->u.request, 1);
14389 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14390 &cq_set->u.request, 0);
14391 bf_set(lpfc_mbx_cq_create_set_num_cq,
14392 &cq_set->u.request, numcq);
14393 switch (cq->entry_count) {
14394 default:
14395 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14396 "3118 Bad CQ count. (%d)\n",
14397 cq->entry_count);
14398 if (cq->entry_count < 256) {
14399 status = -EINVAL;
14400 goto out;
14401 }
14402 /* otherwise default to smallest (drop thru) */
14403 case 256:
14404 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14405 &cq_set->u.request, LPFC_CQ_CNT_256);
14406 break;
14407 case 512:
14408 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14409 &cq_set->u.request, LPFC_CQ_CNT_512);
14410 break;
14411 case 1024:
14412 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14413 &cq_set->u.request, LPFC_CQ_CNT_1024);
14414 break;
14415 }
14416 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14417 &cq_set->u.request, eq->queue_id);
14418 break;
14419 case 1:
14420 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14421 &cq_set->u.request, eq->queue_id);
14422 break;
14423 case 2:
14424 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14425 &cq_set->u.request, eq->queue_id);
14426 break;
14427 case 3:
14428 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14429 &cq_set->u.request, eq->queue_id);
14430 break;
14431 case 4:
14432 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14433 &cq_set->u.request, eq->queue_id);
14434 break;
14435 case 5:
14436 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14437 &cq_set->u.request, eq->queue_id);
14438 break;
14439 case 6:
14440 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14441 &cq_set->u.request, eq->queue_id);
14442 break;
14443 case 7:
14444 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14445 &cq_set->u.request, eq->queue_id);
14446 break;
14447 case 8:
14448 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14449 &cq_set->u.request, eq->queue_id);
14450 break;
14451 case 9:
14452 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14453 &cq_set->u.request, eq->queue_id);
14454 break;
14455 case 10:
14456 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14457 &cq_set->u.request, eq->queue_id);
14458 break;
14459 case 11:
14460 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14461 &cq_set->u.request, eq->queue_id);
14462 break;
14463 case 12:
14464 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14465 &cq_set->u.request, eq->queue_id);
14466 break;
14467 case 13:
14468 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14469 &cq_set->u.request, eq->queue_id);
14470 break;
14471 case 14:
14472 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14473 &cq_set->u.request, eq->queue_id);
14474 break;
14475 case 15:
14476 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14477 &cq_set->u.request, eq->queue_id);
14478 break;
14479 }
14480
14481 /* link the cq onto the parent eq child list */
14482 list_add_tail(&cq->list, &eq->child_list);
14483 /* Set up completion queue's type and subtype */
14484 cq->type = type;
14485 cq->subtype = subtype;
14486 cq->assoc_qid = eq->queue_id;
14487 cq->host_index = 0;
14488 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014489 cq->entry_repost = LPFC_CQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080014490
14491 rc = 0;
14492 list_for_each_entry(dmabuf, &cq->page_list, list) {
14493 memset(dmabuf->virt, 0, hw_page_size);
14494 cnt = page_idx + dmabuf->buffer_tag;
14495 cq_set->u.request.page[cnt].addr_lo =
14496 putPaddrLow(dmabuf->phys);
14497 cq_set->u.request.page[cnt].addr_hi =
14498 putPaddrHigh(dmabuf->phys);
14499 rc++;
14500 }
14501 page_idx += rc;
14502 }
14503
14504 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14505
14506 /* The IOCTL status is embedded in the mailbox subheader. */
14507 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14508 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14509 if (shdr_status || shdr_add_status || rc) {
14510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14511 "3119 CQ_CREATE_SET mailbox failed with "
14512 "status x%x add_status x%x, mbx status x%x\n",
14513 shdr_status, shdr_add_status, rc);
14514 status = -ENXIO;
14515 goto out;
14516 }
14517 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14518 if (rc == 0xFFFF) {
14519 status = -ENXIO;
14520 goto out;
14521 }
14522
14523 for (idx = 0; idx < numcq; idx++) {
14524 cq = cqp[idx];
14525 cq->queue_id = rc + idx;
14526 }
14527
14528out:
14529 lpfc_sli4_mbox_cmd_free(phba, mbox);
14530 return status;
14531}
14532
14533/**
James Smartb19a0612010-04-06 14:48:51 -040014534 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014535 * @phba: HBA structure that indicates port to create a queue on.
14536 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014537 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14538 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014539 *
James Smartb19a0612010-04-06 14:48:51 -040014540 * This function provides failback (fb) functionality when the
14541 * mq_create_ext fails on older FW generations. It's purpose is identical
14542 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014543 *
James Smartb19a0612010-04-06 14:48:51 -040014544 * This routine cannot fail as all attributes were previously accessed and
14545 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014546 **/
James Smartb19a0612010-04-06 14:48:51 -040014547static void
14548lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14549 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014550{
14551 struct lpfc_mbx_mq_create *mq_create;
14552 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014553 int length;
James Smart04c68492009-05-22 14:52:52 -040014554
James Smart04c68492009-05-22 14:52:52 -040014555 length = (sizeof(struct lpfc_mbx_mq_create) -
14556 sizeof(struct lpfc_sli4_cfg_mhdr));
14557 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14558 LPFC_MBOX_OPCODE_MQ_CREATE,
14559 length, LPFC_SLI4_MBX_EMBED);
14560 mq_create = &mbox->u.mqe.un.mq_create;
14561 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014562 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014563 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014564 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014565 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14566 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014567 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014568 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14569 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014570 break;
14571 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014572 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14573 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014574 break;
14575 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014576 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14577 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014578 break;
14579 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014580 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14581 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014582 break;
14583 }
14584 list_for_each_entry(dmabuf, &mq->page_list, list) {
14585 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014586 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014587 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014588 putPaddrHigh(dmabuf->phys);
14589 }
14590}
14591
14592/**
14593 * lpfc_mq_create - Create a mailbox Queue on the HBA
14594 * @phba: HBA structure that indicates port to create a queue on.
14595 * @mq: The queue structure to use to create the mailbox queue.
14596 * @cq: The completion queue to associate with this cq.
14597 * @subtype: The queue's subtype.
14598 *
14599 * This function creates a mailbox queue, as detailed in @mq, on a port,
14600 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14601 *
14602 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14603 * is used to get the entry count and entry size that are necessary to
14604 * determine the number of pages to allocate and use for this queue. This
14605 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14606 * mailbox queue. This function is asynchronous and will wait for the mailbox
14607 * command to finish before continuing.
14608 *
14609 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014610 * memory this function will return -ENOMEM. If the queue create mailbox command
14611 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014612 **/
14613int32_t
14614lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14615 struct lpfc_queue *cq, uint32_t subtype)
14616{
14617 struct lpfc_mbx_mq_create *mq_create;
14618 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14619 struct lpfc_dmabuf *dmabuf;
14620 LPFC_MBOXQ_t *mbox;
14621 int rc, length, status = 0;
14622 uint32_t shdr_status, shdr_add_status;
14623 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014624 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014625
James Smart2e90f4b2011-12-13 13:22:37 -050014626 /* sanity check on queue memory */
14627 if (!mq || !cq)
14628 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014629 if (!phba->sli4_hba.pc_sli4_params.supported)
14630 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014631
14632 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14633 if (!mbox)
14634 return -ENOMEM;
14635 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14636 sizeof(struct lpfc_sli4_cfg_mhdr));
14637 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14638 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14639 length, LPFC_SLI4_MBX_EMBED);
14640
14641 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014642 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014643 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14644 &mq_create_ext->u.request, mq->page_count);
14645 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14646 &mq_create_ext->u.request, 1);
14647 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014648 &mq_create_ext->u.request, 1);
14649 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14650 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014651 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14652 &mq_create_ext->u.request, 1);
14653 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14654 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014655 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014656 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14657 phba->sli4_hba.pc_sli4_params.mqv);
14658 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14659 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14660 cq->queue_id);
14661 else
14662 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14663 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014664 switch (mq->entry_count) {
14665 default:
14666 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14667 "0362 Unsupported MQ count. (%d)\n",
14668 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014669 if (mq->entry_count < 16) {
14670 status = -EINVAL;
14671 goto out;
14672 }
James Smartb19a0612010-04-06 14:48:51 -040014673 /* otherwise default to smallest count (drop through) */
14674 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014675 bf_set(lpfc_mq_context_ring_size,
14676 &mq_create_ext->u.request.context,
14677 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014678 break;
14679 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014680 bf_set(lpfc_mq_context_ring_size,
14681 &mq_create_ext->u.request.context,
14682 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014683 break;
14684 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014685 bf_set(lpfc_mq_context_ring_size,
14686 &mq_create_ext->u.request.context,
14687 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014688 break;
14689 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014690 bf_set(lpfc_mq_context_ring_size,
14691 &mq_create_ext->u.request.context,
14692 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014693 break;
14694 }
14695 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014696 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014697 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14698 putPaddrLow(dmabuf->phys);
14699 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014700 putPaddrHigh(dmabuf->phys);
14701 }
14702 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014703 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14704 &mq_create_ext->u.response);
14705 if (rc != MBX_SUCCESS) {
14706 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14707 "2795 MQ_CREATE_EXT failed with "
14708 "status x%x. Failback to MQ_CREATE.\n",
14709 rc);
14710 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14711 mq_create = &mbox->u.mqe.un.mq_create;
14712 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14713 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14714 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14715 &mq_create->u.response);
14716 }
14717
James Smart04c68492009-05-22 14:52:52 -040014718 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014719 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14720 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14721 if (shdr_status || shdr_add_status || rc) {
14722 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14723 "2502 MQ_CREATE mailbox failed with "
14724 "status x%x add_status x%x, mbx status x%x\n",
14725 shdr_status, shdr_add_status, rc);
14726 status = -ENXIO;
14727 goto out;
14728 }
James Smart04c68492009-05-22 14:52:52 -040014729 if (mq->queue_id == 0xFFFF) {
14730 status = -ENXIO;
14731 goto out;
14732 }
14733 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014734 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014735 mq->subtype = subtype;
14736 mq->host_index = 0;
14737 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014738 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040014739
14740 /* link the mq onto the parent cq child list */
14741 list_add_tail(&mq->list, &cq->child_list);
14742out:
James Smart8fa38512009-07-19 10:01:03 -040014743 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014744 return status;
14745}
14746
14747/**
James Smart4f774512009-05-22 14:52:35 -040014748 * lpfc_wq_create - Create a Work Queue on the HBA
14749 * @phba: HBA structure that indicates port to create a queue on.
14750 * @wq: The queue structure to use to create the work queue.
14751 * @cq: The completion queue to bind this work queue to.
14752 * @subtype: The subtype of the work queue indicating its functionality.
14753 *
14754 * This function creates a work queue, as detailed in @wq, on a port, described
14755 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14756 *
14757 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14758 * is used to get the entry count and entry size that are necessary to
14759 * determine the number of pages to allocate and use for this queue. The @cq
14760 * is used to indicate which completion queue to bind this work queue to. This
14761 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14762 * work queue. This function is asynchronous and will wait for the mailbox
14763 * command to finish before continuing.
14764 *
14765 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014766 * memory this function will return -ENOMEM. If the queue create mailbox command
14767 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014768 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014769int
James Smart4f774512009-05-22 14:52:35 -040014770lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14771 struct lpfc_queue *cq, uint32_t subtype)
14772{
14773 struct lpfc_mbx_wq_create *wq_create;
14774 struct lpfc_dmabuf *dmabuf;
14775 LPFC_MBOXQ_t *mbox;
14776 int rc, length, status = 0;
14777 uint32_t shdr_status, shdr_add_status;
14778 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014779 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014780 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014781 void __iomem *bar_memmap_p;
14782 uint32_t db_offset;
14783 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014784
James Smart2e90f4b2011-12-13 13:22:37 -050014785 /* sanity check on queue memory */
14786 if (!wq || !cq)
14787 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014788 if (!phba->sli4_hba.pc_sli4_params.supported)
14789 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014790
14791 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14792 if (!mbox)
14793 return -ENOMEM;
14794 length = (sizeof(struct lpfc_mbx_wq_create) -
14795 sizeof(struct lpfc_sli4_cfg_mhdr));
14796 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14797 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14798 length, LPFC_SLI4_MBX_EMBED);
14799 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014800 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014801 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14802 wq->page_count);
14803 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14804 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014805
14806 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014807 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14808 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014809
James Smart0c651872013-07-15 18:33:23 -040014810 switch (phba->sli4_hba.pc_sli4_params.wqv) {
14811 case LPFC_Q_CREATE_VERSION_0:
14812 switch (wq->entry_size) {
14813 default:
14814 case 64:
14815 /* Nothing to do, version 0 ONLY supports 64 byte */
14816 page = wq_create->u.request.page;
14817 break;
14818 case 128:
14819 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14820 LPFC_WQ_SZ128_SUPPORT)) {
14821 status = -ERANGE;
14822 goto out;
14823 }
14824 /* If we get here the HBA MUST also support V1 and
14825 * we MUST use it
14826 */
14827 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14828 LPFC_Q_CREATE_VERSION_1);
14829
14830 bf_set(lpfc_mbx_wq_create_wqe_count,
14831 &wq_create->u.request_1, wq->entry_count);
14832 bf_set(lpfc_mbx_wq_create_wqe_size,
14833 &wq_create->u.request_1,
14834 LPFC_WQ_WQE_SIZE_128);
14835 bf_set(lpfc_mbx_wq_create_page_size,
14836 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014837 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014838 page = wq_create->u.request_1.page;
14839 break;
14840 }
14841 break;
14842 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014843 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14844 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070014845 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14846 LPFC_Q_CREATE_VERSION_1);
14847
James Smart5a6f1332011-03-11 16:05:35 -050014848 switch (wq->entry_size) {
14849 default:
14850 case 64:
14851 bf_set(lpfc_mbx_wq_create_wqe_size,
14852 &wq_create->u.request_1,
14853 LPFC_WQ_WQE_SIZE_64);
14854 break;
14855 case 128:
James Smart0c651872013-07-15 18:33:23 -040014856 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14857 LPFC_WQ_SZ128_SUPPORT)) {
14858 status = -ERANGE;
14859 goto out;
14860 }
James Smart5a6f1332011-03-11 16:05:35 -050014861 bf_set(lpfc_mbx_wq_create_wqe_size,
14862 &wq_create->u.request_1,
14863 LPFC_WQ_WQE_SIZE_128);
14864 break;
14865 }
James Smart8ea73db2017-02-12 13:52:25 -080014866 bf_set(lpfc_mbx_wq_create_page_size,
14867 &wq_create->u.request_1,
14868 LPFC_WQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014869 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040014870 break;
14871 default:
14872 status = -ERANGE;
14873 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050014874 }
James Smart0c651872013-07-15 18:33:23 -040014875
James Smart4f774512009-05-22 14:52:35 -040014876 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014877 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050014878 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14879 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040014880 }
James Smart962bc512013-01-03 15:44:00 -050014881
14882 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14883 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14884
James Smart4f774512009-05-22 14:52:35 -040014885 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14886 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014887 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14888 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14889 if (shdr_status || shdr_add_status || rc) {
14890 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14891 "2503 WQ_CREATE mailbox failed with "
14892 "status x%x add_status x%x, mbx status x%x\n",
14893 shdr_status, shdr_add_status, rc);
14894 status = -ENXIO;
14895 goto out;
14896 }
14897 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14898 if (wq->queue_id == 0xFFFF) {
14899 status = -ENXIO;
14900 goto out;
14901 }
James Smart962bc512013-01-03 15:44:00 -050014902 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14903 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14904 &wq_create->u.response);
14905 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14906 (wq->db_format != LPFC_DB_RING_FORMAT)) {
14907 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14908 "3265 WQ[%d] doorbell format not "
14909 "supported: x%x\n", wq->queue_id,
14910 wq->db_format);
14911 status = -EINVAL;
14912 goto out;
14913 }
14914 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14915 &wq_create->u.response);
14916 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14917 if (!bar_memmap_p) {
14918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14919 "3263 WQ[%d] failed to memmap pci "
14920 "barset:x%x\n", wq->queue_id,
14921 pci_barset);
14922 status = -ENOMEM;
14923 goto out;
14924 }
14925 db_offset = wq_create->u.response.doorbell_offset;
14926 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14927 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14929 "3252 WQ[%d] doorbell offset not "
14930 "supported: x%x\n", wq->queue_id,
14931 db_offset);
14932 status = -EINVAL;
14933 goto out;
14934 }
14935 wq->db_regaddr = bar_memmap_p + db_offset;
14936 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014937 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14938 "format:x%x\n", wq->queue_id, pci_barset,
14939 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014940 } else {
14941 wq->db_format = LPFC_DB_LIST_FORMAT;
14942 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14943 }
James Smart895427b2017-02-12 13:52:30 -080014944 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14945 if (wq->pring == NULL) {
14946 status = -ENOMEM;
14947 goto out;
14948 }
James Smart4f774512009-05-22 14:52:35 -040014949 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050014950 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014951 wq->subtype = subtype;
14952 wq->host_index = 0;
14953 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050014954 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040014955
14956 /* link the wq onto the parent cq child list */
14957 list_add_tail(&wq->list, &cq->child_list);
14958out:
James Smart8fa38512009-07-19 10:01:03 -040014959 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014960 return status;
14961}
14962
14963/**
14964 * lpfc_rq_create - Create a Receive Queue on the HBA
14965 * @phba: HBA structure that indicates port to create a queue on.
14966 * @hrq: The queue structure to use to create the header receive queue.
14967 * @drq: The queue structure to use to create the data receive queue.
14968 * @cq: The completion queue to bind this work queue to.
14969 *
14970 * This function creates a receive buffer queue pair , as detailed in @hrq and
14971 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14972 * to the HBA.
14973 *
14974 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14975 * struct is used to get the entry count that is necessary to determine the
14976 * number of pages to use for this queue. The @cq is used to indicate which
14977 * completion queue to bind received buffers that are posted to these queues to.
14978 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14979 * receive queue pair. This function is asynchronous and will wait for the
14980 * mailbox command to finish before continuing.
14981 *
14982 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014983 * memory this function will return -ENOMEM. If the queue create mailbox command
14984 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014985 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014986int
James Smart4f774512009-05-22 14:52:35 -040014987lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14988 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14989{
14990 struct lpfc_mbx_rq_create *rq_create;
14991 struct lpfc_dmabuf *dmabuf;
14992 LPFC_MBOXQ_t *mbox;
14993 int rc, length, status = 0;
14994 uint32_t shdr_status, shdr_add_status;
14995 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014996 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050014997 void __iomem *bar_memmap_p;
14998 uint32_t db_offset;
14999 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015000
James Smart2e90f4b2011-12-13 13:22:37 -050015001 /* sanity check on queue memory */
15002 if (!hrq || !drq || !cq)
15003 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015004 if (!phba->sli4_hba.pc_sli4_params.supported)
15005 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015006
15007 if (hrq->entry_count != drq->entry_count)
15008 return -EINVAL;
15009 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15010 if (!mbox)
15011 return -ENOMEM;
15012 length = (sizeof(struct lpfc_mbx_rq_create) -
15013 sizeof(struct lpfc_sli4_cfg_mhdr));
15014 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15015 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15016 length, LPFC_SLI4_MBX_EMBED);
15017 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015018 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15019 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15020 phba->sli4_hba.pc_sli4_params.rqv);
15021 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15022 bf_set(lpfc_rq_context_rqe_count_1,
15023 &rq_create->u.request.context,
15024 hrq->entry_count);
15025 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015026 bf_set(lpfc_rq_context_rqe_size,
15027 &rq_create->u.request.context,
15028 LPFC_RQE_SIZE_8);
15029 bf_set(lpfc_rq_context_page_size,
15030 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015031 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015032 } else {
15033 switch (hrq->entry_count) {
15034 default:
15035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15036 "2535 Unsupported RQ count. (%d)\n",
15037 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015038 if (hrq->entry_count < 512) {
15039 status = -EINVAL;
15040 goto out;
15041 }
James Smart5a6f1332011-03-11 16:05:35 -050015042 /* otherwise default to smallest count (drop through) */
15043 case 512:
15044 bf_set(lpfc_rq_context_rqe_count,
15045 &rq_create->u.request.context,
15046 LPFC_RQ_RING_SIZE_512);
15047 break;
15048 case 1024:
15049 bf_set(lpfc_rq_context_rqe_count,
15050 &rq_create->u.request.context,
15051 LPFC_RQ_RING_SIZE_1024);
15052 break;
15053 case 2048:
15054 bf_set(lpfc_rq_context_rqe_count,
15055 &rq_create->u.request.context,
15056 LPFC_RQ_RING_SIZE_2048);
15057 break;
15058 case 4096:
15059 bf_set(lpfc_rq_context_rqe_count,
15060 &rq_create->u.request.context,
15061 LPFC_RQ_RING_SIZE_4096);
15062 break;
15063 }
15064 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15065 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015066 }
15067 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15068 cq->queue_id);
15069 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15070 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015071 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015072 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015073 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15074 putPaddrLow(dmabuf->phys);
15075 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15076 putPaddrHigh(dmabuf->phys);
15077 }
James Smart962bc512013-01-03 15:44:00 -050015078 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15079 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15080
James Smart4f774512009-05-22 14:52:35 -040015081 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15082 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015083 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15084 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15085 if (shdr_status || shdr_add_status || rc) {
15086 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15087 "2504 RQ_CREATE mailbox failed with "
15088 "status x%x add_status x%x, mbx status x%x\n",
15089 shdr_status, shdr_add_status, rc);
15090 status = -ENXIO;
15091 goto out;
15092 }
15093 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15094 if (hrq->queue_id == 0xFFFF) {
15095 status = -ENXIO;
15096 goto out;
15097 }
James Smart962bc512013-01-03 15:44:00 -050015098
15099 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15100 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15101 &rq_create->u.response);
15102 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15103 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15104 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15105 "3262 RQ [%d] doorbell format not "
15106 "supported: x%x\n", hrq->queue_id,
15107 hrq->db_format);
15108 status = -EINVAL;
15109 goto out;
15110 }
15111
15112 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15113 &rq_create->u.response);
15114 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15115 if (!bar_memmap_p) {
15116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15117 "3269 RQ[%d] failed to memmap pci "
15118 "barset:x%x\n", hrq->queue_id,
15119 pci_barset);
15120 status = -ENOMEM;
15121 goto out;
15122 }
15123
15124 db_offset = rq_create->u.response.doorbell_offset;
15125 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15126 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15127 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15128 "3270 RQ[%d] doorbell offset not "
15129 "supported: x%x\n", hrq->queue_id,
15130 db_offset);
15131 status = -EINVAL;
15132 goto out;
15133 }
15134 hrq->db_regaddr = bar_memmap_p + db_offset;
15135 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015136 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15137 "format:x%x\n", hrq->queue_id, pci_barset,
15138 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015139 } else {
15140 hrq->db_format = LPFC_DB_RING_FORMAT;
15141 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15142 }
James Smart4f774512009-05-22 14:52:35 -040015143 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015144 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015145 hrq->subtype = subtype;
15146 hrq->host_index = 0;
15147 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015148 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015149
15150 /* now create the data queue */
15151 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15152 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15153 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015154 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15155 phba->sli4_hba.pc_sli4_params.rqv);
15156 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15157 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015158 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015159 if (subtype == LPFC_NVMET)
15160 rq_create->u.request.context.buffer_size =
15161 LPFC_NVMET_DATA_BUF_SIZE;
15162 else
15163 rq_create->u.request.context.buffer_size =
15164 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015165 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15166 LPFC_RQE_SIZE_8);
15167 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15168 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015169 } else {
15170 switch (drq->entry_count) {
15171 default:
15172 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15173 "2536 Unsupported RQ count. (%d)\n",
15174 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015175 if (drq->entry_count < 512) {
15176 status = -EINVAL;
15177 goto out;
15178 }
James Smart5a6f1332011-03-11 16:05:35 -050015179 /* otherwise default to smallest count (drop through) */
15180 case 512:
15181 bf_set(lpfc_rq_context_rqe_count,
15182 &rq_create->u.request.context,
15183 LPFC_RQ_RING_SIZE_512);
15184 break;
15185 case 1024:
15186 bf_set(lpfc_rq_context_rqe_count,
15187 &rq_create->u.request.context,
15188 LPFC_RQ_RING_SIZE_1024);
15189 break;
15190 case 2048:
15191 bf_set(lpfc_rq_context_rqe_count,
15192 &rq_create->u.request.context,
15193 LPFC_RQ_RING_SIZE_2048);
15194 break;
15195 case 4096:
15196 bf_set(lpfc_rq_context_rqe_count,
15197 &rq_create->u.request.context,
15198 LPFC_RQ_RING_SIZE_4096);
15199 break;
15200 }
James Smart3c603be2017-05-15 15:20:44 -070015201 if (subtype == LPFC_NVMET)
15202 bf_set(lpfc_rq_context_buf_size,
15203 &rq_create->u.request.context,
15204 LPFC_NVMET_DATA_BUF_SIZE);
15205 else
15206 bf_set(lpfc_rq_context_buf_size,
15207 &rq_create->u.request.context,
15208 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015209 }
15210 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15211 cq->queue_id);
15212 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15213 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015214 list_for_each_entry(dmabuf, &drq->page_list, list) {
15215 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15216 putPaddrLow(dmabuf->phys);
15217 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15218 putPaddrHigh(dmabuf->phys);
15219 }
James Smart962bc512013-01-03 15:44:00 -050015220 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15221 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015222 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15223 /* The IOCTL status is embedded in the mailbox subheader. */
15224 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15225 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15226 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15227 if (shdr_status || shdr_add_status || rc) {
15228 status = -ENXIO;
15229 goto out;
15230 }
15231 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15232 if (drq->queue_id == 0xFFFF) {
15233 status = -ENXIO;
15234 goto out;
15235 }
15236 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015237 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015238 drq->subtype = subtype;
15239 drq->host_index = 0;
15240 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015241 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015242
15243 /* link the header and data RQs onto the parent cq child list */
15244 list_add_tail(&hrq->list, &cq->child_list);
15245 list_add_tail(&drq->list, &cq->child_list);
15246
15247out:
James Smart8fa38512009-07-19 10:01:03 -040015248 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015249 return status;
15250}
15251
15252/**
James Smart2d7dbc42017-02-12 13:52:35 -080015253 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15254 * @phba: HBA structure that indicates port to create a queue on.
15255 * @hrqp: The queue structure array to use to create the header receive queues.
15256 * @drqp: The queue structure array to use to create the data receive queues.
15257 * @cqp: The completion queue array to bind these receive queues to.
15258 *
15259 * This function creates a receive buffer queue pair , as detailed in @hrq and
15260 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15261 * to the HBA.
15262 *
15263 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15264 * struct is used to get the entry count that is necessary to determine the
15265 * number of pages to use for this queue. The @cq is used to indicate which
15266 * completion queue to bind received buffers that are posted to these queues to.
15267 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15268 * receive queue pair. This function is asynchronous and will wait for the
15269 * mailbox command to finish before continuing.
15270 *
15271 * On success this function will return a zero. If unable to allocate enough
15272 * memory this function will return -ENOMEM. If the queue create mailbox command
15273 * fails this function will return -ENXIO.
15274 **/
15275int
15276lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15277 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15278 uint32_t subtype)
15279{
15280 struct lpfc_queue *hrq, *drq, *cq;
15281 struct lpfc_mbx_rq_create_v2 *rq_create;
15282 struct lpfc_dmabuf *dmabuf;
15283 LPFC_MBOXQ_t *mbox;
15284 int rc, length, alloclen, status = 0;
15285 int cnt, idx, numrq, page_idx = 0;
15286 uint32_t shdr_status, shdr_add_status;
15287 union lpfc_sli4_cfg_shdr *shdr;
15288 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15289
15290 numrq = phba->cfg_nvmet_mrq;
15291 /* sanity check on array memory */
15292 if (!hrqp || !drqp || !cqp || !numrq)
15293 return -ENODEV;
15294 if (!phba->sli4_hba.pc_sli4_params.supported)
15295 hw_page_size = SLI4_PAGE_SIZE;
15296
15297 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15298 if (!mbox)
15299 return -ENOMEM;
15300
15301 length = sizeof(struct lpfc_mbx_rq_create_v2);
15302 length += ((2 * numrq * hrqp[0]->page_count) *
15303 sizeof(struct dma_address));
15304
15305 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15306 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15307 LPFC_SLI4_MBX_NEMBED);
15308 if (alloclen < length) {
15309 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15310 "3099 Allocated DMA memory size (%d) is "
15311 "less than the requested DMA memory size "
15312 "(%d)\n", alloclen, length);
15313 status = -ENOMEM;
15314 goto out;
15315 }
15316
15317
15318
15319 rq_create = mbox->sge_array->addr[0];
15320 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15321
15322 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15323 cnt = 0;
15324
15325 for (idx = 0; idx < numrq; idx++) {
15326 hrq = hrqp[idx];
15327 drq = drqp[idx];
15328 cq = cqp[idx];
15329
James Smart2d7dbc42017-02-12 13:52:35 -080015330 /* sanity check on queue memory */
15331 if (!hrq || !drq || !cq) {
15332 status = -ENODEV;
15333 goto out;
15334 }
15335
James Smart7aabe842017-03-04 09:30:22 -080015336 if (hrq->entry_count != drq->entry_count) {
15337 status = -EINVAL;
15338 goto out;
15339 }
15340
James Smart2d7dbc42017-02-12 13:52:35 -080015341 if (idx == 0) {
15342 bf_set(lpfc_mbx_rq_create_num_pages,
15343 &rq_create->u.request,
15344 hrq->page_count);
15345 bf_set(lpfc_mbx_rq_create_rq_cnt,
15346 &rq_create->u.request, (numrq * 2));
15347 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15348 1);
15349 bf_set(lpfc_rq_context_base_cq,
15350 &rq_create->u.request.context,
15351 cq->queue_id);
15352 bf_set(lpfc_rq_context_data_size,
15353 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015354 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015355 bf_set(lpfc_rq_context_hdr_size,
15356 &rq_create->u.request.context,
15357 LPFC_HDR_BUF_SIZE);
15358 bf_set(lpfc_rq_context_rqe_count_1,
15359 &rq_create->u.request.context,
15360 hrq->entry_count);
15361 bf_set(lpfc_rq_context_rqe_size,
15362 &rq_create->u.request.context,
15363 LPFC_RQE_SIZE_8);
15364 bf_set(lpfc_rq_context_page_size,
15365 &rq_create->u.request.context,
15366 (PAGE_SIZE/SLI4_PAGE_SIZE));
15367 }
15368 rc = 0;
15369 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15370 memset(dmabuf->virt, 0, hw_page_size);
15371 cnt = page_idx + dmabuf->buffer_tag;
15372 rq_create->u.request.page[cnt].addr_lo =
15373 putPaddrLow(dmabuf->phys);
15374 rq_create->u.request.page[cnt].addr_hi =
15375 putPaddrHigh(dmabuf->phys);
15376 rc++;
15377 }
15378 page_idx += rc;
15379
15380 rc = 0;
15381 list_for_each_entry(dmabuf, &drq->page_list, list) {
15382 memset(dmabuf->virt, 0, hw_page_size);
15383 cnt = page_idx + dmabuf->buffer_tag;
15384 rq_create->u.request.page[cnt].addr_lo =
15385 putPaddrLow(dmabuf->phys);
15386 rq_create->u.request.page[cnt].addr_hi =
15387 putPaddrHigh(dmabuf->phys);
15388 rc++;
15389 }
15390 page_idx += rc;
15391
15392 hrq->db_format = LPFC_DB_RING_FORMAT;
15393 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15394 hrq->type = LPFC_HRQ;
15395 hrq->assoc_qid = cq->queue_id;
15396 hrq->subtype = subtype;
15397 hrq->host_index = 0;
15398 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015399 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015400
15401 drq->db_format = LPFC_DB_RING_FORMAT;
15402 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15403 drq->type = LPFC_DRQ;
15404 drq->assoc_qid = cq->queue_id;
15405 drq->subtype = subtype;
15406 drq->host_index = 0;
15407 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015408 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015409
15410 list_add_tail(&hrq->list, &cq->child_list);
15411 list_add_tail(&drq->list, &cq->child_list);
15412 }
15413
15414 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15415 /* The IOCTL status is embedded in the mailbox subheader. */
15416 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15417 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15418 if (shdr_status || shdr_add_status || rc) {
15419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15420 "3120 RQ_CREATE mailbox failed with "
15421 "status x%x add_status x%x, mbx status x%x\n",
15422 shdr_status, shdr_add_status, rc);
15423 status = -ENXIO;
15424 goto out;
15425 }
15426 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15427 if (rc == 0xFFFF) {
15428 status = -ENXIO;
15429 goto out;
15430 }
15431
15432 /* Initialize all RQs with associated queue id */
15433 for (idx = 0; idx < numrq; idx++) {
15434 hrq = hrqp[idx];
15435 hrq->queue_id = rc + (2 * idx);
15436 drq = drqp[idx];
15437 drq->queue_id = rc + (2 * idx) + 1;
15438 }
15439
15440out:
15441 lpfc_sli4_mbox_cmd_free(phba, mbox);
15442 return status;
15443}
15444
15445/**
James Smart4f774512009-05-22 14:52:35 -040015446 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15447 * @eq: The queue structure associated with the queue to destroy.
15448 *
15449 * This function destroys a queue, as detailed in @eq by sending an mailbox
15450 * command, specific to the type of queue, to the HBA.
15451 *
15452 * The @eq struct is used to get the queue ID of the queue to destroy.
15453 *
15454 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015455 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015456 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015457int
James Smart4f774512009-05-22 14:52:35 -040015458lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15459{
15460 LPFC_MBOXQ_t *mbox;
15461 int rc, length, status = 0;
15462 uint32_t shdr_status, shdr_add_status;
15463 union lpfc_sli4_cfg_shdr *shdr;
15464
James Smart2e90f4b2011-12-13 13:22:37 -050015465 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015466 if (!eq)
15467 return -ENODEV;
15468 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15469 if (!mbox)
15470 return -ENOMEM;
15471 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15472 sizeof(struct lpfc_sli4_cfg_mhdr));
15473 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15474 LPFC_MBOX_OPCODE_EQ_DESTROY,
15475 length, LPFC_SLI4_MBX_EMBED);
15476 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15477 eq->queue_id);
15478 mbox->vport = eq->phba->pport;
15479 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15480
15481 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15482 /* The IOCTL status is embedded in the mailbox subheader. */
15483 shdr = (union lpfc_sli4_cfg_shdr *)
15484 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15485 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15486 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15487 if (shdr_status || shdr_add_status || rc) {
15488 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15489 "2505 EQ_DESTROY mailbox failed with "
15490 "status x%x add_status x%x, mbx status x%x\n",
15491 shdr_status, shdr_add_status, rc);
15492 status = -ENXIO;
15493 }
15494
15495 /* Remove eq from any list */
15496 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015497 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015498 return status;
15499}
15500
15501/**
15502 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15503 * @cq: The queue structure associated with the queue to destroy.
15504 *
15505 * This function destroys a queue, as detailed in @cq by sending an mailbox
15506 * command, specific to the type of queue, to the HBA.
15507 *
15508 * The @cq struct is used to get the queue ID of the queue to destroy.
15509 *
15510 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015511 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015512 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015513int
James Smart4f774512009-05-22 14:52:35 -040015514lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15515{
15516 LPFC_MBOXQ_t *mbox;
15517 int rc, length, status = 0;
15518 uint32_t shdr_status, shdr_add_status;
15519 union lpfc_sli4_cfg_shdr *shdr;
15520
James Smart2e90f4b2011-12-13 13:22:37 -050015521 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015522 if (!cq)
15523 return -ENODEV;
15524 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15525 if (!mbox)
15526 return -ENOMEM;
15527 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15528 sizeof(struct lpfc_sli4_cfg_mhdr));
15529 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15530 LPFC_MBOX_OPCODE_CQ_DESTROY,
15531 length, LPFC_SLI4_MBX_EMBED);
15532 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15533 cq->queue_id);
15534 mbox->vport = cq->phba->pport;
15535 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15536 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15537 /* The IOCTL status is embedded in the mailbox subheader. */
15538 shdr = (union lpfc_sli4_cfg_shdr *)
15539 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15540 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15541 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15542 if (shdr_status || shdr_add_status || rc) {
15543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15544 "2506 CQ_DESTROY mailbox failed with "
15545 "status x%x add_status x%x, mbx status x%x\n",
15546 shdr_status, shdr_add_status, rc);
15547 status = -ENXIO;
15548 }
15549 /* Remove cq from any list */
15550 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015551 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015552 return status;
15553}
15554
15555/**
James Smart04c68492009-05-22 14:52:52 -040015556 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15557 * @qm: The queue structure associated with the queue to destroy.
15558 *
15559 * This function destroys a queue, as detailed in @mq by sending an mailbox
15560 * command, specific to the type of queue, to the HBA.
15561 *
15562 * The @mq struct is used to get the queue ID of the queue to destroy.
15563 *
15564 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015565 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015566 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015567int
James Smart04c68492009-05-22 14:52:52 -040015568lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15569{
15570 LPFC_MBOXQ_t *mbox;
15571 int rc, length, status = 0;
15572 uint32_t shdr_status, shdr_add_status;
15573 union lpfc_sli4_cfg_shdr *shdr;
15574
James Smart2e90f4b2011-12-13 13:22:37 -050015575 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015576 if (!mq)
15577 return -ENODEV;
15578 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15579 if (!mbox)
15580 return -ENOMEM;
15581 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15582 sizeof(struct lpfc_sli4_cfg_mhdr));
15583 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15584 LPFC_MBOX_OPCODE_MQ_DESTROY,
15585 length, LPFC_SLI4_MBX_EMBED);
15586 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15587 mq->queue_id);
15588 mbox->vport = mq->phba->pport;
15589 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15590 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15591 /* The IOCTL status is embedded in the mailbox subheader. */
15592 shdr = (union lpfc_sli4_cfg_shdr *)
15593 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15594 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15595 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15596 if (shdr_status || shdr_add_status || rc) {
15597 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15598 "2507 MQ_DESTROY mailbox failed with "
15599 "status x%x add_status x%x, mbx status x%x\n",
15600 shdr_status, shdr_add_status, rc);
15601 status = -ENXIO;
15602 }
15603 /* Remove mq from any list */
15604 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015605 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015606 return status;
15607}
15608
15609/**
James Smart4f774512009-05-22 14:52:35 -040015610 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15611 * @wq: The queue structure associated with the queue to destroy.
15612 *
15613 * This function destroys a queue, as detailed in @wq by sending an mailbox
15614 * command, specific to the type of queue, to the HBA.
15615 *
15616 * The @wq struct is used to get the queue ID of the queue to destroy.
15617 *
15618 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015619 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015620 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015621int
James Smart4f774512009-05-22 14:52:35 -040015622lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15623{
15624 LPFC_MBOXQ_t *mbox;
15625 int rc, length, status = 0;
15626 uint32_t shdr_status, shdr_add_status;
15627 union lpfc_sli4_cfg_shdr *shdr;
15628
James Smart2e90f4b2011-12-13 13:22:37 -050015629 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015630 if (!wq)
15631 return -ENODEV;
15632 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15633 if (!mbox)
15634 return -ENOMEM;
15635 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15636 sizeof(struct lpfc_sli4_cfg_mhdr));
15637 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15638 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15639 length, LPFC_SLI4_MBX_EMBED);
15640 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15641 wq->queue_id);
15642 mbox->vport = wq->phba->pport;
15643 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15644 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15645 shdr = (union lpfc_sli4_cfg_shdr *)
15646 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15647 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15648 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15649 if (shdr_status || shdr_add_status || rc) {
15650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15651 "2508 WQ_DESTROY mailbox failed with "
15652 "status x%x add_status x%x, mbx status x%x\n",
15653 shdr_status, shdr_add_status, rc);
15654 status = -ENXIO;
15655 }
15656 /* Remove wq from any list */
15657 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070015658 kfree(wq->pring);
15659 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040015660 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015661 return status;
15662}
15663
15664/**
15665 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15666 * @rq: The queue structure associated with the queue to destroy.
15667 *
15668 * This function destroys a queue, as detailed in @rq by sending an mailbox
15669 * command, specific to the type of queue, to the HBA.
15670 *
15671 * The @rq struct is used to get the queue ID of the queue to destroy.
15672 *
15673 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015674 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015675 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015676int
James Smart4f774512009-05-22 14:52:35 -040015677lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15678 struct lpfc_queue *drq)
15679{
15680 LPFC_MBOXQ_t *mbox;
15681 int rc, length, status = 0;
15682 uint32_t shdr_status, shdr_add_status;
15683 union lpfc_sli4_cfg_shdr *shdr;
15684
James Smart2e90f4b2011-12-13 13:22:37 -050015685 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015686 if (!hrq || !drq)
15687 return -ENODEV;
15688 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15689 if (!mbox)
15690 return -ENOMEM;
15691 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050015692 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040015693 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15694 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15695 length, LPFC_SLI4_MBX_EMBED);
15696 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15697 hrq->queue_id);
15698 mbox->vport = hrq->phba->pport;
15699 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15700 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15701 /* The IOCTL status is embedded in the mailbox subheader. */
15702 shdr = (union lpfc_sli4_cfg_shdr *)
15703 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15704 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15705 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15706 if (shdr_status || shdr_add_status || rc) {
15707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15708 "2509 RQ_DESTROY mailbox failed with "
15709 "status x%x add_status x%x, mbx status x%x\n",
15710 shdr_status, shdr_add_status, rc);
15711 if (rc != MBX_TIMEOUT)
15712 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15713 return -ENXIO;
15714 }
15715 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15716 drq->queue_id);
15717 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15718 shdr = (union lpfc_sli4_cfg_shdr *)
15719 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15720 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15721 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15722 if (shdr_status || shdr_add_status || rc) {
15723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15724 "2510 RQ_DESTROY mailbox failed with "
15725 "status x%x add_status x%x, mbx status x%x\n",
15726 shdr_status, shdr_add_status, rc);
15727 status = -ENXIO;
15728 }
15729 list_del_init(&hrq->list);
15730 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040015731 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015732 return status;
15733}
15734
15735/**
15736 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15737 * @phba: The virtual port for which this call being executed.
15738 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15739 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15740 * @xritag: the xritag that ties this io to the SGL pages.
15741 *
15742 * This routine will post the sgl pages for the IO that has the xritag
15743 * that is in the iocbq structure. The xritag is assigned during iocbq
15744 * creation and persists for as long as the driver is loaded.
15745 * if the caller has fewer than 256 scatter gather segments to map then
15746 * pdma_phys_addr1 should be 0.
15747 * If the caller needs to map more than 256 scatter gather segment then
15748 * pdma_phys_addr1 should be a valid physical address.
15749 * physical address for SGLs must be 64 byte aligned.
15750 * If you are going to map 2 SGL's then the first one must have 256 entries
15751 * the second sgl can have between 1 and 256 entries.
15752 *
15753 * Return codes:
15754 * 0 - Success
15755 * -ENXIO, -ENOMEM - Failure
15756 **/
15757int
15758lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15759 dma_addr_t pdma_phys_addr0,
15760 dma_addr_t pdma_phys_addr1,
15761 uint16_t xritag)
15762{
15763 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15764 LPFC_MBOXQ_t *mbox;
15765 int rc;
15766 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040015767 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040015768 union lpfc_sli4_cfg_shdr *shdr;
15769
15770 if (xritag == NO_XRI) {
15771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15772 "0364 Invalid param:\n");
15773 return -EINVAL;
15774 }
15775
15776 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15777 if (!mbox)
15778 return -ENOMEM;
15779
15780 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15781 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15782 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050015783 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040015784
15785 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15786 &mbox->u.mqe.un.post_sgl_pages;
15787 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15788 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15789
15790 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15791 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15792 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15793 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15794
15795 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15796 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15797 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15798 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15799 if (!phba->sli4_hba.intr_enable)
15800 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040015801 else {
James Smarta183a152011-10-10 21:32:43 -040015802 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040015803 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15804 }
James Smart4f774512009-05-22 14:52:35 -040015805 /* The IOCTL status is embedded in the mailbox subheader. */
15806 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15807 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15808 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15809 if (rc != MBX_TIMEOUT)
15810 mempool_free(mbox, phba->mbox_mem_pool);
15811 if (shdr_status || shdr_add_status || rc) {
15812 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15813 "2511 POST_SGL mailbox failed with "
15814 "status x%x add_status x%x, mbx status x%x\n",
15815 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040015816 }
15817 return 0;
15818}
James Smart4f774512009-05-22 14:52:35 -040015819
15820/**
James Smart88a2cfb2011-07-22 18:36:33 -040015821 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040015822 * @phba: pointer to lpfc hba data structure.
15823 *
15824 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040015825 * HBA consistent with the SLI-4 interface spec. This routine
15826 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15827 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040015828 *
James Smart88a2cfb2011-07-22 18:36:33 -040015829 * Returns
15830 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15831 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15832 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015833static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015834lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15835{
15836 unsigned long xri;
15837
15838 /*
15839 * Fetch the next logical xri. Because this index is logical,
15840 * the driver starts at 0 each time.
15841 */
15842 spin_lock_irq(&phba->hbalock);
15843 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15844 phba->sli4_hba.max_cfg_param.max_xri, 0);
15845 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15846 spin_unlock_irq(&phba->hbalock);
15847 return NO_XRI;
15848 } else {
15849 set_bit(xri, phba->sli4_hba.xri_bmask);
15850 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015851 }
James Smart6d368e52011-05-24 11:44:12 -040015852 spin_unlock_irq(&phba->hbalock);
15853 return xri;
15854}
15855
15856/**
15857 * lpfc_sli4_free_xri - Release an xri for reuse.
15858 * @phba: pointer to lpfc hba data structure.
15859 *
15860 * This routine is invoked to release an xri to the pool of
15861 * available rpis maintained by the driver.
15862 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015863static void
James Smart6d368e52011-05-24 11:44:12 -040015864__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15865{
15866 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040015867 phba->sli4_hba.max_cfg_param.xri_used--;
15868 }
15869}
15870
15871/**
15872 * lpfc_sli4_free_xri - Release an xri for reuse.
15873 * @phba: pointer to lpfc hba data structure.
15874 *
15875 * This routine is invoked to release an xri to the pool of
15876 * available rpis maintained by the driver.
15877 **/
15878void
15879lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15880{
15881 spin_lock_irq(&phba->hbalock);
15882 __lpfc_sli4_free_xri(phba, xri);
15883 spin_unlock_irq(&phba->hbalock);
15884}
15885
15886/**
James Smart4f774512009-05-22 14:52:35 -040015887 * lpfc_sli4_next_xritag - Get an xritag for the io
15888 * @phba: Pointer to HBA context object.
15889 *
15890 * This function gets an xritag for the iocb. If there is no unused xritag
15891 * it will return 0xffff.
15892 * The function returns the allocated xritag if successful, else returns zero.
15893 * Zero is not a valid xritag.
15894 * The caller is not required to hold any lock.
15895 **/
15896uint16_t
15897lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15898{
James Smart6d368e52011-05-24 11:44:12 -040015899 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040015900
James Smart6d368e52011-05-24 11:44:12 -040015901 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040015902 if (xri_index == NO_XRI)
15903 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15904 "2004 Failed to allocate XRI.last XRITAG is %d"
15905 " Max XRI is %d, Used XRI is %d\n",
15906 xri_index,
15907 phba->sli4_hba.max_cfg_param.max_xri,
15908 phba->sli4_hba.max_cfg_param.xri_used);
15909 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040015910}
15911
15912/**
James Smart895427b2017-02-12 13:52:30 -080015913 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040015914 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040015915 * @post_sgl_list: pointer to els sgl entry list.
15916 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040015917 *
15918 * This routine is invoked to post a block of driver's sgl pages to the
15919 * HBA using non-embedded mailbox command. No Lock is held. This routine
15920 * is only called when the driver is loading and after all IO has been
15921 * stopped.
15922 **/
James Smart8a9d2e82012-05-09 21:16:12 -040015923static int
James Smart895427b2017-02-12 13:52:30 -080015924lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040015925 struct list_head *post_sgl_list,
15926 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040015927{
James Smart8a9d2e82012-05-09 21:16:12 -040015928 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040015929 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15930 struct sgl_page_pairs *sgl_pg_pairs;
15931 void *viraddr;
15932 LPFC_MBOXQ_t *mbox;
15933 uint32_t reqlen, alloclen, pg_pairs;
15934 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040015935 uint16_t xritag_start = 0;
15936 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040015937 uint32_t shdr_status, shdr_add_status;
15938 union lpfc_sli4_cfg_shdr *shdr;
15939
James Smart895427b2017-02-12 13:52:30 -080015940 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015941 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015942 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080015943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040015944 "2559 Block sgl registration required DMA "
15945 "size (%d) great than a page\n", reqlen);
15946 return -ENOMEM;
15947 }
James Smart895427b2017-02-12 13:52:30 -080015948
James Smart4f774512009-05-22 14:52:35 -040015949 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040015950 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040015951 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040015952
15953 /* Allocate DMA memory and set up the non-embedded mailbox command */
15954 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15955 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15956 LPFC_SLI4_MBX_NEMBED);
15957
15958 if (alloclen < reqlen) {
15959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15960 "0285 Allocated DMA memory size (%d) is "
15961 "less than the requested DMA memory "
15962 "size (%d)\n", alloclen, reqlen);
15963 lpfc_sli4_mbox_cmd_free(phba, mbox);
15964 return -ENOMEM;
15965 }
James Smart4f774512009-05-22 14:52:35 -040015966 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040015967 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040015968 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15969 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15970
James Smart8a9d2e82012-05-09 21:16:12 -040015971 pg_pairs = 0;
15972 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040015973 /* Set up the sge entry */
15974 sgl_pg_pairs->sgl_pg0_addr_lo =
15975 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15976 sgl_pg_pairs->sgl_pg0_addr_hi =
15977 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15978 sgl_pg_pairs->sgl_pg1_addr_lo =
15979 cpu_to_le32(putPaddrLow(0));
15980 sgl_pg_pairs->sgl_pg1_addr_hi =
15981 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040015982
James Smart4f774512009-05-22 14:52:35 -040015983 /* Keep the first xritag on the list */
15984 if (pg_pairs == 0)
15985 xritag_start = sglq_entry->sli4_xritag;
15986 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040015987 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040015988 }
James Smart6d368e52011-05-24 11:44:12 -040015989
15990 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040015991 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080015992 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040015993 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080015994
James Smart4f774512009-05-22 14:52:35 -040015995 if (!phba->sli4_hba.intr_enable)
15996 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15997 else {
James Smarta183a152011-10-10 21:32:43 -040015998 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040015999 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16000 }
16001 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16002 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16003 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16004 if (rc != MBX_TIMEOUT)
16005 lpfc_sli4_mbox_cmd_free(phba, mbox);
16006 if (shdr_status || shdr_add_status || rc) {
16007 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16008 "2513 POST_SGL_BLOCK mailbox command failed "
16009 "status x%x add_status x%x mbx status x%x\n",
16010 shdr_status, shdr_add_status, rc);
16011 rc = -ENXIO;
16012 }
16013 return rc;
16014}
16015
16016/**
16017 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16018 * @phba: pointer to lpfc hba data structure.
16019 * @sblist: pointer to scsi buffer list.
16020 * @count: number of scsi buffers on the list.
16021 *
16022 * This routine is invoked to post a block of @count scsi sgl pages from a
16023 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16024 * No Lock is held.
16025 *
16026 **/
16027int
James Smart8a9d2e82012-05-09 21:16:12 -040016028lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16029 struct list_head *sblist,
16030 int count)
James Smart4f774512009-05-22 14:52:35 -040016031{
16032 struct lpfc_scsi_buf *psb;
16033 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16034 struct sgl_page_pairs *sgl_pg_pairs;
16035 void *viraddr;
16036 LPFC_MBOXQ_t *mbox;
16037 uint32_t reqlen, alloclen, pg_pairs;
16038 uint32_t mbox_tmo;
16039 uint16_t xritag_start = 0;
16040 int rc = 0;
16041 uint32_t shdr_status, shdr_add_status;
16042 dma_addr_t pdma_phys_bpl1;
16043 union lpfc_sli4_cfg_shdr *shdr;
16044
16045 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016046 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016047 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016048 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016049 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16050 "0217 Block sgl registration required DMA "
16051 "size (%d) great than a page\n", reqlen);
16052 return -ENOMEM;
16053 }
16054 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16055 if (!mbox) {
16056 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16057 "0283 Failed to allocate mbox cmd memory\n");
16058 return -ENOMEM;
16059 }
16060
16061 /* Allocate DMA memory and set up the non-embedded mailbox command */
16062 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16063 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16064 LPFC_SLI4_MBX_NEMBED);
16065
16066 if (alloclen < reqlen) {
16067 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16068 "2561 Allocated DMA memory size (%d) is "
16069 "less than the requested DMA memory "
16070 "size (%d)\n", alloclen, reqlen);
16071 lpfc_sli4_mbox_cmd_free(phba, mbox);
16072 return -ENOMEM;
16073 }
James Smart6d368e52011-05-24 11:44:12 -040016074
James Smart4f774512009-05-22 14:52:35 -040016075 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016076 viraddr = mbox->sge_array->addr[0];
16077
16078 /* Set up the SGL pages in the non-embedded DMA pages */
16079 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16080 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16081
16082 pg_pairs = 0;
16083 list_for_each_entry(psb, sblist, list) {
16084 /* Set up the sge entry */
16085 sgl_pg_pairs->sgl_pg0_addr_lo =
16086 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16087 sgl_pg_pairs->sgl_pg0_addr_hi =
16088 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16089 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16090 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16091 else
16092 pdma_phys_bpl1 = 0;
16093 sgl_pg_pairs->sgl_pg1_addr_lo =
16094 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16095 sgl_pg_pairs->sgl_pg1_addr_hi =
16096 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16097 /* Keep the first xritag on the list */
16098 if (pg_pairs == 0)
16099 xritag_start = psb->cur_iocbq.sli4_xritag;
16100 sgl_pg_pairs++;
16101 pg_pairs++;
16102 }
16103 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16104 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16105 /* Perform endian conversion if necessary */
16106 sgl->word0 = cpu_to_le32(sgl->word0);
16107
16108 if (!phba->sli4_hba.intr_enable)
16109 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16110 else {
James Smarta183a152011-10-10 21:32:43 -040016111 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016112 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16113 }
16114 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16115 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16116 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16117 if (rc != MBX_TIMEOUT)
16118 lpfc_sli4_mbox_cmd_free(phba, mbox);
16119 if (shdr_status || shdr_add_status || rc) {
16120 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16121 "2564 POST_SGL_BLOCK mailbox command failed "
16122 "status x%x add_status x%x mbx status x%x\n",
16123 shdr_status, shdr_add_status, rc);
16124 rc = -ENXIO;
16125 }
16126 return rc;
16127}
16128
James Smart2ea259e2017-02-12 13:52:27 -080016129static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
16130static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
16131
James Smart4f774512009-05-22 14:52:35 -040016132/**
16133 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16134 * @phba: pointer to lpfc_hba struct that the frame was received on
16135 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16136 *
16137 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16138 * valid type of frame that the LPFC driver will handle. This function will
16139 * return a zero if the frame is a valid frame or a non zero value when the
16140 * frame does not pass the check.
16141 **/
16142static int
16143lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16144{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016145 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016146 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016147 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016148
James Smartae9e28f2017-05-15 15:20:51 -070016149#define FC_RCTL_MDS_DIAGS 0xF4
16150
James Smart4f774512009-05-22 14:52:35 -040016151 switch (fc_hdr->fh_r_ctl) {
16152 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16153 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16154 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16155 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16156 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16157 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16158 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16159 case FC_RCTL_DD_CMD_STATUS: /* command status */
16160 case FC_RCTL_ELS_REQ: /* extended link services request */
16161 case FC_RCTL_ELS_REP: /* extended link services reply */
16162 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16163 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16164 case FC_RCTL_BA_NOP: /* basic link service NOP */
16165 case FC_RCTL_BA_ABTS: /* basic link service abort */
16166 case FC_RCTL_BA_RMC: /* remove connection */
16167 case FC_RCTL_BA_ACC: /* basic accept */
16168 case FC_RCTL_BA_RJT: /* basic reject */
16169 case FC_RCTL_BA_PRMT:
16170 case FC_RCTL_ACK_1: /* acknowledge_1 */
16171 case FC_RCTL_ACK_0: /* acknowledge_0 */
16172 case FC_RCTL_P_RJT: /* port reject */
16173 case FC_RCTL_F_RJT: /* fabric reject */
16174 case FC_RCTL_P_BSY: /* port busy */
16175 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16176 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16177 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016178 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016179 case FC_RCTL_END: /* end */
16180 break;
16181 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16182 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16183 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16184 return lpfc_fc_frame_check(phba, fc_hdr);
16185 default:
16186 goto drop;
16187 }
James Smartae9e28f2017-05-15 15:20:51 -070016188
16189#define FC_TYPE_VENDOR_UNIQUE 0xFF
16190
James Smart4f774512009-05-22 14:52:35 -040016191 switch (fc_hdr->fh_type) {
16192 case FC_TYPE_BLS:
16193 case FC_TYPE_ELS:
16194 case FC_TYPE_FCP:
16195 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016196 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016197 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016198 break;
16199 case FC_TYPE_IP:
16200 case FC_TYPE_ILS:
16201 default:
16202 goto drop;
16203 }
James Smart546fc852011-03-11 16:06:29 -050016204
James Smart4f774512009-05-22 14:52:35 -040016205 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040016206 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
16207 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smartae9e28f2017-05-15 15:20:51 -070016208 (fc_hdr->fh_r_ctl == FC_RCTL_MDS_DIAGS) ? "MDS Diags" :
James Smart2ea259e2017-02-12 13:52:27 -080016209 lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
James Smartae9e28f2017-05-15 15:20:51 -070016210 (fc_hdr->fh_type == FC_TYPE_VENDOR_UNIQUE) ?
16211 "Vendor Unique" : lpfc_type_names[fc_hdr->fh_type],
16212 fc_hdr->fh_type, be32_to_cpu(header[0]),
16213 be32_to_cpu(header[1]), be32_to_cpu(header[2]),
16214 be32_to_cpu(header[3]), be32_to_cpu(header[4]),
16215 be32_to_cpu(header[5]), be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016216 return 0;
16217drop:
16218 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
16219 "2539 Dropped frame rctl:%s type:%s\n",
James Smart2ea259e2017-02-12 13:52:27 -080016220 lpfc_rctl_names[fc_hdr->fh_r_ctl],
16221 lpfc_type_names[fc_hdr->fh_type]);
James Smart4f774512009-05-22 14:52:35 -040016222 return 1;
16223}
16224
16225/**
16226 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16227 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16228 *
16229 * This function processes the FC header to retrieve the VFI from the VF
16230 * header, if one exists. This function will return the VFI if one exists
16231 * or 0 if no VSAN Header exists.
16232 **/
16233static uint32_t
16234lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16235{
16236 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16237
16238 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16239 return 0;
16240 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16241}
16242
16243/**
16244 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16245 * @phba: Pointer to the HBA structure to search for the vport on
16246 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16247 * @fcfi: The FC Fabric ID that the frame came from
16248 *
16249 * This function searches the @phba for a vport that matches the content of the
16250 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16251 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16252 * returns the matching vport pointer or NULL if unable to match frame to a
16253 * vport.
16254 **/
16255static struct lpfc_vport *
16256lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016257 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016258{
16259 struct lpfc_vport **vports;
16260 struct lpfc_vport *vport = NULL;
16261 int i;
James Smart939723a2012-05-09 21:19:03 -040016262
James Smartbf086112011-08-21 21:48:13 -040016263 if (did == Fabric_DID)
16264 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016265 if ((phba->pport->fc_flag & FC_PT2PT) &&
16266 !(phba->link_state == LPFC_HBA_READY))
16267 return phba->pport;
16268
James Smart4f774512009-05-22 14:52:35 -040016269 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016270 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016271 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16272 if (phba->fcf.fcfi == fcfi &&
16273 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16274 vports[i]->fc_myDID == did) {
16275 vport = vports[i];
16276 break;
16277 }
16278 }
James Smart895427b2017-02-12 13:52:30 -080016279 }
James Smart4f774512009-05-22 14:52:35 -040016280 lpfc_destroy_vport_work_array(phba, vports);
16281 return vport;
16282}
16283
16284/**
James Smart45ed1192009-10-02 15:17:02 -040016285 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16286 * @vport: The vport to work on.
16287 *
16288 * This function updates the receive sequence time stamp for this vport. The
16289 * receive sequence time stamp indicates the time that the last frame of the
16290 * the sequence that has been idle for the longest amount of time was received.
16291 * the driver uses this time stamp to indicate if any received sequences have
16292 * timed out.
16293 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016294static void
James Smart45ed1192009-10-02 15:17:02 -040016295lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16296{
16297 struct lpfc_dmabuf *h_buf;
16298 struct hbq_dmabuf *dmabuf = NULL;
16299
16300 /* get the oldest sequence on the rcv list */
16301 h_buf = list_get_first(&vport->rcv_buffer_list,
16302 struct lpfc_dmabuf, list);
16303 if (!h_buf)
16304 return;
16305 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16306 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16307}
16308
16309/**
16310 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16311 * @vport: The vport that the received sequences were sent to.
16312 *
16313 * This function cleans up all outstanding received sequences. This is called
16314 * by the driver when a link event or user action invalidates all the received
16315 * sequences.
16316 **/
16317void
16318lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16319{
16320 struct lpfc_dmabuf *h_buf, *hnext;
16321 struct lpfc_dmabuf *d_buf, *dnext;
16322 struct hbq_dmabuf *dmabuf = NULL;
16323
16324 /* start with the oldest sequence on the rcv list */
16325 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16326 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16327 list_del_init(&dmabuf->hbuf.list);
16328 list_for_each_entry_safe(d_buf, dnext,
16329 &dmabuf->dbuf.list, list) {
16330 list_del_init(&d_buf->list);
16331 lpfc_in_buf_free(vport->phba, d_buf);
16332 }
16333 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16334 }
16335}
16336
16337/**
16338 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16339 * @vport: The vport that the received sequences were sent to.
16340 *
16341 * This function determines whether any received sequences have timed out by
16342 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16343 * indicates that there is at least one timed out sequence this routine will
16344 * go through the received sequences one at a time from most inactive to most
16345 * active to determine which ones need to be cleaned up. Once it has determined
16346 * that a sequence needs to be cleaned up it will simply free up the resources
16347 * without sending an abort.
16348 **/
16349void
16350lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16351{
16352 struct lpfc_dmabuf *h_buf, *hnext;
16353 struct lpfc_dmabuf *d_buf, *dnext;
16354 struct hbq_dmabuf *dmabuf = NULL;
16355 unsigned long timeout;
16356 int abort_count = 0;
16357
16358 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16359 vport->rcv_buffer_time_stamp);
16360 if (list_empty(&vport->rcv_buffer_list) ||
16361 time_before(jiffies, timeout))
16362 return;
16363 /* start with the oldest sequence on the rcv list */
16364 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16365 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16366 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16367 dmabuf->time_stamp);
16368 if (time_before(jiffies, timeout))
16369 break;
16370 abort_count++;
16371 list_del_init(&dmabuf->hbuf.list);
16372 list_for_each_entry_safe(d_buf, dnext,
16373 &dmabuf->dbuf.list, list) {
16374 list_del_init(&d_buf->list);
16375 lpfc_in_buf_free(vport->phba, d_buf);
16376 }
16377 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16378 }
16379 if (abort_count)
16380 lpfc_update_rcv_time_stamp(vport);
16381}
16382
16383/**
James Smart4f774512009-05-22 14:52:35 -040016384 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16385 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16386 *
16387 * This function searches through the existing incomplete sequences that have
16388 * been sent to this @vport. If the frame matches one of the incomplete
16389 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16390 * make up that sequence. If no sequence is found that matches this frame then
16391 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16392 * This function returns a pointer to the first dmabuf in the sequence list that
16393 * the frame was linked to.
16394 **/
16395static struct hbq_dmabuf *
16396lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16397{
16398 struct fc_frame_header *new_hdr;
16399 struct fc_frame_header *temp_hdr;
16400 struct lpfc_dmabuf *d_buf;
16401 struct lpfc_dmabuf *h_buf;
16402 struct hbq_dmabuf *seq_dmabuf = NULL;
16403 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016404 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016405
James Smart4d9ab992009-10-02 15:16:39 -040016406 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016407 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016408 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016409
James Smart4f774512009-05-22 14:52:35 -040016410 /* Use the hdr_buf to find the sequence that this frame belongs to */
16411 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16412 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16413 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16414 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16415 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16416 continue;
16417 /* found a pending sequence that matches this frame */
16418 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16419 break;
16420 }
16421 if (!seq_dmabuf) {
16422 /*
16423 * This indicates first frame received for this sequence.
16424 * Queue the buffer on the vport's rcv_buffer_list.
16425 */
16426 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016427 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016428 return dmabuf;
16429 }
16430 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016431 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16432 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016433 list_del_init(&seq_dmabuf->hbuf.list);
16434 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16435 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016436 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016437 return dmabuf;
16438 }
James Smart45ed1192009-10-02 15:17:02 -040016439 /* move this sequence to the tail to indicate a young sequence */
16440 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16441 seq_dmabuf->time_stamp = jiffies;
16442 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016443 if (list_empty(&seq_dmabuf->dbuf.list)) {
16444 temp_hdr = dmabuf->hbuf.virt;
16445 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16446 return seq_dmabuf;
16447 }
James Smart4f774512009-05-22 14:52:35 -040016448 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016449 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16450 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016451 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16452 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16453 /*
16454 * If the frame's sequence count is greater than the frame on
16455 * the list then insert the frame right after this frame
16456 */
James Smarteeead812009-12-21 17:01:23 -050016457 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16458 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016459 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016460 found = 1;
16461 break;
James Smart4f774512009-05-22 14:52:35 -040016462 }
James Smart4360ca92015-12-16 18:12:04 -050016463
16464 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16465 break;
16466 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016467 }
James Smart4360ca92015-12-16 18:12:04 -050016468
16469 if (found)
16470 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016471 return NULL;
16472}
16473
16474/**
James Smart6669f9b2009-10-02 15:16:45 -040016475 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16476 * @vport: pointer to a vitural port
16477 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16478 *
16479 * This function tries to abort from the partially assembed sequence, described
16480 * by the information from basic abbort @dmabuf. It checks to see whether such
16481 * partially assembled sequence held by the driver. If so, it shall free up all
16482 * the frames from the partially assembled sequence.
16483 *
16484 * Return
16485 * true -- if there is matching partially assembled sequence present and all
16486 * the frames freed with the sequence;
16487 * false -- if there is no matching partially assembled sequence present so
16488 * nothing got aborted in the lower layer driver
16489 **/
16490static bool
16491lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16492 struct hbq_dmabuf *dmabuf)
16493{
16494 struct fc_frame_header *new_hdr;
16495 struct fc_frame_header *temp_hdr;
16496 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16497 struct hbq_dmabuf *seq_dmabuf = NULL;
16498
16499 /* Use the hdr_buf to find the sequence that matches this frame */
16500 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16501 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16502 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16503 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16504 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16505 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16506 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16507 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16508 continue;
16509 /* found a pending sequence that matches this frame */
16510 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16511 break;
16512 }
16513
16514 /* Free up all the frames from the partially assembled sequence */
16515 if (seq_dmabuf) {
16516 list_for_each_entry_safe(d_buf, n_buf,
16517 &seq_dmabuf->dbuf.list, list) {
16518 list_del_init(&d_buf->list);
16519 lpfc_in_buf_free(vport->phba, d_buf);
16520 }
16521 return true;
16522 }
16523 return false;
16524}
16525
16526/**
James Smart6dd9e312013-01-03 15:43:37 -050016527 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16528 * @vport: pointer to a vitural port
16529 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16530 *
16531 * This function tries to abort from the assembed sequence from upper level
16532 * protocol, described by the information from basic abbort @dmabuf. It
16533 * checks to see whether such pending context exists at upper level protocol.
16534 * If so, it shall clean up the pending context.
16535 *
16536 * Return
16537 * true -- if there is matching pending context of the sequence cleaned
16538 * at ulp;
16539 * false -- if there is no matching pending context of the sequence present
16540 * at ulp.
16541 **/
16542static bool
16543lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16544{
16545 struct lpfc_hba *phba = vport->phba;
16546 int handled;
16547
16548 /* Accepting abort at ulp with SLI4 only */
16549 if (phba->sli_rev < LPFC_SLI_REV4)
16550 return false;
16551
16552 /* Register all caring upper level protocols to attend abort */
16553 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16554 if (handled)
16555 return true;
16556
16557 return false;
16558}
16559
16560/**
James Smart546fc852011-03-11 16:06:29 -050016561 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016562 * @phba: Pointer to HBA context object.
16563 * @cmd_iocbq: pointer to the command iocbq structure.
16564 * @rsp_iocbq: pointer to the response iocbq structure.
16565 *
James Smart546fc852011-03-11 16:06:29 -050016566 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016567 * event. It properly releases the memory allocated to the sequence abort
16568 * accept iocb.
16569 **/
16570static void
James Smart546fc852011-03-11 16:06:29 -050016571lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016572 struct lpfc_iocbq *cmd_iocbq,
16573 struct lpfc_iocbq *rsp_iocbq)
16574{
James Smart6dd9e312013-01-03 15:43:37 -050016575 struct lpfc_nodelist *ndlp;
16576
16577 if (cmd_iocbq) {
16578 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16579 lpfc_nlp_put(ndlp);
16580 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016581 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016582 }
James Smart6b5151f2012-01-18 16:24:06 -050016583
16584 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16585 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16587 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16588 rsp_iocbq->iocb.ulpStatus,
16589 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016590}
16591
16592/**
James Smart6d368e52011-05-24 11:44:12 -040016593 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16594 * @phba: Pointer to HBA context object.
16595 * @xri: xri id in transaction.
16596 *
16597 * This function validates the xri maps to the known range of XRIs allocated an
16598 * used by the driver.
16599 **/
James Smart7851fe22011-07-22 18:36:52 -040016600uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016601lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16602 uint16_t xri)
16603{
James Smarta2fc4aef2014-09-03 12:57:55 -040016604 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016605
16606 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16607 if (xri == phba->sli4_hba.xri_ids[i])
16608 return i;
16609 }
16610 return NO_XRI;
16611}
16612
James Smart6d368e52011-05-24 11:44:12 -040016613/**
James Smart546fc852011-03-11 16:06:29 -050016614 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016615 * @phba: Pointer to HBA context object.
16616 * @fc_hdr: pointer to a FC frame header.
16617 *
James Smart546fc852011-03-11 16:06:29 -050016618 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016619 * event after aborting the sequence handling.
16620 **/
James Smart86c67372017-04-21 16:05:04 -070016621void
James Smart6dd9e312013-01-03 15:43:37 -050016622lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16623 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016624{
James Smart6dd9e312013-01-03 15:43:37 -050016625 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040016626 struct lpfc_iocbq *ctiocb = NULL;
16627 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040016628 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050016629 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040016630 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050016631 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040016632
16633 if (!lpfc_is_link_up(phba))
16634 return;
16635
16636 sid = sli4_sid_from_fc_hdr(fc_hdr);
16637 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050016638 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040016639
James Smart6dd9e312013-01-03 15:43:37 -050016640 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040016641 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070016642 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050016643 if (!ndlp) {
16644 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16645 "1268 Failed to allocate ndlp for "
16646 "oxid:x%x SID:x%x\n", oxid, sid);
16647 return;
16648 }
James Smart6dd9e312013-01-03 15:43:37 -050016649 /* Put ndlp onto pport node list */
16650 lpfc_enqueue_node(vport, ndlp);
16651 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16652 /* re-setup ndlp without removing from node list */
16653 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16654 if (!ndlp) {
16655 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16656 "3275 Failed to active ndlp found "
16657 "for oxid:x%x SID:x%x\n", oxid, sid);
16658 return;
16659 }
James Smart6669f9b2009-10-02 15:16:45 -040016660 }
16661
James Smart546fc852011-03-11 16:06:29 -050016662 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040016663 ctiocb = lpfc_sli_get_iocbq(phba);
16664 if (!ctiocb)
16665 return;
16666
James Smart5ffc2662009-11-18 15:39:44 -050016667 /* Extract the F_CTL field from FC_HDR */
16668 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16669
James Smart6669f9b2009-10-02 15:16:45 -040016670 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040016671 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050016672 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040016673 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16674 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16675 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16676
16677 /* Fill in the rest of iocb fields */
16678 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16679 icmd->ulpBdeCount = 0;
16680 icmd->ulpLe = 1;
16681 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040016682 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050016683 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016684
James Smart6669f9b2009-10-02 15:16:45 -040016685 ctiocb->iocb_cmpl = NULL;
16686 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050016687 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040016688 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050016689 ctiocb->sli4_xritag = NO_XRI;
16690
James Smartee0f4fe2012-05-09 21:19:14 -040016691 if (fctl & FC_FC_EX_CTX)
16692 /* Exchange responder sent the abort so we
16693 * own the oxid.
16694 */
16695 xri = oxid;
16696 else
16697 xri = rxid;
16698 lxri = lpfc_sli4_xri_inrange(phba, xri);
16699 if (lxri != NO_XRI)
16700 lpfc_set_rrq_active(phba, ndlp, lxri,
16701 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050016702 /* For BA_ABTS from exchange responder, if the logical xri with
16703 * the oxid maps to the FCP XRI range, the port no longer has
16704 * that exchange context, send a BLS_RJT. Override the IOCB for
16705 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050016706 */
James Smart6dd9e312013-01-03 15:43:37 -050016707 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080016708 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050016709 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16710 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16711 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16712 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16713 }
16714
16715 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16716 * the driver no longer has that exchange, send a BLS_RJT. Override
16717 * the IOCB for a BA_RJT.
16718 */
16719 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050016720 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16721 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16722 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16723 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16724 }
James Smart6669f9b2009-10-02 15:16:45 -040016725
James Smart5ffc2662009-11-18 15:39:44 -050016726 if (fctl & FC_FC_EX_CTX) {
16727 /* ABTS sent by responder to CT exchange, construction
16728 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16729 * field and RX_ID from ABTS for RX_ID field.
16730 */
James Smart546fc852011-03-11 16:06:29 -050016731 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050016732 } else {
16733 /* ABTS sent by initiator to CT exchange, construction
16734 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050016735 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050016736 */
James Smart546fc852011-03-11 16:06:29 -050016737 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050016738 }
James Smartf09c3ac2012-03-01 22:33:29 -050016739 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050016740 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050016741
James Smart546fc852011-03-11 16:06:29 -050016742 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050016743 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16744 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16745 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050016746
16747 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16748 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050016749 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16750 "2925 Failed to issue CT ABTS RSP x%x on "
16751 "xri x%x, Data x%x\n",
16752 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16753 phba->link_state);
16754 lpfc_nlp_put(ndlp);
16755 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050016756 lpfc_sli_release_iocbq(phba, ctiocb);
16757 }
James Smart6669f9b2009-10-02 15:16:45 -040016758}
16759
16760/**
16761 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16762 * @vport: Pointer to the vport on which this sequence was received
16763 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16764 *
16765 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16766 * receive sequence is only partially assembed by the driver, it shall abort
16767 * the partially assembled frames for the sequence. Otherwise, if the
16768 * unsolicited receive sequence has been completely assembled and passed to
16769 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16770 * unsolicited sequence has been aborted. After that, it will issue a basic
16771 * accept to accept the abort.
16772 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016773static void
James Smart6669f9b2009-10-02 15:16:45 -040016774lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16775 struct hbq_dmabuf *dmabuf)
16776{
16777 struct lpfc_hba *phba = vport->phba;
16778 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050016779 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050016780 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040016781
James Smart6669f9b2009-10-02 15:16:45 -040016782 /* Make a copy of fc_hdr before the dmabuf being released */
16783 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050016784 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040016785
James Smart5ffc2662009-11-18 15:39:44 -050016786 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050016787 /* ABTS by responder to exchange, no cleanup needed */
16788 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050016789 } else {
James Smart6dd9e312013-01-03 15:43:37 -050016790 /* ABTS by initiator to exchange, need to do cleanup */
16791 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16792 if (aborted == false)
16793 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050016794 }
James Smart6dd9e312013-01-03 15:43:37 -050016795 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16796
James Smart86c67372017-04-21 16:05:04 -070016797 if (phba->nvmet_support) {
16798 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16799 return;
16800 }
16801
James Smart6dd9e312013-01-03 15:43:37 -050016802 /* Respond with BA_ACC or BA_RJT accordingly */
16803 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040016804}
16805
16806/**
James Smart4f774512009-05-22 14:52:35 -040016807 * lpfc_seq_complete - Indicates if a sequence is complete
16808 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16809 *
16810 * This function checks the sequence, starting with the frame described by
16811 * @dmabuf, to see if all the frames associated with this sequence are present.
16812 * the frames associated with this sequence are linked to the @dmabuf using the
16813 * dbuf list. This function looks for two major things. 1) That the first frame
16814 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16815 * set. 3) That there are no holes in the sequence count. The function will
16816 * return 1 when the sequence is complete, otherwise it will return 0.
16817 **/
16818static int
16819lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16820{
16821 struct fc_frame_header *hdr;
16822 struct lpfc_dmabuf *d_buf;
16823 struct hbq_dmabuf *seq_dmabuf;
16824 uint32_t fctl;
16825 int seq_count = 0;
16826
16827 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16828 /* make sure first fame of sequence has a sequence count of zero */
16829 if (hdr->fh_seq_cnt != seq_count)
16830 return 0;
16831 fctl = (hdr->fh_f_ctl[0] << 16 |
16832 hdr->fh_f_ctl[1] << 8 |
16833 hdr->fh_f_ctl[2]);
16834 /* If last frame of sequence we can return success. */
16835 if (fctl & FC_FC_END_SEQ)
16836 return 1;
16837 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16838 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16839 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16840 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050016841 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040016842 return 0;
16843 fctl = (hdr->fh_f_ctl[0] << 16 |
16844 hdr->fh_f_ctl[1] << 8 |
16845 hdr->fh_f_ctl[2]);
16846 /* If last frame of sequence we can return success. */
16847 if (fctl & FC_FC_END_SEQ)
16848 return 1;
16849 }
16850 return 0;
16851}
16852
16853/**
16854 * lpfc_prep_seq - Prep sequence for ULP processing
16855 * @vport: Pointer to the vport on which this sequence was received
16856 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16857 *
16858 * This function takes a sequence, described by a list of frames, and creates
16859 * a list of iocbq structures to describe the sequence. This iocbq list will be
16860 * used to issue to the generic unsolicited sequence handler. This routine
16861 * returns a pointer to the first iocbq in the list. If the function is unable
16862 * to allocate an iocbq then it throw out the received frames that were not
16863 * able to be described and return a pointer to the first iocbq. If unable to
16864 * allocate any iocbqs (including the first) this function will return NULL.
16865 **/
16866static struct lpfc_iocbq *
16867lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16868{
James Smart7851fe22011-07-22 18:36:52 -040016869 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016870 struct lpfc_dmabuf *d_buf, *n_buf;
16871 struct lpfc_iocbq *first_iocbq, *iocbq;
16872 struct fc_frame_header *fc_hdr;
16873 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040016874 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050016875 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040016876
16877 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16878 /* remove from receive buffer list */
16879 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016880 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016881 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040016882 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040016883 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016884 /* Get an iocbq struct to fill in. */
16885 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16886 if (first_iocbq) {
16887 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040016888 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016889 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080016890 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040016891
16892 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16893 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16894 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16895 first_iocbq->iocb.un.rcvels.parmRo =
16896 sli4_did_from_fc_hdr(fc_hdr);
16897 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16898 } else
16899 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040016900 first_iocbq->iocb.ulpContext = NO_XRI;
16901 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16902 be16_to_cpu(fc_hdr->fh_ox_id);
16903 /* iocbq is prepped for internal consumption. Physical vpi. */
16904 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16905 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040016906 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040016907 tot_len = bf_get(lpfc_rcqe_length,
16908 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16909
James Smart4f774512009-05-22 14:52:35 -040016910 first_iocbq->context2 = &seq_dmabuf->dbuf;
16911 first_iocbq->context3 = NULL;
16912 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040016913 if (tot_len > LPFC_DATA_BUF_SIZE)
16914 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040016915 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040016916 else
16917 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16918
James Smart4f774512009-05-22 14:52:35 -040016919 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040016920
James Smart7851fe22011-07-22 18:36:52 -040016921 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040016922 }
16923 iocbq = first_iocbq;
16924 /*
16925 * Each IOCBq can have two Buffers assigned, so go through the list
16926 * of buffers for this sequence and save two buffers in each IOCBq
16927 */
16928 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16929 if (!iocbq) {
16930 lpfc_in_buf_free(vport->phba, d_buf);
16931 continue;
16932 }
16933 if (!iocbq->context3) {
16934 iocbq->context3 = d_buf;
16935 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040016936 /* We need to get the size out of the right CQE */
16937 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16938 len = bf_get(lpfc_rcqe_length,
16939 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016940 pbde = (struct ulp_bde64 *)
16941 &iocbq->iocb.unsli3.sli3Words[4];
16942 if (len > LPFC_DATA_BUF_SIZE)
16943 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16944 else
16945 pbde->tus.f.bdeSize = len;
16946
James Smart7851fe22011-07-22 18:36:52 -040016947 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16948 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040016949 } else {
16950 iocbq = lpfc_sli_get_iocbq(vport->phba);
16951 if (!iocbq) {
16952 if (first_iocbq) {
16953 first_iocbq->iocb.ulpStatus =
16954 IOSTAT_FCP_RSP_ERROR;
16955 first_iocbq->iocb.un.ulpWord[4] =
16956 IOERR_NO_RESOURCES;
16957 }
16958 lpfc_in_buf_free(vport->phba, d_buf);
16959 continue;
16960 }
James Smart7851fe22011-07-22 18:36:52 -040016961 /* We need to get the size out of the right CQE */
16962 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16963 len = bf_get(lpfc_rcqe_length,
16964 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016965 iocbq->context2 = d_buf;
16966 iocbq->context3 = NULL;
16967 iocbq->iocb.ulpBdeCount = 1;
16968 if (len > LPFC_DATA_BUF_SIZE)
16969 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16970 LPFC_DATA_BUF_SIZE;
16971 else
16972 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
16973
James Smart7851fe22011-07-22 18:36:52 -040016974 tot_len += len;
16975 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16976
James Smart4f774512009-05-22 14:52:35 -040016977 iocbq->iocb.un.rcvels.remoteID = sid;
16978 list_add_tail(&iocbq->list, &first_iocbq->list);
16979 }
16980 }
16981 return first_iocbq;
16982}
16983
James Smart6669f9b2009-10-02 15:16:45 -040016984static void
16985lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16986 struct hbq_dmabuf *seq_dmabuf)
16987{
16988 struct fc_frame_header *fc_hdr;
16989 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16990 struct lpfc_hba *phba = vport->phba;
16991
16992 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16993 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16994 if (!iocbq) {
16995 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16996 "2707 Ring %d handler: Failed to allocate "
16997 "iocb Rctl x%x Type x%x received\n",
16998 LPFC_ELS_RING,
16999 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17000 return;
17001 }
17002 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017003 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017004 iocbq, fc_hdr->fh_r_ctl,
17005 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017006 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017007 "2540 Ring %d handler: unexpected Rctl "
17008 "x%x Type x%x received\n",
17009 LPFC_ELS_RING,
17010 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17011
17012 /* Free iocb created in lpfc_prep_seq */
17013 list_for_each_entry_safe(curr_iocb, next_iocb,
17014 &iocbq->list, list) {
17015 list_del_init(&curr_iocb->list);
17016 lpfc_sli_release_iocbq(phba, curr_iocb);
17017 }
17018 lpfc_sli_release_iocbq(phba, iocbq);
17019}
17020
James Smartae9e28f2017-05-15 15:20:51 -070017021static void
17022lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17023 struct lpfc_iocbq *rspiocb)
17024{
17025 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17026
17027 if (pcmd && pcmd->virt)
17028 pci_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17029 kfree(pcmd);
17030 lpfc_sli_release_iocbq(phba, cmdiocb);
17031}
17032
17033static void
17034lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17035 struct hbq_dmabuf *dmabuf)
17036{
17037 struct fc_frame_header *fc_hdr;
17038 struct lpfc_hba *phba = vport->phba;
17039 struct lpfc_iocbq *iocbq = NULL;
17040 union lpfc_wqe *wqe;
17041 struct lpfc_dmabuf *pcmd = NULL;
17042 uint32_t frame_len;
17043 int rc;
17044
17045 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17046 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17047
17048 /* Send the received frame back */
17049 iocbq = lpfc_sli_get_iocbq(phba);
17050 if (!iocbq)
17051 goto exit;
17052
17053 /* Allocate buffer for command payload */
17054 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17055 if (pcmd)
17056 pcmd->virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
17057 &pcmd->phys);
17058 if (!pcmd || !pcmd->virt)
17059 goto exit;
17060
17061 INIT_LIST_HEAD(&pcmd->list);
17062
17063 /* copyin the payload */
17064 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17065
17066 /* fill in BDE's for command */
17067 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17068 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17069 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17070 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17071
17072 iocbq->context2 = pcmd;
17073 iocbq->vport = vport;
17074 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17075 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17076
17077 /*
17078 * Setup rest of the iocb as though it were a WQE
17079 * Build the SEND_FRAME WQE
17080 */
17081 wqe = (union lpfc_wqe *)&iocbq->iocb;
17082
17083 wqe->send_frame.frame_len = frame_len;
17084 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17085 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17086 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17087 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17088 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17089 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17090
17091 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17092 iocbq->iocb.ulpLe = 1;
17093 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17094 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17095 if (rc == IOCB_ERROR)
17096 goto exit;
17097
17098 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17099 return;
17100
17101exit:
17102 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17103 "2023 Unable to process MDS loopback frame\n");
17104 if (pcmd && pcmd->virt)
17105 pci_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17106 kfree(pcmd);
17107 lpfc_sli_release_iocbq(phba, iocbq);
17108 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17109}
17110
James Smart4f774512009-05-22 14:52:35 -040017111/**
17112 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17113 * @phba: Pointer to HBA context object.
17114 *
17115 * This function is called with no lock held. This function processes all
17116 * the received buffers and gives it to upper layers when a received buffer
17117 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017118 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017119 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17120 * appropriate receive function when the final frame in a sequence is received.
17121 **/
James Smart4d9ab992009-10-02 15:16:39 -040017122void
17123lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17124 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017125{
James Smart4d9ab992009-10-02 15:16:39 -040017126 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017127 struct fc_frame_header *fc_hdr;
17128 struct lpfc_vport *vport;
17129 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017130 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017131
James Smart4f774512009-05-22 14:52:35 -040017132 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017133 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017134
James Smart4d9ab992009-10-02 15:16:39 -040017135 /* check to see if this a valid type of frame */
17136 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17137 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17138 return;
17139 }
James Smart2ea259e2017-02-12 13:52:27 -080017140
James Smart7851fe22011-07-22 18:36:52 -040017141 if ((bf_get(lpfc_cqe_code,
17142 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17143 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17144 &dmabuf->cq_event.cqe.rcqe_cmpl);
17145 else
17146 fcfi = bf_get(lpfc_rcqe_fcf_id,
17147 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017148
James Smartae9e28f2017-05-15 15:20:51 -070017149 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17150 vport = phba->pport;
17151 /* Handle MDS Loopback frames */
17152 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17153 return;
17154 }
17155
James Smart895427b2017-02-12 13:52:30 -080017156 /* d_id this frame is directed to */
17157 did = sli4_did_from_fc_hdr(fc_hdr);
17158
17159 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017160 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017161 /* throw out the frame */
17162 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17163 return;
17164 }
James Smart939723a2012-05-09 21:19:03 -040017165
James Smart939723a2012-05-09 21:19:03 -040017166 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17167 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17168 (did != Fabric_DID)) {
17169 /*
17170 * Throw out the frame if we are not pt2pt.
17171 * The pt2pt protocol allows for discovery frames
17172 * to be received without a registered VPI.
17173 */
17174 if (!(vport->fc_flag & FC_PT2PT) ||
17175 (phba->link_state == LPFC_HBA_READY)) {
17176 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17177 return;
17178 }
17179 }
17180
James Smart6669f9b2009-10-02 15:16:45 -040017181 /* Handle the basic abort sequence (BA_ABTS) event */
17182 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17183 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17184 return;
17185 }
17186
James Smart4d9ab992009-10-02 15:16:39 -040017187 /* Link this frame */
17188 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17189 if (!seq_dmabuf) {
17190 /* unable to add frame to vport - throw it out */
17191 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17192 return;
17193 }
17194 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017195 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017196 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017197
James Smart6669f9b2009-10-02 15:16:45 -040017198 /* Send the complete sequence to the upper layer protocol */
17199 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017200}
James Smart6fb120a2009-05-22 14:52:59 -040017201
17202/**
17203 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17204 * @phba: pointer to lpfc hba data structure.
17205 *
17206 * This routine is invoked to post rpi header templates to the
17207 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017208 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17209 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017210 *
17211 * This routine does not require any locks. It's usage is expected
17212 * to be driver load or reset recovery when the driver is
17213 * sequential.
17214 *
17215 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017216 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017217 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017218 * When this error occurs, the driver is not guaranteed
17219 * to have any rpi regions posted to the device and
17220 * must either attempt to repost the regions or take a
17221 * fatal error.
17222 **/
17223int
17224lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17225{
17226 struct lpfc_rpi_hdr *rpi_page;
17227 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017228 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017229
James Smart6d368e52011-05-24 11:44:12 -040017230 /* SLI4 ports that support extents do not require RPI headers. */
17231 if (!phba->sli4_hba.rpi_hdrs_in_use)
17232 goto exit;
17233 if (phba->sli4_hba.extents_in_use)
17234 return -EIO;
17235
James Smart6fb120a2009-05-22 14:52:59 -040017236 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017237 /*
17238 * Assign the rpi headers a physical rpi only if the driver
17239 * has not initialized those resources. A port reset only
17240 * needs the headers posted.
17241 */
17242 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17243 LPFC_RPI_RSRC_RDY)
17244 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17245
James Smart6fb120a2009-05-22 14:52:59 -040017246 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17247 if (rc != MBX_SUCCESS) {
17248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17249 "2008 Error %d posting all rpi "
17250 "headers\n", rc);
17251 rc = -EIO;
17252 break;
17253 }
17254 }
17255
James Smart6d368e52011-05-24 11:44:12 -040017256 exit:
17257 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17258 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017259 return rc;
17260}
17261
17262/**
17263 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17264 * @phba: pointer to lpfc hba data structure.
17265 * @rpi_page: pointer to the rpi memory region.
17266 *
17267 * This routine is invoked to post a single rpi header to the
17268 * HBA consistent with the SLI-4 interface spec. This memory region
17269 * maps up to 64 rpi context regions.
17270 *
17271 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017272 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017273 * -ENOMEM - No available memory
17274 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017275 **/
17276int
17277lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17278{
17279 LPFC_MBOXQ_t *mboxq;
17280 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17281 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017282 uint32_t shdr_status, shdr_add_status;
17283 union lpfc_sli4_cfg_shdr *shdr;
17284
James Smart6d368e52011-05-24 11:44:12 -040017285 /* SLI4 ports that support extents do not require RPI headers. */
17286 if (!phba->sli4_hba.rpi_hdrs_in_use)
17287 return rc;
17288 if (phba->sli4_hba.extents_in_use)
17289 return -EIO;
17290
James Smart6fb120a2009-05-22 14:52:59 -040017291 /* The port is notified of the header region via a mailbox command. */
17292 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17293 if (!mboxq) {
17294 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17295 "2001 Unable to allocate memory for issuing "
17296 "SLI_CONFIG_SPECIAL mailbox command\n");
17297 return -ENOMEM;
17298 }
17299
17300 /* Post all rpi memory regions to the port. */
17301 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017302 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17303 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17304 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017305 sizeof(struct lpfc_sli4_cfg_mhdr),
17306 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017307
17308
17309 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017310 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17311 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017312 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17313 hdr_tmpl, rpi_page->page_count);
17314
James Smart6fb120a2009-05-22 14:52:59 -040017315 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17316 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017317 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017318 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17319 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17320 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17321 if (rc != MBX_TIMEOUT)
17322 mempool_free(mboxq, phba->mbox_mem_pool);
17323 if (shdr_status || shdr_add_status || rc) {
17324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17325 "2514 POST_RPI_HDR mailbox failed with "
17326 "status x%x add_status x%x, mbx status x%x\n",
17327 shdr_status, shdr_add_status, rc);
17328 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017329 } else {
17330 /*
17331 * The next_rpi stores the next logical module-64 rpi value used
17332 * to post physical rpis in subsequent rpi postings.
17333 */
17334 spin_lock_irq(&phba->hbalock);
17335 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17336 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017337 }
17338 return rc;
17339}
17340
17341/**
17342 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17343 * @phba: pointer to lpfc hba data structure.
17344 *
17345 * This routine is invoked to post rpi header templates to the
17346 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017347 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17348 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017349 *
17350 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017351 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017352 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17353 **/
17354int
17355lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17356{
James Smart6d368e52011-05-24 11:44:12 -040017357 unsigned long rpi;
17358 uint16_t max_rpi, rpi_limit;
17359 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017360 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017361 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017362
James Smart6fb120a2009-05-22 14:52:59 -040017363 /*
James Smart6d368e52011-05-24 11:44:12 -040017364 * Fetch the next logical rpi. Because this index is logical,
17365 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017366 */
James Smart4902b382013-10-10 12:20:35 -040017367 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017368 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17369 rpi_limit = phba->sli4_hba.next_rpi;
17370
James Smart6d368e52011-05-24 11:44:12 -040017371 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17372 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017373 rpi = LPFC_RPI_ALLOC_ERROR;
17374 else {
17375 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17376 phba->sli4_hba.max_cfg_param.rpi_used++;
17377 phba->sli4_hba.rpi_count++;
17378 }
James Smartbe6bb942015-04-07 15:07:22 -040017379 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17380 "0001 rpi:%x max:%x lim:%x\n",
17381 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017382
17383 /*
17384 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017385 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017386 */
17387 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17388 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017389 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017390 return rpi;
17391 }
17392
17393 /*
James Smart6d368e52011-05-24 11:44:12 -040017394 * RPI header postings are not required for SLI4 ports capable of
17395 * extents.
17396 */
17397 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017398 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017399 return rpi;
17400 }
17401
17402 /*
James Smart6fb120a2009-05-22 14:52:59 -040017403 * If the driver is running low on rpi resources, allocate another
17404 * page now. Note that the next_rpi value is used because
17405 * it represents how many are actually in use whereas max_rpi notes
17406 * how many are supported max by the device.
17407 */
James Smart6d368e52011-05-24 11:44:12 -040017408 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017409 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017410 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17411 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17412 if (!rpi_hdr) {
17413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17414 "2002 Error Could not grow rpi "
17415 "count\n");
17416 } else {
James Smart6d368e52011-05-24 11:44:12 -040017417 lrpi = rpi_hdr->start_rpi;
17418 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017419 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17420 }
17421 }
17422
17423 return rpi;
17424}
17425
17426/**
17427 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17428 * @phba: pointer to lpfc hba data structure.
17429 *
17430 * This routine is invoked to release an rpi to the pool of
17431 * available rpis maintained by the driver.
17432 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017433static void
James Smartd7c47992010-06-08 18:31:54 -040017434__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17435{
17436 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17437 phba->sli4_hba.rpi_count--;
17438 phba->sli4_hba.max_cfg_param.rpi_used--;
17439 }
17440}
17441
17442/**
17443 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17444 * @phba: pointer to lpfc hba data structure.
17445 *
17446 * This routine is invoked to release an rpi to the pool of
17447 * available rpis maintained by the driver.
17448 **/
17449void
James Smart6fb120a2009-05-22 14:52:59 -040017450lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17451{
17452 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017453 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017454 spin_unlock_irq(&phba->hbalock);
17455}
17456
17457/**
17458 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17459 * @phba: pointer to lpfc hba data structure.
17460 *
17461 * This routine is invoked to remove the memory region that
17462 * provided rpi via a bitmask.
17463 **/
17464void
17465lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17466{
17467 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017468 kfree(phba->sli4_hba.rpi_ids);
17469 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017470}
17471
17472/**
17473 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17474 * @phba: pointer to lpfc hba data structure.
17475 *
17476 * This routine is invoked to remove the memory region that
17477 * provided rpi via a bitmask.
17478 **/
17479int
James Smart6b5151f2012-01-18 16:24:06 -050017480lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17481 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017482{
17483 LPFC_MBOXQ_t *mboxq;
17484 struct lpfc_hba *phba = ndlp->phba;
17485 int rc;
17486
17487 /* The port is notified of the header region via a mailbox command. */
17488 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17489 if (!mboxq)
17490 return -ENOMEM;
17491
17492 /* Post all rpi memory regions to the port. */
17493 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017494 if (cmpl) {
17495 mboxq->mbox_cmpl = cmpl;
17496 mboxq->context1 = arg;
17497 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017498 } else
17499 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017500 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017501 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17502 if (rc == MBX_NOT_FINISHED) {
17503 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17504 "2010 Resume RPI Mailbox failed "
17505 "status %d, mbxStatus x%x\n", rc,
17506 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17507 mempool_free(mboxq, phba->mbox_mem_pool);
17508 return -EIO;
17509 }
17510 return 0;
17511}
17512
17513/**
17514 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017515 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017516 *
James Smart76a95d72010-11-20 23:11:48 -050017517 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017518 *
17519 * Returns:
17520 * 0 success
17521 * -Evalue otherwise
17522 **/
17523int
James Smart76a95d72010-11-20 23:11:48 -050017524lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017525{
17526 LPFC_MBOXQ_t *mboxq;
17527 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017528 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017529 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017530 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017531 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17532 if (!mboxq)
17533 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017534 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017535 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017536 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017537 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017538 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017539 "2022 INIT VPI Mailbox failed "
17540 "status %d, mbxStatus x%x\n", rc,
17541 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017542 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017543 }
James Smart6a9c52c2009-10-02 15:16:51 -040017544 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017545 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017546
17547 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017548}
17549
17550/**
17551 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17552 * @phba: pointer to lpfc hba data structure.
17553 * @mboxq: Pointer to mailbox object.
17554 *
17555 * This routine is invoked to manually add a single FCF record. The caller
17556 * must pass a completely initialized FCF_Record. This routine takes
17557 * care of the nonembedded mailbox operations.
17558 **/
17559static void
17560lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17561{
17562 void *virt_addr;
17563 union lpfc_sli4_cfg_shdr *shdr;
17564 uint32_t shdr_status, shdr_add_status;
17565
17566 virt_addr = mboxq->sge_array->addr[0];
17567 /* The IOCTL status is embedded in the mailbox subheader. */
17568 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17569 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17570 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17571
17572 if ((shdr_status || shdr_add_status) &&
17573 (shdr_status != STATUS_FCF_IN_USE))
17574 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17575 "2558 ADD_FCF_RECORD mailbox failed with "
17576 "status x%x add_status x%x\n",
17577 shdr_status, shdr_add_status);
17578
17579 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17580}
17581
17582/**
17583 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17584 * @phba: pointer to lpfc hba data structure.
17585 * @fcf_record: pointer to the initialized fcf record to add.
17586 *
17587 * This routine is invoked to manually add a single FCF record. The caller
17588 * must pass a completely initialized FCF_Record. This routine takes
17589 * care of the nonembedded mailbox operations.
17590 **/
17591int
17592lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17593{
17594 int rc = 0;
17595 LPFC_MBOXQ_t *mboxq;
17596 uint8_t *bytep;
17597 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017598 struct lpfc_mbx_sge sge;
17599 uint32_t alloc_len, req_len;
17600 uint32_t fcfindex;
17601
17602 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17603 if (!mboxq) {
17604 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17605 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17606 return -ENOMEM;
17607 }
17608
17609 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17610 sizeof(uint32_t);
17611
17612 /* Allocate DMA memory and set up the non-embedded mailbox command */
17613 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17614 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17615 req_len, LPFC_SLI4_MBX_NEMBED);
17616 if (alloc_len < req_len) {
17617 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17618 "2523 Allocated DMA memory size (x%x) is "
17619 "less than the requested DMA memory "
17620 "size (x%x)\n", alloc_len, req_len);
17621 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17622 return -ENOMEM;
17623 }
17624
17625 /*
17626 * Get the first SGE entry from the non-embedded DMA memory. This
17627 * routine only uses a single SGE.
17628 */
17629 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040017630 virt_addr = mboxq->sge_array->addr[0];
17631 /*
17632 * Configure the FCF record for FCFI 0. This is the driver's
17633 * hardcoded default and gets used in nonFIP mode.
17634 */
17635 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17636 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17637 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17638
17639 /*
17640 * Copy the fcf_index and the FCF Record Data. The data starts after
17641 * the FCoE header plus word10. The data copy needs to be endian
17642 * correct.
17643 */
17644 bytep += sizeof(uint32_t);
17645 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17646 mboxq->vport = phba->pport;
17647 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17648 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17649 if (rc == MBX_NOT_FINISHED) {
17650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17651 "2515 ADD_FCF_RECORD mailbox failed with "
17652 "status 0x%x\n", rc);
17653 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17654 rc = -EIO;
17655 } else
17656 rc = 0;
17657
17658 return rc;
17659}
17660
17661/**
17662 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17663 * @phba: pointer to lpfc hba data structure.
17664 * @fcf_record: pointer to the fcf record to write the default data.
17665 * @fcf_index: FCF table entry index.
17666 *
17667 * This routine is invoked to build the driver's default FCF record. The
17668 * values used are hardcoded. This routine handles memory initialization.
17669 *
17670 **/
17671void
17672lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17673 struct fcf_record *fcf_record,
17674 uint16_t fcf_index)
17675{
17676 memset(fcf_record, 0, sizeof(struct fcf_record));
17677 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17678 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17679 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17680 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17681 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17682 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17683 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17684 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17685 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17686 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17687 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17688 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17689 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040017690 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040017691 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17692 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17693 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17694 /* Set the VLAN bit map */
17695 if (phba->valid_vlan) {
17696 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17697 = 1 << (phba->vlan_id % 8);
17698 }
17699}
17700
17701/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017702 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040017703 * @phba: pointer to lpfc hba data structure.
17704 * @fcf_index: FCF table entry offset.
17705 *
James Smart0c9ab6f2010-02-26 14:15:57 -050017706 * This routine is invoked to scan the entire FCF table by reading FCF
17707 * record and processing it one at a time starting from the @fcf_index
17708 * for initial FCF discovery or fast FCF failover rediscovery.
17709 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017710 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017711 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040017712 **/
17713int
James Smart0c9ab6f2010-02-26 14:15:57 -050017714lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040017715{
17716 int rc = 0, error;
17717 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040017718
James Smart32b97932009-07-19 10:01:21 -040017719 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050017720 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040017721 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17722 if (!mboxq) {
17723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17724 "2000 Failed to allocate mbox for "
17725 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040017726 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050017727 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017728 }
James Smartecfd03c2010-02-12 14:41:27 -050017729 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050017730 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050017731 if (rc) {
17732 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050017733 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017734 }
James Smartecfd03c2010-02-12 14:41:27 -050017735 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040017736 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050017737 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040017738
17739 spin_lock_irq(&phba->hbalock);
17740 phba->hba_flag |= FCF_TS_INPROG;
17741 spin_unlock_irq(&phba->hbalock);
17742
James Smart6fb120a2009-05-22 14:52:59 -040017743 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050017744 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040017745 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050017746 else {
James Smart38b92ef2010-08-04 16:11:39 -040017747 /* Reset eligible FCF count for new scan */
17748 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040017749 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017750 error = 0;
James Smart32b97932009-07-19 10:01:21 -040017751 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017752fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040017753 if (error) {
17754 if (mboxq)
17755 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040017756 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040017757 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040017758 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040017759 spin_unlock_irq(&phba->hbalock);
17760 }
James Smart6fb120a2009-05-22 14:52:59 -040017761 return error;
17762}
James Smarta0c87cb2009-07-19 10:01:10 -040017763
17764/**
James Smarta93ff372010-10-22 11:06:08 -040017765 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050017766 * @phba: pointer to lpfc hba data structure.
17767 * @fcf_index: FCF table entry offset.
17768 *
17769 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040017770 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050017771 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017772 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017773 * otherwise.
17774 **/
17775int
17776lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17777{
17778 int rc = 0, error;
17779 LPFC_MBOXQ_t *mboxq;
17780
17781 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17782 if (!mboxq) {
17783 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17784 "2763 Failed to allocate mbox for "
17785 "READ_FCF cmd\n");
17786 error = -ENOMEM;
17787 goto fail_fcf_read;
17788 }
17789 /* Construct the read FCF record mailbox command */
17790 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17791 if (rc) {
17792 error = -EINVAL;
17793 goto fail_fcf_read;
17794 }
17795 /* Issue the mailbox command asynchronously */
17796 mboxq->vport = phba->pport;
17797 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17798 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17799 if (rc == MBX_NOT_FINISHED)
17800 error = -EIO;
17801 else
17802 error = 0;
17803
17804fail_fcf_read:
17805 if (error && mboxq)
17806 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17807 return error;
17808}
17809
17810/**
17811 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17812 * @phba: pointer to lpfc hba data structure.
17813 * @fcf_index: FCF table entry offset.
17814 *
17815 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040017816 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050017817 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017818 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017819 * otherwise.
17820 **/
17821int
17822lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17823{
17824 int rc = 0, error;
17825 LPFC_MBOXQ_t *mboxq;
17826
17827 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17828 if (!mboxq) {
17829 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17830 "2758 Failed to allocate mbox for "
17831 "READ_FCF cmd\n");
17832 error = -ENOMEM;
17833 goto fail_fcf_read;
17834 }
17835 /* Construct the read FCF record mailbox command */
17836 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17837 if (rc) {
17838 error = -EINVAL;
17839 goto fail_fcf_read;
17840 }
17841 /* Issue the mailbox command asynchronously */
17842 mboxq->vport = phba->pport;
17843 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17844 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17845 if (rc == MBX_NOT_FINISHED)
17846 error = -EIO;
17847 else
17848 error = 0;
17849
17850fail_fcf_read:
17851 if (error && mboxq)
17852 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17853 return error;
17854}
17855
17856/**
James Smartf5cb5302015-12-16 18:11:52 -050017857 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040017858 * phba pointer to the lpfc_hba struct for this port.
17859 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17860 * routine when the rr_bmask is empty. The FCF indecies are put into the
17861 * rr_bmask based on their priority level. Starting from the highest priority
17862 * to the lowest. The most likely FCF candidate will be in the highest
17863 * priority group. When this routine is called it searches the fcf_pri list for
17864 * next lowest priority group and repopulates the rr_bmask with only those
17865 * fcf_indexes.
17866 * returns:
17867 * 1=success 0=failure
17868 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017869static int
James Smart7d791df2011-07-22 18:37:52 -040017870lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17871{
17872 uint16_t next_fcf_pri;
17873 uint16_t last_index;
17874 struct lpfc_fcf_pri *fcf_pri;
17875 int rc;
17876 int ret = 0;
17877
17878 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17879 LPFC_SLI4_FCF_TBL_INDX_MAX);
17880 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17881 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050017882
17883 /* Verify the priority list has 2 or more entries */
17884 spin_lock_irq(&phba->hbalock);
17885 if (list_empty(&phba->fcf.fcf_pri_list) ||
17886 list_is_singular(&phba->fcf.fcf_pri_list)) {
17887 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040017888 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17889 "3061 Last IDX %d\n", last_index);
17890 return 0; /* Empty rr list */
17891 }
James Smart25626692013-03-01 16:36:54 -050017892 spin_unlock_irq(&phba->hbalock);
17893
James Smart7d791df2011-07-22 18:37:52 -040017894 next_fcf_pri = 0;
17895 /*
17896 * Clear the rr_bmask and set all of the bits that are at this
17897 * priority.
17898 */
17899 memset(phba->fcf.fcf_rr_bmask, 0,
17900 sizeof(*phba->fcf.fcf_rr_bmask));
17901 spin_lock_irq(&phba->hbalock);
17902 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17903 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
17904 continue;
17905 /*
17906 * the 1st priority that has not FLOGI failed
17907 * will be the highest.
17908 */
17909 if (!next_fcf_pri)
17910 next_fcf_pri = fcf_pri->fcf_rec.priority;
17911 spin_unlock_irq(&phba->hbalock);
17912 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17913 rc = lpfc_sli4_fcf_rr_index_set(phba,
17914 fcf_pri->fcf_rec.fcf_index);
17915 if (rc)
17916 return 0;
17917 }
17918 spin_lock_irq(&phba->hbalock);
17919 }
17920 /*
17921 * if next_fcf_pri was not set above and the list is not empty then
17922 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070017923 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040017924 */
17925 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
17926 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17927 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
17928 /*
17929 * the 1st priority that has not FLOGI failed
17930 * will be the highest.
17931 */
17932 if (!next_fcf_pri)
17933 next_fcf_pri = fcf_pri->fcf_rec.priority;
17934 spin_unlock_irq(&phba->hbalock);
17935 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17936 rc = lpfc_sli4_fcf_rr_index_set(phba,
17937 fcf_pri->fcf_rec.fcf_index);
17938 if (rc)
17939 return 0;
17940 }
17941 spin_lock_irq(&phba->hbalock);
17942 }
17943 } else
17944 ret = 1;
17945 spin_unlock_irq(&phba->hbalock);
17946
17947 return ret;
17948}
17949/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017950 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17951 * @phba: pointer to lpfc hba data structure.
17952 *
17953 * This routine is to get the next eligible FCF record index in a round
17954 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040017955 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050017956 * shall be returned, otherwise, the next eligible FCF record's index
17957 * shall be returned.
17958 **/
17959uint16_t
17960lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17961{
17962 uint16_t next_fcf_index;
17963
James Smart421c6622013-01-03 15:44:16 -050017964initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040017965 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050017966 next_fcf_index = phba->fcf.current_rec.fcf_indx;
17967
James Smart7d791df2011-07-22 18:37:52 -040017968next_priority:
James Smart421c6622013-01-03 15:44:16 -050017969 /* Determine the next fcf index to check */
17970 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050017971 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17972 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040017973 next_fcf_index);
17974
James Smart0c9ab6f2010-02-26 14:15:57 -050017975 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040017976 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17977 /*
17978 * If we have wrapped then we need to clear the bits that
17979 * have been tested so that we can detect when we should
17980 * change the priority level.
17981 */
James Smart0c9ab6f2010-02-26 14:15:57 -050017982 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17983 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040017984 }
17985
James Smart0c9ab6f2010-02-26 14:15:57 -050017986
James Smart3804dc82010-07-14 15:31:37 -040017987 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040017988 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17989 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17990 /*
17991 * If next fcf index is not found check if there are lower
17992 * Priority level fcf's in the fcf_priority list.
17993 * Set up the rr_bmask with all of the avaiable fcf bits
17994 * at that level and continue the selection process.
17995 */
17996 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050017997 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040017998 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17999 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018000 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18001 return LPFC_FCOE_FCF_NEXT_NONE;
18002 else {
18003 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18004 "3063 Only FCF available idx %d, flag %x\n",
18005 next_fcf_index,
18006 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18007 return next_fcf_index;
18008 }
James Smart3804dc82010-07-14 15:31:37 -040018009 }
18010
James Smart7d791df2011-07-22 18:37:52 -040018011 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18012 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018013 LPFC_FCF_FLOGI_FAILED) {
18014 if (list_is_singular(&phba->fcf.fcf_pri_list))
18015 return LPFC_FCOE_FCF_NEXT_NONE;
18016
James Smart7d791df2011-07-22 18:37:52 -040018017 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018018 }
James Smart7d791df2011-07-22 18:37:52 -040018019
James Smart3804dc82010-07-14 15:31:37 -040018020 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018021 "2845 Get next roundrobin failover FCF (x%x)\n",
18022 next_fcf_index);
18023
James Smart0c9ab6f2010-02-26 14:15:57 -050018024 return next_fcf_index;
18025}
18026
18027/**
18028 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18029 * @phba: pointer to lpfc hba data structure.
18030 *
18031 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018032 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018033 * does not go beyond the range of the driver allocated bmask dimension
18034 * before setting the bit.
18035 *
18036 * Returns 0 if the index bit successfully set, otherwise, it returns
18037 * -EINVAL.
18038 **/
18039int
18040lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18041{
18042 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18043 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018044 "2610 FCF (x%x) reached driver's book "
18045 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018046 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18047 return -EINVAL;
18048 }
18049 /* Set the eligible FCF record index bmask */
18050 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18051
James Smart3804dc82010-07-14 15:31:37 -040018052 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018053 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018054 "bmask\n", fcf_index);
18055
James Smart0c9ab6f2010-02-26 14:15:57 -050018056 return 0;
18057}
18058
18059/**
James Smart3804dc82010-07-14 15:31:37 -040018060 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018061 * @phba: pointer to lpfc hba data structure.
18062 *
18063 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018064 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018065 * does not go beyond the range of the driver allocated bmask dimension
18066 * before clearing the bit.
18067 **/
18068void
18069lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18070{
James Smart9a803a72013-09-06 12:17:56 -040018071 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018072 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18073 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018074 "2762 FCF (x%x) reached driver's book "
18075 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018076 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18077 return;
18078 }
18079 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018080 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018081 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18082 list) {
James Smart7d791df2011-07-22 18:37:52 -040018083 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18084 list_del_init(&fcf_pri->list);
18085 break;
18086 }
18087 }
18088 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018089 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018090
18091 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018092 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018093 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018094}
18095
18096/**
James Smartecfd03c2010-02-12 14:41:27 -050018097 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18098 * @phba: pointer to lpfc hba data structure.
18099 *
18100 * This routine is the completion routine for the rediscover FCF table mailbox
18101 * command. If the mailbox command returned failure, it will try to stop the
18102 * FCF rediscover wait timer.
18103 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018104static void
James Smartecfd03c2010-02-12 14:41:27 -050018105lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18106{
18107 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18108 uint32_t shdr_status, shdr_add_status;
18109
18110 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18111
18112 shdr_status = bf_get(lpfc_mbox_hdr_status,
18113 &redisc_fcf->header.cfg_shdr.response);
18114 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18115 &redisc_fcf->header.cfg_shdr.response);
18116 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018117 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018118 "2746 Requesting for FCF rediscovery failed "
18119 "status x%x add_status x%x\n",
18120 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018121 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018122 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018123 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018124 spin_unlock_irq(&phba->hbalock);
18125 /*
18126 * CVL event triggered FCF rediscover request failed,
18127 * last resort to re-try current registered FCF entry.
18128 */
18129 lpfc_retry_pport_discovery(phba);
18130 } else {
18131 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018132 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018133 spin_unlock_irq(&phba->hbalock);
18134 /*
18135 * DEAD FCF event triggered FCF rediscover request
18136 * failed, last resort to fail over as a link down
18137 * to FCF registration.
18138 */
18139 lpfc_sli4_fcf_dead_failthrough(phba);
18140 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018141 } else {
18142 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018143 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018144 /*
18145 * Start FCF rediscovery wait timer for pending FCF
18146 * before rescan FCF record table.
18147 */
18148 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018149 }
James Smartecfd03c2010-02-12 14:41:27 -050018150
18151 mempool_free(mbox, phba->mbox_mem_pool);
18152}
18153
18154/**
James Smart3804dc82010-07-14 15:31:37 -040018155 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018156 * @phba: pointer to lpfc hba data structure.
18157 *
18158 * This routine is invoked to request for rediscovery of the entire FCF table
18159 * by the port.
18160 **/
18161int
18162lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18163{
18164 LPFC_MBOXQ_t *mbox;
18165 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18166 int rc, length;
18167
James Smart0c9ab6f2010-02-26 14:15:57 -050018168 /* Cancel retry delay timers to all vports before FCF rediscover */
18169 lpfc_cancel_all_vport_retry_delay_timer(phba);
18170
James Smartecfd03c2010-02-12 14:41:27 -050018171 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18172 if (!mbox) {
18173 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18174 "2745 Failed to allocate mbox for "
18175 "requesting FCF rediscover.\n");
18176 return -ENOMEM;
18177 }
18178
18179 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18180 sizeof(struct lpfc_sli4_cfg_mhdr));
18181 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18182 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18183 length, LPFC_SLI4_MBX_EMBED);
18184
18185 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18186 /* Set count to 0 for invalidating the entire FCF database */
18187 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18188
18189 /* Issue the mailbox command asynchronously */
18190 mbox->vport = phba->pport;
18191 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18192 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18193
18194 if (rc == MBX_NOT_FINISHED) {
18195 mempool_free(mbox, phba->mbox_mem_pool);
18196 return -EIO;
18197 }
18198 return 0;
18199}
18200
18201/**
James Smartfc2b9892010-02-26 14:15:29 -050018202 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18203 * @phba: pointer to lpfc hba data structure.
18204 *
18205 * This function is the failover routine as a last resort to the FCF DEAD
18206 * event when driver failed to perform fast FCF failover.
18207 **/
18208void
18209lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18210{
18211 uint32_t link_state;
18212
18213 /*
18214 * Last resort as FCF DEAD event failover will treat this as
18215 * a link down, but save the link state because we don't want
18216 * it to be changed to Link Down unless it is already down.
18217 */
18218 link_state = phba->link_state;
18219 lpfc_linkdown(phba);
18220 phba->link_state = link_state;
18221
18222 /* Unregister FCF if no devices connected to it */
18223 lpfc_unregister_unused_fcf(phba);
18224}
18225
18226/**
James Smart026abb82011-12-13 13:20:45 -050018227 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018228 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018229 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018230 *
James Smart026abb82011-12-13 13:20:45 -050018231 * This function gets SLI3 port configure region 23 data through memory dump
18232 * mailbox command. When it successfully retrieves data, the size of the data
18233 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018234 **/
James Smart026abb82011-12-13 13:20:45 -050018235static uint32_t
18236lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018237{
18238 LPFC_MBOXQ_t *pmb = NULL;
18239 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018240 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018241 int rc;
18242
James Smart026abb82011-12-13 13:20:45 -050018243 if (!rgn23_data)
18244 return 0;
18245
James Smarta0c87cb2009-07-19 10:01:10 -040018246 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18247 if (!pmb) {
18248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018249 "2600 failed to allocate mailbox memory\n");
18250 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018251 }
18252 mb = &pmb->u.mb;
18253
James Smarta0c87cb2009-07-19 10:01:10 -040018254 do {
18255 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18256 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18257
18258 if (rc != MBX_SUCCESS) {
18259 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018260 "2601 failed to read config "
18261 "region 23, rc 0x%x Status 0x%x\n",
18262 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018263 mb->un.varDmp.word_cnt = 0;
18264 }
18265 /*
18266 * dump mem may return a zero when finished or we got a
18267 * mailbox error, either way we are done.
18268 */
18269 if (mb->un.varDmp.word_cnt == 0)
18270 break;
18271 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18272 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18273
18274 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018275 rgn23_data + offset,
18276 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018277 offset += mb->un.varDmp.word_cnt;
18278 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18279
James Smart026abb82011-12-13 13:20:45 -050018280 mempool_free(pmb, phba->mbox_mem_pool);
18281 return offset;
18282}
18283
18284/**
18285 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18286 * @phba: pointer to lpfc hba data structure.
18287 * @rgn23_data: pointer to configure region 23 data.
18288 *
18289 * This function gets SLI4 port configure region 23 data through memory dump
18290 * mailbox command. When it successfully retrieves data, the size of the data
18291 * will be returned, otherwise, 0 will be returned.
18292 **/
18293static uint32_t
18294lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18295{
18296 LPFC_MBOXQ_t *mboxq = NULL;
18297 struct lpfc_dmabuf *mp = NULL;
18298 struct lpfc_mqe *mqe;
18299 uint32_t data_length = 0;
18300 int rc;
18301
18302 if (!rgn23_data)
18303 return 0;
18304
18305 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18306 if (!mboxq) {
18307 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18308 "3105 failed to allocate mailbox memory\n");
18309 return 0;
18310 }
18311
18312 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18313 goto out;
18314 mqe = &mboxq->u.mqe;
18315 mp = (struct lpfc_dmabuf *) mboxq->context1;
18316 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18317 if (rc)
18318 goto out;
18319 data_length = mqe->un.mb_words[5];
18320 if (data_length == 0)
18321 goto out;
18322 if (data_length > DMP_RGN23_SIZE) {
18323 data_length = 0;
18324 goto out;
18325 }
18326 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18327out:
18328 mempool_free(mboxq, phba->mbox_mem_pool);
18329 if (mp) {
18330 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18331 kfree(mp);
18332 }
18333 return data_length;
18334}
18335
18336/**
18337 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18338 * @phba: pointer to lpfc hba data structure.
18339 *
18340 * This function read region 23 and parse TLV for port status to
18341 * decide if the user disaled the port. If the TLV indicates the
18342 * port is disabled, the hba_flag is set accordingly.
18343 **/
18344void
18345lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18346{
18347 uint8_t *rgn23_data = NULL;
18348 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18349 uint32_t offset = 0;
18350
18351 /* Get adapter Region 23 data */
18352 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18353 if (!rgn23_data)
18354 goto out;
18355
18356 if (phba->sli_rev < LPFC_SLI_REV4)
18357 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18358 else {
18359 if_type = bf_get(lpfc_sli_intf_if_type,
18360 &phba->sli4_hba.sli_intf);
18361 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18362 goto out;
18363 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18364 }
James Smarta0c87cb2009-07-19 10:01:10 -040018365
18366 if (!data_size)
18367 goto out;
18368
18369 /* Check the region signature first */
18370 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18372 "2619 Config region 23 has bad signature\n");
18373 goto out;
18374 }
18375 offset += 4;
18376
18377 /* Check the data structure version */
18378 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18379 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18380 "2620 Config region 23 has bad version\n");
18381 goto out;
18382 }
18383 offset += 4;
18384
18385 /* Parse TLV entries in the region */
18386 while (offset < data_size) {
18387 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18388 break;
18389 /*
18390 * If the TLV is not driver specific TLV or driver id is
18391 * not linux driver id, skip the record.
18392 */
18393 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18394 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18395 (rgn23_data[offset + 3] != 0)) {
18396 offset += rgn23_data[offset + 1] * 4 + 4;
18397 continue;
18398 }
18399
18400 /* Driver found a driver specific TLV in the config region */
18401 sub_tlv_len = rgn23_data[offset + 1] * 4;
18402 offset += 4;
18403 tlv_offset = 0;
18404
18405 /*
18406 * Search for configured port state sub-TLV.
18407 */
18408 while ((offset < data_size) &&
18409 (tlv_offset < sub_tlv_len)) {
18410 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18411 offset += 4;
18412 tlv_offset += 4;
18413 break;
18414 }
18415 if (rgn23_data[offset] != PORT_STE_TYPE) {
18416 offset += rgn23_data[offset + 1] * 4 + 4;
18417 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18418 continue;
18419 }
18420
18421 /* This HBA contains PORT_STE configured */
18422 if (!rgn23_data[offset + 2])
18423 phba->hba_flag |= LINK_DISABLED;
18424
18425 goto out;
18426 }
18427 }
James Smart026abb82011-12-13 13:20:45 -050018428
James Smarta0c87cb2009-07-19 10:01:10 -040018429out:
James Smarta0c87cb2009-07-19 10:01:10 -040018430 kfree(rgn23_data);
18431 return;
18432}
James Smart695a8142010-01-26 23:08:03 -050018433
18434/**
James Smart52d52442011-05-24 11:42:45 -040018435 * lpfc_wr_object - write an object to the firmware
18436 * @phba: HBA structure that indicates port to create a queue on.
18437 * @dmabuf_list: list of dmabufs to write to the port.
18438 * @size: the total byte value of the objects to write to the port.
18439 * @offset: the current offset to be used to start the transfer.
18440 *
18441 * This routine will create a wr_object mailbox command to send to the port.
18442 * the mailbox command will be constructed using the dma buffers described in
18443 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18444 * BDEs that the imbedded mailbox can support. The @offset variable will be
18445 * used to indicate the starting offset of the transfer and will also return
18446 * the offset after the write object mailbox has completed. @size is used to
18447 * determine the end of the object and whether the eof bit should be set.
18448 *
18449 * Return 0 is successful and offset will contain the the new offset to use
18450 * for the next write.
18451 * Return negative value for error cases.
18452 **/
18453int
18454lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18455 uint32_t size, uint32_t *offset)
18456{
18457 struct lpfc_mbx_wr_object *wr_object;
18458 LPFC_MBOXQ_t *mbox;
18459 int rc = 0, i = 0;
18460 uint32_t shdr_status, shdr_add_status;
18461 uint32_t mbox_tmo;
18462 union lpfc_sli4_cfg_shdr *shdr;
18463 struct lpfc_dmabuf *dmabuf;
18464 uint32_t written = 0;
18465
18466 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18467 if (!mbox)
18468 return -ENOMEM;
18469
18470 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18471 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18472 sizeof(struct lpfc_mbx_wr_object) -
18473 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18474
18475 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18476 wr_object->u.request.write_offset = *offset;
18477 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18478 wr_object->u.request.object_name[0] =
18479 cpu_to_le32(wr_object->u.request.object_name[0]);
18480 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18481 list_for_each_entry(dmabuf, dmabuf_list, list) {
18482 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18483 break;
18484 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18485 wr_object->u.request.bde[i].addrHigh =
18486 putPaddrHigh(dmabuf->phys);
18487 if (written + SLI4_PAGE_SIZE >= size) {
18488 wr_object->u.request.bde[i].tus.f.bdeSize =
18489 (size - written);
18490 written += (size - written);
18491 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18492 } else {
18493 wr_object->u.request.bde[i].tus.f.bdeSize =
18494 SLI4_PAGE_SIZE;
18495 written += SLI4_PAGE_SIZE;
18496 }
18497 i++;
18498 }
18499 wr_object->u.request.bde_count = i;
18500 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18501 if (!phba->sli4_hba.intr_enable)
18502 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18503 else {
James Smarta183a152011-10-10 21:32:43 -040018504 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018505 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18506 }
18507 /* The IOCTL status is embedded in the mailbox subheader. */
18508 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18509 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18510 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18511 if (rc != MBX_TIMEOUT)
18512 mempool_free(mbox, phba->mbox_mem_pool);
18513 if (shdr_status || shdr_add_status || rc) {
18514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18515 "3025 Write Object mailbox failed with "
18516 "status x%x add_status x%x, mbx status x%x\n",
18517 shdr_status, shdr_add_status, rc);
18518 rc = -ENXIO;
18519 } else
18520 *offset += wr_object->u.response.actual_write_length;
18521 return rc;
18522}
18523
18524/**
James Smart695a8142010-01-26 23:08:03 -050018525 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18526 * @vport: pointer to vport data structure.
18527 *
18528 * This function iterate through the mailboxq and clean up all REG_LOGIN
18529 * and REG_VPI mailbox commands associated with the vport. This function
18530 * is called when driver want to restart discovery of the vport due to
18531 * a Clear Virtual Link event.
18532 **/
18533void
18534lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18535{
18536 struct lpfc_hba *phba = vport->phba;
18537 LPFC_MBOXQ_t *mb, *nextmb;
18538 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018539 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018540 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018541 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018542 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018543 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018544
James Smartd439d282010-09-29 11:18:45 -040018545 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018546 spin_lock_irq(&phba->hbalock);
18547 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18548 if (mb->vport != vport)
18549 continue;
18550
18551 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18552 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18553 continue;
18554
James Smartd439d282010-09-29 11:18:45 -040018555 list_del(&mb->list);
18556 list_add_tail(&mb->list, &mbox_cmd_list);
18557 }
18558 /* Clean up active mailbox command with the vport */
18559 mb = phba->sli.mbox_active;
18560 if (mb && (mb->vport == vport)) {
18561 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18562 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18563 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18564 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18565 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18566 /* Put reference count for delayed processing */
18567 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18568 /* Unregister the RPI when mailbox complete */
18569 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18570 }
18571 }
James Smart63e801c2010-11-20 23:14:19 -050018572 /* Cleanup any mailbox completions which are not yet processed */
18573 do {
18574 restart_loop = 0;
18575 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18576 /*
18577 * If this mailox is already processed or it is
18578 * for another vport ignore it.
18579 */
18580 if ((mb->vport != vport) ||
18581 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18582 continue;
18583
18584 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18585 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18586 continue;
18587
18588 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18589 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18590 ndlp = (struct lpfc_nodelist *)mb->context2;
18591 /* Unregister the RPI when mailbox complete */
18592 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18593 restart_loop = 1;
18594 spin_unlock_irq(&phba->hbalock);
18595 spin_lock(shost->host_lock);
18596 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18597 spin_unlock(shost->host_lock);
18598 spin_lock_irq(&phba->hbalock);
18599 break;
18600 }
18601 }
18602 } while (restart_loop);
18603
James Smartd439d282010-09-29 11:18:45 -040018604 spin_unlock_irq(&phba->hbalock);
18605
18606 /* Release the cleaned-up mailbox commands */
18607 while (!list_empty(&mbox_cmd_list)) {
18608 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018609 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18610 mp = (struct lpfc_dmabuf *) (mb->context1);
18611 if (mp) {
18612 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18613 kfree(mp);
18614 }
James Smart78730cf2010-04-06 15:06:30 -040018615 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018616 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018617 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018618 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018619 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018620 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018621 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018622 }
James Smart695a8142010-01-26 23:08:03 -050018623 }
James Smart695a8142010-01-26 23:08:03 -050018624 mempool_free(mb, phba->mbox_mem_pool);
18625 }
James Smartd439d282010-09-29 11:18:45 -040018626
18627 /* Release the ndlp with the cleaned-up active mailbox command */
18628 if (act_mbx_ndlp) {
18629 spin_lock(shost->host_lock);
18630 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18631 spin_unlock(shost->host_lock);
18632 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050018633 }
James Smart695a8142010-01-26 23:08:03 -050018634}
18635
James Smart2a9bf3d2010-06-07 15:24:45 -040018636/**
18637 * lpfc_drain_txq - Drain the txq
18638 * @phba: Pointer to HBA context object.
18639 *
18640 * This function attempt to submit IOCBs on the txq
18641 * to the adapter. For SLI4 adapters, the txq contains
18642 * ELS IOCBs that have been deferred because the there
18643 * are no SGLs. This congestion can occur with large
18644 * vport counts during node discovery.
18645 **/
18646
18647uint32_t
18648lpfc_drain_txq(struct lpfc_hba *phba)
18649{
18650 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080018651 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090018652 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040018653 unsigned long iflags = 0;
18654 char *fail_msg = NULL;
18655 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080018656 union lpfc_wqe128 wqe128;
18657 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040018658 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040018659
James Smart895427b2017-02-12 13:52:30 -080018660 pring = lpfc_phba_elsring(phba);
18661
James Smart398d81c2013-05-31 17:04:19 -040018662 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050018663 list_for_each_entry(piocbq, &pring->txq, list) {
18664 txq_cnt++;
18665 }
18666
18667 if (txq_cnt > pring->txq_max)
18668 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018669
James Smart398d81c2013-05-31 17:04:19 -040018670 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018671
James Smart0e9bb8d2013-03-01 16:35:12 -050018672 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040018673 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018674
James Smart19ca7602010-11-20 23:11:55 -050018675 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040018676 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040018677 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040018678 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18679 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050018680 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040018681 break;
18682 }
James Smart895427b2017-02-12 13:52:30 -080018683 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040018684 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050018685 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040018686 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018687 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040018688 }
James Smart0e9bb8d2013-03-01 16:35:12 -050018689 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040018690
18691 /* The xri and iocb resources secured,
18692 * attempt to issue request
18693 */
James Smart6d368e52011-05-24 11:44:12 -040018694 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040018695 piocbq->sli4_xritag = sglq->sli4_xritag;
18696 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18697 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080018698 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018699 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080018700 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018701 fail_msg = " - Wq is full";
18702 else
18703 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18704
18705 if (fail_msg) {
18706 /* Failed means we can't issue and need to cancel */
18707 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18708 "2822 IOCB failed %s iotag 0x%x "
18709 "xri 0x%x\n",
18710 fail_msg,
18711 piocbq->iotag, piocbq->sli4_xritag);
18712 list_add_tail(&piocbq->list, &completions);
18713 }
James Smart398d81c2013-05-31 17:04:19 -040018714 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018715 }
18716
James Smart2a9bf3d2010-06-07 15:24:45 -040018717 /* Cancel all the IOCBs that cannot be issued */
18718 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18719 IOERR_SLI_ABORTED);
18720
James Smart0e9bb8d2013-03-01 16:35:12 -050018721 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018722}
James Smart895427b2017-02-12 13:52:30 -080018723
18724/**
18725 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18726 * @phba: Pointer to HBA context object.
18727 * @pwqe: Pointer to command WQE.
18728 * @sglq: Pointer to the scatter gather queue object.
18729 *
18730 * This routine converts the bpl or bde that is in the WQE
18731 * to a sgl list for the sli4 hardware. The physical address
18732 * of the bpl/bde is converted back to a virtual address.
18733 * If the WQE contains a BPL then the list of BDE's is
18734 * converted to sli4_sge's. If the WQE contains a single
18735 * BDE then it is converted to a single sli_sge.
18736 * The WQE is still in cpu endianness so the contents of
18737 * the bpl can be used without byte swapping.
18738 *
18739 * Returns valid XRI = Success, NO_XRI = Failure.
18740 */
18741static uint16_t
18742lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18743 struct lpfc_sglq *sglq)
18744{
18745 uint16_t xritag = NO_XRI;
18746 struct ulp_bde64 *bpl = NULL;
18747 struct ulp_bde64 bde;
18748 struct sli4_sge *sgl = NULL;
18749 struct lpfc_dmabuf *dmabuf;
18750 union lpfc_wqe *wqe;
18751 int numBdes = 0;
18752 int i = 0;
18753 uint32_t offset = 0; /* accumulated offset in the sg request list */
18754 int inbound = 0; /* number of sg reply entries inbound from firmware */
18755 uint32_t cmd;
18756
18757 if (!pwqeq || !sglq)
18758 return xritag;
18759
18760 sgl = (struct sli4_sge *)sglq->sgl;
18761 wqe = &pwqeq->wqe;
18762 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18763
18764 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18765 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18766 return sglq->sli4_xritag;
18767 numBdes = pwqeq->rsvd2;
18768 if (numBdes) {
18769 /* The addrHigh and addrLow fields within the WQE
18770 * have not been byteswapped yet so there is no
18771 * need to swap them back.
18772 */
18773 if (pwqeq->context3)
18774 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18775 else
18776 return xritag;
18777
18778 bpl = (struct ulp_bde64 *)dmabuf->virt;
18779 if (!bpl)
18780 return xritag;
18781
18782 for (i = 0; i < numBdes; i++) {
18783 /* Should already be byte swapped. */
18784 sgl->addr_hi = bpl->addrHigh;
18785 sgl->addr_lo = bpl->addrLow;
18786
18787 sgl->word2 = le32_to_cpu(sgl->word2);
18788 if ((i+1) == numBdes)
18789 bf_set(lpfc_sli4_sge_last, sgl, 1);
18790 else
18791 bf_set(lpfc_sli4_sge_last, sgl, 0);
18792 /* swap the size field back to the cpu so we
18793 * can assign it to the sgl.
18794 */
18795 bde.tus.w = le32_to_cpu(bpl->tus.w);
18796 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18797 /* The offsets in the sgl need to be accumulated
18798 * separately for the request and reply lists.
18799 * The request is always first, the reply follows.
18800 */
18801 switch (cmd) {
18802 case CMD_GEN_REQUEST64_WQE:
18803 /* add up the reply sg entries */
18804 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18805 inbound++;
18806 /* first inbound? reset the offset */
18807 if (inbound == 1)
18808 offset = 0;
18809 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18810 bf_set(lpfc_sli4_sge_type, sgl,
18811 LPFC_SGE_TYPE_DATA);
18812 offset += bde.tus.f.bdeSize;
18813 break;
18814 case CMD_FCP_TRSP64_WQE:
18815 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18816 bf_set(lpfc_sli4_sge_type, sgl,
18817 LPFC_SGE_TYPE_DATA);
18818 break;
18819 case CMD_FCP_TSEND64_WQE:
18820 case CMD_FCP_TRECEIVE64_WQE:
18821 bf_set(lpfc_sli4_sge_type, sgl,
18822 bpl->tus.f.bdeFlags);
18823 if (i < 3)
18824 offset = 0;
18825 else
18826 offset += bde.tus.f.bdeSize;
18827 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18828 break;
18829 }
18830 sgl->word2 = cpu_to_le32(sgl->word2);
18831 bpl++;
18832 sgl++;
18833 }
18834 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18835 /* The addrHigh and addrLow fields of the BDE have not
18836 * been byteswapped yet so they need to be swapped
18837 * before putting them in the sgl.
18838 */
18839 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18840 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18841 sgl->word2 = le32_to_cpu(sgl->word2);
18842 bf_set(lpfc_sli4_sge_last, sgl, 1);
18843 sgl->word2 = cpu_to_le32(sgl->word2);
18844 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18845 }
18846 return sglq->sli4_xritag;
18847}
18848
18849/**
18850 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18851 * @phba: Pointer to HBA context object.
18852 * @ring_number: Base sli ring number
18853 * @pwqe: Pointer to command WQE.
18854 **/
18855int
18856lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18857 struct lpfc_iocbq *pwqe)
18858{
18859 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080018860 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080018861 struct lpfc_queue *wq;
18862 struct lpfc_sglq *sglq;
18863 struct lpfc_sli_ring *pring;
18864 unsigned long iflags;
18865
18866 /* NVME_LS and NVME_LS ABTS requests. */
18867 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18868 pring = phba->sli4_hba.nvmels_wq->pring;
18869 spin_lock_irqsave(&pring->ring_lock, iflags);
18870 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18871 if (!sglq) {
18872 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18873 return WQE_BUSY;
18874 }
18875 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18876 pwqe->sli4_xritag = sglq->sli4_xritag;
18877 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18878 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18879 return WQE_ERROR;
18880 }
18881 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18882 pwqe->sli4_xritag);
18883 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
18884 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18885 return WQE_ERROR;
18886 }
18887 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18888 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18889 return 0;
18890 }
18891
18892 /* NVME_FCREQ and NVME_ABTS requests */
18893 if (pwqe->iocb_flag & LPFC_IO_NVME) {
18894 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18895 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18896
18897 spin_lock_irqsave(&pring->ring_lock, iflags);
18898 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18899 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18900 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18901 if (lpfc_sli4_wq_put(wq, wqe)) {
18902 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18903 return WQE_ERROR;
18904 }
18905 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18906 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18907 return 0;
18908 }
18909
James Smartf358dd02017-02-12 13:52:34 -080018910 /* NVMET requests */
18911 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
18912 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18913 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18914
18915 spin_lock_irqsave(&pring->ring_lock, iflags);
18916 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070018917 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080018918 if (pwqe->sli4_xritag == NO_XRI) {
18919 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18920 pwqe->sli4_xritag = sglq->sli4_xritag;
18921 }
18922 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18923 pwqe->sli4_xritag);
18924 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18925 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18926 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18927 if (lpfc_sli4_wq_put(wq, wqe)) {
18928 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18929 return WQE_ERROR;
18930 }
18931 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18932 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18933 return 0;
18934 }
James Smart895427b2017-02-12 13:52:30 -080018935 return WQE_ERROR;
18936}