blob: 040575adf9c6efa41c90845451933687ac302a9b [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) {
James Smart14041bd2017-06-01 21:07:01 -0700971 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -0500972 sglq = NULL;
973 break;
974 } else
975 continue;
976 }
977 sglq->ndlp = ndlp;
978 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400979 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500980 sglq->state = SGL_ALLOCATED;
981 }
James Smart895427b2017-02-12 13:52:30 -0800982 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400983 return sglq;
984}
985
986/**
James Smartf358dd02017-02-12 13:52:34 -0800987 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
988 * @phba: Pointer to HBA context object.
989 * @piocb: Pointer to the iocbq.
990 *
991 * This function is called with the sgl_list lock held. This function
992 * gets a new driver sglq object from the sglq list. If the
993 * list is not empty then it is successful, it returns pointer to the newly
994 * allocated sglq object else it returns NULL.
995 **/
996struct lpfc_sglq *
997__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
998{
999 struct list_head *lpfc_nvmet_sgl_list;
1000 struct lpfc_sglq *sglq = NULL;
1001
1002 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1003
1004 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1005
1006 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1007 if (!sglq)
1008 return NULL;
1009 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1010 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001011 return sglq;
1012}
1013
James Smarte59058c2008-08-24 21:49:00 -04001014/**
James Smart3621a712009-04-06 18:47:14 -04001015 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001016 * @phba: Pointer to HBA context object.
1017 *
1018 * This function is called with no lock held. This function
1019 * allocates a new driver iocb object from the iocb pool. If the
1020 * allocation is successful, it returns pointer to the newly
1021 * allocated iocb object else it returns NULL.
1022 **/
James Smart2e0fef82007-06-17 19:56:36 -05001023struct lpfc_iocbq *
1024lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001025{
James Smart2e0fef82007-06-17 19:56:36 -05001026 struct lpfc_iocbq * iocbq = NULL;
1027 unsigned long iflags;
1028
1029 spin_lock_irqsave(&phba->hbalock, iflags);
1030 iocbq = __lpfc_sli_get_iocbq(phba);
1031 spin_unlock_irqrestore(&phba->hbalock, iflags);
1032 return iocbq;
1033}
1034
James Smarte59058c2008-08-24 21:49:00 -04001035/**
James Smart4f774512009-05-22 14:52:35 -04001036 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1037 * @phba: Pointer to HBA context object.
1038 * @iocbq: Pointer to driver iocb object.
1039 *
1040 * This function is called with hbalock held to release driver
1041 * iocb object to the iocb pool. The iotag in the iocb object
1042 * does not change for each use of the iocb object. This function
1043 * clears all other fields of the iocb object when it is freed.
1044 * The sqlq structure that holds the xritag and phys and virtual
1045 * mappings for the scatter gather list is retrieved from the
1046 * active array of sglq. The get of the sglq pointer also clears
1047 * the entry in the array. If the status of the IO indiactes that
1048 * this IO was aborted then the sglq entry it put on the
1049 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1050 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001051 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001052 **/
1053static void
1054__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1055{
1056 struct lpfc_sglq *sglq;
1057 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001058 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001059 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001060
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001061 lockdep_assert_held(&phba->hbalock);
1062
James Smart4f774512009-05-22 14:52:35 -04001063 if (iocbq->sli4_xritag == NO_XRI)
1064 sglq = NULL;
1065 else
James Smart6d368e52011-05-24 11:44:12 -04001066 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1067
James Smart0e9bb8d2013-03-01 16:35:12 -05001068
James Smart4f774512009-05-22 14:52:35 -04001069 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001070 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1071 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1072 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001073 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001074 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001075 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001076 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1077 spin_unlock_irqrestore(
1078 &phba->sli4_hba.sgl_list_lock, iflag);
1079 goto out;
1080 }
1081
James Smart895427b2017-02-12 13:52:30 -08001082 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001083 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1084 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001085 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1086 iflag);
James Smart341af102010-01-26 23:07:37 -05001087 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001088 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001089 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001090 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001091 } else {
James Smart895427b2017-02-12 13:52:30 -08001092 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1093 iflag);
James Smart4f774512009-05-22 14:52:35 -04001094 sglq->state = SGL_FREED;
1095 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001096 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001097 &phba->sli4_hba.lpfc_els_sgl_list);
1098 spin_unlock_irqrestore(
1099 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001100
1101 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001102 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001103 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001104 }
James Smart4f774512009-05-22 14:52:35 -04001105 }
1106
James Smartf358dd02017-02-12 13:52:34 -08001107out:
James Smart4f774512009-05-22 14:52:35 -04001108 /*
1109 * Clean all volatile data fields, preserve iotag and node struct.
1110 */
1111 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001112 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001113 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001114 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1115 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001116 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1117}
1118
James Smart2a9bf3d2010-06-07 15:24:45 -04001119
James Smart4f774512009-05-22 14:52:35 -04001120/**
James Smart3772a992009-05-22 14:50:54 -04001121 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1122 * @phba: Pointer to HBA context object.
1123 * @iocbq: Pointer to driver iocb object.
1124 *
1125 * This function is called with hbalock held to release driver
1126 * iocb object to the iocb pool. The iotag in the iocb object
1127 * does not change for each use of the iocb object. This function
1128 * clears all other fields of the iocb object when it is freed.
1129 **/
1130static void
1131__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1132{
1133 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1134
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001135 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001136
1137 /*
James Smart3772a992009-05-22 14:50:54 -04001138 * Clean all volatile data fields, preserve iotag and node struct.
1139 */
1140 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1141 iocbq->sli4_xritag = NO_XRI;
1142 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1143}
1144
1145/**
James Smart3621a712009-04-06 18:47:14 -04001146 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001147 * @phba: Pointer to HBA context object.
1148 * @iocbq: Pointer to driver iocb object.
1149 *
1150 * This function is called with hbalock held to release driver
1151 * iocb object to the iocb pool. The iotag in the iocb object
1152 * does not change for each use of the iocb object. This function
1153 * clears all other fields of the iocb object when it is freed.
1154 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001155static void
James Smart2e0fef82007-06-17 19:56:36 -05001156__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1157{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001158 lockdep_assert_held(&phba->hbalock);
1159
James Smart3772a992009-05-22 14:50:54 -04001160 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001161 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001162}
1163
James Smarte59058c2008-08-24 21:49:00 -04001164/**
James Smart3621a712009-04-06 18:47:14 -04001165 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001166 * @phba: Pointer to HBA context object.
1167 * @iocbq: Pointer to driver iocb object.
1168 *
1169 * This function is called with no lock held to release the iocb to
1170 * iocb pool.
1171 **/
James Smart2e0fef82007-06-17 19:56:36 -05001172void
1173lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1174{
1175 unsigned long iflags;
1176
1177 /*
1178 * Clean all volatile data fields, preserve iotag and node struct.
1179 */
1180 spin_lock_irqsave(&phba->hbalock, iflags);
1181 __lpfc_sli_release_iocbq(phba, iocbq);
1182 spin_unlock_irqrestore(&phba->hbalock, iflags);
1183}
1184
James Smarte59058c2008-08-24 21:49:00 -04001185/**
James Smarta257bf92009-04-06 18:48:10 -04001186 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1187 * @phba: Pointer to HBA context object.
1188 * @iocblist: List of IOCBs.
1189 * @ulpstatus: ULP status in IOCB command field.
1190 * @ulpWord4: ULP word-4 in IOCB command field.
1191 *
1192 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1193 * on the list by invoking the complete callback function associated with the
1194 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1195 * fields.
1196 **/
1197void
1198lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1199 uint32_t ulpstatus, uint32_t ulpWord4)
1200{
1201 struct lpfc_iocbq *piocb;
1202
1203 while (!list_empty(iocblist)) {
1204 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001205 if (!piocb->iocb_cmpl)
1206 lpfc_sli_release_iocbq(phba, piocb);
1207 else {
1208 piocb->iocb.ulpStatus = ulpstatus;
1209 piocb->iocb.un.ulpWord[4] = ulpWord4;
1210 (piocb->iocb_cmpl) (phba, piocb, piocb);
1211 }
1212 }
1213 return;
1214}
1215
1216/**
James Smart3621a712009-04-06 18:47:14 -04001217 * lpfc_sli_iocb_cmd_type - Get the iocb type
1218 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001219 *
1220 * This function is called by ring event handler function to get the iocb type.
1221 * This function translates the iocb command to an iocb command type used to
1222 * decide the final disposition of each completed IOCB.
1223 * The function returns
1224 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1225 * LPFC_SOL_IOCB if it is a solicited iocb completion
1226 * LPFC_ABORT_IOCB if it is an abort iocb
1227 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1228 *
1229 * The caller is not required to hold any lock.
1230 **/
dea31012005-04-17 16:05:31 -05001231static lpfc_iocb_type
1232lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1233{
1234 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1235
1236 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1237 return 0;
1238
1239 switch (iocb_cmnd) {
1240 case CMD_XMIT_SEQUENCE_CR:
1241 case CMD_XMIT_SEQUENCE_CX:
1242 case CMD_XMIT_BCAST_CN:
1243 case CMD_XMIT_BCAST_CX:
1244 case CMD_ELS_REQUEST_CR:
1245 case CMD_ELS_REQUEST_CX:
1246 case CMD_CREATE_XRI_CR:
1247 case CMD_CREATE_XRI_CX:
1248 case CMD_GET_RPI_CN:
1249 case CMD_XMIT_ELS_RSP_CX:
1250 case CMD_GET_RPI_CR:
1251 case CMD_FCP_IWRITE_CR:
1252 case CMD_FCP_IWRITE_CX:
1253 case CMD_FCP_IREAD_CR:
1254 case CMD_FCP_IREAD_CX:
1255 case CMD_FCP_ICMND_CR:
1256 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001257 case CMD_FCP_TSEND_CX:
1258 case CMD_FCP_TRSP_CX:
1259 case CMD_FCP_TRECEIVE_CX:
1260 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001261 case CMD_ADAPTER_MSG:
1262 case CMD_ADAPTER_DUMP:
1263 case CMD_XMIT_SEQUENCE64_CR:
1264 case CMD_XMIT_SEQUENCE64_CX:
1265 case CMD_XMIT_BCAST64_CN:
1266 case CMD_XMIT_BCAST64_CX:
1267 case CMD_ELS_REQUEST64_CR:
1268 case CMD_ELS_REQUEST64_CX:
1269 case CMD_FCP_IWRITE64_CR:
1270 case CMD_FCP_IWRITE64_CX:
1271 case CMD_FCP_IREAD64_CR:
1272 case CMD_FCP_IREAD64_CX:
1273 case CMD_FCP_ICMND64_CR:
1274 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001275 case CMD_FCP_TSEND64_CX:
1276 case CMD_FCP_TRSP64_CX:
1277 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001278 case CMD_GEN_REQUEST64_CR:
1279 case CMD_GEN_REQUEST64_CX:
1280 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001281 case DSSCMD_IWRITE64_CR:
1282 case DSSCMD_IWRITE64_CX:
1283 case DSSCMD_IREAD64_CR:
1284 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001285 type = LPFC_SOL_IOCB;
1286 break;
1287 case CMD_ABORT_XRI_CN:
1288 case CMD_ABORT_XRI_CX:
1289 case CMD_CLOSE_XRI_CN:
1290 case CMD_CLOSE_XRI_CX:
1291 case CMD_XRI_ABORTED_CX:
1292 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001293 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001294 type = LPFC_ABORT_IOCB;
1295 break;
1296 case CMD_RCV_SEQUENCE_CX:
1297 case CMD_RCV_ELS_REQ_CX:
1298 case CMD_RCV_SEQUENCE64_CX:
1299 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001300 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001301 case CMD_IOCB_RCV_SEQ64_CX:
1302 case CMD_IOCB_RCV_ELS64_CX:
1303 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001304 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001305 type = LPFC_UNSOL_IOCB;
1306 break;
James Smart3163f722008-02-08 18:50:25 -05001307 case CMD_IOCB_XMIT_MSEQ64_CR:
1308 case CMD_IOCB_XMIT_MSEQ64_CX:
1309 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1310 case CMD_IOCB_RCV_ELS_LIST64_CX:
1311 case CMD_IOCB_CLOSE_EXTENDED_CN:
1312 case CMD_IOCB_ABORT_EXTENDED_CN:
1313 case CMD_IOCB_RET_HBQE64_CN:
1314 case CMD_IOCB_FCP_IBIDIR64_CR:
1315 case CMD_IOCB_FCP_IBIDIR64_CX:
1316 case CMD_IOCB_FCP_ITASKMGT64_CX:
1317 case CMD_IOCB_LOGENTRY_CN:
1318 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1319 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001320 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001321 type = LPFC_UNKNOWN_IOCB;
1322 break;
dea31012005-04-17 16:05:31 -05001323 default:
1324 type = LPFC_UNKNOWN_IOCB;
1325 break;
1326 }
1327
1328 return type;
1329}
1330
James Smarte59058c2008-08-24 21:49:00 -04001331/**
James Smart3621a712009-04-06 18:47:14 -04001332 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001333 * @phba: Pointer to HBA context object.
1334 *
1335 * This function is called from SLI initialization code
1336 * to configure every ring of the HBA's SLI interface. The
1337 * caller is not required to hold any lock. This function issues
1338 * a config_ring mailbox command for each ring.
1339 * This function returns zero if successful else returns a negative
1340 * error code.
1341 **/
dea31012005-04-17 16:05:31 -05001342static int
James Smarted957682007-06-17 19:56:37 -05001343lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001344{
1345 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001346 LPFC_MBOXQ_t *pmb;
1347 MAILBOX_t *pmbox;
1348 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001349
James Smarted957682007-06-17 19:56:37 -05001350 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1351 if (!pmb)
1352 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001353 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001354 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001355 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001356 lpfc_config_ring(phba, i, pmb);
1357 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1358 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001360 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001361 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1362 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001363 rc, pmbox->mbxCommand,
1364 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001365 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001366 ret = -ENXIO;
1367 break;
dea31012005-04-17 16:05:31 -05001368 }
1369 }
James Smarted957682007-06-17 19:56:37 -05001370 mempool_free(pmb, phba->mbox_mem_pool);
1371 return ret;
dea31012005-04-17 16:05:31 -05001372}
1373
James Smarte59058c2008-08-24 21:49:00 -04001374/**
James Smart3621a712009-04-06 18:47:14 -04001375 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001376 * @phba: Pointer to HBA context object.
1377 * @pring: Pointer to driver SLI ring object.
1378 * @piocb: Pointer to the driver iocb object.
1379 *
1380 * This function is called with hbalock held. The function adds the
1381 * new iocb to txcmplq of the given ring. This function always returns
1382 * 0. If this function is called for ELS ring, this function checks if
1383 * there is a vport associated with the ELS command. This function also
1384 * starts els_tmofunc timer if this is an ELS command.
1385 **/
dea31012005-04-17 16:05:31 -05001386static int
James Smart2e0fef82007-06-17 19:56:36 -05001387lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1388 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001389{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001390 lockdep_assert_held(&phba->hbalock);
1391
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001392 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001393
dea31012005-04-17 16:05:31 -05001394 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001395 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001396
James Smart92d7f7b2007-06-17 19:56:38 -05001397 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1398 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001399 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1400 BUG_ON(!piocb->vport);
1401 if (!(piocb->vport->load_flag & FC_UNLOADING))
1402 mod_timer(&piocb->vport->els_tmofunc,
1403 jiffies +
1404 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1405 }
dea31012005-04-17 16:05:31 -05001406
James Smart2e0fef82007-06-17 19:56:36 -05001407 return 0;
dea31012005-04-17 16:05:31 -05001408}
1409
James Smarte59058c2008-08-24 21:49:00 -04001410/**
James Smart3621a712009-04-06 18:47:14 -04001411 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001412 * @phba: Pointer to HBA context object.
1413 * @pring: Pointer to driver SLI ring object.
1414 *
1415 * This function is called with hbalock held to get next
1416 * iocb in txq of the given ring. If there is any iocb in
1417 * the txq, the function returns first iocb in the list after
1418 * removing the iocb from the list, else it returns NULL.
1419 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001420struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001421lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001422{
dea31012005-04-17 16:05:31 -05001423 struct lpfc_iocbq *cmd_iocb;
1424
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001425 lockdep_assert_held(&phba->hbalock);
1426
James Smart858c9f62007-06-17 19:56:39 -05001427 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001428 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001429}
1430
James Smarte59058c2008-08-24 21:49:00 -04001431/**
James Smart3621a712009-04-06 18:47:14 -04001432 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001433 * @phba: Pointer to HBA context object.
1434 * @pring: Pointer to driver SLI ring object.
1435 *
1436 * This function is called with hbalock held and the caller must post the
1437 * iocb without releasing the lock. If the caller releases the lock,
1438 * iocb slot returned by the function is not guaranteed to be available.
1439 * The function returns pointer to the next available iocb slot if there
1440 * is available slot in the ring, else it returns NULL.
1441 * If the get index of the ring is ahead of the put index, the function
1442 * will post an error attention event to the worker thread to take the
1443 * HBA to offline state.
1444 **/
dea31012005-04-17 16:05:31 -05001445static IOCB_t *
1446lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1447{
James Smart34b02dc2008-08-24 21:49:55 -04001448 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001449 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001450
1451 lockdep_assert_held(&phba->hbalock);
1452
James Smart7e56aa22012-08-03 12:35:34 -04001453 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1454 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1455 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001456
James Smart7e56aa22012-08-03 12:35:34 -04001457 if (unlikely(pring->sli.sli3.local_getidx ==
1458 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001459
James Smart7e56aa22012-08-03 12:35:34 -04001460 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001461
James Smart7e56aa22012-08-03 12:35:34 -04001462 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001463 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001464 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001465 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001466 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001467 pring->sli.sli3.local_getidx,
1468 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001469
James Smart2e0fef82007-06-17 19:56:36 -05001470 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001471 /*
1472 * All error attention handlers are posted to
1473 * worker thread
1474 */
1475 phba->work_ha |= HA_ERATT;
1476 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001477
James Smart5e9d9b82008-06-14 22:52:53 -04001478 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001479
1480 return NULL;
1481 }
1482
James Smart7e56aa22012-08-03 12:35:34 -04001483 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001484 return NULL;
1485 }
1486
James Smarted957682007-06-17 19:56:37 -05001487 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001488}
1489
James Smarte59058c2008-08-24 21:49:00 -04001490/**
James Smart3621a712009-04-06 18:47:14 -04001491 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001492 * @phba: Pointer to HBA context object.
1493 * @iocbq: Pointer to driver iocb object.
1494 *
1495 * This function gets an iotag for the iocb. If there is no unused iotag and
1496 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1497 * array and assigns a new iotag.
1498 * The function returns the allocated iotag if successful, else returns zero.
1499 * Zero is not a valid iotag.
1500 * The caller is not required to hold any lock.
1501 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001502uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001503lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001504{
James Smart2e0fef82007-06-17 19:56:36 -05001505 struct lpfc_iocbq **new_arr;
1506 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001507 size_t new_len;
1508 struct lpfc_sli *psli = &phba->sli;
1509 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001510
James Smart2e0fef82007-06-17 19:56:36 -05001511 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001512 iotag = psli->last_iotag;
1513 if(++iotag < psli->iocbq_lookup_len) {
1514 psli->last_iotag = iotag;
1515 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001516 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001517 iocbq->iotag = iotag;
1518 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001519 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001520 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1521 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001522 spin_unlock_irq(&phba->hbalock);
1523 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001524 GFP_KERNEL);
1525 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001526 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001527 old_arr = psli->iocbq_lookup;
1528 if (new_len <= psli->iocbq_lookup_len) {
1529 /* highly unprobable case */
1530 kfree(new_arr);
1531 iotag = psli->last_iotag;
1532 if(++iotag < psli->iocbq_lookup_len) {
1533 psli->last_iotag = iotag;
1534 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001535 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001536 iocbq->iotag = iotag;
1537 return iotag;
1538 }
James Smart2e0fef82007-06-17 19:56:36 -05001539 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001540 return 0;
1541 }
1542 if (psli->iocbq_lookup)
1543 memcpy(new_arr, old_arr,
1544 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001545 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001546 psli->iocbq_lookup = new_arr;
1547 psli->iocbq_lookup_len = new_len;
1548 psli->last_iotag = iotag;
1549 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001550 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001551 iocbq->iotag = iotag;
1552 kfree(old_arr);
1553 return iotag;
1554 }
James Smart8f6d98d2006-08-01 07:34:00 -04001555 } else
James Smart2e0fef82007-06-17 19:56:36 -05001556 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001557
James Smartbc739052010-08-04 16:11:18 -04001558 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001559 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1560 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001561
James Bottomley604a3e32005-10-29 10:28:33 -05001562 return 0;
dea31012005-04-17 16:05:31 -05001563}
1564
James Smarte59058c2008-08-24 21:49:00 -04001565/**
James Smart3621a712009-04-06 18:47:14 -04001566 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001567 * @phba: Pointer to HBA context object.
1568 * @pring: Pointer to driver SLI ring object.
1569 * @iocb: Pointer to iocb slot in the ring.
1570 * @nextiocb: Pointer to driver iocb object which need to be
1571 * posted to firmware.
1572 *
1573 * This function is called with hbalock held to post a new iocb to
1574 * the firmware. This function copies the new iocb to ring iocb slot and
1575 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1576 * a completion call back for this iocb else the function will free the
1577 * iocb object.
1578 **/
dea31012005-04-17 16:05:31 -05001579static void
1580lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1581 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1582{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001583 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001584 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001585 * Set up an iotag
dea31012005-04-17 16:05:31 -05001586 */
James Bottomley604a3e32005-10-29 10:28:33 -05001587 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001588
James Smarte2a0a9d2008-12-04 22:40:02 -05001589
James Smarta58cbd52007-08-02 11:09:43 -04001590 if (pring->ringno == LPFC_ELS_RING) {
1591 lpfc_debugfs_slow_ring_trc(phba,
1592 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1593 *(((uint32_t *) &nextiocb->iocb) + 4),
1594 *(((uint32_t *) &nextiocb->iocb) + 6),
1595 *(((uint32_t *) &nextiocb->iocb) + 7));
1596 }
1597
dea31012005-04-17 16:05:31 -05001598 /*
1599 * Issue iocb command to adapter
1600 */
James Smart92d7f7b2007-06-17 19:56:38 -05001601 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001602 wmb();
1603 pring->stats.iocb_cmd++;
1604
1605 /*
1606 * If there is no completion routine to call, we can release the
1607 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1608 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1609 */
1610 if (nextiocb->iocb_cmpl)
1611 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001612 else
James Smart2e0fef82007-06-17 19:56:36 -05001613 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001614
1615 /*
1616 * Let the HBA know what IOCB slot will be the next one the
1617 * driver will put a command into.
1618 */
James Smart7e56aa22012-08-03 12:35:34 -04001619 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1620 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001621}
1622
James Smarte59058c2008-08-24 21:49:00 -04001623/**
James Smart3621a712009-04-06 18:47:14 -04001624 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001625 * @phba: Pointer to HBA context object.
1626 * @pring: Pointer to driver SLI ring object.
1627 *
1628 * The caller is not required to hold any lock for calling this function.
1629 * This function updates the chip attention bits for the ring to inform firmware
1630 * that there are pending work to be done for this ring and requests an
1631 * interrupt when there is space available in the ring. This function is
1632 * called when the driver is unable to post more iocbs to the ring due
1633 * to unavailability of space in the ring.
1634 **/
dea31012005-04-17 16:05:31 -05001635static void
James Smart2e0fef82007-06-17 19:56:36 -05001636lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001637{
1638 int ringno = pring->ringno;
1639
1640 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1641
1642 wmb();
1643
1644 /*
1645 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1646 * The HBA will tell us when an IOCB entry is available.
1647 */
1648 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1649 readl(phba->CAregaddr); /* flush */
1650
1651 pring->stats.iocb_cmd_full++;
1652}
1653
James Smarte59058c2008-08-24 21:49:00 -04001654/**
James Smart3621a712009-04-06 18:47:14 -04001655 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001656 * @phba: Pointer to HBA context object.
1657 * @pring: Pointer to driver SLI ring object.
1658 *
1659 * This function updates the chip attention register bit for the
1660 * given ring to inform HBA that there is more work to be done
1661 * in this ring. The caller is not required to hold any lock.
1662 **/
dea31012005-04-17 16:05:31 -05001663static void
James Smart2e0fef82007-06-17 19:56:36 -05001664lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001665{
1666 int ringno = pring->ringno;
1667
1668 /*
1669 * Tell the HBA that there is work to do in this ring.
1670 */
James Smart34b02dc2008-08-24 21:49:55 -04001671 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1672 wmb();
1673 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1674 readl(phba->CAregaddr); /* flush */
1675 }
dea31012005-04-17 16:05:31 -05001676}
1677
James Smarte59058c2008-08-24 21:49:00 -04001678/**
James Smart3621a712009-04-06 18:47:14 -04001679 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001680 * @phba: Pointer to HBA context object.
1681 * @pring: Pointer to driver SLI ring object.
1682 *
1683 * This function is called with hbalock held to post pending iocbs
1684 * in the txq to the firmware. This function is called when driver
1685 * detects space available in the ring.
1686 **/
dea31012005-04-17 16:05:31 -05001687static void
James Smart2e0fef82007-06-17 19:56:36 -05001688lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001689{
1690 IOCB_t *iocb;
1691 struct lpfc_iocbq *nextiocb;
1692
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001693 lockdep_assert_held(&phba->hbalock);
1694
dea31012005-04-17 16:05:31 -05001695 /*
1696 * Check to see if:
1697 * (a) there is anything on the txq to send
1698 * (b) link is up
1699 * (c) link attention events can be processed (fcp ring only)
1700 * (d) IOCB processing is not blocked by the outstanding mbox command.
1701 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001702
1703 if (lpfc_is_link_up(phba) &&
1704 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001705 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001706 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001707
1708 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1709 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1710 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1711
1712 if (iocb)
1713 lpfc_sli_update_ring(phba, pring);
1714 else
1715 lpfc_sli_update_full_ring(phba, pring);
1716 }
1717
1718 return;
1719}
1720
James Smarte59058c2008-08-24 21:49:00 -04001721/**
James Smart3621a712009-04-06 18:47:14 -04001722 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001723 * @phba: Pointer to HBA context object.
1724 * @hbqno: HBQ number.
1725 *
1726 * This function is called with hbalock held to get the next
1727 * available slot for the given HBQ. If there is free slot
1728 * available for the HBQ it will return pointer to the next available
1729 * HBQ entry else it will return NULL.
1730 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001731static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001732lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1733{
1734 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1735
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001736 lockdep_assert_held(&phba->hbalock);
1737
James Smarted957682007-06-17 19:56:37 -05001738 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1739 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1740 hbqp->next_hbqPutIdx = 0;
1741
1742 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001743 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001744 uint32_t getidx = le32_to_cpu(raw_index);
1745
1746 hbqp->local_hbqGetIdx = getidx;
1747
1748 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1749 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001750 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001751 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001752 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001753 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001754 hbqp->entry_count);
1755
1756 phba->link_state = LPFC_HBA_ERROR;
1757 return NULL;
1758 }
1759
1760 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1761 return NULL;
1762 }
1763
James Smart51ef4c22007-08-02 11:10:31 -04001764 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1765 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001766}
1767
James Smarte59058c2008-08-24 21:49:00 -04001768/**
James Smart3621a712009-04-06 18:47:14 -04001769 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001770 * @phba: Pointer to HBA context object.
1771 *
1772 * This function is called with no lock held to free all the
1773 * hbq buffers while uninitializing the SLI interface. It also
1774 * frees the HBQ buffers returned by the firmware but not yet
1775 * processed by the upper layers.
1776 **/
James Smarted957682007-06-17 19:56:37 -05001777void
1778lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1779{
James Smart92d7f7b2007-06-17 19:56:38 -05001780 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1781 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001782 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001783 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001784
James Smart51ef4c22007-08-02 11:10:31 -04001785 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001786 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001787 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001788 for (i = 0; i < hbq_count; ++i) {
1789 list_for_each_entry_safe(dmabuf, next_dmabuf,
1790 &phba->hbqs[i].hbq_buffer_list, list) {
1791 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1792 list_del(&hbq_buf->dbuf.list);
1793 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1794 }
James Smarta8adb832007-10-27 13:37:53 -04001795 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001796 }
James Smart3163f722008-02-08 18:50:25 -05001797
1798 /* Mark the HBQs not in use */
1799 phba->hbq_in_use = 0;
1800 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001801}
1802
James Smarte59058c2008-08-24 21:49:00 -04001803/**
James Smart3621a712009-04-06 18:47:14 -04001804 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001805 * @phba: Pointer to HBA context object.
1806 * @hbqno: HBQ number.
1807 * @hbq_buf: Pointer to HBQ buffer.
1808 *
1809 * This function is called with the hbalock held to post a
1810 * hbq buffer to the firmware. If the function finds an empty
1811 * slot in the HBQ, it will post the buffer. The function will return
1812 * pointer to the hbq entry if it successfully post the buffer
1813 * else it will return NULL.
1814 **/
James Smart3772a992009-05-22 14:50:54 -04001815static int
James Smarted957682007-06-17 19:56:37 -05001816lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001817 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001818{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001819 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001820 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1821}
1822
1823/**
1824 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1825 * @phba: Pointer to HBA context object.
1826 * @hbqno: HBQ number.
1827 * @hbq_buf: Pointer to HBQ buffer.
1828 *
1829 * This function is called with the hbalock held to post a hbq buffer to the
1830 * firmware. If the function finds an empty slot in the HBQ, it will post the
1831 * buffer and place it on the hbq_buffer_list. The function will return zero if
1832 * it successfully post the buffer else it will return an error.
1833 **/
1834static int
1835lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1836 struct hbq_dmabuf *hbq_buf)
1837{
James Smarted957682007-06-17 19:56:37 -05001838 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001839 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001840
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001841 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001842 /* Get next HBQ entry slot to use */
1843 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1844 if (hbqe) {
1845 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1846
James Smart92d7f7b2007-06-17 19:56:38 -05001847 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1848 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08001849 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05001850 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001851 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1852 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1853 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001854 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1855 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001856 /* flush */
James Smarted957682007-06-17 19:56:37 -05001857 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001858 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001859 return 0;
1860 } else
1861 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001862}
1863
James Smart4f774512009-05-22 14:52:35 -04001864/**
1865 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1866 * @phba: Pointer to HBA context object.
1867 * @hbqno: HBQ number.
1868 * @hbq_buf: Pointer to HBQ buffer.
1869 *
1870 * This function is called with the hbalock held to post an RQE to the SLI4
1871 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1872 * the hbq_buffer_list and return zero, otherwise it will return an error.
1873 **/
1874static int
1875lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1876 struct hbq_dmabuf *hbq_buf)
1877{
1878 int rc;
1879 struct lpfc_rqe hrqe;
1880 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08001881 struct lpfc_queue *hrq;
1882 struct lpfc_queue *drq;
1883
1884 if (hbqno != LPFC_ELS_HBQ)
1885 return 1;
1886 hrq = phba->sli4_hba.hdr_rq;
1887 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04001888
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001889 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001890 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1891 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1892 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1893 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08001894 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04001895 if (rc < 0)
1896 return rc;
James Smart895427b2017-02-12 13:52:30 -08001897 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04001898 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1899 return 0;
1900}
1901
James Smarte59058c2008-08-24 21:49:00 -04001902/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001903static struct lpfc_hbq_init lpfc_els_hbq = {
1904 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001905 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001906 .mask_count = 0,
1907 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001908 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001909 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001910 .init_count = 40,
1911 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001912};
James Smarted957682007-06-17 19:56:37 -05001913
James Smarte59058c2008-08-24 21:49:00 -04001914/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001915struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001916 &lpfc_els_hbq,
1917};
1918
James Smarte59058c2008-08-24 21:49:00 -04001919/**
James Smart3621a712009-04-06 18:47:14 -04001920 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001921 * @phba: Pointer to HBA context object.
1922 * @hbqno: HBQ number.
1923 * @count: Number of HBQ buffers to be posted.
1924 *
James Smartd7c255b2008-08-24 21:50:00 -04001925 * This function is called with no lock held to post more hbq buffers to the
1926 * given HBQ. The function returns the number of HBQ buffers successfully
1927 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001928 **/
James Smart311464e2007-08-02 11:10:37 -04001929static int
James Smart92d7f7b2007-06-17 19:56:38 -05001930lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1931{
James Smartd7c255b2008-08-24 21:50:00 -04001932 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001933 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001934 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001935 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001936 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001937 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001938
James Smartd7c255b2008-08-24 21:50:00 -04001939 if ((phba->hbqs[hbqno].buffer_count + count) >
1940 lpfc_hbq_defs[hbqno]->entry_count)
1941 count = lpfc_hbq_defs[hbqno]->entry_count -
1942 phba->hbqs[hbqno].buffer_count;
1943 if (!count)
1944 return 0;
1945 /* Allocate HBQ entries */
1946 for (i = 0; i < count; i++) {
1947 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1948 if (!hbq_buffer)
1949 break;
1950 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1951 }
James Smart3163f722008-02-08 18:50:25 -05001952 /* Check whether HBQ is still in use */
1953 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001954 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001955 goto err;
1956 while (!list_empty(&hbq_buf_list)) {
1957 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1958 dbuf.list);
1959 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1960 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001961 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001962 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001963 posted++;
1964 } else
James Smart51ef4c22007-08-02 11:10:31 -04001965 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001966 }
James Smart3163f722008-02-08 18:50:25 -05001967 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001968 return posted;
1969err:
1970 spin_unlock_irqrestore(&phba->hbalock, flags);
1971 while (!list_empty(&hbq_buf_list)) {
1972 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1973 dbuf.list);
1974 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1975 }
James Smart92d7f7b2007-06-17 19:56:38 -05001976 return 0;
James Smarted957682007-06-17 19:56:37 -05001977}
1978
James Smarte59058c2008-08-24 21:49:00 -04001979/**
James Smart3621a712009-04-06 18:47:14 -04001980 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001981 * @phba: Pointer to HBA context object.
1982 * @qno: HBQ number.
1983 *
1984 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001985 * is called with no lock held. The function returns the number of HBQ entries
1986 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001987 **/
James Smarted957682007-06-17 19:56:37 -05001988int
James Smart92d7f7b2007-06-17 19:56:38 -05001989lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001990{
James Smartdef9c7a2009-12-21 17:02:28 -05001991 if (phba->sli_rev == LPFC_SLI_REV4)
1992 return 0;
1993 else
1994 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1995 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001996}
1997
James Smarte59058c2008-08-24 21:49:00 -04001998/**
James Smart3621a712009-04-06 18:47:14 -04001999 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002000 * @phba: Pointer to HBA context object.
2001 * @qno: HBQ queue number.
2002 *
2003 * This function is called from SLI initialization code path with
2004 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002005 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002006 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002007static int
James Smart92d7f7b2007-06-17 19:56:38 -05002008lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002009{
James Smartdef9c7a2009-12-21 17:02:28 -05002010 if (phba->sli_rev == LPFC_SLI_REV4)
2011 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002012 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002013 else
2014 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2015 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002016}
2017
James Smarte59058c2008-08-24 21:49:00 -04002018/**
James Smart3772a992009-05-22 14:50:54 -04002019 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2020 * @phba: Pointer to HBA context object.
2021 * @hbqno: HBQ number.
2022 *
2023 * This function removes the first hbq buffer on an hbq list and returns a
2024 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2025 **/
2026static struct hbq_dmabuf *
2027lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2028{
2029 struct lpfc_dmabuf *d_buf;
2030
2031 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2032 if (!d_buf)
2033 return NULL;
2034 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2035}
2036
2037/**
James Smart2d7dbc42017-02-12 13:52:35 -08002038 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2039 * @phba: Pointer to HBA context object.
2040 * @hbqno: HBQ number.
2041 *
2042 * This function removes the first RQ buffer on an RQ buffer list and returns a
2043 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2044 **/
2045static struct rqb_dmabuf *
2046lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2047{
2048 struct lpfc_dmabuf *h_buf;
2049 struct lpfc_rqb *rqbp;
2050
2051 rqbp = hrq->rqbp;
2052 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2053 struct lpfc_dmabuf, list);
2054 if (!h_buf)
2055 return NULL;
2056 rqbp->buffer_count--;
2057 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2058}
2059
2060/**
James Smart3621a712009-04-06 18:47:14 -04002061 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002062 * @phba: Pointer to HBA context object.
2063 * @tag: Tag of the hbq buffer.
2064 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002065 * This function searches for the hbq buffer associated with the given tag in
2066 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2067 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002068 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002069static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002070lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2071{
James Smart92d7f7b2007-06-17 19:56:38 -05002072 struct lpfc_dmabuf *d_buf;
2073 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002074 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002075
James Smart51ef4c22007-08-02 11:10:31 -04002076 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002077 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002078 return NULL;
2079
James Smart3772a992009-05-22 14:50:54 -04002080 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002081 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002082 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002083 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002084 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002085 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002086 }
2087 }
James Smart3772a992009-05-22 14:50:54 -04002088 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002089 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002090 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002091 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002092 return NULL;
James Smarted957682007-06-17 19:56:37 -05002093}
2094
James Smarte59058c2008-08-24 21:49:00 -04002095/**
James Smart3621a712009-04-06 18:47:14 -04002096 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002097 * @phba: Pointer to HBA context object.
2098 * @hbq_buffer: Pointer to HBQ buffer.
2099 *
2100 * This function is called with hbalock. This function gives back
2101 * the hbq buffer to firmware. If the HBQ does not have space to
2102 * post the buffer, it will free the buffer.
2103 **/
James Smarted957682007-06-17 19:56:37 -05002104void
James Smart51ef4c22007-08-02 11:10:31 -04002105lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002106{
2107 uint32_t hbqno;
2108
James Smart51ef4c22007-08-02 11:10:31 -04002109 if (hbq_buffer) {
2110 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002111 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002112 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002113 }
2114}
2115
James Smarte59058c2008-08-24 21:49:00 -04002116/**
James Smart3621a712009-04-06 18:47:14 -04002117 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002118 * @mbxCommand: mailbox command code.
2119 *
2120 * This function is called by the mailbox event handler function to verify
2121 * that the completed mailbox command is a legitimate mailbox command. If the
2122 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2123 * and the mailbox event handler will take the HBA offline.
2124 **/
dea31012005-04-17 16:05:31 -05002125static int
2126lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2127{
2128 uint8_t ret;
2129
2130 switch (mbxCommand) {
2131 case MBX_LOAD_SM:
2132 case MBX_READ_NV:
2133 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002134 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002135 case MBX_RUN_BIU_DIAG:
2136 case MBX_INIT_LINK:
2137 case MBX_DOWN_LINK:
2138 case MBX_CONFIG_LINK:
2139 case MBX_CONFIG_RING:
2140 case MBX_RESET_RING:
2141 case MBX_READ_CONFIG:
2142 case MBX_READ_RCONFIG:
2143 case MBX_READ_SPARM:
2144 case MBX_READ_STATUS:
2145 case MBX_READ_RPI:
2146 case MBX_READ_XRI:
2147 case MBX_READ_REV:
2148 case MBX_READ_LNK_STAT:
2149 case MBX_REG_LOGIN:
2150 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002151 case MBX_CLEAR_LA:
2152 case MBX_DUMP_MEMORY:
2153 case MBX_DUMP_CONTEXT:
2154 case MBX_RUN_DIAGS:
2155 case MBX_RESTART:
2156 case MBX_UPDATE_CFG:
2157 case MBX_DOWN_LOAD:
2158 case MBX_DEL_LD_ENTRY:
2159 case MBX_RUN_PROGRAM:
2160 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002161 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002162 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002163 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002164 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002165 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002166 case MBX_LOAD_AREA:
2167 case MBX_RUN_BIU_DIAG64:
2168 case MBX_CONFIG_PORT:
2169 case MBX_READ_SPARM64:
2170 case MBX_READ_RPI64:
2171 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002172 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002173 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002174 case MBX_SET_DEBUG:
2175 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002176 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002177 case MBX_REG_VPI:
2178 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002179 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002180 case MBX_PORT_CAPABILITIES:
2181 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002182 case MBX_SLI4_CONFIG:
2183 case MBX_SLI4_REQ_FTRS:
2184 case MBX_REG_FCFI:
2185 case MBX_UNREG_FCFI:
2186 case MBX_REG_VFI:
2187 case MBX_UNREG_VFI:
2188 case MBX_INIT_VPI:
2189 case MBX_INIT_VFI:
2190 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002191 case MBX_READ_EVENT_LOG_STATUS:
2192 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002193 case MBX_SECURITY_MGMT:
2194 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002195 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002196 ret = mbxCommand;
2197 break;
2198 default:
2199 ret = MBX_SHUTDOWN;
2200 break;
2201 }
James Smart2e0fef82007-06-17 19:56:36 -05002202 return ret;
dea31012005-04-17 16:05:31 -05002203}
James Smarte59058c2008-08-24 21:49:00 -04002204
2205/**
James Smart3621a712009-04-06 18:47:14 -04002206 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002207 * @phba: Pointer to HBA context object.
2208 * @pmboxq: Pointer to mailbox command.
2209 *
2210 * This is completion handler function for mailbox commands issued from
2211 * lpfc_sli_issue_mbox_wait function. This function is called by the
2212 * mailbox event handler function with no lock held. This function
2213 * will wake up thread waiting on the wait queue pointed by context1
2214 * of the mailbox.
2215 **/
James Smart04c68492009-05-22 14:52:52 -04002216void
James Smart2e0fef82007-06-17 19:56:36 -05002217lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002218{
2219 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002220 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002221
2222 /*
2223 * If pdone_q is empty, the driver thread gave up waiting and
2224 * continued running.
2225 */
James Smart7054a602007-04-25 09:52:34 -04002226 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002227 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002228 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2229 if (pdone_q)
2230 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002231 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002232 return;
2233}
2234
James Smarte59058c2008-08-24 21:49:00 -04002235
2236/**
James Smart3621a712009-04-06 18:47:14 -04002237 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002238 * @phba: Pointer to HBA context object.
2239 * @pmb: Pointer to mailbox object.
2240 *
2241 * This function is the default mailbox completion handler. It
2242 * frees the memory resources associated with the completed mailbox
2243 * command. If the completed command is a REG_LOGIN mailbox command,
2244 * this function will issue a UREG_LOGIN to re-claim the RPI.
2245 **/
dea31012005-04-17 16:05:31 -05002246void
James Smart2e0fef82007-06-17 19:56:36 -05002247lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002248{
James Smartd439d282010-09-29 11:18:45 -04002249 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002250 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002251 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002252 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002253 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002254 int rc;
2255
dea31012005-04-17 16:05:31 -05002256 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002257
dea31012005-04-17 16:05:31 -05002258 if (mp) {
2259 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2260 kfree(mp);
2261 }
James Smart7054a602007-04-25 09:52:34 -04002262
2263 /*
2264 * If a REG_LOGIN succeeded after node is destroyed or node
2265 * is in re-discovery driver need to cleanup the RPI.
2266 */
James Smart2e0fef82007-06-17 19:56:36 -05002267 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002268 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2269 !pmb->u.mb.mbxStatus) {
2270 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002271 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002272 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002273 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002274 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002275 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2276 if (rc != MBX_NOT_FINISHED)
2277 return;
2278 }
2279
James Smart695a8142010-01-26 23:08:03 -05002280 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2281 !(phba->pport->load_flag & FC_UNLOADING) &&
2282 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002283 shost = lpfc_shost_from_vport(vport);
2284 spin_lock_irq(shost->host_lock);
2285 vport->vpi_state |= LPFC_VPI_REGISTERED;
2286 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2287 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002288 }
2289
James Smartd439d282010-09-29 11:18:45 -04002290 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2291 ndlp = (struct lpfc_nodelist *)pmb->context2;
2292 lpfc_nlp_put(ndlp);
2293 pmb->context2 = NULL;
2294 }
2295
James Smartdcf2a4e2010-09-29 11:18:53 -04002296 /* Check security permission status on INIT_LINK mailbox command */
2297 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2298 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2299 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2300 "2860 SLI authentication is required "
2301 "for INIT_LINK but has not done yet\n");
2302
James Smart04c68492009-05-22 14:52:52 -04002303 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2304 lpfc_sli4_mbox_cmd_free(phba, pmb);
2305 else
2306 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002307}
James Smartbe6bb942015-04-07 15:07:22 -04002308 /**
2309 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2310 * @phba: Pointer to HBA context object.
2311 * @pmb: Pointer to mailbox object.
2312 *
2313 * This function is the unreg rpi mailbox completion handler. It
2314 * frees the memory resources associated with the completed mailbox
2315 * command. An additional refrenece is put on the ndlp to prevent
2316 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2317 * the unreg mailbox command completes, this routine puts the
2318 * reference back.
2319 *
2320 **/
2321void
2322lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2323{
2324 struct lpfc_vport *vport = pmb->vport;
2325 struct lpfc_nodelist *ndlp;
2326
2327 ndlp = pmb->context1;
2328 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2329 if (phba->sli_rev == LPFC_SLI_REV4 &&
2330 (bf_get(lpfc_sli_intf_if_type,
2331 &phba->sli4_hba.sli_intf) ==
2332 LPFC_SLI_INTF_IF_TYPE_2)) {
2333 if (ndlp) {
2334 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2335 "0010 UNREG_LOGIN vpi:%x "
2336 "rpi:%x DID:%x map:%x %p\n",
2337 vport->vpi, ndlp->nlp_rpi,
2338 ndlp->nlp_DID,
2339 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002340 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002341 lpfc_nlp_put(ndlp);
2342 }
2343 }
2344 }
2345
2346 mempool_free(pmb, phba->mbox_mem_pool);
2347}
dea31012005-04-17 16:05:31 -05002348
James Smarte59058c2008-08-24 21:49:00 -04002349/**
James Smart3621a712009-04-06 18:47:14 -04002350 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002351 * @phba: Pointer to HBA context object.
2352 *
2353 * This function is called with no lock held. This function processes all
2354 * the completed mailbox commands and gives it to upper layers. The interrupt
2355 * service routine processes mailbox completion interrupt and adds completed
2356 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2357 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2358 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2359 * function returns the mailbox commands to the upper layer by calling the
2360 * completion handler function of each mailbox.
2361 **/
dea31012005-04-17 16:05:31 -05002362int
James Smart2e0fef82007-06-17 19:56:36 -05002363lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002364{
James Smart92d7f7b2007-06-17 19:56:38 -05002365 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002366 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002367 int rc;
2368 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002369
2370 phba->sli.slistat.mbox_event++;
2371
James Smart92d7f7b2007-06-17 19:56:38 -05002372 /* Get all completed mailboxe buffers into the cmplq */
2373 spin_lock_irq(&phba->hbalock);
2374 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2375 spin_unlock_irq(&phba->hbalock);
2376
dea31012005-04-17 16:05:31 -05002377 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002378 do {
2379 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2380 if (pmb == NULL)
2381 break;
2382
James Smart04c68492009-05-22 14:52:52 -04002383 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002384
James Smart858c9f62007-06-17 19:56:39 -05002385 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2386 if (pmb->vport) {
2387 lpfc_debugfs_disc_trc(pmb->vport,
2388 LPFC_DISC_TRC_MBOX_VPORT,
2389 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2390 (uint32_t)pmbox->mbxCommand,
2391 pmbox->un.varWords[0],
2392 pmbox->un.varWords[1]);
2393 }
2394 else {
2395 lpfc_debugfs_disc_trc(phba->pport,
2396 LPFC_DISC_TRC_MBOX,
2397 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2398 (uint32_t)pmbox->mbxCommand,
2399 pmbox->un.varWords[0],
2400 pmbox->un.varWords[1]);
2401 }
2402 }
2403
dea31012005-04-17 16:05:31 -05002404 /*
2405 * It is a fatal error if unknown mbox command completion.
2406 */
2407 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2408 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002409 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002410 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002411 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002412 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002413 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002414 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002415 lpfc_sli_config_mbox_subsys_get(phba,
2416 pmb),
2417 lpfc_sli_config_mbox_opcode_get(phba,
2418 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002419 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002420 phba->work_hs = HS_FFER3;
2421 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002422 continue;
dea31012005-04-17 16:05:31 -05002423 }
2424
dea31012005-04-17 16:05:31 -05002425 if (pmbox->mbxStatus) {
2426 phba->sli.slistat.mbox_stat_err++;
2427 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2428 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002429 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002430 LOG_MBOX | LOG_SLI,
2431 "(%d):0305 Mbox cmd cmpl "
2432 "error - RETRYing Data: x%x "
2433 "(x%x/x%x) x%x x%x x%x\n",
2434 pmb->vport ? pmb->vport->vpi : 0,
2435 pmbox->mbxCommand,
2436 lpfc_sli_config_mbox_subsys_get(phba,
2437 pmb),
2438 lpfc_sli_config_mbox_opcode_get(phba,
2439 pmb),
2440 pmbox->mbxStatus,
2441 pmbox->un.varWords[0],
2442 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002443 pmbox->mbxStatus = 0;
2444 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002445 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002446 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002447 continue;
dea31012005-04-17 16:05:31 -05002448 }
2449 }
2450
2451 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002452 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002453 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002454 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2455 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002456 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002457 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002458 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2459 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002460 pmb->mbox_cmpl,
2461 *((uint32_t *) pmbox),
2462 pmbox->un.varWords[0],
2463 pmbox->un.varWords[1],
2464 pmbox->un.varWords[2],
2465 pmbox->un.varWords[3],
2466 pmbox->un.varWords[4],
2467 pmbox->un.varWords[5],
2468 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002469 pmbox->un.varWords[7],
2470 pmbox->un.varWords[8],
2471 pmbox->un.varWords[9],
2472 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002473
James Smart92d7f7b2007-06-17 19:56:38 -05002474 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002475 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002476 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002477 return 0;
dea31012005-04-17 16:05:31 -05002478}
James Smart92d7f7b2007-06-17 19:56:38 -05002479
James Smarte59058c2008-08-24 21:49:00 -04002480/**
James Smart3621a712009-04-06 18:47:14 -04002481 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002482 * @phba: Pointer to HBA context object.
2483 * @pring: Pointer to driver SLI ring object.
2484 * @tag: buffer tag.
2485 *
2486 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2487 * is set in the tag the buffer is posted for a particular exchange,
2488 * the function will return the buffer without replacing the buffer.
2489 * If the buffer is for unsolicited ELS or CT traffic, this function
2490 * returns the buffer and also posts another buffer to the firmware.
2491 **/
James Smart76bb24e2007-10-27 13:38:00 -04002492static struct lpfc_dmabuf *
2493lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002494 struct lpfc_sli_ring *pring,
2495 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002496{
James Smart9f1e1b52008-12-04 22:39:40 -05002497 struct hbq_dmabuf *hbq_entry;
2498
James Smart76bb24e2007-10-27 13:38:00 -04002499 if (tag & QUE_BUFTAG_BIT)
2500 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002501 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2502 if (!hbq_entry)
2503 return NULL;
2504 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002505}
James Smart57127f12007-10-27 13:37:05 -04002506
James Smart3772a992009-05-22 14:50:54 -04002507/**
2508 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2509 * @phba: Pointer to HBA context object.
2510 * @pring: Pointer to driver SLI ring object.
2511 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2512 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2513 * @fch_type: the type for the first frame of the sequence.
2514 *
2515 * This function is called with no lock held. This function uses the r_ctl and
2516 * type of the received sequence to find the correct callback function to call
2517 * to process the sequence.
2518 **/
2519static int
2520lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2521 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2522 uint32_t fch_type)
2523{
2524 int i;
2525
James Smartf358dd02017-02-12 13:52:34 -08002526 switch (fch_type) {
2527 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002528 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002529 return 1;
2530 default:
2531 break;
2532 }
2533
James Smart3772a992009-05-22 14:50:54 -04002534 /* unSolicited Responses */
2535 if (pring->prt[0].profile) {
2536 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2537 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2538 saveq);
2539 return 1;
2540 }
2541 /* We must search, based on rctl / type
2542 for the right routine */
2543 for (i = 0; i < pring->num_mask; i++) {
2544 if ((pring->prt[i].rctl == fch_r_ctl) &&
2545 (pring->prt[i].type == fch_type)) {
2546 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2547 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2548 (phba, pring, saveq);
2549 return 1;
2550 }
2551 }
2552 return 0;
2553}
James Smarte59058c2008-08-24 21:49:00 -04002554
2555/**
James Smart3621a712009-04-06 18:47:14 -04002556 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002557 * @phba: Pointer to HBA context object.
2558 * @pring: Pointer to driver SLI ring object.
2559 * @saveq: Pointer to the unsolicited iocb.
2560 *
2561 * This function is called with no lock held by the ring event handler
2562 * when there is an unsolicited iocb posted to the response ring by the
2563 * firmware. This function gets the buffer associated with the iocbs
2564 * and calls the event handler for the ring. This function handles both
2565 * qring buffers and hbq buffers.
2566 * When the function returns 1 the caller can free the iocb object otherwise
2567 * upper layer functions will free the iocb objects.
2568 **/
dea31012005-04-17 16:05:31 -05002569static int
2570lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2571 struct lpfc_iocbq *saveq)
2572{
2573 IOCB_t * irsp;
2574 WORD5 * w5p;
2575 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002576 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002577 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002578
dea31012005-04-17 16:05:31 -05002579 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002580
2581 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2582 if (pring->lpfc_sli_rcv_async_status)
2583 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2584 else
2585 lpfc_printf_log(phba,
2586 KERN_WARNING,
2587 LOG_SLI,
2588 "0316 Ring %d handler: unexpected "
2589 "ASYNC_STATUS iocb received evt_code "
2590 "0x%x\n",
2591 pring->ringno,
2592 irsp->un.asyncstat.evt_code);
2593 return 1;
2594 }
2595
James Smart3163f722008-02-08 18:50:25 -05002596 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2597 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2598 if (irsp->ulpBdeCount > 0) {
2599 dmzbuf = lpfc_sli_get_buff(phba, pring,
2600 irsp->un.ulpWord[3]);
2601 lpfc_in_buf_free(phba, dmzbuf);
2602 }
2603
2604 if (irsp->ulpBdeCount > 1) {
2605 dmzbuf = lpfc_sli_get_buff(phba, pring,
2606 irsp->unsli3.sli3Words[3]);
2607 lpfc_in_buf_free(phba, dmzbuf);
2608 }
2609
2610 if (irsp->ulpBdeCount > 2) {
2611 dmzbuf = lpfc_sli_get_buff(phba, pring,
2612 irsp->unsli3.sli3Words[7]);
2613 lpfc_in_buf_free(phba, dmzbuf);
2614 }
2615
2616 return 1;
2617 }
2618
James Smart92d7f7b2007-06-17 19:56:38 -05002619 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002620 if (irsp->ulpBdeCount != 0) {
2621 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002622 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002623 if (!saveq->context2)
2624 lpfc_printf_log(phba,
2625 KERN_ERR,
2626 LOG_SLI,
2627 "0341 Ring %d Cannot find buffer for "
2628 "an unsolicited iocb. tag 0x%x\n",
2629 pring->ringno,
2630 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002631 }
2632 if (irsp->ulpBdeCount == 2) {
2633 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002634 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002635 if (!saveq->context3)
2636 lpfc_printf_log(phba,
2637 KERN_ERR,
2638 LOG_SLI,
2639 "0342 Ring %d Cannot find buffer for an"
2640 " unsolicited iocb. tag 0x%x\n",
2641 pring->ringno,
2642 irsp->unsli3.sli3Words[7]);
2643 }
2644 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002645 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002646 if (irsp->ulpBdeCount != 0) {
2647 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2648 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002649 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002650 lpfc_printf_log(phba,
2651 KERN_ERR,
2652 LOG_SLI,
2653 "0343 Ring %d Cannot find "
2654 "buffer for an unsolicited iocb"
2655 ". tag 0x%x\n", pring->ringno,
2656 irsp->un.ulpWord[3]);
2657 }
2658 if (irsp->ulpBdeCount == 2) {
2659 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2660 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002661 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002662 lpfc_printf_log(phba,
2663 KERN_ERR,
2664 LOG_SLI,
2665 "0344 Ring %d Cannot find "
2666 "buffer for an unsolicited "
2667 "iocb. tag 0x%x\n",
2668 pring->ringno,
2669 irsp->unsli3.sli3Words[7]);
2670 }
2671 }
James Smart92d7f7b2007-06-17 19:56:38 -05002672 }
James Smart9c2face2008-01-11 01:53:18 -05002673 if (irsp->ulpBdeCount != 0 &&
2674 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2675 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2676 int found = 0;
2677
2678 /* search continue save q for same XRI */
2679 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002680 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2681 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002682 list_add_tail(&saveq->list, &iocbq->list);
2683 found = 1;
2684 break;
2685 }
2686 }
2687 if (!found)
2688 list_add_tail(&saveq->clist,
2689 &pring->iocb_continue_saveq);
2690 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2691 list_del_init(&iocbq->clist);
2692 saveq = iocbq;
2693 irsp = &(saveq->iocb);
2694 } else
2695 return 0;
2696 }
2697 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2698 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2699 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002700 Rctl = FC_RCTL_ELS_REQ;
2701 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002702 } else {
2703 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2704 Rctl = w5p->hcsw.Rctl;
2705 Type = w5p->hcsw.Type;
2706
2707 /* Firmware Workaround */
2708 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2709 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2710 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002711 Rctl = FC_RCTL_ELS_REQ;
2712 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002713 w5p->hcsw.Rctl = Rctl;
2714 w5p->hcsw.Type = Type;
2715 }
2716 }
James Smart92d7f7b2007-06-17 19:56:38 -05002717
James Smart3772a992009-05-22 14:50:54 -04002718 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002719 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002720 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002721 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002722 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002723
James Smart92d7f7b2007-06-17 19:56:38 -05002724 return 1;
dea31012005-04-17 16:05:31 -05002725}
2726
James Smarte59058c2008-08-24 21:49:00 -04002727/**
James Smart3621a712009-04-06 18:47:14 -04002728 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002729 * @phba: Pointer to HBA context object.
2730 * @pring: Pointer to driver SLI ring object.
2731 * @prspiocb: Pointer to response iocb object.
2732 *
2733 * This function looks up the iocb_lookup table to get the command iocb
2734 * corresponding to the given response iocb using the iotag of the
2735 * response iocb. This function is called with the hbalock held.
2736 * This function returns the command iocb object if it finds the command
2737 * iocb else returns NULL.
2738 **/
dea31012005-04-17 16:05:31 -05002739static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002740lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2741 struct lpfc_sli_ring *pring,
2742 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002743{
dea31012005-04-17 16:05:31 -05002744 struct lpfc_iocbq *cmd_iocb = NULL;
2745 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002746 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002747
James Bottomley604a3e32005-10-29 10:28:33 -05002748 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002749
James Bottomley604a3e32005-10-29 10:28:33 -05002750 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2751 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002752 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002753 /* remove from txcmpl queue list */
2754 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002755 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002756 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002757 }
dea31012005-04-17 16:05:31 -05002758 }
2759
dea31012005-04-17 16:05:31 -05002760 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002761 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002762 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002763 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002764 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002765 return NULL;
2766}
2767
James Smarte59058c2008-08-24 21:49:00 -04002768/**
James Smart3772a992009-05-22 14:50:54 -04002769 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2770 * @phba: Pointer to HBA context object.
2771 * @pring: Pointer to driver SLI ring object.
2772 * @iotag: IOCB tag.
2773 *
2774 * This function looks up the iocb_lookup table to get the command iocb
2775 * corresponding to the given iotag. This function is called with the
2776 * hbalock held.
2777 * This function returns the command iocb object if it finds the command
2778 * iocb else returns NULL.
2779 **/
2780static struct lpfc_iocbq *
2781lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2782 struct lpfc_sli_ring *pring, uint16_t iotag)
2783{
James Smart895427b2017-02-12 13:52:30 -08002784 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002785
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002786 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002787 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2788 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002789 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2790 /* remove from txcmpl queue list */
2791 list_del_init(&cmd_iocb->list);
2792 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002793 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002794 }
James Smart3772a992009-05-22 14:50:54 -04002795 }
James Smart89533e92016-10-13 15:06:15 -07002796
James Smart3772a992009-05-22 14:50:54 -04002797 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002798 "0372 iotag x%x lookup error: max iotag (x%x) "
2799 "iocb_flag x%x\n",
2800 iotag, phba->sli.last_iotag,
2801 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002802 return NULL;
2803}
2804
2805/**
James Smart3621a712009-04-06 18:47:14 -04002806 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002807 * @phba: Pointer to HBA context object.
2808 * @pring: Pointer to driver SLI ring object.
2809 * @saveq: Pointer to the response iocb to be processed.
2810 *
2811 * This function is called by the ring event handler for non-fcp
2812 * rings when there is a new response iocb in the response ring.
2813 * The caller is not required to hold any locks. This function
2814 * gets the command iocb associated with the response iocb and
2815 * calls the completion handler for the command iocb. If there
2816 * is no completion handler, the function will free the resources
2817 * associated with command iocb. If the response iocb is for
2818 * an already aborted command iocb, the status of the completion
2819 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2820 * This function always returns 1.
2821 **/
dea31012005-04-17 16:05:31 -05002822static int
James Smart2e0fef82007-06-17 19:56:36 -05002823lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002824 struct lpfc_iocbq *saveq)
2825{
James Smart2e0fef82007-06-17 19:56:36 -05002826 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002827 int rc = 1;
2828 unsigned long iflag;
2829
2830 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002831 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002832 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002833 spin_unlock_irqrestore(&phba->hbalock, iflag);
2834
dea31012005-04-17 16:05:31 -05002835 if (cmdiocbp) {
2836 if (cmdiocbp->iocb_cmpl) {
2837 /*
James Smartea2151b2008-09-07 11:52:10 -04002838 * If an ELS command failed send an event to mgmt
2839 * application.
2840 */
2841 if (saveq->iocb.ulpStatus &&
2842 (pring->ringno == LPFC_ELS_RING) &&
2843 (cmdiocbp->iocb.ulpCommand ==
2844 CMD_ELS_REQUEST64_CR))
2845 lpfc_send_els_failure_event(phba,
2846 cmdiocbp, saveq);
2847
2848 /*
dea31012005-04-17 16:05:31 -05002849 * Post all ELS completions to the worker thread.
2850 * All other are passed to the completion callback.
2851 */
2852 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002853 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2854 (cmdiocbp->iocb_flag &
2855 LPFC_DRIVER_ABORTED)) {
2856 spin_lock_irqsave(&phba->hbalock,
2857 iflag);
James Smart07951072007-04-25 09:51:38 -04002858 cmdiocbp->iocb_flag &=
2859 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002860 spin_unlock_irqrestore(&phba->hbalock,
2861 iflag);
James Smart07951072007-04-25 09:51:38 -04002862 saveq->iocb.ulpStatus =
2863 IOSTAT_LOCAL_REJECT;
2864 saveq->iocb.un.ulpWord[4] =
2865 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002866
2867 /* Firmware could still be in progress
2868 * of DMAing payload, so don't free data
2869 * buffer till after a hbeat.
2870 */
James Smart341af102010-01-26 23:07:37 -05002871 spin_lock_irqsave(&phba->hbalock,
2872 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002873 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002874 spin_unlock_irqrestore(&phba->hbalock,
2875 iflag);
2876 }
James Smart0f65ff62010-02-26 14:14:23 -05002877 if (phba->sli_rev == LPFC_SLI_REV4) {
2878 if (saveq->iocb_flag &
2879 LPFC_EXCHANGE_BUSY) {
2880 /* Set cmdiocb flag for the
2881 * exchange busy so sgl (xri)
2882 * will not be released until
2883 * the abort xri is received
2884 * from hba.
2885 */
2886 spin_lock_irqsave(
2887 &phba->hbalock, iflag);
2888 cmdiocbp->iocb_flag |=
2889 LPFC_EXCHANGE_BUSY;
2890 spin_unlock_irqrestore(
2891 &phba->hbalock, iflag);
2892 }
2893 if (cmdiocbp->iocb_flag &
2894 LPFC_DRIVER_ABORTED) {
2895 /*
2896 * Clear LPFC_DRIVER_ABORTED
2897 * bit in case it was driver
2898 * initiated abort.
2899 */
2900 spin_lock_irqsave(
2901 &phba->hbalock, iflag);
2902 cmdiocbp->iocb_flag &=
2903 ~LPFC_DRIVER_ABORTED;
2904 spin_unlock_irqrestore(
2905 &phba->hbalock, iflag);
2906 cmdiocbp->iocb.ulpStatus =
2907 IOSTAT_LOCAL_REJECT;
2908 cmdiocbp->iocb.un.ulpWord[4] =
2909 IOERR_ABORT_REQUESTED;
2910 /*
2911 * For SLI4, irsiocb contains
2912 * NO_XRI in sli_xritag, it
2913 * shall not affect releasing
2914 * sgl (xri) process.
2915 */
2916 saveq->iocb.ulpStatus =
2917 IOSTAT_LOCAL_REJECT;
2918 saveq->iocb.un.ulpWord[4] =
2919 IOERR_SLI_ABORTED;
2920 spin_lock_irqsave(
2921 &phba->hbalock, iflag);
2922 saveq->iocb_flag |=
2923 LPFC_DELAY_MEM_FREE;
2924 spin_unlock_irqrestore(
2925 &phba->hbalock, iflag);
2926 }
James Smart07951072007-04-25 09:51:38 -04002927 }
dea31012005-04-17 16:05:31 -05002928 }
James Smart2e0fef82007-06-17 19:56:36 -05002929 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002930 } else
2931 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002932 } else {
2933 /*
2934 * Unknown initiating command based on the response iotag.
2935 * This could be the case on the ELS ring because of
2936 * lpfc_els_abort().
2937 */
2938 if (pring->ringno != LPFC_ELS_RING) {
2939 /*
2940 * Ring <ringno> handler: unexpected completion IoTag
2941 * <IoTag>
2942 */
James Smarta257bf92009-04-06 18:48:10 -04002943 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002944 "0322 Ring %d handler: "
2945 "unexpected completion IoTag x%x "
2946 "Data: x%x x%x x%x x%x\n",
2947 pring->ringno,
2948 saveq->iocb.ulpIoTag,
2949 saveq->iocb.ulpStatus,
2950 saveq->iocb.un.ulpWord[4],
2951 saveq->iocb.ulpCommand,
2952 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002953 }
2954 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002955
dea31012005-04-17 16:05:31 -05002956 return rc;
2957}
2958
James Smarte59058c2008-08-24 21:49:00 -04002959/**
James Smart3621a712009-04-06 18:47:14 -04002960 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002961 * @phba: Pointer to HBA context object.
2962 * @pring: Pointer to driver SLI ring object.
2963 *
2964 * This function is called from the iocb ring event handlers when
2965 * put pointer is ahead of the get pointer for a ring. This function signal
2966 * an error attention condition to the worker thread and the worker
2967 * thread will transition the HBA to offline state.
2968 **/
James Smart2e0fef82007-06-17 19:56:36 -05002969static void
2970lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002971{
James Smart34b02dc2008-08-24 21:49:55 -04002972 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002973 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002974 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002975 * rsp ring <portRspMax>
2976 */
2977 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002978 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002979 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002980 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002981 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002982
James Smart2e0fef82007-06-17 19:56:36 -05002983 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002984
2985 /*
2986 * All error attention handlers are posted to
2987 * worker thread
2988 */
2989 phba->work_ha |= HA_ERATT;
2990 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002991
James Smart5e9d9b82008-06-14 22:52:53 -04002992 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002993
2994 return;
2995}
2996
James Smarte59058c2008-08-24 21:49:00 -04002997/**
James Smart3621a712009-04-06 18:47:14 -04002998 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002999 * @ptr: Pointer to address of HBA context object.
3000 *
3001 * This function is invoked by the Error Attention polling timer when the
3002 * timer times out. It will check the SLI Error Attention register for
3003 * possible attention events. If so, it will post an Error Attention event
3004 * and wake up worker thread to process it. Otherwise, it will set up the
3005 * Error Attention polling timer for the next poll.
3006 **/
3007void lpfc_poll_eratt(unsigned long ptr)
3008{
3009 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003010 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003011 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003012
3013 phba = (struct lpfc_hba *)ptr;
3014
James Smartaa6fbb72012-08-03 12:36:03 -04003015 /* Here we will also keep track of interrupts per sec of the hba */
3016 sli_intr = phba->sli.slistat.sli_intr;
3017
3018 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3019 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3020 sli_intr);
3021 else
3022 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3023
James Smart65791f12016-07-06 12:35:56 -07003024 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3025 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003026 phba->sli.slistat.sli_ips = cnt;
3027
3028 phba->sli.slistat.sli_prev_intr = sli_intr;
3029
James Smart93996272008-08-24 21:50:30 -04003030 /* Check chip HA register for error event */
3031 eratt = lpfc_sli_check_eratt(phba);
3032
3033 if (eratt)
3034 /* Tell the worker thread there is work to do */
3035 lpfc_worker_wake_up(phba);
3036 else
3037 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003038 mod_timer(&phba->eratt_poll,
3039 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003040 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003041 return;
3042}
3043
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003044
James Smarte59058c2008-08-24 21:49:00 -04003045/**
James Smart3621a712009-04-06 18:47:14 -04003046 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003047 * @phba: Pointer to HBA context object.
3048 * @pring: Pointer to driver SLI ring object.
3049 * @mask: Host attention register mask for this ring.
3050 *
3051 * This function is called from the interrupt context when there is a ring
3052 * event for the fcp ring. The caller does not hold any lock.
3053 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003054 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003055 * LE bit set. The function will call the completion handler of the command iocb
3056 * if the response iocb indicates a completion for a command iocb or it is
3057 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3058 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003059 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003060 * to check it explicitly.
3061 */
3062int
James Smart2e0fef82007-06-17 19:56:36 -05003063lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3064 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003065{
James Smart34b02dc2008-08-24 21:49:55 -04003066 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003067 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003068 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003069 struct lpfc_iocbq *cmdiocbq = NULL;
3070 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003071 uint32_t status;
3072 uint32_t portRspPut, portRspMax;
3073 int rc = 1;
3074 lpfc_iocb_type type;
3075 unsigned long iflag;
3076 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003077
James Smart2e0fef82007-06-17 19:56:36 -05003078 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003079 pring->stats.iocb_event++;
3080
dea31012005-04-17 16:05:31 -05003081 /*
3082 * The next available response entry should never exceed the maximum
3083 * entries. If it does, treat it as an adapter hardware error.
3084 */
James Smart7e56aa22012-08-03 12:35:34 -04003085 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003086 portRspPut = le32_to_cpu(pgp->rspPutInx);
3087 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003088 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003089 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003090 return 1;
3091 }
James Smart45ed1192009-10-02 15:17:02 -04003092 if (phba->fcp_ring_in_use) {
3093 spin_unlock_irqrestore(&phba->hbalock, iflag);
3094 return 1;
3095 } else
3096 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003097
3098 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003099 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003100 /*
3101 * Fetch an entry off the ring and copy it into a local data
3102 * structure. The copy involves a byte-swap since the
3103 * network byte order and pci byte orders are different.
3104 */
James Smarted957682007-06-17 19:56:37 -05003105 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003106 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003107
James Smart7e56aa22012-08-03 12:35:34 -04003108 if (++pring->sli.sli3.rspidx >= portRspMax)
3109 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003110
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003111 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3112 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003113 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003114 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003115 irsp = &rspiocbq.iocb;
3116
dea31012005-04-17 16:05:31 -05003117 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3118 pring->stats.iocb_rsp++;
3119 rsp_cmpl++;
3120
3121 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003122 /*
3123 * If resource errors reported from HBA, reduce
3124 * queuedepths of the SCSI device.
3125 */
3126 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003127 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3128 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003129 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003130 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003131 spin_lock_irqsave(&phba->hbalock, iflag);
3132 }
3133
dea31012005-04-17 16:05:31 -05003134 /* Rsp ring <ringno> error: IOCB */
3135 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003136 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003137 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003138 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003139 irsp->un.ulpWord[0],
3140 irsp->un.ulpWord[1],
3141 irsp->un.ulpWord[2],
3142 irsp->un.ulpWord[3],
3143 irsp->un.ulpWord[4],
3144 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003145 *(uint32_t *)&irsp->un1,
3146 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003147 }
3148
3149 switch (type) {
3150 case LPFC_ABORT_IOCB:
3151 case LPFC_SOL_IOCB:
3152 /*
3153 * Idle exchange closed via ABTS from port. No iocb
3154 * resources need to be recovered.
3155 */
3156 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003157 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003158 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003159 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003160 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003161 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003162 break;
3163 }
3164
James Bottomley604a3e32005-10-29 10:28:33 -05003165 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3166 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003167 if (unlikely(!cmdiocbq))
3168 break;
3169 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3170 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3171 if (cmdiocbq->iocb_cmpl) {
3172 spin_unlock_irqrestore(&phba->hbalock, iflag);
3173 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3174 &rspiocbq);
3175 spin_lock_irqsave(&phba->hbalock, iflag);
3176 }
dea31012005-04-17 16:05:31 -05003177 break;
James Smarta4bc3372006-12-02 13:34:16 -05003178 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003179 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003180 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003181 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003182 break;
dea31012005-04-17 16:05:31 -05003183 default:
3184 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3185 char adaptermsg[LPFC_MAX_ADPTMSG];
3186 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3187 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3188 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003189 dev_warn(&((phba->pcidev)->dev),
3190 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003191 phba->brd_no, adaptermsg);
3192 } else {
3193 /* Unknown IOCB command */
3194 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003195 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003196 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003197 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003198 irsp->ulpStatus,
3199 irsp->ulpIoTag,
3200 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003201 }
3202 break;
3203 }
3204
3205 /*
3206 * The response IOCB has been processed. Update the ring
3207 * pointer in SLIM. If the port response put pointer has not
3208 * been updated, sync the pgp->rspPutInx and fetch the new port
3209 * response put pointer.
3210 */
James Smart7e56aa22012-08-03 12:35:34 -04003211 writel(pring->sli.sli3.rspidx,
3212 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003213
James Smart7e56aa22012-08-03 12:35:34 -04003214 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003215 portRspPut = le32_to_cpu(pgp->rspPutInx);
3216 }
3217
3218 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3219 pring->stats.iocb_rsp_full++;
3220 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3221 writel(status, phba->CAregaddr);
3222 readl(phba->CAregaddr);
3223 }
3224 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3225 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3226 pring->stats.iocb_cmd_empty++;
3227
3228 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003229 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003230 lpfc_sli_resume_iocb(phba, pring);
3231
3232 if ((pring->lpfc_sli_cmd_available))
3233 (pring->lpfc_sli_cmd_available) (phba, pring);
3234
3235 }
3236
James Smart45ed1192009-10-02 15:17:02 -04003237 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003238 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003239 return rc;
3240}
3241
James Smarte59058c2008-08-24 21:49:00 -04003242/**
James Smart3772a992009-05-22 14:50:54 -04003243 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3244 * @phba: Pointer to HBA context object.
3245 * @pring: Pointer to driver SLI ring object.
3246 * @rspiocbp: Pointer to driver response IOCB object.
3247 *
3248 * This function is called from the worker thread when there is a slow-path
3249 * response IOCB to process. This function chains all the response iocbs until
3250 * seeing the iocb with the LE bit set. The function will call
3251 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3252 * completion of a command iocb. The function will call the
3253 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3254 * The function frees the resources or calls the completion handler if this
3255 * iocb is an abort completion. The function returns NULL when the response
3256 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3257 * this function shall chain the iocb on to the iocb_continueq and return the
3258 * response iocb passed in.
3259 **/
3260static struct lpfc_iocbq *
3261lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3262 struct lpfc_iocbq *rspiocbp)
3263{
3264 struct lpfc_iocbq *saveq;
3265 struct lpfc_iocbq *cmdiocbp;
3266 struct lpfc_iocbq *next_iocb;
3267 IOCB_t *irsp = NULL;
3268 uint32_t free_saveq;
3269 uint8_t iocb_cmd_type;
3270 lpfc_iocb_type type;
3271 unsigned long iflag;
3272 int rc;
3273
3274 spin_lock_irqsave(&phba->hbalock, iflag);
3275 /* First add the response iocb to the countinueq list */
3276 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3277 pring->iocb_continueq_cnt++;
3278
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003279 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003280 irsp = &rspiocbp->iocb;
3281 if (irsp->ulpLe) {
3282 /*
3283 * By default, the driver expects to free all resources
3284 * associated with this iocb completion.
3285 */
3286 free_saveq = 1;
3287 saveq = list_get_first(&pring->iocb_continueq,
3288 struct lpfc_iocbq, list);
3289 irsp = &(saveq->iocb);
3290 list_del_init(&pring->iocb_continueq);
3291 pring->iocb_continueq_cnt = 0;
3292
3293 pring->stats.iocb_rsp++;
3294
3295 /*
3296 * If resource errors reported from HBA, reduce
3297 * queuedepths of the SCSI device.
3298 */
3299 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003300 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3301 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003302 spin_unlock_irqrestore(&phba->hbalock, iflag);
3303 phba->lpfc_rampdown_queue_depth(phba);
3304 spin_lock_irqsave(&phba->hbalock, iflag);
3305 }
3306
3307 if (irsp->ulpStatus) {
3308 /* Rsp ring <ringno> error: IOCB */
3309 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3310 "0328 Rsp Ring %d error: "
3311 "IOCB Data: "
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 "
3315 "x%x x%x x%x x%x\n",
3316 pring->ringno,
3317 irsp->un.ulpWord[0],
3318 irsp->un.ulpWord[1],
3319 irsp->un.ulpWord[2],
3320 irsp->un.ulpWord[3],
3321 irsp->un.ulpWord[4],
3322 irsp->un.ulpWord[5],
3323 *(((uint32_t *) irsp) + 6),
3324 *(((uint32_t *) irsp) + 7),
3325 *(((uint32_t *) irsp) + 8),
3326 *(((uint32_t *) irsp) + 9),
3327 *(((uint32_t *) irsp) + 10),
3328 *(((uint32_t *) irsp) + 11),
3329 *(((uint32_t *) irsp) + 12),
3330 *(((uint32_t *) irsp) + 13),
3331 *(((uint32_t *) irsp) + 14),
3332 *(((uint32_t *) irsp) + 15));
3333 }
3334
3335 /*
3336 * Fetch the IOCB command type and call the correct completion
3337 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3338 * get freed back to the lpfc_iocb_list by the discovery
3339 * kernel thread.
3340 */
3341 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3342 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3343 switch (type) {
3344 case LPFC_SOL_IOCB:
3345 spin_unlock_irqrestore(&phba->hbalock, iflag);
3346 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3347 spin_lock_irqsave(&phba->hbalock, iflag);
3348 break;
3349
3350 case LPFC_UNSOL_IOCB:
3351 spin_unlock_irqrestore(&phba->hbalock, iflag);
3352 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3353 spin_lock_irqsave(&phba->hbalock, iflag);
3354 if (!rc)
3355 free_saveq = 0;
3356 break;
3357
3358 case LPFC_ABORT_IOCB:
3359 cmdiocbp = NULL;
3360 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3361 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3362 saveq);
3363 if (cmdiocbp) {
3364 /* Call the specified completion routine */
3365 if (cmdiocbp->iocb_cmpl) {
3366 spin_unlock_irqrestore(&phba->hbalock,
3367 iflag);
3368 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3369 saveq);
3370 spin_lock_irqsave(&phba->hbalock,
3371 iflag);
3372 } else
3373 __lpfc_sli_release_iocbq(phba,
3374 cmdiocbp);
3375 }
3376 break;
3377
3378 case LPFC_UNKNOWN_IOCB:
3379 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3380 char adaptermsg[LPFC_MAX_ADPTMSG];
3381 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3382 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3383 MAX_MSG_DATA);
3384 dev_warn(&((phba->pcidev)->dev),
3385 "lpfc%d: %s\n",
3386 phba->brd_no, adaptermsg);
3387 } else {
3388 /* Unknown IOCB command */
3389 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3390 "0335 Unknown IOCB "
3391 "command Data: x%x "
3392 "x%x x%x x%x\n",
3393 irsp->ulpCommand,
3394 irsp->ulpStatus,
3395 irsp->ulpIoTag,
3396 irsp->ulpContext);
3397 }
3398 break;
3399 }
3400
3401 if (free_saveq) {
3402 list_for_each_entry_safe(rspiocbp, next_iocb,
3403 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003404 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003405 __lpfc_sli_release_iocbq(phba, rspiocbp);
3406 }
3407 __lpfc_sli_release_iocbq(phba, saveq);
3408 }
3409 rspiocbp = NULL;
3410 }
3411 spin_unlock_irqrestore(&phba->hbalock, iflag);
3412 return rspiocbp;
3413}
3414
3415/**
3416 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003417 * @phba: Pointer to HBA context object.
3418 * @pring: Pointer to driver SLI ring object.
3419 * @mask: Host attention register mask for this ring.
3420 *
James Smart3772a992009-05-22 14:50:54 -04003421 * This routine wraps the actual slow_ring event process routine from the
3422 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003423 **/
James Smart3772a992009-05-22 14:50:54 -04003424void
James Smart2e0fef82007-06-17 19:56:36 -05003425lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3426 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003427{
James Smart3772a992009-05-22 14:50:54 -04003428 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3429}
3430
3431/**
3432 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3433 * @phba: Pointer to HBA context object.
3434 * @pring: Pointer to driver SLI ring object.
3435 * @mask: Host attention register mask for this ring.
3436 *
3437 * This function is called from the worker thread when there is a ring event
3438 * for non-fcp rings. The caller does not hold any lock. The function will
3439 * remove each response iocb in the response ring and calls the handle
3440 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3441 **/
3442static void
3443lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3444 struct lpfc_sli_ring *pring, uint32_t mask)
3445{
James Smart34b02dc2008-08-24 21:49:55 -04003446 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003447 IOCB_t *entry;
3448 IOCB_t *irsp = NULL;
3449 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003450 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003451 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003452 uint32_t status;
dea31012005-04-17 16:05:31 -05003453
James Smart34b02dc2008-08-24 21:49:55 -04003454 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003455 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003456 pring->stats.iocb_event++;
3457
dea31012005-04-17 16:05:31 -05003458 /*
3459 * The next available response entry should never exceed the maximum
3460 * entries. If it does, treat it as an adapter hardware error.
3461 */
James Smart7e56aa22012-08-03 12:35:34 -04003462 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003463 portRspPut = le32_to_cpu(pgp->rspPutInx);
3464 if (portRspPut >= portRspMax) {
3465 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003466 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003467 * rsp ring <portRspMax>
3468 */
James Smarted957682007-06-17 19:56:37 -05003469 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003470 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003471 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003472 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003473
James Smart2e0fef82007-06-17 19:56:36 -05003474 phba->link_state = LPFC_HBA_ERROR;
3475 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003476
3477 phba->work_hs = HS_FFER3;
3478 lpfc_handle_eratt(phba);
3479
James Smart3772a992009-05-22 14:50:54 -04003480 return;
dea31012005-04-17 16:05:31 -05003481 }
3482
3483 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003484 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003485 /*
3486 * Build a completion list and call the appropriate handler.
3487 * The process is to get the next available response iocb, get
3488 * a free iocb from the list, copy the response data into the
3489 * free iocb, insert to the continuation list, and update the
3490 * next response index to slim. This process makes response
3491 * iocb's in the ring available to DMA as fast as possible but
3492 * pays a penalty for a copy operation. Since the iocb is
3493 * only 32 bytes, this penalty is considered small relative to
3494 * the PCI reads for register values and a slim write. When
3495 * the ulpLe field is set, the entire Command has been
3496 * received.
3497 */
James Smarted957682007-06-17 19:56:37 -05003498 entry = lpfc_resp_iocb(phba, pring);
3499
James Smart858c9f62007-06-17 19:56:39 -05003500 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003501 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003502 if (rspiocbp == NULL) {
3503 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003504 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003505 break;
3506 }
3507
James Smarted957682007-06-17 19:56:37 -05003508 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3509 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003510 irsp = &rspiocbp->iocb;
3511
James Smart7e56aa22012-08-03 12:35:34 -04003512 if (++pring->sli.sli3.rspidx >= portRspMax)
3513 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003514
James Smarta58cbd52007-08-02 11:09:43 -04003515 if (pring->ringno == LPFC_ELS_RING) {
3516 lpfc_debugfs_slow_ring_trc(phba,
3517 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3518 *(((uint32_t *) irsp) + 4),
3519 *(((uint32_t *) irsp) + 6),
3520 *(((uint32_t *) irsp) + 7));
3521 }
3522
James Smart7e56aa22012-08-03 12:35:34 -04003523 writel(pring->sli.sli3.rspidx,
3524 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003525
James Smart3772a992009-05-22 14:50:54 -04003526 spin_unlock_irqrestore(&phba->hbalock, iflag);
3527 /* Handle the response IOCB */
3528 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3529 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003530
3531 /*
3532 * If the port response put pointer has not been updated, sync
3533 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3534 * response put pointer.
3535 */
James Smart7e56aa22012-08-03 12:35:34 -04003536 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003537 portRspPut = le32_to_cpu(pgp->rspPutInx);
3538 }
James Smart7e56aa22012-08-03 12:35:34 -04003539 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003540
James Smart92d7f7b2007-06-17 19:56:38 -05003541 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003542 /* At least one response entry has been freed */
3543 pring->stats.iocb_rsp_full++;
3544 /* SET RxRE_RSP in Chip Att register */
3545 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3546 writel(status, phba->CAregaddr);
3547 readl(phba->CAregaddr); /* flush */
3548 }
3549 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3550 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3551 pring->stats.iocb_cmd_empty++;
3552
3553 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003554 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003555 lpfc_sli_resume_iocb(phba, pring);
3556
3557 if ((pring->lpfc_sli_cmd_available))
3558 (pring->lpfc_sli_cmd_available) (phba, pring);
3559
3560 }
3561
James Smart2e0fef82007-06-17 19:56:36 -05003562 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003563 return;
dea31012005-04-17 16:05:31 -05003564}
3565
James Smarte59058c2008-08-24 21:49:00 -04003566/**
James Smart4f774512009-05-22 14:52:35 -04003567 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3568 * @phba: Pointer to HBA context object.
3569 * @pring: Pointer to driver SLI ring object.
3570 * @mask: Host attention register mask for this ring.
3571 *
3572 * This function is called from the worker thread when there is a pending
3573 * ELS response iocb on the driver internal slow-path response iocb worker
3574 * queue. The caller does not hold any lock. The function will remove each
3575 * response iocb from the response worker queue and calls the handle
3576 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3577 **/
3578static void
3579lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3580 struct lpfc_sli_ring *pring, uint32_t mask)
3581{
3582 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003583 struct hbq_dmabuf *dmabuf;
3584 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003585 unsigned long iflag;
3586
James Smart45ed1192009-10-02 15:17:02 -04003587 spin_lock_irqsave(&phba->hbalock, iflag);
3588 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3589 spin_unlock_irqrestore(&phba->hbalock, iflag);
3590 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003591 /* Get the response iocb from the head of work queue */
3592 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003593 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003594 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003595 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003596
3597 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3598 case CQE_CODE_COMPL_WQE:
3599 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3600 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003601 /* Translate ELS WCQE to response IOCBQ */
3602 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3603 irspiocbq);
3604 if (irspiocbq)
3605 lpfc_sli_sp_handle_rspiocb(phba, pring,
3606 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003607 break;
3608 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003609 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003610 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3611 cq_event);
3612 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3613 break;
3614 default:
3615 break;
3616 }
James Smart4f774512009-05-22 14:52:35 -04003617 }
3618}
3619
3620/**
James Smart3621a712009-04-06 18:47:14 -04003621 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003622 * @phba: Pointer to HBA context object.
3623 * @pring: Pointer to driver SLI ring object.
3624 *
3625 * This function aborts all iocbs in the given ring and frees all the iocb
3626 * objects in txq. This function issues an abort iocb for all the iocb commands
3627 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3628 * the return of this function. The caller is not required to hold any locks.
3629 **/
James Smart2e0fef82007-06-17 19:56:36 -05003630void
dea31012005-04-17 16:05:31 -05003631lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3632{
James Smart2534ba72007-04-25 09:52:20 -04003633 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003634 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003635
James Smart92d7f7b2007-06-17 19:56:38 -05003636 if (pring->ringno == LPFC_ELS_RING) {
3637 lpfc_fabric_abort_hba(phba);
3638 }
3639
dea31012005-04-17 16:05:31 -05003640 /* Error everything on txq and txcmplq
3641 * First do the txq.
3642 */
James Smartdb55fba2014-04-04 13:52:02 -04003643 if (phba->sli_rev >= LPFC_SLI_REV4) {
3644 spin_lock_irq(&pring->ring_lock);
3645 list_splice_init(&pring->txq, &completions);
3646 pring->txq_cnt = 0;
3647 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003648
James Smartdb55fba2014-04-04 13:52:02 -04003649 spin_lock_irq(&phba->hbalock);
3650 /* Next issue ABTS for everything on the txcmplq */
3651 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3652 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3653 spin_unlock_irq(&phba->hbalock);
3654 } else {
3655 spin_lock_irq(&phba->hbalock);
3656 list_splice_init(&pring->txq, &completions);
3657 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003658
James Smartdb55fba2014-04-04 13:52:02 -04003659 /* Next issue ABTS for everything on the txcmplq */
3660 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3661 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3662 spin_unlock_irq(&phba->hbalock);
3663 }
James Smart2534ba72007-04-25 09:52:20 -04003664
James Smarta257bf92009-04-06 18:48:10 -04003665 /* Cancel all the IOCBs from the completions list */
3666 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3667 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003668}
3669
James Smarte59058c2008-08-24 21:49:00 -04003670/**
James Smart895427b2017-02-12 13:52:30 -08003671 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3672 * @phba: Pointer to HBA context object.
3673 * @pring: Pointer to driver SLI ring object.
3674 *
3675 * This function aborts all iocbs in the given ring and frees all the iocb
3676 * objects in txq. This function issues an abort iocb for all the iocb commands
3677 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3678 * the return of this function. The caller is not required to hold any locks.
3679 **/
3680void
3681lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3682{
3683 LIST_HEAD(completions);
3684 struct lpfc_iocbq *iocb, *next_iocb;
3685
3686 if (pring->ringno == LPFC_ELS_RING)
3687 lpfc_fabric_abort_hba(phba);
3688
3689 spin_lock_irq(&phba->hbalock);
3690 /* Next issue ABTS for everything on the txcmplq */
3691 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3692 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3693 spin_unlock_irq(&phba->hbalock);
3694}
3695
3696
3697/**
James Smartdb55fba2014-04-04 13:52:02 -04003698 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3699 * @phba: Pointer to HBA context object.
3700 * @pring: Pointer to driver SLI ring object.
3701 *
3702 * This function aborts all iocbs in FCP rings and frees all the iocb
3703 * objects in txq. This function issues an abort iocb for all the iocb commands
3704 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3705 * the return of this function. The caller is not required to hold any locks.
3706 **/
3707void
3708lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3709{
3710 struct lpfc_sli *psli = &phba->sli;
3711 struct lpfc_sli_ring *pring;
3712 uint32_t i;
3713
3714 /* Look on all the FCP Rings for the iotag */
3715 if (phba->sli_rev >= LPFC_SLI_REV4) {
3716 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003717 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003718 lpfc_sli_abort_iocb_ring(phba, pring);
3719 }
3720 } else {
James Smart895427b2017-02-12 13:52:30 -08003721 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003722 lpfc_sli_abort_iocb_ring(phba, pring);
3723 }
3724}
3725
James Smart895427b2017-02-12 13:52:30 -08003726/**
3727 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3728 * @phba: Pointer to HBA context object.
3729 *
3730 * This function aborts all wqes in NVME rings. This function issues an
3731 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3732 * the txcmplq is not guaranteed to complete before the return of this
3733 * function. The caller is not required to hold any locks.
3734 **/
3735void
3736lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3737{
3738 struct lpfc_sli_ring *pring;
3739 uint32_t i;
3740
3741 if (phba->sli_rev < LPFC_SLI_REV4)
3742 return;
3743
3744 /* Abort all IO on each NVME ring. */
3745 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3746 pring = phba->sli4_hba.nvme_wq[i]->pring;
3747 lpfc_sli_abort_wqe_ring(phba, pring);
3748 }
3749}
3750
James Smartdb55fba2014-04-04 13:52:02 -04003751
3752/**
James Smart3621a712009-04-06 18:47:14 -04003753 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003754 * @phba: Pointer to HBA context object.
3755 *
3756 * This function flushes all iocbs in the fcp ring and frees all the iocb
3757 * objects in txq and txcmplq. This function will not issue abort iocbs
3758 * for all the iocb commands in txcmplq, they will just be returned with
3759 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3760 * slot has been permanently disabled.
3761 **/
3762void
3763lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3764{
3765 LIST_HEAD(txq);
3766 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003767 struct lpfc_sli *psli = &phba->sli;
3768 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003769 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003770
3771 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003772 /* Indicate the I/O queues are flushed */
3773 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003774 spin_unlock_irq(&phba->hbalock);
3775
James Smartdb55fba2014-04-04 13:52:02 -04003776 /* Look on all the FCP Rings for the iotag */
3777 if (phba->sli_rev >= LPFC_SLI_REV4) {
3778 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003779 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003780
James Smartdb55fba2014-04-04 13:52:02 -04003781 spin_lock_irq(&pring->ring_lock);
3782 /* Retrieve everything on txq */
3783 list_splice_init(&pring->txq, &txq);
3784 /* Retrieve everything on the txcmplq */
3785 list_splice_init(&pring->txcmplq, &txcmplq);
3786 pring->txq_cnt = 0;
3787 pring->txcmplq_cnt = 0;
3788 spin_unlock_irq(&pring->ring_lock);
3789
3790 /* Flush the txq */
3791 lpfc_sli_cancel_iocbs(phba, &txq,
3792 IOSTAT_LOCAL_REJECT,
3793 IOERR_SLI_DOWN);
3794 /* Flush the txcmpq */
3795 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3796 IOSTAT_LOCAL_REJECT,
3797 IOERR_SLI_DOWN);
3798 }
3799 } else {
James Smart895427b2017-02-12 13:52:30 -08003800 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003801
3802 spin_lock_irq(&phba->hbalock);
3803 /* Retrieve everything on txq */
3804 list_splice_init(&pring->txq, &txq);
3805 /* Retrieve everything on the txcmplq */
3806 list_splice_init(&pring->txcmplq, &txcmplq);
3807 pring->txq_cnt = 0;
3808 pring->txcmplq_cnt = 0;
3809 spin_unlock_irq(&phba->hbalock);
3810
3811 /* Flush the txq */
3812 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3813 IOERR_SLI_DOWN);
3814 /* Flush the txcmpq */
3815 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3816 IOERR_SLI_DOWN);
3817 }
James Smarta8e497d2008-08-24 21:50:11 -04003818}
3819
3820/**
James Smart895427b2017-02-12 13:52:30 -08003821 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3822 * @phba: Pointer to HBA context object.
3823 *
3824 * This function flushes all wqes in the nvme rings and frees all resources
3825 * in the txcmplq. This function does not issue abort wqes for the IO
3826 * commands in txcmplq, they will just be returned with
3827 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3828 * slot has been permanently disabled.
3829 **/
3830void
3831lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3832{
3833 LIST_HEAD(txcmplq);
3834 struct lpfc_sli_ring *pring;
3835 uint32_t i;
3836
3837 if (phba->sli_rev < LPFC_SLI_REV4)
3838 return;
3839
3840 /* Hint to other driver operations that a flush is in progress. */
3841 spin_lock_irq(&phba->hbalock);
3842 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3843 spin_unlock_irq(&phba->hbalock);
3844
3845 /* Cycle through all NVME rings and complete each IO with
3846 * a local driver reason code. This is a flush so no
3847 * abort exchange to FW.
3848 */
3849 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3850 pring = phba->sli4_hba.nvme_wq[i]->pring;
3851
3852 /* Retrieve everything on the txcmplq */
3853 spin_lock_irq(&pring->ring_lock);
3854 list_splice_init(&pring->txcmplq, &txcmplq);
3855 pring->txcmplq_cnt = 0;
3856 spin_unlock_irq(&pring->ring_lock);
3857
3858 /* Flush the txcmpq &&&PAE */
3859 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3860 IOSTAT_LOCAL_REJECT,
3861 IOERR_SLI_DOWN);
3862 }
3863}
3864
3865/**
James Smart3772a992009-05-22 14:50:54 -04003866 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003867 * @phba: Pointer to HBA context object.
3868 * @mask: Bit mask to be checked.
3869 *
3870 * This function reads the host status register and compares
3871 * with the provided bit mask to check if HBA completed
3872 * the restart. This function will wait in a loop for the
3873 * HBA to complete restart. If the HBA does not restart within
3874 * 15 iterations, the function will reset the HBA again. The
3875 * function returns 1 when HBA fail to restart otherwise returns
3876 * zero.
3877 **/
James Smart3772a992009-05-22 14:50:54 -04003878static int
3879lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003880{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003881 uint32_t status;
3882 int i = 0;
3883 int retval = 0;
dea31012005-04-17 16:05:31 -05003884
Jamie Wellnitz41415862006-02-28 19:25:27 -05003885 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003886 if (lpfc_readl(phba->HSregaddr, &status))
3887 return 1;
dea31012005-04-17 16:05:31 -05003888
Jamie Wellnitz41415862006-02-28 19:25:27 -05003889 /*
3890 * Check status register every 100ms for 5 retries, then every
3891 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3892 * every 2.5 sec for 4.
3893 * Break our of the loop if errors occurred during init.
3894 */
3895 while (((status & mask) != mask) &&
3896 !(status & HS_FFERM) &&
3897 i++ < 20) {
dea31012005-04-17 16:05:31 -05003898
Jamie Wellnitz41415862006-02-28 19:25:27 -05003899 if (i <= 5)
3900 msleep(10);
3901 else if (i <= 10)
3902 msleep(500);
3903 else
3904 msleep(2500);
dea31012005-04-17 16:05:31 -05003905
Jamie Wellnitz41415862006-02-28 19:25:27 -05003906 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003907 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003908 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003909 lpfc_sli_brdrestart(phba);
3910 }
3911 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003912 if (lpfc_readl(phba->HSregaddr, &status)) {
3913 retval = 1;
3914 break;
3915 }
dea31012005-04-17 16:05:31 -05003916 }
dea31012005-04-17 16:05:31 -05003917
Jamie Wellnitz41415862006-02-28 19:25:27 -05003918 /* Check to see if any errors occurred during init */
3919 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003920 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3921 "2751 Adapter failed to restart, "
3922 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3923 status,
3924 readl(phba->MBslimaddr + 0xa8),
3925 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003926 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003927 retval = 1;
3928 }
dea31012005-04-17 16:05:31 -05003929
Jamie Wellnitz41415862006-02-28 19:25:27 -05003930 return retval;
dea31012005-04-17 16:05:31 -05003931}
3932
James Smartda0436e2009-05-22 14:51:39 -04003933/**
3934 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3935 * @phba: Pointer to HBA context object.
3936 * @mask: Bit mask to be checked.
3937 *
3938 * This function checks the host status register to check if HBA is
3939 * ready. This function will wait in a loop for the HBA to be ready
3940 * If the HBA is not ready , the function will will reset the HBA PCI
3941 * function again. The function returns 1 when HBA fail to be ready
3942 * otherwise returns zero.
3943 **/
3944static int
3945lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3946{
3947 uint32_t status;
3948 int retval = 0;
3949
3950 /* Read the HBA Host Status Register */
3951 status = lpfc_sli4_post_status_check(phba);
3952
3953 if (status) {
3954 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3955 lpfc_sli_brdrestart(phba);
3956 status = lpfc_sli4_post_status_check(phba);
3957 }
3958
3959 /* Check to see if any errors occurred during init */
3960 if (status) {
3961 phba->link_state = LPFC_HBA_ERROR;
3962 retval = 1;
3963 } else
3964 phba->sli4_hba.intr_enable = 0;
3965
3966 return retval;
3967}
3968
3969/**
3970 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3971 * @phba: Pointer to HBA context object.
3972 * @mask: Bit mask to be checked.
3973 *
3974 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3975 * from the API jump table function pointer from the lpfc_hba struct.
3976 **/
3977int
3978lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3979{
3980 return phba->lpfc_sli_brdready(phba, mask);
3981}
3982
James Smart92908312006-03-07 15:04:13 -05003983#define BARRIER_TEST_PATTERN (0xdeadbeef)
3984
James Smarte59058c2008-08-24 21:49:00 -04003985/**
James Smart3621a712009-04-06 18:47:14 -04003986 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003987 * @phba: Pointer to HBA context object.
3988 *
James Smart1b511972011-12-13 13:23:09 -05003989 * This function is called before resetting an HBA. This function is called
3990 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003991 **/
James Smart2e0fef82007-06-17 19:56:36 -05003992void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003993{
James Smart65a29c12006-07-06 15:50:50 -04003994 uint32_t __iomem *resp_buf;
3995 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003996 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003997 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003998 int i;
3999 uint8_t hdrtype;
4000
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004001 lockdep_assert_held(&phba->hbalock);
4002
James Smart92908312006-03-07 15:04:13 -05004003 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4004 if (hdrtype != 0x80 ||
4005 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4006 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4007 return;
4008
4009 /*
4010 * Tell the other part of the chip to suspend temporarily all
4011 * its DMA activity.
4012 */
James Smart65a29c12006-07-06 15:50:50 -04004013 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004014
4015 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004016 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4017 return;
James Smart92908312006-03-07 15:04:13 -05004018 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4019 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004020 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004021
James Smart9940b972011-03-11 16:06:12 -05004022 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4023 return;
4024 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004025 /* Clear Chip error bit */
4026 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004027 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004028 }
4029
4030 mbox = 0;
4031 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4032 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4033
4034 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004035 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004036 writel(mbox, mbox_buf);
4037
James Smart9940b972011-03-11 16:06:12 -05004038 for (i = 0; i < 50; i++) {
4039 if (lpfc_readl((resp_buf + 1), &resp_data))
4040 return;
4041 if (resp_data != ~(BARRIER_TEST_PATTERN))
4042 mdelay(1);
4043 else
4044 break;
4045 }
4046 resp_data = 0;
4047 if (lpfc_readl((resp_buf + 1), &resp_data))
4048 return;
4049 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004050 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004051 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004052 goto restore_hc;
4053 else
4054 goto clear_errat;
4055 }
4056
4057 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004058 resp_data = 0;
4059 for (i = 0; i < 500; i++) {
4060 if (lpfc_readl(resp_buf, &resp_data))
4061 return;
4062 if (resp_data != mbox)
4063 mdelay(1);
4064 else
4065 break;
4066 }
James Smart92908312006-03-07 15:04:13 -05004067
4068clear_errat:
4069
James Smart9940b972011-03-11 16:06:12 -05004070 while (++i < 500) {
4071 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4072 return;
4073 if (!(ha_copy & HA_ERATT))
4074 mdelay(1);
4075 else
4076 break;
4077 }
James Smart92908312006-03-07 15:04:13 -05004078
4079 if (readl(phba->HAregaddr) & HA_ERATT) {
4080 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004081 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004082 }
4083
4084restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004085 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004086 writel(hc_copy, phba->HCregaddr);
4087 readl(phba->HCregaddr); /* flush */
4088}
4089
James Smarte59058c2008-08-24 21:49:00 -04004090/**
James Smart3621a712009-04-06 18:47:14 -04004091 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004092 * @phba: Pointer to HBA context object.
4093 *
4094 * This function issues a kill_board mailbox command and waits for
4095 * the error attention interrupt. This function is called for stopping
4096 * the firmware processing. The caller is not required to hold any
4097 * locks. This function calls lpfc_hba_down_post function to free
4098 * any pending commands after the kill. The function will return 1 when it
4099 * fails to kill the board else will return 0.
4100 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004101int
James Smart2e0fef82007-06-17 19:56:36 -05004102lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004103{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004104 struct lpfc_sli *psli;
4105 LPFC_MBOXQ_t *pmb;
4106 uint32_t status;
4107 uint32_t ha_copy;
4108 int retval;
4109 int i = 0;
4110
4111 psli = &phba->sli;
4112
4113 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004114 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004115 "0329 Kill HBA Data: x%x x%x\n",
4116 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004117
James Smart98c9ea52007-10-27 13:37:33 -04004118 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4119 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004120 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004121
4122 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004123 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004124 if (lpfc_readl(phba->HCregaddr, &status)) {
4125 spin_unlock_irq(&phba->hbalock);
4126 mempool_free(pmb, phba->mbox_mem_pool);
4127 return 1;
4128 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004129 status &= ~HC_ERINT_ENA;
4130 writel(status, phba->HCregaddr);
4131 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004132 phba->link_flag |= LS_IGNORE_ERATT;
4133 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004134
4135 lpfc_kill_board(phba, pmb);
4136 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4137 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4138
4139 if (retval != MBX_SUCCESS) {
4140 if (retval != MBX_BUSY)
4141 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004142 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4143 "2752 KILL_BOARD command failed retval %d\n",
4144 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004145 spin_lock_irq(&phba->hbalock);
4146 phba->link_flag &= ~LS_IGNORE_ERATT;
4147 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004148 return 1;
4149 }
4150
James Smartf4b4c682009-05-22 14:53:12 -04004151 spin_lock_irq(&phba->hbalock);
4152 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4153 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004154
Jamie Wellnitz41415862006-02-28 19:25:27 -05004155 mempool_free(pmb, phba->mbox_mem_pool);
4156
4157 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4158 * attention every 100ms for 3 seconds. If we don't get ERATT after
4159 * 3 seconds we still set HBA_ERROR state because the status of the
4160 * board is now undefined.
4161 */
James Smart9940b972011-03-11 16:06:12 -05004162 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4163 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004164 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4165 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004166 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4167 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004168 }
4169
4170 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004171 if (ha_copy & HA_ERATT) {
4172 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004173 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004174 }
James Smart2e0fef82007-06-17 19:56:36 -05004175 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004176 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004177 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004178 phba->link_flag &= ~LS_IGNORE_ERATT;
4179 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004180
Jamie Wellnitz41415862006-02-28 19:25:27 -05004181 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004182 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004183
James Smart2e0fef82007-06-17 19:56:36 -05004184 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004185}
4186
James Smarte59058c2008-08-24 21:49:00 -04004187/**
James Smart3772a992009-05-22 14:50:54 -04004188 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004189 * @phba: Pointer to HBA context object.
4190 *
4191 * This function resets the HBA by writing HC_INITFF to the control
4192 * register. After the HBA resets, this function resets all the iocb ring
4193 * indices. This function disables PCI layer parity checking during
4194 * the reset.
4195 * This function returns 0 always.
4196 * The caller is not required to hold any locks.
4197 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004198int
James Smart2e0fef82007-06-17 19:56:36 -05004199lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004200{
4201 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004202 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004203 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004204 int i;
dea31012005-04-17 16:05:31 -05004205
Jamie Wellnitz41415862006-02-28 19:25:27 -05004206 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004207
Jamie Wellnitz41415862006-02-28 19:25:27 -05004208 /* Reset HBA */
4209 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004210 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004211 (phba->pport) ? phba->pport->port_state : 0,
4212 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004213
4214 /* perform board reset */
4215 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004216 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004217 if (phba->pport) {
4218 phba->pport->fc_myDID = 0;
4219 phba->pport->fc_prevDID = 0;
4220 }
dea31012005-04-17 16:05:31 -05004221
Jamie Wellnitz41415862006-02-28 19:25:27 -05004222 /* Turn off parity checking and serr during the physical reset */
4223 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4224 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4225 (cfg_value &
4226 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4227
James Smart3772a992009-05-22 14:50:54 -04004228 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4229
Jamie Wellnitz41415862006-02-28 19:25:27 -05004230 /* Now toggle INITFF bit in the Host Control Register */
4231 writel(HC_INITFF, phba->HCregaddr);
4232 mdelay(1);
4233 readl(phba->HCregaddr); /* flush */
4234 writel(0, phba->HCregaddr);
4235 readl(phba->HCregaddr); /* flush */
4236
4237 /* Restore PCI cmd register */
4238 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004239
4240 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004241 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004242 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004243 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004244 pring->sli.sli3.rspidx = 0;
4245 pring->sli.sli3.next_cmdidx = 0;
4246 pring->sli.sli3.local_getidx = 0;
4247 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004248 pring->missbufcnt = 0;
4249 }
dea31012005-04-17 16:05:31 -05004250
James Smart2e0fef82007-06-17 19:56:36 -05004251 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004252 return 0;
4253}
4254
James Smarte59058c2008-08-24 21:49:00 -04004255/**
James Smartda0436e2009-05-22 14:51:39 -04004256 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4257 * @phba: Pointer to HBA context object.
4258 *
4259 * This function resets a SLI4 HBA. This function disables PCI layer parity
4260 * checking during resets the device. The caller is not required to hold
4261 * any locks.
4262 *
4263 * This function returns 0 always.
4264 **/
4265int
4266lpfc_sli4_brdreset(struct lpfc_hba *phba)
4267{
4268 struct lpfc_sli *psli = &phba->sli;
4269 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004270 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004271
4272 /* Reset HBA */
4273 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004274 "0295 Reset HBA Data: x%x x%x x%x\n",
4275 phba->pport->port_state, psli->sli_flag,
4276 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004277
4278 /* perform board reset */
4279 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004280 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004281 phba->pport->fc_myDID = 0;
4282 phba->pport->fc_prevDID = 0;
4283
James Smartda0436e2009-05-22 14:51:39 -04004284 spin_lock_irq(&phba->hbalock);
4285 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4286 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004287 spin_unlock_irq(&phba->hbalock);
4288
James Smart02936352014-04-04 13:52:12 -04004289 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4290 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4291 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4292 return rc;
4293 }
4294
James Smartda0436e2009-05-22 14:51:39 -04004295 /* Now physically reset the device */
4296 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4297 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004298
4299 /* Turn off parity checking and serr during the physical reset */
4300 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4301 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4302 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4303
James Smart88318812012-09-29 11:29:29 -04004304 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004305 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004306 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004307
James Smartbe858b62010-12-15 17:57:20 -05004308 /* Restore PCI cmd register */
4309 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4310
James Smart27b01b82012-05-09 21:19:44 -04004311 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004312}
4313
4314/**
4315 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004316 * @phba: Pointer to HBA context object.
4317 *
4318 * This function is called in the SLI initialization code path to
4319 * restart the HBA. The caller is not required to hold any lock.
4320 * This function writes MBX_RESTART mailbox command to the SLIM and
4321 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4322 * function to free any pending commands. The function enables
4323 * POST only during the first initialization. The function returns zero.
4324 * The function does not guarantee completion of MBX_RESTART mailbox
4325 * command before the return of this function.
4326 **/
James Smartda0436e2009-05-22 14:51:39 -04004327static int
4328lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004329{
4330 MAILBOX_t *mb;
4331 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004332 volatile uint32_t word0;
4333 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004334 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004335
James Smart2e0fef82007-06-17 19:56:36 -05004336 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004337
James Smart0d878412009-10-02 15:16:56 -04004338 /* Take PCIe device Advanced Error Reporting (AER) state */
4339 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4340
Jamie Wellnitz41415862006-02-28 19:25:27 -05004341 psli = &phba->sli;
4342
4343 /* Restart HBA */
4344 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004345 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004346 (phba->pport) ? phba->pport->port_state : 0,
4347 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004348
4349 word0 = 0;
4350 mb = (MAILBOX_t *) &word0;
4351 mb->mbxCommand = MBX_RESTART;
4352 mb->mbxHc = 1;
4353
James Smart92908312006-03-07 15:04:13 -05004354 lpfc_reset_barrier(phba);
4355
Jamie Wellnitz41415862006-02-28 19:25:27 -05004356 to_slim = phba->MBslimaddr;
4357 writel(*(uint32_t *) mb, to_slim);
4358 readl(to_slim); /* flush */
4359
4360 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004361 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004362 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004363 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004364 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004365 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004366 writel(*(uint32_t *) mb, to_slim);
4367 readl(to_slim); /* flush */
4368
4369 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004370 if (phba->pport)
4371 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004372 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004373 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004374 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004375
James Smart64ba8812006-08-02 15:24:34 -04004376 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4377 psli->stats_start = get_seconds();
4378
James Smarteaf15d52008-12-04 22:39:29 -05004379 /* Give the INITFF and Post time to settle. */
4380 mdelay(100);
dea31012005-04-17 16:05:31 -05004381
James Smart0d878412009-10-02 15:16:56 -04004382 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4383 if (hba_aer_enabled)
4384 pci_disable_pcie_error_reporting(phba->pcidev);
4385
Jamie Wellnitz41415862006-02-28 19:25:27 -05004386 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004387
4388 return 0;
4389}
4390
James Smarte59058c2008-08-24 21:49:00 -04004391/**
James Smartda0436e2009-05-22 14:51:39 -04004392 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4393 * @phba: Pointer to HBA context object.
4394 *
4395 * This function is called in the SLI initialization code path to restart
4396 * a SLI4 HBA. The caller is not required to hold any lock.
4397 * At the end of the function, it calls lpfc_hba_down_post function to
4398 * free any pending commands.
4399 **/
4400static int
4401lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4402{
4403 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004404 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004405 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004406
4407 /* Restart HBA */
4408 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4409 "0296 Restart HBA Data: x%x x%x\n",
4410 phba->pport->port_state, psli->sli_flag);
4411
James Smart75baf692010-06-08 18:31:21 -04004412 /* Take PCIe device Advanced Error Reporting (AER) state */
4413 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4414
James Smart27b01b82012-05-09 21:19:44 -04004415 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004416
4417 spin_lock_irq(&phba->hbalock);
4418 phba->pport->stopped = 0;
4419 phba->link_state = LPFC_INIT_START;
4420 phba->hba_flag = 0;
4421 spin_unlock_irq(&phba->hbalock);
4422
4423 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4424 psli->stats_start = get_seconds();
4425
James Smart75baf692010-06-08 18:31:21 -04004426 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4427 if (hba_aer_enabled)
4428 pci_disable_pcie_error_reporting(phba->pcidev);
4429
James Smartda0436e2009-05-22 14:51:39 -04004430 lpfc_hba_down_post(phba);
4431
James Smart27b01b82012-05-09 21:19:44 -04004432 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004433}
4434
4435/**
4436 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4437 * @phba: Pointer to HBA context object.
4438 *
4439 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4440 * API jump table function pointer from the lpfc_hba struct.
4441**/
4442int
4443lpfc_sli_brdrestart(struct lpfc_hba *phba)
4444{
4445 return phba->lpfc_sli_brdrestart(phba);
4446}
4447
4448/**
James Smart3621a712009-04-06 18:47:14 -04004449 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004450 * @phba: Pointer to HBA context object.
4451 *
4452 * This function is called after a HBA restart to wait for successful
4453 * restart of the HBA. Successful restart of the HBA is indicated by
4454 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4455 * iteration, the function will restart the HBA again. The function returns
4456 * zero if HBA successfully restarted else returns negative error code.
4457 **/
James Smart4492b732017-04-27 15:08:26 -07004458int
dea31012005-04-17 16:05:31 -05004459lpfc_sli_chipset_init(struct lpfc_hba *phba)
4460{
4461 uint32_t status, i = 0;
4462
4463 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004464 if (lpfc_readl(phba->HSregaddr, &status))
4465 return -EIO;
dea31012005-04-17 16:05:31 -05004466
4467 /* Check status register to see what current state is */
4468 i = 0;
4469 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4470
James Smartdcf2a4e2010-09-29 11:18:53 -04004471 /* Check every 10ms for 10 retries, then every 100ms for 90
4472 * retries, then every 1 sec for 50 retires for a total of
4473 * ~60 seconds before reset the board again and check every
4474 * 1 sec for 50 retries. The up to 60 seconds before the
4475 * board ready is required by the Falcon FIPS zeroization
4476 * complete, and any reset the board in between shall cause
4477 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004478 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004479 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004480 /* Adapter failed to init, timeout, status reg
4481 <status> */
James Smarted957682007-06-17 19:56:37 -05004482 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004483 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004484 "timeout, status reg x%x, "
4485 "FW Data: A8 x%x AC x%x\n", status,
4486 readl(phba->MBslimaddr + 0xa8),
4487 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004488 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004489 return -ETIMEDOUT;
4490 }
4491
4492 /* Check to see if any errors occurred during init */
4493 if (status & HS_FFERM) {
4494 /* ERROR: During chipset initialization */
4495 /* Adapter failed to init, chipset, status reg
4496 <status> */
James Smarted957682007-06-17 19:56:37 -05004497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004498 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004499 "chipset, status reg x%x, "
4500 "FW Data: A8 x%x AC x%x\n", status,
4501 readl(phba->MBslimaddr + 0xa8),
4502 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004503 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004504 return -EIO;
4505 }
4506
James Smartdcf2a4e2010-09-29 11:18:53 -04004507 if (i <= 10)
dea31012005-04-17 16:05:31 -05004508 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004509 else if (i <= 100)
4510 msleep(100);
4511 else
4512 msleep(1000);
dea31012005-04-17 16:05:31 -05004513
James Smartdcf2a4e2010-09-29 11:18:53 -04004514 if (i == 150) {
4515 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004516 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004517 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004518 }
4519 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004520 if (lpfc_readl(phba->HSregaddr, &status))
4521 return -EIO;
dea31012005-04-17 16:05:31 -05004522 }
4523
4524 /* Check to see if any errors occurred during init */
4525 if (status & HS_FFERM) {
4526 /* ERROR: During chipset initialization */
4527 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004528 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004529 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004530 "status reg x%x, "
4531 "FW Data: A8 x%x AC x%x\n", status,
4532 readl(phba->MBslimaddr + 0xa8),
4533 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004534 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004535 return -EIO;
4536 }
4537
4538 /* Clear all interrupt enable conditions */
4539 writel(0, phba->HCregaddr);
4540 readl(phba->HCregaddr); /* flush */
4541
4542 /* setup host attn register */
4543 writel(0xffffffff, phba->HAregaddr);
4544 readl(phba->HAregaddr); /* flush */
4545 return 0;
4546}
4547
James Smarte59058c2008-08-24 21:49:00 -04004548/**
James Smart3621a712009-04-06 18:47:14 -04004549 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004550 *
4551 * This function calculates and returns the number of HBQs required to be
4552 * configured.
4553 **/
James Smart78b2d852007-08-02 11:10:21 -04004554int
James Smarted957682007-06-17 19:56:37 -05004555lpfc_sli_hbq_count(void)
4556{
James Smart92d7f7b2007-06-17 19:56:38 -05004557 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004558}
4559
James Smarte59058c2008-08-24 21:49:00 -04004560/**
James Smart3621a712009-04-06 18:47:14 -04004561 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004562 *
4563 * This function adds the number of hbq entries in every HBQ to get
4564 * the total number of hbq entries required for the HBA and returns
4565 * the total count.
4566 **/
James Smarted957682007-06-17 19:56:37 -05004567static int
4568lpfc_sli_hbq_entry_count(void)
4569{
4570 int hbq_count = lpfc_sli_hbq_count();
4571 int count = 0;
4572 int i;
4573
4574 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004575 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004576 return count;
4577}
4578
James Smarte59058c2008-08-24 21:49:00 -04004579/**
James Smart3621a712009-04-06 18:47:14 -04004580 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004581 *
4582 * This function calculates amount of memory required for all hbq entries
4583 * to be configured and returns the total memory required.
4584 **/
dea31012005-04-17 16:05:31 -05004585int
James Smarted957682007-06-17 19:56:37 -05004586lpfc_sli_hbq_size(void)
4587{
4588 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4589}
4590
James Smarte59058c2008-08-24 21:49:00 -04004591/**
James Smart3621a712009-04-06 18:47:14 -04004592 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004593 * @phba: Pointer to HBA context object.
4594 *
4595 * This function is called during the SLI initialization to configure
4596 * all the HBQs and post buffers to the HBQ. The caller is not
4597 * required to hold any locks. This function will return zero if successful
4598 * else it will return negative error code.
4599 **/
James Smarted957682007-06-17 19:56:37 -05004600static int
4601lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4602{
4603 int hbq_count = lpfc_sli_hbq_count();
4604 LPFC_MBOXQ_t *pmb;
4605 MAILBOX_t *pmbox;
4606 uint32_t hbqno;
4607 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004608
James Smart92d7f7b2007-06-17 19:56:38 -05004609 /* Get a Mailbox buffer to setup mailbox
4610 * commands for HBA initialization
4611 */
James Smarted957682007-06-17 19:56:37 -05004612 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4613
4614 if (!pmb)
4615 return -ENOMEM;
4616
James Smart04c68492009-05-22 14:52:52 -04004617 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004618
4619 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4620 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004621 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004622
4623 hbq_entry_index = 0;
4624 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4625 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4626 phba->hbqs[hbqno].hbqPutIdx = 0;
4627 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4628 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004629 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004630 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4631 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004632 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4633
4634 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4635 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4636 mbxStatus <status>, ring <num> */
4637
4638 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004639 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004640 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004641 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004642 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004643 pmbox->mbxStatus, hbqno);
4644
4645 phba->link_state = LPFC_HBA_ERROR;
4646 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004647 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004648 }
4649 }
4650 phba->hbq_count = hbq_count;
4651
James Smarted957682007-06-17 19:56:37 -05004652 mempool_free(pmb, phba->mbox_mem_pool);
4653
James Smart92d7f7b2007-06-17 19:56:38 -05004654 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004655 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4656 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004657 return 0;
4658}
4659
James Smarte59058c2008-08-24 21:49:00 -04004660/**
James Smart4f774512009-05-22 14:52:35 -04004661 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4662 * @phba: Pointer to HBA context object.
4663 *
4664 * This function is called during the SLI initialization to configure
4665 * all the HBQs and post buffers to the HBQ. The caller is not
4666 * required to hold any locks. This function will return zero if successful
4667 * else it will return negative error code.
4668 **/
4669static int
4670lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4671{
4672 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004673 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4674 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004675 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004676 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004677 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004678 return 0;
4679}
4680
4681/**
James Smart3621a712009-04-06 18:47:14 -04004682 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004683 * @phba: Pointer to HBA context object.
4684 * @sli_mode: sli mode - 2/3
4685 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004686 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004687 * to issue config_port mailbox command. This function restarts the
4688 * HBA firmware and issues a config_port mailbox command to configure
4689 * the SLI interface in the sli mode specified by sli_mode
4690 * variable. The caller is not required to hold any locks.
4691 * The function returns 0 if successful, else returns negative error
4692 * code.
4693 **/
James Smart93996272008-08-24 21:50:30 -04004694int
4695lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004696{
4697 LPFC_MBOXQ_t *pmb;
4698 uint32_t resetcount = 0, rc = 0, done = 0;
4699
4700 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4701 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004702 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004703 return -ENOMEM;
4704 }
4705
James Smarted957682007-06-17 19:56:37 -05004706 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004707 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004708 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004709 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004710 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004711 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004712 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004713 rc = lpfc_sli_chipset_init(phba);
4714 if (rc)
4715 break;
4716
James Smart2e0fef82007-06-17 19:56:36 -05004717 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004718 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004719 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004720 resetcount++;
4721
James Smarted957682007-06-17 19:56:37 -05004722 /* Call pre CONFIG_PORT mailbox command initialization. A
4723 * value of 0 means the call was successful. Any other
4724 * nonzero value is a failure, but if ERESTART is returned,
4725 * the driver may reset the HBA and try again.
4726 */
dea31012005-04-17 16:05:31 -05004727 rc = lpfc_config_port_prep(phba);
4728 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004729 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004730 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004731 } else if (rc)
dea31012005-04-17 16:05:31 -05004732 break;
James Smart6d368e52011-05-24 11:44:12 -04004733
James Smart2e0fef82007-06-17 19:56:36 -05004734 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004735 lpfc_config_port(phba, pmb);
4736 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004737 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4738 LPFC_SLI3_HBQ_ENABLED |
4739 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004740 LPFC_SLI3_BG_ENABLED |
4741 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004742 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004744 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004745 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004746 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004747 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004748 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004749 spin_unlock_irq(&phba->hbalock);
4750 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004751 } else {
4752 /* Allow asynchronous mailbox command to go through */
4753 spin_lock_irq(&phba->hbalock);
4754 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4755 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004756 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004757
4758 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4759 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4760 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4761 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004762 }
dea31012005-04-17 16:05:31 -05004763 }
James Smarted957682007-06-17 19:56:37 -05004764 if (!done) {
4765 rc = -EINVAL;
4766 goto do_prep_failed;
4767 }
James Smart04c68492009-05-22 14:52:52 -04004768 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4769 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004770 rc = -ENXIO;
4771 goto do_prep_failed;
4772 }
James Smart04c68492009-05-22 14:52:52 -04004773 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004774 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004775 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4776 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4777 phba->max_vpi : phba->max_vports;
4778
James Smart34b02dc2008-08-24 21:49:55 -04004779 } else
4780 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004781 phba->fips_level = 0;
4782 phba->fips_spec_rev = 0;
4783 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004784 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004785 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4786 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4787 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4788 "2850 Security Crypto Active. FIPS x%d "
4789 "(Spec Rev: x%d)",
4790 phba->fips_level, phba->fips_spec_rev);
4791 }
4792 if (pmb->u.mb.un.varCfgPort.sec_err) {
4793 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4794 "2856 Config Port Security Crypto "
4795 "Error: x%x ",
4796 pmb->u.mb.un.varCfgPort.sec_err);
4797 }
James Smart04c68492009-05-22 14:52:52 -04004798 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004799 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004800 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004801 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004802
4803 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4804 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004805
4806 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004807 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004808 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4809 else
4810 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4811 "0443 Adapter did not grant "
4812 "BlockGuard\n");
4813 }
James Smart34b02dc2008-08-24 21:49:55 -04004814 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004815 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004816 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004817 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004818 }
James Smart92d7f7b2007-06-17 19:56:38 -05004819do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004820 mempool_free(pmb, phba->mbox_mem_pool);
4821 return rc;
4822}
4823
James Smarte59058c2008-08-24 21:49:00 -04004824
4825/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08004826 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04004827 * @phba: Pointer to HBA context object.
4828 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004829 * This function is the main SLI initialization function. This function
4830 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04004831 * error attention handler code. Caller is not required to hold any
4832 * locks. This function issues config_port mailbox command to configure
4833 * the SLI, setup iocb rings and HBQ rings. In the end the function
4834 * calls the config_port_post function to issue init_link mailbox
4835 * command and to start the discovery. The function will return zero
4836 * if successful, else it will return negative error code.
4837 **/
James Smarted957682007-06-17 19:56:37 -05004838int
4839lpfc_sli_hba_setup(struct lpfc_hba *phba)
4840{
4841 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004842 int mode = 3, i;
4843 int longs;
James Smarted957682007-06-17 19:56:37 -05004844
James Smart12247e82016-07-06 12:36:09 -07004845 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004846 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004847 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004849 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004850 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004851 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004852 break;
4853 }
James Smarted957682007-06-17 19:56:37 -05004854 mode = 2;
4855 break;
4856 case 0:
4857 case 3:
4858 break;
4859 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004861 "1819 Unrecognized sli_mode parameter: %d.\n",
4862 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004863
4864 break;
4865 }
James Smartb5c53952016-03-31 14:12:30 -07004866 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004867
James Smart93996272008-08-24 21:50:30 -04004868 rc = lpfc_sli_config_port(phba, mode);
4869
James Smart12247e82016-07-06 12:36:09 -07004870 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004872 "1820 Unable to select SLI-3. "
4873 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004874 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004875 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004876 else if (rc && mode == 2)
4877 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004878 if (rc)
dea31012005-04-17 16:05:31 -05004879 goto lpfc_sli_hba_setup_error;
4880
James Smart0d878412009-10-02 15:16:56 -04004881 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4882 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4883 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4884 if (!rc) {
4885 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4886 "2709 This device supports "
4887 "Advanced Error Reporting (AER)\n");
4888 spin_lock_irq(&phba->hbalock);
4889 phba->hba_flag |= HBA_AER_ENABLED;
4890 spin_unlock_irq(&phba->hbalock);
4891 } else {
4892 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4893 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004894 "Advanced Error Reporting (AER): %d\n",
4895 rc);
James Smart0d878412009-10-02 15:16:56 -04004896 phba->cfg_aer_support = 0;
4897 }
4898 }
4899
James Smarted957682007-06-17 19:56:37 -05004900 if (phba->sli_rev == 3) {
4901 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4902 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004903 } else {
4904 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4905 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004906 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004907 }
4908
4909 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004910 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4911 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004912 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004913
4914 if (rc)
4915 goto lpfc_sli_hba_setup_error;
4916
James Smart6d368e52011-05-24 11:44:12 -04004917 /* Initialize VPIs. */
4918 if (phba->sli_rev == LPFC_SLI_REV3) {
4919 /*
4920 * The VPI bitmask and physical ID array are allocated
4921 * and initialized once only - at driver load. A port
4922 * reset doesn't need to reinitialize this memory.
4923 */
4924 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4925 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4926 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4927 GFP_KERNEL);
4928 if (!phba->vpi_bmask) {
4929 rc = -ENOMEM;
4930 goto lpfc_sli_hba_setup_error;
4931 }
4932
4933 phba->vpi_ids = kzalloc(
4934 (phba->max_vpi+1) * sizeof(uint16_t),
4935 GFP_KERNEL);
4936 if (!phba->vpi_ids) {
4937 kfree(phba->vpi_bmask);
4938 rc = -ENOMEM;
4939 goto lpfc_sli_hba_setup_error;
4940 }
4941 for (i = 0; i < phba->max_vpi; i++)
4942 phba->vpi_ids[i] = i;
4943 }
4944 }
4945
James Smart93996272008-08-24 21:50:30 -04004946 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004947 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4948 rc = lpfc_sli_hbq_setup(phba);
4949 if (rc)
4950 goto lpfc_sli_hba_setup_error;
4951 }
James Smart04c68492009-05-22 14:52:52 -04004952 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004953 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004954 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004955
4956 rc = lpfc_config_port_post(phba);
4957 if (rc)
4958 goto lpfc_sli_hba_setup_error;
4959
James Smarted957682007-06-17 19:56:37 -05004960 return rc;
4961
James Smart92d7f7b2007-06-17 19:56:38 -05004962lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004963 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004965 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004966 return rc;
4967}
4968
James Smartda0436e2009-05-22 14:51:39 -04004969/**
4970 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4971 * @phba: Pointer to HBA context object.
4972 * @mboxq: mailbox pointer.
4973 * This function issue a dump mailbox command to read config region
4974 * 23 and parse the records in the region and populate driver
4975 * data structure.
4976 **/
4977static int
James Smartff78d8f2011-12-13 13:21:35 -05004978lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004979{
James Smartff78d8f2011-12-13 13:21:35 -05004980 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004981 struct lpfc_dmabuf *mp;
4982 struct lpfc_mqe *mqe;
4983 uint32_t data_length;
4984 int rc;
4985
4986 /* Program the default value of vlan_id and fc_map */
4987 phba->valid_vlan = 0;
4988 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4989 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4990 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4991
James Smartff78d8f2011-12-13 13:21:35 -05004992 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4993 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004994 return -ENOMEM;
4995
James Smartff78d8f2011-12-13 13:21:35 -05004996 mqe = &mboxq->u.mqe;
4997 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4998 rc = -ENOMEM;
4999 goto out_free_mboxq;
5000 }
5001
James Smartda0436e2009-05-22 14:51:39 -04005002 mp = (struct lpfc_dmabuf *) mboxq->context1;
5003 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5004
5005 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5006 "(%d):2571 Mailbox cmd x%x Status x%x "
5007 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5008 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5009 "CQ: x%x x%x x%x x%x\n",
5010 mboxq->vport ? mboxq->vport->vpi : 0,
5011 bf_get(lpfc_mqe_command, mqe),
5012 bf_get(lpfc_mqe_status, mqe),
5013 mqe->un.mb_words[0], mqe->un.mb_words[1],
5014 mqe->un.mb_words[2], mqe->un.mb_words[3],
5015 mqe->un.mb_words[4], mqe->un.mb_words[5],
5016 mqe->un.mb_words[6], mqe->un.mb_words[7],
5017 mqe->un.mb_words[8], mqe->un.mb_words[9],
5018 mqe->un.mb_words[10], mqe->un.mb_words[11],
5019 mqe->un.mb_words[12], mqe->un.mb_words[13],
5020 mqe->un.mb_words[14], mqe->un.mb_words[15],
5021 mqe->un.mb_words[16], mqe->un.mb_words[50],
5022 mboxq->mcqe.word0,
5023 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5024 mboxq->mcqe.trailer);
5025
5026 if (rc) {
5027 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5028 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005029 rc = -EIO;
5030 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005031 }
5032 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005033 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005034 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5035 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005036 rc = -EIO;
5037 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005038 }
James Smartda0436e2009-05-22 14:51:39 -04005039
5040 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5041 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5042 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005043 rc = 0;
5044
5045out_free_mboxq:
5046 mempool_free(mboxq, phba->mbox_mem_pool);
5047 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005048}
5049
5050/**
5051 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5052 * @phba: pointer to lpfc hba data structure.
5053 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5054 * @vpd: pointer to the memory to hold resulting port vpd data.
5055 * @vpd_size: On input, the number of bytes allocated to @vpd.
5056 * On output, the number of data bytes in @vpd.
5057 *
5058 * This routine executes a READ_REV SLI4 mailbox command. In
5059 * addition, this routine gets the port vpd data.
5060 *
5061 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005062 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005063 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005064 **/
5065static int
5066lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5067 uint8_t *vpd, uint32_t *vpd_size)
5068{
5069 int rc = 0;
5070 uint32_t dma_size;
5071 struct lpfc_dmabuf *dmabuf;
5072 struct lpfc_mqe *mqe;
5073
5074 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5075 if (!dmabuf)
5076 return -ENOMEM;
5077
5078 /*
5079 * Get a DMA buffer for the vpd data resulting from the READ_REV
5080 * mailbox command.
5081 */
5082 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005083 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5084 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005085 if (!dmabuf->virt) {
5086 kfree(dmabuf);
5087 return -ENOMEM;
5088 }
James Smartda0436e2009-05-22 14:51:39 -04005089
5090 /*
5091 * The SLI4 implementation of READ_REV conflicts at word1,
5092 * bits 31:16 and SLI4 adds vpd functionality not present
5093 * in SLI3. This code corrects the conflicts.
5094 */
5095 lpfc_read_rev(phba, mboxq);
5096 mqe = &mboxq->u.mqe;
5097 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5098 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5099 mqe->un.read_rev.word1 &= 0x0000FFFF;
5100 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5101 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5102
5103 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5104 if (rc) {
5105 dma_free_coherent(&phba->pcidev->dev, dma_size,
5106 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005107 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005108 return -EIO;
5109 }
5110
James Smartda0436e2009-05-22 14:51:39 -04005111 /*
5112 * The available vpd length cannot be bigger than the
5113 * DMA buffer passed to the port. Catch the less than
5114 * case and update the caller's size.
5115 */
5116 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5117 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5118
James Smartd7c47992010-06-08 18:31:54 -04005119 memcpy(vpd, dmabuf->virt, *vpd_size);
5120
James Smartda0436e2009-05-22 14:51:39 -04005121 dma_free_coherent(&phba->pcidev->dev, dma_size,
5122 dmabuf->virt, dmabuf->phys);
5123 kfree(dmabuf);
5124 return 0;
5125}
5126
5127/**
James Smartcd1c8302011-10-10 21:33:25 -04005128 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5129 * @phba: pointer to lpfc hba data structure.
5130 *
5131 * This routine retrieves SLI4 device physical port name this PCI function
5132 * is attached to.
5133 *
5134 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005135 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005136 * otherwise - failed to retrieve physical port name
5137 **/
5138static int
5139lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5140{
5141 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005142 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5143 struct lpfc_controller_attribute *cntl_attr;
5144 struct lpfc_mbx_get_port_name *get_port_name;
5145 void *virtaddr = NULL;
5146 uint32_t alloclen, reqlen;
5147 uint32_t shdr_status, shdr_add_status;
5148 union lpfc_sli4_cfg_shdr *shdr;
5149 char cport_name = 0;
5150 int rc;
5151
5152 /* We assume nothing at this point */
5153 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5154 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5155
5156 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5157 if (!mboxq)
5158 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005159 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005160 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5161 lpfc_sli4_read_config(phba);
5162 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5163 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005164
5165 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5166 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5167 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5168 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5169 LPFC_SLI4_MBX_NEMBED);
5170 if (alloclen < reqlen) {
5171 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5172 "3084 Allocated DMA memory size (%d) is "
5173 "less than the requested DMA memory size "
5174 "(%d)\n", alloclen, reqlen);
5175 rc = -ENOMEM;
5176 goto out_free_mboxq;
5177 }
5178 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5179 virtaddr = mboxq->sge_array->addr[0];
5180 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5181 shdr = &mbx_cntl_attr->cfg_shdr;
5182 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5183 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5184 if (shdr_status || shdr_add_status || rc) {
5185 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5186 "3085 Mailbox x%x (x%x/x%x) failed, "
5187 "rc:x%x, status:x%x, add_status:x%x\n",
5188 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5189 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5190 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5191 rc, shdr_status, shdr_add_status);
5192 rc = -ENXIO;
5193 goto out_free_mboxq;
5194 }
5195 cntl_attr = &mbx_cntl_attr->cntl_attr;
5196 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5197 phba->sli4_hba.lnk_info.lnk_tp =
5198 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5199 phba->sli4_hba.lnk_info.lnk_no =
5200 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5201 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5202 "3086 lnk_type:%d, lnk_numb:%d\n",
5203 phba->sli4_hba.lnk_info.lnk_tp,
5204 phba->sli4_hba.lnk_info.lnk_no);
5205
5206retrieve_ppname:
5207 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5208 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5209 sizeof(struct lpfc_mbx_get_port_name) -
5210 sizeof(struct lpfc_sli4_cfg_mhdr),
5211 LPFC_SLI4_MBX_EMBED);
5212 get_port_name = &mboxq->u.mqe.un.get_port_name;
5213 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5214 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5215 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5216 phba->sli4_hba.lnk_info.lnk_tp);
5217 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5218 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5219 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5220 if (shdr_status || shdr_add_status || rc) {
5221 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5222 "3087 Mailbox x%x (x%x/x%x) failed: "
5223 "rc:x%x, status:x%x, add_status:x%x\n",
5224 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5225 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5226 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5227 rc, shdr_status, shdr_add_status);
5228 rc = -ENXIO;
5229 goto out_free_mboxq;
5230 }
5231 switch (phba->sli4_hba.lnk_info.lnk_no) {
5232 case LPFC_LINK_NUMBER_0:
5233 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5234 &get_port_name->u.response);
5235 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5236 break;
5237 case LPFC_LINK_NUMBER_1:
5238 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5239 &get_port_name->u.response);
5240 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5241 break;
5242 case LPFC_LINK_NUMBER_2:
5243 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5244 &get_port_name->u.response);
5245 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5246 break;
5247 case LPFC_LINK_NUMBER_3:
5248 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5249 &get_port_name->u.response);
5250 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5251 break;
5252 default:
5253 break;
5254 }
5255
5256 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5257 phba->Port[0] = cport_name;
5258 phba->Port[1] = '\0';
5259 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5260 "3091 SLI get port name: %s\n", phba->Port);
5261 }
5262
5263out_free_mboxq:
5264 if (rc != MBX_TIMEOUT) {
5265 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5266 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5267 else
5268 mempool_free(mboxq, phba->mbox_mem_pool);
5269 }
5270 return rc;
5271}
5272
5273/**
James Smartda0436e2009-05-22 14:51:39 -04005274 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5275 * @phba: pointer to lpfc hba data structure.
5276 *
5277 * This routine is called to explicitly arm the SLI4 device's completion and
5278 * event queues
5279 **/
5280static void
5281lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5282{
James Smart895427b2017-02-12 13:52:30 -08005283 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04005284
5285 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5286 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005287 if (phba->sli4_hba.nvmels_cq)
5288 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5289 LPFC_QUEUE_REARM);
5290
5291 if (phba->sli4_hba.fcp_cq)
5292 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5293 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5294 LPFC_QUEUE_REARM);
5295
5296 if (phba->sli4_hba.nvme_cq)
5297 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5298 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5299 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005300
James Smartf38fa0b2014-04-04 13:52:21 -04005301 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005302 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5303
James Smart895427b2017-02-12 13:52:30 -08005304 if (phba->sli4_hba.hba_eq)
5305 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5306 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5307 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005308
James Smart2d7dbc42017-02-12 13:52:35 -08005309 if (phba->nvmet_support) {
5310 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5311 lpfc_sli4_cq_release(
5312 phba->sli4_hba.nvmet_cqset[qidx],
5313 LPFC_QUEUE_REARM);
5314 }
James Smart2e90f4b2011-12-13 13:22:37 -05005315 }
James Smart1ba981f2014-02-20 09:56:45 -05005316
5317 if (phba->cfg_fof)
5318 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005319}
5320
5321/**
James Smart6d368e52011-05-24 11:44:12 -04005322 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5323 * @phba: Pointer to HBA context object.
5324 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005325 * @extnt_count: buffer to hold port available extent count.
5326 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005327 *
James Smartb76f2dc2011-07-22 18:37:42 -04005328 * This function calls the port and retrievs the number of available
5329 * extents and their size for a particular extent type.
5330 *
5331 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005332 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005333int
James Smart6d368e52011-05-24 11:44:12 -04005334lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5335 uint16_t *extnt_count, uint16_t *extnt_size)
5336{
5337 int rc = 0;
5338 uint32_t length;
5339 uint32_t mbox_tmo;
5340 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5341 LPFC_MBOXQ_t *mbox;
5342
5343 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5344 if (!mbox)
5345 return -ENOMEM;
5346
5347 /* Find out how many extents are available for this resource type */
5348 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5349 sizeof(struct lpfc_sli4_cfg_mhdr));
5350 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5351 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5352 length, LPFC_SLI4_MBX_EMBED);
5353
5354 /* Send an extents count of 0 - the GET doesn't use it. */
5355 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5356 LPFC_SLI4_MBX_EMBED);
5357 if (unlikely(rc)) {
5358 rc = -EIO;
5359 goto err_exit;
5360 }
5361
5362 if (!phba->sli4_hba.intr_enable)
5363 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5364 else {
James Smarta183a152011-10-10 21:32:43 -04005365 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005366 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5367 }
5368 if (unlikely(rc)) {
5369 rc = -EIO;
5370 goto err_exit;
5371 }
5372
5373 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5374 if (bf_get(lpfc_mbox_hdr_status,
5375 &rsrc_info->header.cfg_shdr.response)) {
5376 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5377 "2930 Failed to get resource extents "
5378 "Status 0x%x Add'l Status 0x%x\n",
5379 bf_get(lpfc_mbox_hdr_status,
5380 &rsrc_info->header.cfg_shdr.response),
5381 bf_get(lpfc_mbox_hdr_add_status,
5382 &rsrc_info->header.cfg_shdr.response));
5383 rc = -EIO;
5384 goto err_exit;
5385 }
5386
5387 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5388 &rsrc_info->u.rsp);
5389 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5390 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005391
5392 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5393 "3162 Retrieved extents type-%d from port: count:%d, "
5394 "size:%d\n", type, *extnt_count, *extnt_size);
5395
5396err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005397 mempool_free(mbox, phba->mbox_mem_pool);
5398 return rc;
5399}
5400
5401/**
5402 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5403 * @phba: Pointer to HBA context object.
5404 * @type: The extent type to check.
5405 *
5406 * This function reads the current available extents from the port and checks
5407 * if the extent count or extent size has changed since the last access.
5408 * Callers use this routine post port reset to understand if there is a
5409 * extent reprovisioning requirement.
5410 *
5411 * Returns:
5412 * -Error: error indicates problem.
5413 * 1: Extent count or size has changed.
5414 * 0: No changes.
5415 **/
5416static int
5417lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5418{
5419 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5420 uint16_t size_diff, rsrc_ext_size;
5421 int rc = 0;
5422 struct lpfc_rsrc_blks *rsrc_entry;
5423 struct list_head *rsrc_blk_list = NULL;
5424
5425 size_diff = 0;
5426 curr_ext_cnt = 0;
5427 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5428 &rsrc_ext_cnt,
5429 &rsrc_ext_size);
5430 if (unlikely(rc))
5431 return -EIO;
5432
5433 switch (type) {
5434 case LPFC_RSC_TYPE_FCOE_RPI:
5435 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5436 break;
5437 case LPFC_RSC_TYPE_FCOE_VPI:
5438 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5439 break;
5440 case LPFC_RSC_TYPE_FCOE_XRI:
5441 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5442 break;
5443 case LPFC_RSC_TYPE_FCOE_VFI:
5444 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5445 break;
5446 default:
5447 break;
5448 }
5449
5450 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5451 curr_ext_cnt++;
5452 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5453 size_diff++;
5454 }
5455
5456 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5457 rc = 1;
5458
5459 return rc;
5460}
5461
5462/**
5463 * lpfc_sli4_cfg_post_extnts -
5464 * @phba: Pointer to HBA context object.
5465 * @extnt_cnt - number of available extents.
5466 * @type - the extent type (rpi, xri, vfi, vpi).
5467 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5468 * @mbox - pointer to the caller's allocated mailbox structure.
5469 *
5470 * This function executes the extents allocation request. It also
5471 * takes care of the amount of memory needed to allocate or get the
5472 * allocated extents. It is the caller's responsibility to evaluate
5473 * the response.
5474 *
5475 * Returns:
5476 * -Error: Error value describes the condition found.
5477 * 0: if successful
5478 **/
5479static int
James Smart8a9d2e82012-05-09 21:16:12 -04005480lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005481 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5482{
5483 int rc = 0;
5484 uint32_t req_len;
5485 uint32_t emb_len;
5486 uint32_t alloc_len, mbox_tmo;
5487
5488 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005489 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005490
5491 /*
5492 * Calculate the size of an embedded mailbox. The uint32_t
5493 * accounts for extents-specific word.
5494 */
5495 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5496 sizeof(uint32_t);
5497
5498 /*
5499 * Presume the allocation and response will fit into an embedded
5500 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5501 */
5502 *emb = LPFC_SLI4_MBX_EMBED;
5503 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005504 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005505 sizeof(union lpfc_sli4_cfg_shdr) +
5506 sizeof(uint32_t);
5507 *emb = LPFC_SLI4_MBX_NEMBED;
5508 }
5509
5510 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5511 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5512 req_len, *emb);
5513 if (alloc_len < req_len) {
5514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005515 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005516 "less than the requested DMA memory "
5517 "size (x%x)\n", alloc_len, req_len);
5518 return -ENOMEM;
5519 }
James Smart8a9d2e82012-05-09 21:16:12 -04005520 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005521 if (unlikely(rc))
5522 return -EIO;
5523
5524 if (!phba->sli4_hba.intr_enable)
5525 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5526 else {
James Smarta183a152011-10-10 21:32:43 -04005527 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005528 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5529 }
5530
5531 if (unlikely(rc))
5532 rc = -EIO;
5533 return rc;
5534}
5535
5536/**
5537 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5538 * @phba: Pointer to HBA context object.
5539 * @type: The resource extent type to allocate.
5540 *
5541 * This function allocates the number of elements for the specified
5542 * resource type.
5543 **/
5544static int
5545lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5546{
5547 bool emb = false;
5548 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5549 uint16_t rsrc_id, rsrc_start, j, k;
5550 uint16_t *ids;
5551 int i, rc;
5552 unsigned long longs;
5553 unsigned long *bmask;
5554 struct lpfc_rsrc_blks *rsrc_blks;
5555 LPFC_MBOXQ_t *mbox;
5556 uint32_t length;
5557 struct lpfc_id_range *id_array = NULL;
5558 void *virtaddr = NULL;
5559 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5560 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5561 struct list_head *ext_blk_list;
5562
5563 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5564 &rsrc_cnt,
5565 &rsrc_size);
5566 if (unlikely(rc))
5567 return -EIO;
5568
5569 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5570 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5571 "3009 No available Resource Extents "
5572 "for resource type 0x%x: Count: 0x%x, "
5573 "Size 0x%x\n", type, rsrc_cnt,
5574 rsrc_size);
5575 return -ENOMEM;
5576 }
5577
James Smart8a9d2e82012-05-09 21:16:12 -04005578 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5579 "2903 Post resource extents type-0x%x: "
5580 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005581
5582 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5583 if (!mbox)
5584 return -ENOMEM;
5585
James Smart8a9d2e82012-05-09 21:16:12 -04005586 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005587 if (unlikely(rc)) {
5588 rc = -EIO;
5589 goto err_exit;
5590 }
5591
5592 /*
5593 * Figure out where the response is located. Then get local pointers
5594 * to the response data. The port does not guarantee to respond to
5595 * all extents counts request so update the local variable with the
5596 * allocated count from the port.
5597 */
5598 if (emb == LPFC_SLI4_MBX_EMBED) {
5599 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5600 id_array = &rsrc_ext->u.rsp.id[0];
5601 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5602 } else {
5603 virtaddr = mbox->sge_array->addr[0];
5604 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5605 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5606 id_array = &n_rsrc->id;
5607 }
5608
5609 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5610 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5611
5612 /*
5613 * Based on the resource size and count, correct the base and max
5614 * resource values.
5615 */
5616 length = sizeof(struct lpfc_rsrc_blks);
5617 switch (type) {
5618 case LPFC_RSC_TYPE_FCOE_RPI:
5619 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5620 sizeof(unsigned long),
5621 GFP_KERNEL);
5622 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5623 rc = -ENOMEM;
5624 goto err_exit;
5625 }
5626 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5627 sizeof(uint16_t),
5628 GFP_KERNEL);
5629 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5630 kfree(phba->sli4_hba.rpi_bmask);
5631 rc = -ENOMEM;
5632 goto err_exit;
5633 }
5634
5635 /*
5636 * The next_rpi was initialized with the maximum available
5637 * count but the port may allocate a smaller number. Catch
5638 * that case and update the next_rpi.
5639 */
5640 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5641
5642 /* Initialize local ptrs for common extent processing later. */
5643 bmask = phba->sli4_hba.rpi_bmask;
5644 ids = phba->sli4_hba.rpi_ids;
5645 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5646 break;
5647 case LPFC_RSC_TYPE_FCOE_VPI:
5648 phba->vpi_bmask = kzalloc(longs *
5649 sizeof(unsigned long),
5650 GFP_KERNEL);
5651 if (unlikely(!phba->vpi_bmask)) {
5652 rc = -ENOMEM;
5653 goto err_exit;
5654 }
5655 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5656 sizeof(uint16_t),
5657 GFP_KERNEL);
5658 if (unlikely(!phba->vpi_ids)) {
5659 kfree(phba->vpi_bmask);
5660 rc = -ENOMEM;
5661 goto err_exit;
5662 }
5663
5664 /* Initialize local ptrs for common extent processing later. */
5665 bmask = phba->vpi_bmask;
5666 ids = phba->vpi_ids;
5667 ext_blk_list = &phba->lpfc_vpi_blk_list;
5668 break;
5669 case LPFC_RSC_TYPE_FCOE_XRI:
5670 phba->sli4_hba.xri_bmask = kzalloc(longs *
5671 sizeof(unsigned long),
5672 GFP_KERNEL);
5673 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5674 rc = -ENOMEM;
5675 goto err_exit;
5676 }
James Smart8a9d2e82012-05-09 21:16:12 -04005677 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005678 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5679 sizeof(uint16_t),
5680 GFP_KERNEL);
5681 if (unlikely(!phba->sli4_hba.xri_ids)) {
5682 kfree(phba->sli4_hba.xri_bmask);
5683 rc = -ENOMEM;
5684 goto err_exit;
5685 }
5686
5687 /* Initialize local ptrs for common extent processing later. */
5688 bmask = phba->sli4_hba.xri_bmask;
5689 ids = phba->sli4_hba.xri_ids;
5690 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5691 break;
5692 case LPFC_RSC_TYPE_FCOE_VFI:
5693 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5694 sizeof(unsigned long),
5695 GFP_KERNEL);
5696 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5697 rc = -ENOMEM;
5698 goto err_exit;
5699 }
5700 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5701 sizeof(uint16_t),
5702 GFP_KERNEL);
5703 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5704 kfree(phba->sli4_hba.vfi_bmask);
5705 rc = -ENOMEM;
5706 goto err_exit;
5707 }
5708
5709 /* Initialize local ptrs for common extent processing later. */
5710 bmask = phba->sli4_hba.vfi_bmask;
5711 ids = phba->sli4_hba.vfi_ids;
5712 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5713 break;
5714 default:
5715 /* Unsupported Opcode. Fail call. */
5716 id_array = NULL;
5717 bmask = NULL;
5718 ids = NULL;
5719 ext_blk_list = NULL;
5720 goto err_exit;
5721 }
5722
5723 /*
5724 * Complete initializing the extent configuration with the
5725 * allocated ids assigned to this function. The bitmask serves
5726 * as an index into the array and manages the available ids. The
5727 * array just stores the ids communicated to the port via the wqes.
5728 */
5729 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5730 if ((i % 2) == 0)
5731 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5732 &id_array[k]);
5733 else
5734 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5735 &id_array[k]);
5736
5737 rsrc_blks = kzalloc(length, GFP_KERNEL);
5738 if (unlikely(!rsrc_blks)) {
5739 rc = -ENOMEM;
5740 kfree(bmask);
5741 kfree(ids);
5742 goto err_exit;
5743 }
5744 rsrc_blks->rsrc_start = rsrc_id;
5745 rsrc_blks->rsrc_size = rsrc_size;
5746 list_add_tail(&rsrc_blks->list, ext_blk_list);
5747 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005748 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005749 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005750 lpfc_sli4_get_iocb_cnt(phba);
5751 phba->sli4_hba.nvme_xri_start =
5752 phba->sli4_hba.scsi_xri_start +
5753 phba->sli4_hba.scsi_xri_max;
5754 }
James Smart6d368e52011-05-24 11:44:12 -04005755
5756 while (rsrc_id < (rsrc_start + rsrc_size)) {
5757 ids[j] = rsrc_id;
5758 rsrc_id++;
5759 j++;
5760 }
5761 /* Entire word processed. Get next word.*/
5762 if ((i % 2) == 1)
5763 k++;
5764 }
5765 err_exit:
5766 lpfc_sli4_mbox_cmd_free(phba, mbox);
5767 return rc;
5768}
5769
James Smart895427b2017-02-12 13:52:30 -08005770
5771
James Smart6d368e52011-05-24 11:44:12 -04005772/**
5773 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5774 * @phba: Pointer to HBA context object.
5775 * @type: the extent's type.
5776 *
5777 * This function deallocates all extents of a particular resource type.
5778 * SLI4 does not allow for deallocating a particular extent range. It
5779 * is the caller's responsibility to release all kernel memory resources.
5780 **/
5781static int
5782lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5783{
5784 int rc;
5785 uint32_t length, mbox_tmo = 0;
5786 LPFC_MBOXQ_t *mbox;
5787 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5788 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5789
5790 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5791 if (!mbox)
5792 return -ENOMEM;
5793
5794 /*
5795 * This function sends an embedded mailbox because it only sends the
5796 * the resource type. All extents of this type are released by the
5797 * port.
5798 */
5799 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5800 sizeof(struct lpfc_sli4_cfg_mhdr));
5801 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5802 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5803 length, LPFC_SLI4_MBX_EMBED);
5804
5805 /* Send an extents count of 0 - the dealloc doesn't use it. */
5806 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5807 LPFC_SLI4_MBX_EMBED);
5808 if (unlikely(rc)) {
5809 rc = -EIO;
5810 goto out_free_mbox;
5811 }
5812 if (!phba->sli4_hba.intr_enable)
5813 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5814 else {
James Smarta183a152011-10-10 21:32:43 -04005815 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005816 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5817 }
5818 if (unlikely(rc)) {
5819 rc = -EIO;
5820 goto out_free_mbox;
5821 }
5822
5823 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5824 if (bf_get(lpfc_mbox_hdr_status,
5825 &dealloc_rsrc->header.cfg_shdr.response)) {
5826 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5827 "2919 Failed to release resource extents "
5828 "for type %d - Status 0x%x Add'l Status 0x%x. "
5829 "Resource memory not released.\n",
5830 type,
5831 bf_get(lpfc_mbox_hdr_status,
5832 &dealloc_rsrc->header.cfg_shdr.response),
5833 bf_get(lpfc_mbox_hdr_add_status,
5834 &dealloc_rsrc->header.cfg_shdr.response));
5835 rc = -EIO;
5836 goto out_free_mbox;
5837 }
5838
5839 /* Release kernel memory resources for the specific type. */
5840 switch (type) {
5841 case LPFC_RSC_TYPE_FCOE_VPI:
5842 kfree(phba->vpi_bmask);
5843 kfree(phba->vpi_ids);
5844 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5845 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5846 &phba->lpfc_vpi_blk_list, list) {
5847 list_del_init(&rsrc_blk->list);
5848 kfree(rsrc_blk);
5849 }
James Smart16a3a202013-04-17 20:14:38 -04005850 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005851 break;
5852 case LPFC_RSC_TYPE_FCOE_XRI:
5853 kfree(phba->sli4_hba.xri_bmask);
5854 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005855 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5856 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5857 list_del_init(&rsrc_blk->list);
5858 kfree(rsrc_blk);
5859 }
5860 break;
5861 case LPFC_RSC_TYPE_FCOE_VFI:
5862 kfree(phba->sli4_hba.vfi_bmask);
5863 kfree(phba->sli4_hba.vfi_ids);
5864 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5865 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5866 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5867 list_del_init(&rsrc_blk->list);
5868 kfree(rsrc_blk);
5869 }
5870 break;
5871 case LPFC_RSC_TYPE_FCOE_RPI:
5872 /* RPI bitmask and physical id array are cleaned up earlier. */
5873 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5874 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5875 list_del_init(&rsrc_blk->list);
5876 kfree(rsrc_blk);
5877 }
5878 break;
5879 default:
5880 break;
5881 }
5882
5883 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5884
5885 out_free_mbox:
5886 mempool_free(mbox, phba->mbox_mem_pool);
5887 return rc;
5888}
5889
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005890static void
James Smart7bdedb32016-07-06 12:36:00 -07005891lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5892 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005893{
James Smart65791f12016-07-06 12:35:56 -07005894 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005895
James Smart65791f12016-07-06 12:35:56 -07005896 len = sizeof(struct lpfc_mbx_set_feature) -
5897 sizeof(struct lpfc_sli4_cfg_mhdr);
5898 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5899 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5900 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005901
James Smart7bdedb32016-07-06 12:36:00 -07005902 switch (feature) {
5903 case LPFC_SET_UE_RECOVERY:
5904 bf_set(lpfc_mbx_set_feature_UER,
5905 &mbox->u.mqe.un.set_feature, 1);
5906 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5907 mbox->u.mqe.un.set_feature.param_len = 8;
5908 break;
5909 case LPFC_SET_MDS_DIAGS:
5910 bf_set(lpfc_mbx_set_feature_mds,
5911 &mbox->u.mqe.un.set_feature, 1);
5912 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07005913 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07005914 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5915 mbox->u.mqe.un.set_feature.param_len = 8;
5916 break;
James Smart65791f12016-07-06 12:35:56 -07005917 }
James Smart7bdedb32016-07-06 12:36:00 -07005918
5919 return;
James Smart65791f12016-07-06 12:35:56 -07005920}
5921
James Smart6d368e52011-05-24 11:44:12 -04005922/**
5923 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5924 * @phba: Pointer to HBA context object.
5925 *
5926 * This function allocates all SLI4 resource identifiers.
5927 **/
5928int
5929lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5930{
5931 int i, rc, error = 0;
5932 uint16_t count, base;
5933 unsigned long longs;
5934
James Smartff78d8f2011-12-13 13:21:35 -05005935 if (!phba->sli4_hba.rpi_hdrs_in_use)
5936 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005937 if (phba->sli4_hba.extents_in_use) {
5938 /*
5939 * The port supports resource extents. The XRI, VPI, VFI, RPI
5940 * resource extent count must be read and allocated before
5941 * provisioning the resource id arrays.
5942 */
5943 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5944 LPFC_IDX_RSRC_RDY) {
5945 /*
5946 * Extent-based resources are set - the driver could
5947 * be in a port reset. Figure out if any corrective
5948 * actions need to be taken.
5949 */
5950 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5951 LPFC_RSC_TYPE_FCOE_VFI);
5952 if (rc != 0)
5953 error++;
5954 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5955 LPFC_RSC_TYPE_FCOE_VPI);
5956 if (rc != 0)
5957 error++;
5958 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5959 LPFC_RSC_TYPE_FCOE_XRI);
5960 if (rc != 0)
5961 error++;
5962 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5963 LPFC_RSC_TYPE_FCOE_RPI);
5964 if (rc != 0)
5965 error++;
5966
5967 /*
5968 * It's possible that the number of resources
5969 * provided to this port instance changed between
5970 * resets. Detect this condition and reallocate
5971 * resources. Otherwise, there is no action.
5972 */
5973 if (error) {
5974 lpfc_printf_log(phba, KERN_INFO,
5975 LOG_MBOX | LOG_INIT,
5976 "2931 Detected extent resource "
5977 "change. Reallocating all "
5978 "extents.\n");
5979 rc = lpfc_sli4_dealloc_extent(phba,
5980 LPFC_RSC_TYPE_FCOE_VFI);
5981 rc = lpfc_sli4_dealloc_extent(phba,
5982 LPFC_RSC_TYPE_FCOE_VPI);
5983 rc = lpfc_sli4_dealloc_extent(phba,
5984 LPFC_RSC_TYPE_FCOE_XRI);
5985 rc = lpfc_sli4_dealloc_extent(phba,
5986 LPFC_RSC_TYPE_FCOE_RPI);
5987 } else
5988 return 0;
5989 }
5990
5991 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5992 if (unlikely(rc))
5993 goto err_exit;
5994
5995 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5996 if (unlikely(rc))
5997 goto err_exit;
5998
5999 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6000 if (unlikely(rc))
6001 goto err_exit;
6002
6003 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6004 if (unlikely(rc))
6005 goto err_exit;
6006 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6007 LPFC_IDX_RSRC_RDY);
6008 return rc;
6009 } else {
6010 /*
6011 * The port does not support resource extents. The XRI, VPI,
6012 * VFI, RPI resource ids were determined from READ_CONFIG.
6013 * Just allocate the bitmasks and provision the resource id
6014 * arrays. If a port reset is active, the resources don't
6015 * need any action - just exit.
6016 */
6017 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006018 LPFC_IDX_RSRC_RDY) {
6019 lpfc_sli4_dealloc_resource_identifiers(phba);
6020 lpfc_sli4_remove_rpis(phba);
6021 }
James Smart6d368e52011-05-24 11:44:12 -04006022 /* RPIs. */
6023 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006024 if (count <= 0) {
6025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6026 "3279 Invalid provisioning of "
6027 "rpi:%d\n", count);
6028 rc = -EINVAL;
6029 goto err_exit;
6030 }
James Smart6d368e52011-05-24 11:44:12 -04006031 base = phba->sli4_hba.max_cfg_param.rpi_base;
6032 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6033 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6034 sizeof(unsigned long),
6035 GFP_KERNEL);
6036 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6037 rc = -ENOMEM;
6038 goto err_exit;
6039 }
6040 phba->sli4_hba.rpi_ids = kzalloc(count *
6041 sizeof(uint16_t),
6042 GFP_KERNEL);
6043 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6044 rc = -ENOMEM;
6045 goto free_rpi_bmask;
6046 }
6047
6048 for (i = 0; i < count; i++)
6049 phba->sli4_hba.rpi_ids[i] = base + i;
6050
6051 /* VPIs. */
6052 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006053 if (count <= 0) {
6054 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6055 "3280 Invalid provisioning of "
6056 "vpi:%d\n", count);
6057 rc = -EINVAL;
6058 goto free_rpi_ids;
6059 }
James Smart6d368e52011-05-24 11:44:12 -04006060 base = phba->sli4_hba.max_cfg_param.vpi_base;
6061 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6062 phba->vpi_bmask = kzalloc(longs *
6063 sizeof(unsigned long),
6064 GFP_KERNEL);
6065 if (unlikely(!phba->vpi_bmask)) {
6066 rc = -ENOMEM;
6067 goto free_rpi_ids;
6068 }
6069 phba->vpi_ids = kzalloc(count *
6070 sizeof(uint16_t),
6071 GFP_KERNEL);
6072 if (unlikely(!phba->vpi_ids)) {
6073 rc = -ENOMEM;
6074 goto free_vpi_bmask;
6075 }
6076
6077 for (i = 0; i < count; i++)
6078 phba->vpi_ids[i] = base + i;
6079
6080 /* XRIs. */
6081 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006082 if (count <= 0) {
6083 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6084 "3281 Invalid provisioning of "
6085 "xri:%d\n", count);
6086 rc = -EINVAL;
6087 goto free_vpi_ids;
6088 }
James Smart6d368e52011-05-24 11:44:12 -04006089 base = phba->sli4_hba.max_cfg_param.xri_base;
6090 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6091 phba->sli4_hba.xri_bmask = kzalloc(longs *
6092 sizeof(unsigned long),
6093 GFP_KERNEL);
6094 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6095 rc = -ENOMEM;
6096 goto free_vpi_ids;
6097 }
James Smart41899be2012-03-01 22:34:19 -05006098 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006099 phba->sli4_hba.xri_ids = kzalloc(count *
6100 sizeof(uint16_t),
6101 GFP_KERNEL);
6102 if (unlikely(!phba->sli4_hba.xri_ids)) {
6103 rc = -ENOMEM;
6104 goto free_xri_bmask;
6105 }
6106
6107 for (i = 0; i < count; i++)
6108 phba->sli4_hba.xri_ids[i] = base + i;
6109
6110 /* VFIs. */
6111 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006112 if (count <= 0) {
6113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6114 "3282 Invalid provisioning of "
6115 "vfi:%d\n", count);
6116 rc = -EINVAL;
6117 goto free_xri_ids;
6118 }
James Smart6d368e52011-05-24 11:44:12 -04006119 base = phba->sli4_hba.max_cfg_param.vfi_base;
6120 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6121 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6122 sizeof(unsigned long),
6123 GFP_KERNEL);
6124 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6125 rc = -ENOMEM;
6126 goto free_xri_ids;
6127 }
6128 phba->sli4_hba.vfi_ids = kzalloc(count *
6129 sizeof(uint16_t),
6130 GFP_KERNEL);
6131 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6132 rc = -ENOMEM;
6133 goto free_vfi_bmask;
6134 }
6135
6136 for (i = 0; i < count; i++)
6137 phba->sli4_hba.vfi_ids[i] = base + i;
6138
6139 /*
6140 * Mark all resources ready. An HBA reset doesn't need
6141 * to reset the initialization.
6142 */
6143 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6144 LPFC_IDX_RSRC_RDY);
6145 return 0;
6146 }
6147
6148 free_vfi_bmask:
6149 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006150 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006151 free_xri_ids:
6152 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006153 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006154 free_xri_bmask:
6155 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006156 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006157 free_vpi_ids:
6158 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006159 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006160 free_vpi_bmask:
6161 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006162 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006163 free_rpi_ids:
6164 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006165 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006166 free_rpi_bmask:
6167 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006168 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006169 err_exit:
6170 return rc;
6171}
6172
6173/**
6174 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6175 * @phba: Pointer to HBA context object.
6176 *
6177 * This function allocates the number of elements for the specified
6178 * resource type.
6179 **/
6180int
6181lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6182{
6183 if (phba->sli4_hba.extents_in_use) {
6184 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6185 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6186 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6187 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6188 } else {
6189 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006190 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006191 kfree(phba->vpi_ids);
6192 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6193 kfree(phba->sli4_hba.xri_bmask);
6194 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006195 kfree(phba->sli4_hba.vfi_bmask);
6196 kfree(phba->sli4_hba.vfi_ids);
6197 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6198 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6199 }
6200
6201 return 0;
6202}
6203
6204/**
James Smartb76f2dc2011-07-22 18:37:42 -04006205 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6206 * @phba: Pointer to HBA context object.
6207 * @type: The resource extent type.
6208 * @extnt_count: buffer to hold port extent count response
6209 * @extnt_size: buffer to hold port extent size response.
6210 *
6211 * This function calls the port to read the host allocated extents
6212 * for a particular type.
6213 **/
6214int
6215lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6216 uint16_t *extnt_cnt, uint16_t *extnt_size)
6217{
6218 bool emb;
6219 int rc = 0;
6220 uint16_t curr_blks = 0;
6221 uint32_t req_len, emb_len;
6222 uint32_t alloc_len, mbox_tmo;
6223 struct list_head *blk_list_head;
6224 struct lpfc_rsrc_blks *rsrc_blk;
6225 LPFC_MBOXQ_t *mbox;
6226 void *virtaddr = NULL;
6227 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6228 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6229 union lpfc_sli4_cfg_shdr *shdr;
6230
6231 switch (type) {
6232 case LPFC_RSC_TYPE_FCOE_VPI:
6233 blk_list_head = &phba->lpfc_vpi_blk_list;
6234 break;
6235 case LPFC_RSC_TYPE_FCOE_XRI:
6236 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6237 break;
6238 case LPFC_RSC_TYPE_FCOE_VFI:
6239 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6240 break;
6241 case LPFC_RSC_TYPE_FCOE_RPI:
6242 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6243 break;
6244 default:
6245 return -EIO;
6246 }
6247
6248 /* Count the number of extents currently allocatd for this type. */
6249 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6250 if (curr_blks == 0) {
6251 /*
6252 * The GET_ALLOCATED mailbox does not return the size,
6253 * just the count. The size should be just the size
6254 * stored in the current allocated block and all sizes
6255 * for an extent type are the same so set the return
6256 * value now.
6257 */
6258 *extnt_size = rsrc_blk->rsrc_size;
6259 }
6260 curr_blks++;
6261 }
6262
James Smartb76f2dc2011-07-22 18:37:42 -04006263 /*
6264 * Calculate the size of an embedded mailbox. The uint32_t
6265 * accounts for extents-specific word.
6266 */
6267 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6268 sizeof(uint32_t);
6269
6270 /*
6271 * Presume the allocation and response will fit into an embedded
6272 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6273 */
6274 emb = LPFC_SLI4_MBX_EMBED;
6275 req_len = emb_len;
6276 if (req_len > emb_len) {
6277 req_len = curr_blks * sizeof(uint16_t) +
6278 sizeof(union lpfc_sli4_cfg_shdr) +
6279 sizeof(uint32_t);
6280 emb = LPFC_SLI4_MBX_NEMBED;
6281 }
6282
6283 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6284 if (!mbox)
6285 return -ENOMEM;
6286 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6287
6288 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6289 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6290 req_len, emb);
6291 if (alloc_len < req_len) {
6292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6293 "2983 Allocated DMA memory size (x%x) is "
6294 "less than the requested DMA memory "
6295 "size (x%x)\n", alloc_len, req_len);
6296 rc = -ENOMEM;
6297 goto err_exit;
6298 }
6299 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6300 if (unlikely(rc)) {
6301 rc = -EIO;
6302 goto err_exit;
6303 }
6304
6305 if (!phba->sli4_hba.intr_enable)
6306 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6307 else {
James Smarta183a152011-10-10 21:32:43 -04006308 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006309 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6310 }
6311
6312 if (unlikely(rc)) {
6313 rc = -EIO;
6314 goto err_exit;
6315 }
6316
6317 /*
6318 * Figure out where the response is located. Then get local pointers
6319 * to the response data. The port does not guarantee to respond to
6320 * all extents counts request so update the local variable with the
6321 * allocated count from the port.
6322 */
6323 if (emb == LPFC_SLI4_MBX_EMBED) {
6324 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6325 shdr = &rsrc_ext->header.cfg_shdr;
6326 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6327 } else {
6328 virtaddr = mbox->sge_array->addr[0];
6329 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6330 shdr = &n_rsrc->cfg_shdr;
6331 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6332 }
6333
6334 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6335 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6336 "2984 Failed to read allocated resources "
6337 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6338 type,
6339 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6340 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6341 rc = -EIO;
6342 goto err_exit;
6343 }
6344 err_exit:
6345 lpfc_sli4_mbox_cmd_free(phba, mbox);
6346 return rc;
6347}
6348
6349/**
James Smart0ef69962017-04-21 16:04:50 -07006350 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006351 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006352 * @pring: Pointer to driver SLI ring object.
6353 * @sgl_list: linked link of sgl buffers to post
6354 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006355 *
James Smart895427b2017-02-12 13:52:30 -08006356 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006357 * repost them to the port by using SGL block post. This is needed after a
6358 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006359 * of buffer sgls which contains contiguous xris and uses the non-embedded
6360 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006361 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6362 * mailbox command for posting.
6363 *
6364 * Returns: 0 = success, non-zero failure.
6365 **/
6366static int
James Smart895427b2017-02-12 13:52:30 -08006367lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6368 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006369{
6370 struct lpfc_sglq *sglq_entry = NULL;
6371 struct lpfc_sglq *sglq_entry_next = NULL;
6372 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006373 int status, total_cnt;
6374 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006375 int last_xritag = NO_XRI;
6376 LIST_HEAD(prep_sgl_list);
6377 LIST_HEAD(blck_sgl_list);
6378 LIST_HEAD(allc_sgl_list);
6379 LIST_HEAD(post_sgl_list);
6380 LIST_HEAD(free_sgl_list);
6381
James Smart38c20672013-03-01 16:37:44 -05006382 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006383 spin_lock(&phba->sli4_hba.sgl_list_lock);
6384 list_splice_init(sgl_list, &allc_sgl_list);
6385 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006386 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006387
James Smart895427b2017-02-12 13:52:30 -08006388 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006389 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6390 &allc_sgl_list, list) {
6391 list_del_init(&sglq_entry->list);
6392 block_cnt++;
6393 if ((last_xritag != NO_XRI) &&
6394 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6395 /* a hole in xri block, form a sgl posting block */
6396 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6397 post_cnt = block_cnt - 1;
6398 /* prepare list for next posting block */
6399 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6400 block_cnt = 1;
6401 } else {
6402 /* prepare list for next posting block */
6403 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6404 /* enough sgls for non-embed sgl mbox command */
6405 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6406 list_splice_init(&prep_sgl_list,
6407 &blck_sgl_list);
6408 post_cnt = block_cnt;
6409 block_cnt = 0;
6410 }
6411 }
6412 num_posted++;
6413
6414 /* keep track of last sgl's xritag */
6415 last_xritag = sglq_entry->sli4_xritag;
6416
James Smart895427b2017-02-12 13:52:30 -08006417 /* end of repost sgl list condition for buffers */
6418 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006419 if (post_cnt == 0) {
6420 list_splice_init(&prep_sgl_list,
6421 &blck_sgl_list);
6422 post_cnt = block_cnt;
6423 } else if (block_cnt == 1) {
6424 status = lpfc_sli4_post_sgl(phba,
6425 sglq_entry->phys, 0,
6426 sglq_entry->sli4_xritag);
6427 if (!status) {
6428 /* successful, put sgl to posted list */
6429 list_add_tail(&sglq_entry->list,
6430 &post_sgl_list);
6431 } else {
6432 /* Failure, put sgl to free list */
6433 lpfc_printf_log(phba, KERN_WARNING,
6434 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006435 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006436 "sgl, xritag:x%x\n",
6437 sglq_entry->sli4_xritag);
6438 list_add_tail(&sglq_entry->list,
6439 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006440 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006441 }
6442 }
6443 }
6444
6445 /* continue until a nembed page worth of sgls */
6446 if (post_cnt == 0)
6447 continue;
6448
James Smart895427b2017-02-12 13:52:30 -08006449 /* post the buffer list sgls as a block */
6450 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6451 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006452
6453 if (!status) {
6454 /* success, put sgl list to posted sgl list */
6455 list_splice_init(&blck_sgl_list, &post_sgl_list);
6456 } else {
6457 /* Failure, put sgl list to free sgl list */
6458 sglq_entry_first = list_first_entry(&blck_sgl_list,
6459 struct lpfc_sglq,
6460 list);
6461 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006462 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006463 "xritag:x%x-x%x\n",
6464 sglq_entry_first->sli4_xritag,
6465 (sglq_entry_first->sli4_xritag +
6466 post_cnt - 1));
6467 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006468 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006469 }
6470
6471 /* don't reset xirtag due to hole in xri block */
6472 if (block_cnt == 0)
6473 last_xritag = NO_XRI;
6474
James Smart895427b2017-02-12 13:52:30 -08006475 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006476 post_cnt = 0;
6477 }
6478
James Smart895427b2017-02-12 13:52:30 -08006479 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006480 lpfc_free_sgl_list(phba, &free_sgl_list);
6481
James Smart895427b2017-02-12 13:52:30 -08006482 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006483 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006484 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006485 spin_lock(&phba->sli4_hba.sgl_list_lock);
6486 list_splice_init(&post_sgl_list, sgl_list);
6487 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006488 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006489 } else {
6490 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006491 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006492 return -EIO;
6493 }
James Smart895427b2017-02-12 13:52:30 -08006494
6495 /* return the number of XRIs actually posted */
6496 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006497}
6498
James Smart61bda8f2016-10-13 15:06:05 -07006499void
6500lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6501{
6502 uint32_t len;
6503
6504 len = sizeof(struct lpfc_mbx_set_host_data) -
6505 sizeof(struct lpfc_sli4_cfg_mhdr);
6506 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6507 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6508 LPFC_SLI4_MBX_EMBED);
6509
6510 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006511 mbox->u.mqe.un.set_host_data.param_len =
6512 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006513 snprintf(mbox->u.mqe.un.set_host_data.data,
6514 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6515 "Linux %s v"LPFC_DRIVER_VERSION,
6516 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6517}
6518
James Smarta8cf5df2017-05-15 15:20:46 -07006519int
James Smart6c621a22017-05-15 15:20:45 -07006520lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006521 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006522{
6523 int rc, i;
6524 struct lpfc_rqe hrqe;
6525 struct lpfc_rqe drqe;
6526 struct lpfc_rqb *rqbp;
6527 struct rqb_dmabuf *rqb_buffer;
6528 LIST_HEAD(rqb_buf_list);
6529
6530 rqbp = hrq->rqbp;
6531 for (i = 0; i < count; i++) {
6532 /* IF RQ is already full, don't bother */
6533 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6534 break;
6535 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6536 if (!rqb_buffer)
6537 break;
6538 rqb_buffer->hrq = hrq;
6539 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006540 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006541 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6542 }
6543 while (!list_empty(&rqb_buf_list)) {
6544 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6545 hbuf.list);
6546
6547 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6548 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6549 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6550 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6551 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6552 if (rc < 0) {
6553 rqbp->rqb_free_buffer(phba, rqb_buffer);
6554 } else {
6555 list_add_tail(&rqb_buffer->hbuf.list,
6556 &rqbp->rqb_buffer_list);
6557 rqbp->buffer_count++;
6558 }
6559 }
6560 return 1;
6561}
6562
James Smart8a9d2e82012-05-09 21:16:12 -04006563/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006564 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006565 * @phba: Pointer to HBA context object.
6566 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006567 * This function is the main SLI4 device initialization PCI function. This
6568 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006569 * HBA error attention handler code. Caller is not required to hold any
6570 * locks.
6571 **/
6572int
6573lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6574{
James Smart6c621a22017-05-15 15:20:45 -07006575 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006576 LPFC_MBOXQ_t *mboxq;
6577 struct lpfc_mqe *mqe;
6578 uint8_t *vpd;
6579 uint32_t vpd_size;
6580 uint32_t ftr_rsp = 0;
6581 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6582 struct lpfc_vport *vport = phba->pport;
6583 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006584 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006585
6586 /* Perform a PCI function reset to start from clean */
6587 rc = lpfc_pci_function_reset(phba);
6588 if (unlikely(rc))
6589 return -ENODEV;
6590
6591 /* Check the HBA Host Status Register for readyness */
6592 rc = lpfc_sli4_post_status_check(phba);
6593 if (unlikely(rc))
6594 return -ENODEV;
6595 else {
6596 spin_lock_irq(&phba->hbalock);
6597 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6598 spin_unlock_irq(&phba->hbalock);
6599 }
6600
6601 /*
6602 * Allocate a single mailbox container for initializing the
6603 * port.
6604 */
6605 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6606 if (!mboxq)
6607 return -ENOMEM;
6608
James Smartda0436e2009-05-22 14:51:39 -04006609 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006610 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006611 vpd = kzalloc(vpd_size, GFP_KERNEL);
6612 if (!vpd) {
6613 rc = -ENOMEM;
6614 goto out_free_mbox;
6615 }
6616
6617 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006618 if (unlikely(rc)) {
6619 kfree(vpd);
6620 goto out_free_mbox;
6621 }
James Smart572709e2013-07-15 18:32:43 -04006622
James Smartda0436e2009-05-22 14:51:39 -04006623 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006624 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006625 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006626 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006627 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6628 } else {
James Smart76a95d72010-11-20 23:11:48 -05006629 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006630 }
James Smart45ed1192009-10-02 15:17:02 -04006631
6632 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6633 LPFC_DCBX_CEE_MODE)
6634 phba->hba_flag |= HBA_FIP_SUPPORT;
6635 else
6636 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6637
James Smart4f2e66c2012-05-09 21:17:07 -04006638 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6639
James Smartc31098c2011-04-16 11:03:33 -04006640 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006641 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6642 "0376 READ_REV Error. SLI Level %d "
6643 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006644 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006645 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006646 kfree(vpd);
6647 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006648 }
James Smartcd1c8302011-10-10 21:33:25 -04006649
6650 /*
James Smartff78d8f2011-12-13 13:21:35 -05006651 * Continue initialization with default values even if driver failed
6652 * to read FCoE param config regions, only read parameters if the
6653 * board is FCoE
6654 */
6655 if (phba->hba_flag & HBA_FCOE_MODE &&
6656 lpfc_sli4_read_fcoe_params(phba))
6657 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6658 "2570 Failed to read FCoE parameters\n");
6659
6660 /*
James Smartcd1c8302011-10-10 21:33:25 -04006661 * Retrieve sli4 device physical port name, failure of doing it
6662 * is considered as non-fatal.
6663 */
6664 rc = lpfc_sli4_retrieve_pport_name(phba);
6665 if (!rc)
6666 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6667 "3080 Successful retrieving SLI4 device "
6668 "physical port name: %s.\n", phba->Port);
6669
James Smartda0436e2009-05-22 14:51:39 -04006670 /*
6671 * Evaluate the read rev and vpd data. Populate the driver
6672 * state with the results. If this routine fails, the failure
6673 * is not fatal as the driver will use generic values.
6674 */
6675 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6676 if (unlikely(!rc)) {
6677 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6678 "0377 Error %d parsing vpd. "
6679 "Using defaults.\n", rc);
6680 rc = 0;
6681 }
James Smart76a95d72010-11-20 23:11:48 -05006682 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006683
James Smartf1126682009-06-10 17:22:44 -04006684 /* Save information as VPD data */
6685 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6686 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6687 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6688 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6689 &mqe->un.read_rev);
6690 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6691 &mqe->un.read_rev);
6692 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6693 &mqe->un.read_rev);
6694 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6695 &mqe->un.read_rev);
6696 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6697 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6698 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6699 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6700 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6701 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6702 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6703 "(%d):0380 READ_REV Status x%x "
6704 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6705 mboxq->vport ? mboxq->vport->vpi : 0,
6706 bf_get(lpfc_mqe_status, mqe),
6707 phba->vpd.rev.opFwName,
6708 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6709 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006710
James Smart572709e2013-07-15 18:32:43 -04006711 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6712 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6713 if (phba->pport->cfg_lun_queue_depth > rc) {
6714 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6715 "3362 LUN queue depth changed from %d to %d\n",
6716 phba->pport->cfg_lun_queue_depth, rc);
6717 phba->pport->cfg_lun_queue_depth = rc;
6718 }
6719
James Smart65791f12016-07-06 12:35:56 -07006720 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006721 LPFC_SLI_INTF_IF_TYPE_0) {
6722 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6723 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6724 if (rc == MBX_SUCCESS) {
6725 phba->hba_flag |= HBA_RECOVERABLE_UE;
6726 /* Set 1Sec interval to detect UE */
6727 phba->eratt_poll_interval = 1;
6728 phba->sli4_hba.ue_to_sr = bf_get(
6729 lpfc_mbx_set_feature_UESR,
6730 &mboxq->u.mqe.un.set_feature);
6731 phba->sli4_hba.ue_to_rp = bf_get(
6732 lpfc_mbx_set_feature_UERP,
6733 &mboxq->u.mqe.un.set_feature);
6734 }
6735 }
6736
6737 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6738 /* Enable MDS Diagnostics only if the SLI Port supports it */
6739 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6740 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6741 if (rc != MBX_SUCCESS)
6742 phba->mds_diags_support = 0;
6743 }
James Smart572709e2013-07-15 18:32:43 -04006744
James Smartda0436e2009-05-22 14:51:39 -04006745 /*
6746 * Discover the port's supported feature set and match it against the
6747 * hosts requests.
6748 */
6749 lpfc_request_features(phba, mboxq);
6750 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6751 if (unlikely(rc)) {
6752 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006753 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006754 }
6755
6756 /*
6757 * The port must support FCP initiator mode as this is the
6758 * only mode running in the host.
6759 */
6760 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6761 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6762 "0378 No support for fcpi mode.\n");
6763 ftr_rsp++;
6764 }
James Smartfedd3b72011-02-16 12:39:24 -05006765 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6766 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6767 else
6768 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006769 /*
6770 * If the port cannot support the host's requested features
6771 * then turn off the global config parameters to disable the
6772 * feature in the driver. This is not a fatal error.
6773 */
James Smartbf086112011-08-21 21:48:13 -04006774 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6775 if (phba->cfg_enable_bg) {
6776 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6777 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6778 else
6779 ftr_rsp++;
6780 }
James Smartda0436e2009-05-22 14:51:39 -04006781
6782 if (phba->max_vpi && phba->cfg_enable_npiv &&
6783 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6784 ftr_rsp++;
6785
6786 if (ftr_rsp) {
6787 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6788 "0379 Feature Mismatch Data: x%08x %08x "
6789 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6790 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6791 phba->cfg_enable_npiv, phba->max_vpi);
6792 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6793 phba->cfg_enable_bg = 0;
6794 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6795 phba->cfg_enable_npiv = 0;
6796 }
6797
6798 /* These SLI3 features are assumed in SLI4 */
6799 spin_lock_irq(&phba->hbalock);
6800 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6801 spin_unlock_irq(&phba->hbalock);
6802
James Smart6d368e52011-05-24 11:44:12 -04006803 /*
6804 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6805 * calls depends on these resources to complete port setup.
6806 */
6807 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6808 if (rc) {
6809 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6810 "2920 Failed to alloc Resource IDs "
6811 "rc = x%x\n", rc);
6812 goto out_free_mbox;
6813 }
6814
James Smart61bda8f2016-10-13 15:06:05 -07006815 lpfc_set_host_data(phba, mboxq);
6816
6817 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6818 if (rc) {
6819 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6820 "2134 Failed to set host os driver version %x",
6821 rc);
6822 }
6823
James Smartda0436e2009-05-22 14:51:39 -04006824 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006825 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6826 if (rc) {
6827 phba->link_state = LPFC_HBA_ERROR;
6828 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006829 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006830 }
6831
James Smartda0436e2009-05-22 14:51:39 -04006832 mboxq->vport = vport;
6833 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6834 mp = (struct lpfc_dmabuf *) mboxq->context1;
6835 if (rc == MBX_SUCCESS) {
6836 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6837 rc = 0;
6838 }
6839
6840 /*
6841 * This memory was allocated by the lpfc_read_sparam routine. Release
6842 * it to the mbuf pool.
6843 */
6844 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6845 kfree(mp);
6846 mboxq->context1 = NULL;
6847 if (unlikely(rc)) {
6848 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6849 "0382 READ_SPARAM command failed "
6850 "status %d, mbxStatus x%x\n",
6851 rc, bf_get(lpfc_mqe_status, mqe));
6852 phba->link_state = LPFC_HBA_ERROR;
6853 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006854 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006855 }
6856
James Smart05580562011-05-24 11:40:48 -04006857 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006858
6859 /* Update the fc_host data structures with new wwn. */
6860 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6861 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6862
James Smart895427b2017-02-12 13:52:30 -08006863 /* Create all the SLI4 queues */
6864 rc = lpfc_sli4_queue_create(phba);
6865 if (rc) {
6866 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6867 "3089 Failed to allocate queues\n");
6868 rc = -ENODEV;
6869 goto out_free_mbox;
6870 }
6871 /* Set up all the queues to the device */
6872 rc = lpfc_sli4_queue_setup(phba);
6873 if (unlikely(rc)) {
6874 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6875 "0381 Error %d during queue setup.\n ", rc);
6876 goto out_stop_timers;
6877 }
6878 /* Initialize the driver internal SLI layer lists. */
6879 lpfc_sli4_setup(phba);
6880 lpfc_sli4_queue_init(phba);
6881
6882 /* update host els xri-sgl sizes and mappings */
6883 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006884 if (unlikely(rc)) {
6885 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6886 "1400 Failed to update xri-sgl size and "
6887 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08006888 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006889 }
6890
James Smart8a9d2e82012-05-09 21:16:12 -04006891 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08006892 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6893 phba->sli4_hba.els_xri_cnt);
6894 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04006895 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6896 "0582 Error %d during els sgl post "
6897 "operation\n", rc);
6898 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006899 goto out_destroy_queue;
6900 }
6901 phba->sli4_hba.els_xri_cnt = rc;
6902
James Smartf358dd02017-02-12 13:52:34 -08006903 if (phba->nvmet_support) {
6904 /* update host nvmet xri-sgl sizes and mappings */
6905 rc = lpfc_sli4_nvmet_sgl_update(phba);
6906 if (unlikely(rc)) {
6907 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6908 "6308 Failed to update nvmet-sgl size "
6909 "and mapping: %d\n", rc);
6910 goto out_destroy_queue;
6911 }
6912
6913 /* register the nvmet sgl pool to the port */
6914 rc = lpfc_sli4_repost_sgl_list(
6915 phba,
6916 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6917 phba->sli4_hba.nvmet_xri_cnt);
6918 if (unlikely(rc < 0)) {
6919 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6920 "3117 Error %d during nvmet "
6921 "sgl post\n", rc);
6922 rc = -ENODEV;
6923 goto out_destroy_queue;
6924 }
6925 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07006926
6927 cnt = phba->cfg_iocb_cnt * 1024;
6928 /* We need 1 iocbq for every SGL, for IO processing */
6929 cnt += phba->sli4_hba.nvmet_xri_cnt;
6930 /* Initialize and populate the iocb list per host */
6931 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6932 "2821 initialize iocb list %d total %d\n",
6933 phba->cfg_iocb_cnt, cnt);
6934 rc = lpfc_init_iocb_list(phba, cnt);
6935 if (rc) {
6936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6937 "1413 Failed to init iocb list.\n");
6938 goto out_destroy_queue;
6939 }
6940
James Smartd613b6a2017-02-12 13:52:37 -08006941 lpfc_nvmet_create_targetport(phba);
James Smartf358dd02017-02-12 13:52:34 -08006942 } else {
James Smart895427b2017-02-12 13:52:30 -08006943 /* update host scsi xri-sgl sizes and mappings */
6944 rc = lpfc_sli4_scsi_sgl_update(phba);
6945 if (unlikely(rc)) {
6946 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6947 "6309 Failed to update scsi-sgl size "
6948 "and mapping: %d\n", rc);
6949 goto out_destroy_queue;
6950 }
6951
6952 /* update host nvme xri-sgl sizes and mappings */
6953 rc = lpfc_sli4_nvme_sgl_update(phba);
6954 if (unlikely(rc)) {
6955 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6956 "6082 Failed to update nvme-sgl size "
6957 "and mapping: %d\n", rc);
6958 goto out_destroy_queue;
6959 }
James Smart6c621a22017-05-15 15:20:45 -07006960
6961 cnt = phba->cfg_iocb_cnt * 1024;
6962 /* Initialize and populate the iocb list per host */
6963 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6964 "2820 initialize iocb list %d total %d\n",
6965 phba->cfg_iocb_cnt, cnt);
6966 rc = lpfc_init_iocb_list(phba, cnt);
6967 if (rc) {
6968 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6969 "6301 Failed to init iocb list.\n");
6970 goto out_destroy_queue;
6971 }
James Smart8a9d2e82012-05-09 21:16:12 -04006972 }
6973
James Smart2d7dbc42017-02-12 13:52:35 -08006974 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08006975 /* Post initial buffers to all RQs created */
6976 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
6977 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
6978 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
6979 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
6980 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07006981 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08006982 rqbp->buffer_count = 0;
6983
James Smart2d7dbc42017-02-12 13:52:35 -08006984 lpfc_post_rq_buffer(
6985 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
6986 phba->sli4_hba.nvmet_mrq_data[i],
James Smarta8cf5df2017-05-15 15:20:46 -07006987 LPFC_NVMET_RQE_DEF_COUNT, i);
James Smart2d7dbc42017-02-12 13:52:35 -08006988 }
6989 }
6990
James Smart895427b2017-02-12 13:52:30 -08006991 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6992 /* register the allocated scsi sgl pool to the port */
6993 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6994 if (unlikely(rc)) {
6995 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6996 "0383 Error %d during scsi sgl post "
6997 "operation\n", rc);
6998 /* Some Scsi buffers were moved to abort scsi list */
6999 /* A pci function reset will repost them */
7000 rc = -ENODEV;
7001 goto out_destroy_queue;
7002 }
James Smartda0436e2009-05-22 14:51:39 -04007003 }
7004
James Smart01649562017-02-12 13:52:32 -08007005 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7006 (phba->nvmet_support == 0)) {
7007
7008 /* register the allocated nvme sgl pool to the port */
7009 rc = lpfc_repost_nvme_sgl_list(phba);
7010 if (unlikely(rc)) {
7011 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7012 "6116 Error %d during nvme sgl post "
7013 "operation\n", rc);
7014 /* Some NVME buffers were moved to abort nvme list */
7015 /* A pci function reset will repost them */
7016 rc = -ENODEV;
7017 goto out_destroy_queue;
7018 }
James Smartda0436e2009-05-22 14:51:39 -04007019 }
7020
7021 /* Post the rpi header region to the device. */
7022 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7023 if (unlikely(rc)) {
7024 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7025 "0393 Error %d during rpi post operation\n",
7026 rc);
7027 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007028 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007029 }
James Smart97f2ecf2012-03-01 22:35:23 -05007030 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007031
James Smart895427b2017-02-12 13:52:30 -08007032 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007033 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007034 /*
7035 * The FC Port needs to register FCFI (index 0)
7036 */
7037 lpfc_reg_fcfi(phba, mboxq);
7038 mboxq->vport = phba->pport;
7039 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7040 if (rc != MBX_SUCCESS)
7041 goto out_unset_queue;
7042 rc = 0;
7043 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7044 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007045 } else {
7046 /* We are a NVME Target mode with MRQ > 1 */
7047
7048 /* First register the FCFI */
7049 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7050 mboxq->vport = phba->pport;
7051 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7052 if (rc != MBX_SUCCESS)
7053 goto out_unset_queue;
7054 rc = 0;
7055 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7056 &mboxq->u.mqe.un.reg_fcfi_mrq);
7057
7058 /* Next register the MRQs */
7059 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7060 mboxq->vport = phba->pport;
7061 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7062 if (rc != MBX_SUCCESS)
7063 goto out_unset_queue;
7064 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007065 }
7066 /* Check if the port is configured to be disabled */
7067 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007068 }
7069
7070 /* Arm the CQs and then EQs on device */
7071 lpfc_sli4_arm_cqeq_intr(phba);
7072
7073 /* Indicate device interrupt mode */
7074 phba->sli4_hba.intr_enable = 1;
7075
7076 /* Allow asynchronous mailbox command to go through */
7077 spin_lock_irq(&phba->hbalock);
7078 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7079 spin_unlock_irq(&phba->hbalock);
7080
7081 /* Post receive buffers to the device */
7082 lpfc_sli4_rb_setup(phba);
7083
James Smartfc2b9892010-02-26 14:15:29 -05007084 /* Reset HBA FCF states after HBA reset */
7085 phba->fcf.fcf_flag = 0;
7086 phba->fcf.current_rec.flag = 0;
7087
James Smartda0436e2009-05-22 14:51:39 -04007088 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007089 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007090 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007091
7092 /* Start heart beat timer */
7093 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007094 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007095 phba->hb_outstanding = 0;
7096 phba->last_completion_time = jiffies;
7097
7098 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007099 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007100 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007101
James Smart75baf692010-06-08 18:31:21 -04007102 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7103 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7104 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7105 if (!rc) {
7106 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7107 "2829 This device supports "
7108 "Advanced Error Reporting (AER)\n");
7109 spin_lock_irq(&phba->hbalock);
7110 phba->hba_flag |= HBA_AER_ENABLED;
7111 spin_unlock_irq(&phba->hbalock);
7112 } else {
7113 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7114 "2830 This device does not support "
7115 "Advanced Error Reporting (AER)\n");
7116 phba->cfg_aer_support = 0;
7117 }
James Smart0a96e972011-07-22 18:37:28 -04007118 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007119 }
7120
James Smartda0436e2009-05-22 14:51:39 -04007121 /*
7122 * The port is ready, set the host's link state to LINK_DOWN
7123 * in preparation for link interrupts.
7124 */
James Smartda0436e2009-05-22 14:51:39 -04007125 spin_lock_irq(&phba->hbalock);
7126 phba->link_state = LPFC_LINK_DOWN;
7127 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007128 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7129 (phba->hba_flag & LINK_DISABLED)) {
7130 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7131 "3103 Adapter Link is disabled.\n");
7132 lpfc_down_link(phba, mboxq);
7133 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7134 if (rc != MBX_SUCCESS) {
7135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7136 "3104 Adapter failed to issue "
7137 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7138 goto out_unset_queue;
7139 }
7140 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007141 /* don't perform init_link on SLI4 FC port loopback test */
7142 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7143 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7144 if (rc)
7145 goto out_unset_queue;
7146 }
James Smart5350d872011-10-10 21:33:49 -04007147 }
7148 mempool_free(mboxq, phba->mbox_mem_pool);
7149 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007150out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007151 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007152 lpfc_sli4_queue_unset(phba);
7153out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007154 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007155 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007156out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007157 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007158out_free_mbox:
7159 mempool_free(mboxq, phba->mbox_mem_pool);
7160 return rc;
7161}
James Smarte59058c2008-08-24 21:49:00 -04007162
7163/**
James Smart3621a712009-04-06 18:47:14 -04007164 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007165 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007166 *
James Smarte59058c2008-08-24 21:49:00 -04007167 * This is the callback function for mailbox timer. The mailbox
7168 * timer is armed when a new mailbox command is issued and the timer
7169 * is deleted when the mailbox complete. The function is called by
7170 * the kernel timer code when a mailbox does not complete within
7171 * expected time. This function wakes up the worker thread to
7172 * process the mailbox timeout and returns. All the processing is
7173 * done by the worker thread function lpfc_mbox_timeout_handler.
7174 **/
dea31012005-04-17 16:05:31 -05007175void
7176lpfc_mbox_timeout(unsigned long ptr)
7177{
James Smart92d7f7b2007-06-17 19:56:38 -05007178 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05007179 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007180 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007181
James Smart2e0fef82007-06-17 19:56:36 -05007182 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007183 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007184 if (!tmo_posted)
7185 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7186 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7187
James Smart5e9d9b82008-06-14 22:52:53 -04007188 if (!tmo_posted)
7189 lpfc_worker_wake_up(phba);
7190 return;
dea31012005-04-17 16:05:31 -05007191}
7192
James Smarte8d3c3b2013-10-10 12:21:30 -04007193/**
7194 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7195 * are pending
7196 * @phba: Pointer to HBA context object.
7197 *
7198 * This function checks if any mailbox completions are present on the mailbox
7199 * completion queue.
7200 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007201static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007202lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7203{
7204
7205 uint32_t idx;
7206 struct lpfc_queue *mcq;
7207 struct lpfc_mcqe *mcqe;
7208 bool pending_completions = false;
7209
7210 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7211 return false;
7212
7213 /* Check for completions on mailbox completion queue */
7214
7215 mcq = phba->sli4_hba.mbx_cq;
7216 idx = mcq->hba_index;
7217 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7218 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7219 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7220 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7221 pending_completions = true;
7222 break;
7223 }
7224 idx = (idx + 1) % mcq->entry_count;
7225 if (mcq->hba_index == idx)
7226 break;
7227 }
7228 return pending_completions;
7229
7230}
7231
7232/**
7233 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7234 * that were missed.
7235 * @phba: Pointer to HBA context object.
7236 *
7237 * For sli4, it is possible to miss an interrupt. As such mbox completions
7238 * maybe missed causing erroneous mailbox timeouts to occur. This function
7239 * checks to see if mbox completions are on the mailbox completion queue
7240 * and will process all the completions associated with the eq for the
7241 * mailbox completion queue.
7242 **/
7243bool
7244lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7245{
7246
7247 uint32_t eqidx;
7248 struct lpfc_queue *fpeq = NULL;
7249 struct lpfc_eqe *eqe;
7250 bool mbox_pending;
7251
7252 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7253 return false;
7254
7255 /* Find the eq associated with the mcq */
7256
7257 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007258 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007259 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7260 phba->sli4_hba.mbx_cq->assoc_qid) {
7261 fpeq = phba->sli4_hba.hba_eq[eqidx];
7262 break;
7263 }
7264 if (!fpeq)
7265 return false;
7266
7267 /* Turn off interrupts from this EQ */
7268
7269 lpfc_sli4_eq_clr_intr(fpeq);
7270
7271 /* Check to see if a mbox completion is pending */
7272
7273 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7274
7275 /*
7276 * If a mbox completion is pending, process all the events on EQ
7277 * associated with the mbox completion queue (this could include
7278 * mailbox commands, async events, els commands, receive queue data
7279 * and fcp commands)
7280 */
7281
7282 if (mbox_pending)
7283 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7284 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7285 fpeq->EQ_processed++;
7286 }
7287
7288 /* Always clear and re-arm the EQ */
7289
7290 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7291
7292 return mbox_pending;
7293
7294}
James Smarte59058c2008-08-24 21:49:00 -04007295
7296/**
James Smart3621a712009-04-06 18:47:14 -04007297 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007298 * @phba: Pointer to HBA context object.
7299 *
7300 * This function is called from worker thread when a mailbox command times out.
7301 * The caller is not required to hold any locks. This function will reset the
7302 * HBA and recover all the pending commands.
7303 **/
dea31012005-04-17 16:05:31 -05007304void
7305lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7306{
James Smart2e0fef82007-06-17 19:56:36 -05007307 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007308 MAILBOX_t *mb = NULL;
7309
James Smart1dcb58e2007-04-25 09:51:30 -04007310 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007311
James Smarte8d3c3b2013-10-10 12:21:30 -04007312 /* If the mailbox completed, process the completion and return */
7313 if (lpfc_sli4_process_missed_mbox_completions(phba))
7314 return;
7315
James Smarteb016562014-09-03 12:58:06 -04007316 if (pmbox != NULL)
7317 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007318 /* Check the pmbox pointer first. There is a race condition
7319 * between the mbox timeout handler getting executed in the
7320 * worklist and the mailbox actually completing. When this
7321 * race condition occurs, the mbox_active will be NULL.
7322 */
7323 spin_lock_irq(&phba->hbalock);
7324 if (pmbox == NULL) {
7325 lpfc_printf_log(phba, KERN_WARNING,
7326 LOG_MBOX | LOG_SLI,
7327 "0353 Active Mailbox cleared - mailbox timeout "
7328 "exiting\n");
7329 spin_unlock_irq(&phba->hbalock);
7330 return;
7331 }
7332
dea31012005-04-17 16:05:31 -05007333 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007334 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007335 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007336 mb->mbxCommand,
7337 phba->pport->port_state,
7338 phba->sli.sli_flag,
7339 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007340 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007341
James Smart1dcb58e2007-04-25 09:51:30 -04007342 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7343 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007344 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007345 */
James Smart2e0fef82007-06-17 19:56:36 -05007346 spin_lock_irq(&phba->pport->work_port_lock);
7347 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7348 spin_unlock_irq(&phba->pport->work_port_lock);
7349 spin_lock_irq(&phba->hbalock);
7350 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007351 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007352 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007353
James Smartdb55fba2014-04-04 13:52:02 -04007354 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007355
7356 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007357 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007358
7359 /* Reset the HBA device */
7360 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007361}
7362
James Smarte59058c2008-08-24 21:49:00 -04007363/**
James Smart3772a992009-05-22 14:50:54 -04007364 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007365 * @phba: Pointer to HBA context object.
7366 * @pmbox: Pointer to mailbox object.
7367 * @flag: Flag indicating how the mailbox need to be processed.
7368 *
7369 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007370 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7371 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007372 * The mailbox command can be submitted in polling mode, in which case
7373 * this function will wait in a polling loop for the completion of the
7374 * mailbox.
7375 * If the mailbox is submitted in no_wait mode (not polling) the
7376 * function will submit the command and returns immediately without waiting
7377 * for the mailbox completion. The no_wait is supported only when HBA
7378 * is in SLI2/SLI3 mode - interrupts are enabled.
7379 * The SLI interface allows only one mailbox pending at a time. If the
7380 * mailbox is issued in polling mode and there is already a mailbox
7381 * pending, then the function will return an error. If the mailbox is issued
7382 * in NO_WAIT mode and there is a mailbox pending already, the function
7383 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7384 * The sli layer owns the mailbox object until the completion of mailbox
7385 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7386 * return codes the caller owns the mailbox command after the return of
7387 * the function.
7388 **/
James Smart3772a992009-05-22 14:50:54 -04007389static int
7390lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7391 uint32_t flag)
dea31012005-04-17 16:05:31 -05007392{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007393 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007394 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007395 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007396 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007397 int i;
James Smart09372822008-01-11 01:52:54 -05007398 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007399 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007400 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007401 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007402 int processing_queue = 0;
7403
7404 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7405 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007406 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007407 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007408 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7409 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7410 return MBX_SUCCESS;
7411 }
James Smart58da1ff2008-04-07 10:15:56 -04007412 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007413 pmbox = lpfc_mbox_get(phba);
7414 if (!pmbox) {
7415 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7416 return MBX_SUCCESS;
7417 }
7418 }
dea31012005-04-17 16:05:31 -05007419
James Smarted957682007-06-17 19:56:37 -05007420 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007421 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007422 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007423 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007424 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007425 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007426 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007427 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007428 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007429 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007430 }
7431 }
7432
Linas Vepstas8d63f372007-02-14 14:28:36 -06007433 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007434 if (unlikely(pci_channel_offline(phba->pcidev))) {
7435 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7436 goto out_not_finished;
7437 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007438
James Smarta257bf92009-04-06 18:48:10 -04007439 /* If HBA has a deferred error attention, fail the iocb. */
7440 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7441 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7442 goto out_not_finished;
7443 }
7444
dea31012005-04-17 16:05:31 -05007445 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007446
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007447 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007448 status = MBX_SUCCESS;
7449
James Smart2e0fef82007-06-17 19:56:36 -05007450 if (phba->link_state == LPFC_HBA_ERROR) {
7451 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007452
7453 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007454 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7455 "(%d):0311 Mailbox command x%x cannot "
7456 "issue Data: x%x x%x\n",
7457 pmbox->vport ? pmbox->vport->vpi : 0,
7458 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007459 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007460 }
7461
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007462 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007463 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7464 !(hc_copy & HC_MBINT_ENA)) {
7465 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7466 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007467 "(%d):2528 Mailbox command x%x cannot "
7468 "issue Data: x%x x%x\n",
7469 pmbox->vport ? pmbox->vport->vpi : 0,
7470 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007471 goto out_not_finished;
7472 }
James Smart92908312006-03-07 15:04:13 -05007473 }
7474
dea31012005-04-17 16:05:31 -05007475 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7476 /* Polling for a mbox command when another one is already active
7477 * is not allowed in SLI. Also, the driver must have established
7478 * SLI2 mode to queue and process multiple mbox commands.
7479 */
7480
7481 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007482 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007483
7484 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007485 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7486 "(%d):2529 Mailbox command x%x "
7487 "cannot issue Data: x%x x%x\n",
7488 pmbox->vport ? pmbox->vport->vpi : 0,
7489 pmbox->u.mb.mbxCommand,
7490 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007491 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007492 }
7493
James Smart3772a992009-05-22 14:50:54 -04007494 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007495 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007496 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007497 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7498 "(%d):2530 Mailbox command x%x "
7499 "cannot issue Data: x%x x%x\n",
7500 pmbox->vport ? pmbox->vport->vpi : 0,
7501 pmbox->u.mb.mbxCommand,
7502 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007503 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007504 }
7505
dea31012005-04-17 16:05:31 -05007506 /* Another mailbox command is still being processed, queue this
7507 * command to be processed later.
7508 */
7509 lpfc_mbox_put(phba, pmbox);
7510
7511 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007512 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007513 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007514 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007515 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07007516 mbx->mbxCommand,
7517 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007518 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007519
7520 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007521 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007522
James Smart858c9f62007-06-17 19:56:39 -05007523 if (pmbox->vport) {
7524 lpfc_debugfs_disc_trc(pmbox->vport,
7525 LPFC_DISC_TRC_MBOX_VPORT,
7526 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007527 (uint32_t)mbx->mbxCommand,
7528 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007529 }
7530 else {
7531 lpfc_debugfs_disc_trc(phba->pport,
7532 LPFC_DISC_TRC_MBOX,
7533 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007534 (uint32_t)mbx->mbxCommand,
7535 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007536 }
7537
James Smart2e0fef82007-06-17 19:56:36 -05007538 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007539 }
7540
dea31012005-04-17 16:05:31 -05007541 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7542
7543 /* If we are not polling, we MUST be in SLI2 mode */
7544 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007545 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007546 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007547 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007548 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007549 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007550 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7551 "(%d):2531 Mailbox command x%x "
7552 "cannot issue Data: x%x x%x\n",
7553 pmbox->vport ? pmbox->vport->vpi : 0,
7554 pmbox->u.mb.mbxCommand,
7555 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007556 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007557 }
7558 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007559 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7560 1000);
7561 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007562 }
7563
7564 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007565 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007566 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007567 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007568 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07007569 mbx->mbxCommand,
7570 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007571 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007572
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007573 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007574 if (pmbox->vport) {
7575 lpfc_debugfs_disc_trc(pmbox->vport,
7576 LPFC_DISC_TRC_MBOX_VPORT,
7577 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007578 (uint32_t)mbx->mbxCommand,
7579 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007580 }
7581 else {
7582 lpfc_debugfs_disc_trc(phba->pport,
7583 LPFC_DISC_TRC_MBOX,
7584 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007585 (uint32_t)mbx->mbxCommand,
7586 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007587 }
7588 }
7589
dea31012005-04-17 16:05:31 -05007590 psli->slistat.mbox_cmd++;
7591 evtctr = psli->slistat.mbox_event;
7592
7593 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007594 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007595
James Smart3772a992009-05-22 14:50:54 -04007596 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007597 /* Populate mbox extension offset word. */
7598 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007599 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007600 = (uint8_t *)phba->mbox_ext
7601 - (uint8_t *)phba->mbox;
7602 }
7603
7604 /* Copy the mailbox extension data */
7605 if (pmbox->in_ext_byte_len && pmbox->context2) {
7606 lpfc_sli_pcimem_bcopy(pmbox->context2,
7607 (uint8_t *)phba->mbox_ext,
7608 pmbox->in_ext_byte_len);
7609 }
7610 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007611 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007612 } else {
James Smart7a470272010-03-15 11:25:20 -04007613 /* Populate mbox extension offset word. */
7614 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007615 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007616 = MAILBOX_HBA_EXT_OFFSET;
7617
7618 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007619 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007620 lpfc_memcpy_to_slim(phba->MBslimaddr +
7621 MAILBOX_HBA_EXT_OFFSET,
7622 pmbox->context2, pmbox->in_ext_byte_len);
7623
James Smart895427b2017-02-12 13:52:30 -08007624 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007625 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007626 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7627 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007628
7629 /* First copy mbox command data to HBA SLIM, skip past first
7630 word */
7631 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007632 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007633 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7634
7635 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007636 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007637 to_slim = phba->MBslimaddr;
7638 writel(ldata, to_slim);
7639 readl(to_slim); /* flush */
7640
James Smart895427b2017-02-12 13:52:30 -08007641 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007642 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007643 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007644 }
7645
7646 wmb();
dea31012005-04-17 16:05:31 -05007647
7648 switch (flag) {
7649 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007650 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007651 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007652 /* Interrupt board to do it */
7653 writel(CA_MBATT, phba->CAregaddr);
7654 readl(phba->CAregaddr); /* flush */
7655 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007656 break;
7657
7658 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007659 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007660 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007661 /* Interrupt board to do it */
7662 writel(CA_MBATT, phba->CAregaddr);
7663 readl(phba->CAregaddr); /* flush */
7664
James Smart3772a992009-05-22 14:50:54 -04007665 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007666 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007667 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007668 word0 = le32_to_cpu(word0);
7669 } else {
7670 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007671 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7672 spin_unlock_irqrestore(&phba->hbalock,
7673 drvr_flag);
7674 goto out_not_finished;
7675 }
dea31012005-04-17 16:05:31 -05007676 }
7677
7678 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007679 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7680 spin_unlock_irqrestore(&phba->hbalock,
7681 drvr_flag);
7682 goto out_not_finished;
7683 }
James Smarta183a152011-10-10 21:32:43 -04007684 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7685 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007686 i = 0;
dea31012005-04-17 16:05:31 -05007687 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007688 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7689 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007690 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007691 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007692 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007693 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007694 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007695 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007696 }
7697
7698 /* Check if we took a mbox interrupt while we were
7699 polling */
7700 if (((word0 & OWN_CHIP) != OWN_CHIP)
7701 && (evtctr != psli->slistat.mbox_event))
7702 break;
7703
James Smart09372822008-01-11 01:52:54 -05007704 if (i++ > 10) {
7705 spin_unlock_irqrestore(&phba->hbalock,
7706 drvr_flag);
7707 msleep(1);
7708 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7709 }
dea31012005-04-17 16:05:31 -05007710
James Smart3772a992009-05-22 14:50:54 -04007711 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007712 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007713 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007714 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007715 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007716 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007717 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007718 /* Check real SLIM for any errors */
7719 slimword0 = readl(phba->MBslimaddr);
7720 slimmb = (MAILBOX_t *) & slimword0;
7721 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7722 && slimmb->mbxStatus) {
7723 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007724 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007725 word0 = slimword0;
7726 }
7727 }
7728 } else {
7729 /* First copy command data */
7730 word0 = readl(phba->MBslimaddr);
7731 }
7732 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007733 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7734 spin_unlock_irqrestore(&phba->hbalock,
7735 drvr_flag);
7736 goto out_not_finished;
7737 }
dea31012005-04-17 16:05:31 -05007738 }
7739
James Smart3772a992009-05-22 14:50:54 -04007740 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007741 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007742 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7743 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007744 /* Copy the mailbox extension data */
7745 if (pmbox->out_ext_byte_len && pmbox->context2) {
7746 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7747 pmbox->context2,
7748 pmbox->out_ext_byte_len);
7749 }
dea31012005-04-17 16:05:31 -05007750 } else {
7751 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007752 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007753 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007754 /* Copy the mailbox extension data */
7755 if (pmbox->out_ext_byte_len && pmbox->context2) {
7756 lpfc_memcpy_from_slim(pmbox->context2,
7757 phba->MBslimaddr +
7758 MAILBOX_HBA_EXT_OFFSET,
7759 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007760 }
7761 }
7762
7763 writel(HA_MBATT, phba->HAregaddr);
7764 readl(phba->HAregaddr); /* flush */
7765
7766 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007767 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007768 }
7769
James Smart2e0fef82007-06-17 19:56:36 -05007770 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7771 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007772
7773out_not_finished:
7774 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007775 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007776 lpfc_mbox_cmpl_put(phba, pmbox);
7777 }
7778 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007779}
7780
James Smarte59058c2008-08-24 21:49:00 -04007781/**
James Smartf1126682009-06-10 17:22:44 -04007782 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7783 * @phba: Pointer to HBA context object.
7784 *
7785 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7786 * the driver internal pending mailbox queue. It will then try to wait out the
7787 * possible outstanding mailbox command before return.
7788 *
7789 * Returns:
7790 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7791 * the outstanding mailbox command timed out.
7792 **/
7793static int
7794lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7795{
7796 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007797 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007798 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007799
7800 /* Mark the asynchronous mailbox command posting as blocked */
7801 spin_lock_irq(&phba->hbalock);
7802 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007803 /* Determine how long we might wait for the active mailbox
7804 * command to be gracefully completed by firmware.
7805 */
James Smarta183a152011-10-10 21:32:43 -04007806 if (phba->sli.mbox_active)
7807 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7808 phba->sli.mbox_active) *
7809 1000) + jiffies;
7810 spin_unlock_irq(&phba->hbalock);
7811
James Smarte8d3c3b2013-10-10 12:21:30 -04007812 /* Make sure the mailbox is really active */
7813 if (timeout)
7814 lpfc_sli4_process_missed_mbox_completions(phba);
7815
James Smartf1126682009-06-10 17:22:44 -04007816 /* Wait for the outstnading mailbox command to complete */
7817 while (phba->sli.mbox_active) {
7818 /* Check active mailbox complete status every 2ms */
7819 msleep(2);
7820 if (time_after(jiffies, timeout)) {
7821 /* Timeout, marked the outstanding cmd not complete */
7822 rc = 1;
7823 break;
7824 }
7825 }
7826
7827 /* Can not cleanly block async mailbox command, fails it */
7828 if (rc) {
7829 spin_lock_irq(&phba->hbalock);
7830 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7831 spin_unlock_irq(&phba->hbalock);
7832 }
7833 return rc;
7834}
7835
7836/**
7837 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7838 * @phba: Pointer to HBA context object.
7839 *
7840 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7841 * commands from the driver internal pending mailbox queue. It makes sure
7842 * that there is no outstanding mailbox command before resuming posting
7843 * asynchronous mailbox commands. If, for any reason, there is outstanding
7844 * mailbox command, it will try to wait it out before resuming asynchronous
7845 * mailbox command posting.
7846 **/
7847static void
7848lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7849{
7850 struct lpfc_sli *psli = &phba->sli;
7851
7852 spin_lock_irq(&phba->hbalock);
7853 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7854 /* Asynchronous mailbox posting is not blocked, do nothing */
7855 spin_unlock_irq(&phba->hbalock);
7856 return;
7857 }
7858
7859 /* Outstanding synchronous mailbox command is guaranteed to be done,
7860 * successful or timeout, after timing-out the outstanding mailbox
7861 * command shall always be removed, so just unblock posting async
7862 * mailbox command and resume
7863 */
7864 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7865 spin_unlock_irq(&phba->hbalock);
7866
7867 /* wake up worker thread to post asynchronlous mailbox command */
7868 lpfc_worker_wake_up(phba);
7869}
7870
7871/**
James Smart2d843ed2012-09-29 11:29:06 -04007872 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7873 * @phba: Pointer to HBA context object.
7874 * @mboxq: Pointer to mailbox object.
7875 *
7876 * The function waits for the bootstrap mailbox register ready bit from
7877 * port for twice the regular mailbox command timeout value.
7878 *
7879 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7880 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7881 **/
7882static int
7883lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7884{
7885 uint32_t db_ready;
7886 unsigned long timeout;
7887 struct lpfc_register bmbx_reg;
7888
7889 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7890 * 1000) + jiffies;
7891
7892 do {
7893 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7894 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7895 if (!db_ready)
7896 msleep(2);
7897
7898 if (time_after(jiffies, timeout))
7899 return MBXERR_ERROR;
7900 } while (!db_ready);
7901
7902 return 0;
7903}
7904
7905/**
James Smartda0436e2009-05-22 14:51:39 -04007906 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7907 * @phba: Pointer to HBA context object.
7908 * @mboxq: Pointer to mailbox object.
7909 *
7910 * The function posts a mailbox to the port. The mailbox is expected
7911 * to be comletely filled in and ready for the port to operate on it.
7912 * This routine executes a synchronous completion operation on the
7913 * mailbox by polling for its completion.
7914 *
7915 * The caller must not be holding any locks when calling this routine.
7916 *
7917 * Returns:
7918 * MBX_SUCCESS - mailbox posted successfully
7919 * Any of the MBX error values.
7920 **/
7921static int
7922lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7923{
7924 int rc = MBX_SUCCESS;
7925 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007926 uint32_t mcqe_status;
7927 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007928 struct lpfc_sli *psli = &phba->sli;
7929 struct lpfc_mqe *mb = &mboxq->u.mqe;
7930 struct lpfc_bmbx_create *mbox_rgn;
7931 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007932
7933 /*
7934 * Only one mailbox can be active to the bootstrap mailbox region
7935 * at a time and there is no queueing provided.
7936 */
7937 spin_lock_irqsave(&phba->hbalock, iflag);
7938 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7939 spin_unlock_irqrestore(&phba->hbalock, iflag);
7940 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007941 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007942 "cannot issue Data: x%x x%x\n",
7943 mboxq->vport ? mboxq->vport->vpi : 0,
7944 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007945 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7946 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007947 psli->sli_flag, MBX_POLL);
7948 return MBXERR_ERROR;
7949 }
7950 /* The server grabs the token and owns it until release */
7951 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7952 phba->sli.mbox_active = mboxq;
7953 spin_unlock_irqrestore(&phba->hbalock, iflag);
7954
James Smart2d843ed2012-09-29 11:29:06 -04007955 /* wait for bootstrap mbox register for readyness */
7956 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7957 if (rc)
7958 goto exit;
7959
James Smartda0436e2009-05-22 14:51:39 -04007960 /*
7961 * Initialize the bootstrap memory region to avoid stale data areas
7962 * in the mailbox post. Then copy the caller's mailbox contents to
7963 * the bmbx mailbox region.
7964 */
7965 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7966 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7967 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7968 sizeof(struct lpfc_mqe));
7969
7970 /* Post the high mailbox dma address to the port and wait for ready. */
7971 dma_address = &phba->sli4_hba.bmbx.dma_address;
7972 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7973
James Smart2d843ed2012-09-29 11:29:06 -04007974 /* wait for bootstrap mbox register for hi-address write done */
7975 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7976 if (rc)
7977 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007978
7979 /* Post the low mailbox dma address to the port. */
7980 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007981
James Smart2d843ed2012-09-29 11:29:06 -04007982 /* wait for bootstrap mbox register for low address write done */
7983 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7984 if (rc)
7985 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007986
7987 /*
7988 * Read the CQ to ensure the mailbox has completed.
7989 * If so, update the mailbox status so that the upper layers
7990 * can complete the request normally.
7991 */
7992 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7993 sizeof(struct lpfc_mqe));
7994 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7995 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7996 sizeof(struct lpfc_mcqe));
7997 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007998 /*
7999 * When the CQE status indicates a failure and the mailbox status
8000 * indicates success then copy the CQE status into the mailbox status
8001 * (and prefix it with x4000).
8002 */
James Smartda0436e2009-05-22 14:51:39 -04008003 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008004 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8005 bf_set(lpfc_mqe_status, mb,
8006 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008007 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008008 } else
8009 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008010
8011 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008012 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008013 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8014 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008015 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8016 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8017 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008018 bf_get(lpfc_mqe_status, mb),
8019 mb->un.mb_words[0], mb->un.mb_words[1],
8020 mb->un.mb_words[2], mb->un.mb_words[3],
8021 mb->un.mb_words[4], mb->un.mb_words[5],
8022 mb->un.mb_words[6], mb->un.mb_words[7],
8023 mb->un.mb_words[8], mb->un.mb_words[9],
8024 mb->un.mb_words[10], mb->un.mb_words[11],
8025 mb->un.mb_words[12], mboxq->mcqe.word0,
8026 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8027 mboxq->mcqe.trailer);
8028exit:
8029 /* We are holding the token, no needed for lock when release */
8030 spin_lock_irqsave(&phba->hbalock, iflag);
8031 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8032 phba->sli.mbox_active = NULL;
8033 spin_unlock_irqrestore(&phba->hbalock, iflag);
8034 return rc;
8035}
8036
8037/**
8038 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8039 * @phba: Pointer to HBA context object.
8040 * @pmbox: Pointer to mailbox object.
8041 * @flag: Flag indicating how the mailbox need to be processed.
8042 *
8043 * This function is called by discovery code and HBA management code to submit
8044 * a mailbox command to firmware with SLI-4 interface spec.
8045 *
8046 * Return codes the caller owns the mailbox command after the return of the
8047 * function.
8048 **/
8049static int
8050lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8051 uint32_t flag)
8052{
8053 struct lpfc_sli *psli = &phba->sli;
8054 unsigned long iflags;
8055 int rc;
8056
James Smartb76f2dc2011-07-22 18:37:42 -04008057 /* dump from issue mailbox command if setup */
8058 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8059
James Smart8fa38512009-07-19 10:01:03 -04008060 rc = lpfc_mbox_dev_check(phba);
8061 if (unlikely(rc)) {
8062 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008063 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008064 "cannot issue Data: x%x x%x\n",
8065 mboxq->vport ? mboxq->vport->vpi : 0,
8066 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008067 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8068 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008069 psli->sli_flag, flag);
8070 goto out_not_finished;
8071 }
8072
James Smartda0436e2009-05-22 14:51:39 -04008073 /* Detect polling mode and jump to a handler */
8074 if (!phba->sli4_hba.intr_enable) {
8075 if (flag == MBX_POLL)
8076 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8077 else
8078 rc = -EIO;
8079 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008080 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008081 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008082 "(x%x/x%x) failure: "
8083 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8084 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008085 mboxq->vport ? mboxq->vport->vpi : 0,
8086 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008087 lpfc_sli_config_mbox_subsys_get(phba,
8088 mboxq),
8089 lpfc_sli_config_mbox_opcode_get(phba,
8090 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008091 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8092 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8093 bf_get(lpfc_mcqe_ext_status,
8094 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008095 psli->sli_flag, flag);
8096 return rc;
8097 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008098 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8099 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04008100 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04008101 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008102 mboxq->vport ? mboxq->vport->vpi : 0,
8103 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008104 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8105 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008106 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008107 /* Try to block the asynchronous mailbox posting */
8108 rc = lpfc_sli4_async_mbox_block(phba);
8109 if (!rc) {
8110 /* Successfully blocked, now issue sync mbox cmd */
8111 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8112 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008113 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008114 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008115 "(%d):2597 Sync Mailbox command "
8116 "x%x (x%x/x%x) failure: "
8117 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8118 "Data: x%x x%x\n,",
8119 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008120 mboxq->u.mb.mbxCommand,
8121 lpfc_sli_config_mbox_subsys_get(phba,
8122 mboxq),
8123 lpfc_sli_config_mbox_opcode_get(phba,
8124 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008125 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8126 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8127 bf_get(lpfc_mcqe_ext_status,
8128 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008129 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008130 /* Unblock the async mailbox posting afterward */
8131 lpfc_sli4_async_mbox_unblock(phba);
8132 }
8133 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008134 }
8135
8136 /* Now, interrupt mode asynchrous mailbox command */
8137 rc = lpfc_mbox_cmd_check(phba, mboxq);
8138 if (rc) {
8139 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008140 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008141 "cannot issue Data: x%x x%x\n",
8142 mboxq->vport ? mboxq->vport->vpi : 0,
8143 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008144 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8145 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008146 psli->sli_flag, flag);
8147 goto out_not_finished;
8148 }
James Smartda0436e2009-05-22 14:51:39 -04008149
8150 /* Put the mailbox command to the driver internal FIFO */
8151 psli->slistat.mbox_busy++;
8152 spin_lock_irqsave(&phba->hbalock, iflags);
8153 lpfc_mbox_put(phba, mboxq);
8154 spin_unlock_irqrestore(&phba->hbalock, iflags);
8155 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8156 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008157 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008158 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8159 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008160 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8161 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008162 phba->pport->port_state,
8163 psli->sli_flag, MBX_NOWAIT);
8164 /* Wake up worker thread to transport mailbox command from head */
8165 lpfc_worker_wake_up(phba);
8166
8167 return MBX_BUSY;
8168
8169out_not_finished:
8170 return MBX_NOT_FINISHED;
8171}
8172
8173/**
8174 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8175 * @phba: Pointer to HBA context object.
8176 *
8177 * This function is called by worker thread to send a mailbox command to
8178 * SLI4 HBA firmware.
8179 *
8180 **/
8181int
8182lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8183{
8184 struct lpfc_sli *psli = &phba->sli;
8185 LPFC_MBOXQ_t *mboxq;
8186 int rc = MBX_SUCCESS;
8187 unsigned long iflags;
8188 struct lpfc_mqe *mqe;
8189 uint32_t mbx_cmnd;
8190
8191 /* Check interrupt mode before post async mailbox command */
8192 if (unlikely(!phba->sli4_hba.intr_enable))
8193 return MBX_NOT_FINISHED;
8194
8195 /* Check for mailbox command service token */
8196 spin_lock_irqsave(&phba->hbalock, iflags);
8197 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8198 spin_unlock_irqrestore(&phba->hbalock, iflags);
8199 return MBX_NOT_FINISHED;
8200 }
8201 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8202 spin_unlock_irqrestore(&phba->hbalock, iflags);
8203 return MBX_NOT_FINISHED;
8204 }
8205 if (unlikely(phba->sli.mbox_active)) {
8206 spin_unlock_irqrestore(&phba->hbalock, iflags);
8207 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8208 "0384 There is pending active mailbox cmd\n");
8209 return MBX_NOT_FINISHED;
8210 }
8211 /* Take the mailbox command service token */
8212 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8213
8214 /* Get the next mailbox command from head of queue */
8215 mboxq = lpfc_mbox_get(phba);
8216
8217 /* If no more mailbox command waiting for post, we're done */
8218 if (!mboxq) {
8219 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8220 spin_unlock_irqrestore(&phba->hbalock, iflags);
8221 return MBX_SUCCESS;
8222 }
8223 phba->sli.mbox_active = mboxq;
8224 spin_unlock_irqrestore(&phba->hbalock, iflags);
8225
8226 /* Check device readiness for posting mailbox command */
8227 rc = lpfc_mbox_dev_check(phba);
8228 if (unlikely(rc))
8229 /* Driver clean routine will clean up pending mailbox */
8230 goto out_not_finished;
8231
8232 /* Prepare the mbox command to be posted */
8233 mqe = &mboxq->u.mqe;
8234 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8235
8236 /* Start timer for the mbox_tmo and log some mailbox post messages */
8237 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008238 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008239
8240 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008241 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008242 "x%x x%x\n",
8243 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008244 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8245 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008246 phba->pport->port_state, psli->sli_flag);
8247
8248 if (mbx_cmnd != MBX_HEARTBEAT) {
8249 if (mboxq->vport) {
8250 lpfc_debugfs_disc_trc(mboxq->vport,
8251 LPFC_DISC_TRC_MBOX_VPORT,
8252 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8253 mbx_cmnd, mqe->un.mb_words[0],
8254 mqe->un.mb_words[1]);
8255 } else {
8256 lpfc_debugfs_disc_trc(phba->pport,
8257 LPFC_DISC_TRC_MBOX,
8258 "MBOX Send: cmd:x%x mb:x%x x%x",
8259 mbx_cmnd, mqe->un.mb_words[0],
8260 mqe->un.mb_words[1]);
8261 }
8262 }
8263 psli->slistat.mbox_cmd++;
8264
8265 /* Post the mailbox command to the port */
8266 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8267 if (rc != MBX_SUCCESS) {
8268 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008269 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008270 "cannot issue Data: x%x x%x\n",
8271 mboxq->vport ? mboxq->vport->vpi : 0,
8272 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008273 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8274 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008275 psli->sli_flag, MBX_NOWAIT);
8276 goto out_not_finished;
8277 }
8278
8279 return rc;
8280
8281out_not_finished:
8282 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008283 if (phba->sli.mbox_active) {
8284 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8285 __lpfc_mbox_cmpl_put(phba, mboxq);
8286 /* Release the token */
8287 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8288 phba->sli.mbox_active = NULL;
8289 }
James Smartda0436e2009-05-22 14:51:39 -04008290 spin_unlock_irqrestore(&phba->hbalock, iflags);
8291
8292 return MBX_NOT_FINISHED;
8293}
8294
8295/**
8296 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8297 * @phba: Pointer to HBA context object.
8298 * @pmbox: Pointer to mailbox object.
8299 * @flag: Flag indicating how the mailbox need to be processed.
8300 *
8301 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8302 * the API jump table function pointer from the lpfc_hba struct.
8303 *
8304 * Return codes the caller owns the mailbox command after the return of the
8305 * function.
8306 **/
8307int
8308lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8309{
8310 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8311}
8312
8313/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008314 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008315 * @phba: The hba struct for which this call is being executed.
8316 * @dev_grp: The HBA PCI-Device group number.
8317 *
8318 * This routine sets up the mbox interface API function jump table in @phba
8319 * struct.
8320 * Returns: 0 - success, -ENODEV - failure.
8321 **/
8322int
8323lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8324{
8325
8326 switch (dev_grp) {
8327 case LPFC_PCI_DEV_LP:
8328 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8329 phba->lpfc_sli_handle_slow_ring_event =
8330 lpfc_sli_handle_slow_ring_event_s3;
8331 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8332 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8333 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8334 break;
8335 case LPFC_PCI_DEV_OC:
8336 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8337 phba->lpfc_sli_handle_slow_ring_event =
8338 lpfc_sli_handle_slow_ring_event_s4;
8339 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8340 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8341 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8342 break;
8343 default:
8344 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8345 "1420 Invalid HBA PCI-device group: 0x%x\n",
8346 dev_grp);
8347 return -ENODEV;
8348 break;
8349 }
8350 return 0;
8351}
8352
8353/**
James Smart3621a712009-04-06 18:47:14 -04008354 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008355 * @phba: Pointer to HBA context object.
8356 * @pring: Pointer to driver SLI ring object.
8357 * @piocb: Pointer to address of newly added command iocb.
8358 *
8359 * This function is called with hbalock held to add a command
8360 * iocb to the txq when SLI layer cannot submit the command iocb
8361 * to the ring.
8362 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008363void
James Smart92d7f7b2007-06-17 19:56:38 -05008364__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008365 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008366{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008367 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008368 /* Insert the caller's iocb in the txq tail for later processing. */
8369 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008370}
8371
James Smarte59058c2008-08-24 21:49:00 -04008372/**
James Smart3621a712009-04-06 18:47:14 -04008373 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008374 * @phba: Pointer to HBA context object.
8375 * @pring: Pointer to driver SLI ring object.
8376 * @piocb: Pointer to address of newly added command iocb.
8377 *
8378 * This function is called with hbalock held before a new
8379 * iocb is submitted to the firmware. This function checks
8380 * txq to flush the iocbs in txq to Firmware before
8381 * submitting new iocbs to the Firmware.
8382 * If there are iocbs in the txq which need to be submitted
8383 * to firmware, lpfc_sli_next_iocb returns the first element
8384 * of the txq after dequeuing it from txq.
8385 * If there is no iocb in the txq then the function will return
8386 * *piocb and *piocb is set to NULL. Caller needs to check
8387 * *piocb to find if there are more commands in the txq.
8388 **/
dea31012005-04-17 16:05:31 -05008389static struct lpfc_iocbq *
8390lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008391 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008392{
8393 struct lpfc_iocbq * nextiocb;
8394
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008395 lockdep_assert_held(&phba->hbalock);
8396
dea31012005-04-17 16:05:31 -05008397 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8398 if (!nextiocb) {
8399 nextiocb = *piocb;
8400 *piocb = NULL;
8401 }
8402
8403 return nextiocb;
8404}
8405
James Smarte59058c2008-08-24 21:49:00 -04008406/**
James Smart3772a992009-05-22 14:50:54 -04008407 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008408 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008409 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008410 * @piocb: Pointer to command iocb.
8411 * @flag: Flag indicating if this command can be put into txq.
8412 *
James Smart3772a992009-05-22 14:50:54 -04008413 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8414 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8415 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8416 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8417 * this function allows only iocbs for posting buffers. This function finds
8418 * next available slot in the command ring and posts the command to the
8419 * available slot and writes the port attention register to request HBA start
8420 * processing new iocb. If there is no slot available in the ring and
8421 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8422 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008423 *
James Smart3772a992009-05-22 14:50:54 -04008424 * This function is called with hbalock held. The function will return success
8425 * after it successfully submit the iocb to firmware or after adding to the
8426 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008427 **/
James Smart98c9ea52007-10-27 13:37:33 -04008428static int
James Smart3772a992009-05-22 14:50:54 -04008429__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008430 struct lpfc_iocbq *piocb, uint32_t flag)
8431{
8432 struct lpfc_iocbq *nextiocb;
8433 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008434 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008435
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008436 lockdep_assert_held(&phba->hbalock);
8437
James Smart92d7f7b2007-06-17 19:56:38 -05008438 if (piocb->iocb_cmpl && (!piocb->vport) &&
8439 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8440 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8441 lpfc_printf_log(phba, KERN_ERR,
8442 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008443 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008444 piocb->iocb.ulpCommand);
8445 dump_stack();
8446 return IOCB_ERROR;
8447 }
8448
8449
Linas Vepstas8d63f372007-02-14 14:28:36 -06008450 /* If the PCI channel is in offline state, do not post iocbs. */
8451 if (unlikely(pci_channel_offline(phba->pcidev)))
8452 return IOCB_ERROR;
8453
James Smarta257bf92009-04-06 18:48:10 -04008454 /* If HBA has a deferred error attention, fail the iocb. */
8455 if (unlikely(phba->hba_flag & DEFER_ERATT))
8456 return IOCB_ERROR;
8457
dea31012005-04-17 16:05:31 -05008458 /*
8459 * We should never get an IOCB if we are in a < LINK_DOWN state
8460 */
James Smart2e0fef82007-06-17 19:56:36 -05008461 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008462 return IOCB_ERROR;
8463
8464 /*
8465 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008466 * outstanding event.
dea31012005-04-17 16:05:31 -05008467 */
James Smart0b727fe2007-10-27 13:37:25 -04008468 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008469 goto iocb_busy;
8470
James Smart2e0fef82007-06-17 19:56:36 -05008471 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008472 /*
James Smart2680eea2007-04-25 09:52:55 -04008473 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008474 * can be issued if the link is not up.
8475 */
8476 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008477 case CMD_GEN_REQUEST64_CR:
8478 case CMD_GEN_REQUEST64_CX:
8479 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8480 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008481 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008482 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8483 MENLO_TRANSPORT_TYPE))
8484
8485 goto iocb_busy;
8486 break;
dea31012005-04-17 16:05:31 -05008487 case CMD_QUE_RING_BUF_CN:
8488 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008489 /*
8490 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8491 * completion, iocb_cmpl MUST be 0.
8492 */
8493 if (piocb->iocb_cmpl)
8494 piocb->iocb_cmpl = NULL;
8495 /*FALLTHROUGH*/
8496 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008497 case CMD_CLOSE_XRI_CN:
8498 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008499 break;
8500 default:
8501 goto iocb_busy;
8502 }
8503
8504 /*
8505 * For FCP commands, we must be in a state where we can process link
8506 * attention events.
8507 */
James Smart895427b2017-02-12 13:52:30 -08008508 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008509 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008510 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008511 }
dea31012005-04-17 16:05:31 -05008512
dea31012005-04-17 16:05:31 -05008513 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8514 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8515 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8516
8517 if (iocb)
8518 lpfc_sli_update_ring(phba, pring);
8519 else
8520 lpfc_sli_update_full_ring(phba, pring);
8521
8522 if (!piocb)
8523 return IOCB_SUCCESS;
8524
8525 goto out_busy;
8526
8527 iocb_busy:
8528 pring->stats.iocb_cmd_delay++;
8529
8530 out_busy:
8531
8532 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008533 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008534 return IOCB_SUCCESS;
8535 }
8536
8537 return IOCB_BUSY;
8538}
8539
James Smart3772a992009-05-22 14:50:54 -04008540/**
James Smart4f774512009-05-22 14:52:35 -04008541 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8542 * @phba: Pointer to HBA context object.
8543 * @piocb: Pointer to command iocb.
8544 * @sglq: Pointer to the scatter gather queue object.
8545 *
8546 * This routine converts the bpl or bde that is in the IOCB
8547 * to a sgl list for the sli4 hardware. The physical address
8548 * of the bpl/bde is converted back to a virtual address.
8549 * If the IOCB contains a BPL then the list of BDE's is
8550 * converted to sli4_sge's. If the IOCB contains a single
8551 * BDE then it is converted to a single sli_sge.
8552 * The IOCB is still in cpu endianess so the contents of
8553 * the bpl can be used without byte swapping.
8554 *
8555 * Returns valid XRI = Success, NO_XRI = Failure.
8556**/
8557static uint16_t
8558lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8559 struct lpfc_sglq *sglq)
8560{
8561 uint16_t xritag = NO_XRI;
8562 struct ulp_bde64 *bpl = NULL;
8563 struct ulp_bde64 bde;
8564 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008565 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008566 IOCB_t *icmd;
8567 int numBdes = 0;
8568 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008569 uint32_t offset = 0; /* accumulated offset in the sg request list */
8570 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008571
8572 if (!piocbq || !sglq)
8573 return xritag;
8574
8575 sgl = (struct sli4_sge *)sglq->sgl;
8576 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008577 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8578 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008579 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8580 numBdes = icmd->un.genreq64.bdl.bdeSize /
8581 sizeof(struct ulp_bde64);
8582 /* The addrHigh and addrLow fields within the IOCB
8583 * have not been byteswapped yet so there is no
8584 * need to swap them back.
8585 */
James Smart1b511972011-12-13 13:23:09 -05008586 if (piocbq->context3)
8587 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8588 else
8589 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008590
James Smart1b511972011-12-13 13:23:09 -05008591 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008592 if (!bpl)
8593 return xritag;
8594
8595 for (i = 0; i < numBdes; i++) {
8596 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008597 sgl->addr_hi = bpl->addrHigh;
8598 sgl->addr_lo = bpl->addrLow;
8599
James Smart05580562011-05-24 11:40:48 -04008600 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008601 if ((i+1) == numBdes)
8602 bf_set(lpfc_sli4_sge_last, sgl, 1);
8603 else
8604 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008605 /* swap the size field back to the cpu so we
8606 * can assign it to the sgl.
8607 */
8608 bde.tus.w = le32_to_cpu(bpl->tus.w);
8609 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008610 /* The offsets in the sgl need to be accumulated
8611 * separately for the request and reply lists.
8612 * The request is always first, the reply follows.
8613 */
8614 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8615 /* add up the reply sg entries */
8616 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8617 inbound++;
8618 /* first inbound? reset the offset */
8619 if (inbound == 1)
8620 offset = 0;
8621 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008622 bf_set(lpfc_sli4_sge_type, sgl,
8623 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008624 offset += bde.tus.f.bdeSize;
8625 }
James Smart546fc852011-03-11 16:06:29 -05008626 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008627 bpl++;
8628 sgl++;
8629 }
8630 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8631 /* The addrHigh and addrLow fields of the BDE have not
8632 * been byteswapped yet so they need to be swapped
8633 * before putting them in the sgl.
8634 */
8635 sgl->addr_hi =
8636 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8637 sgl->addr_lo =
8638 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008639 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008640 bf_set(lpfc_sli4_sge_last, sgl, 1);
8641 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008642 sgl->sge_len =
8643 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008644 }
8645 return sglq->sli4_xritag;
8646}
8647
8648/**
James Smart4f774512009-05-22 14:52:35 -04008649 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8650 * @phba: Pointer to HBA context object.
8651 * @piocb: Pointer to command iocb.
8652 * @wqe: Pointer to the work queue entry.
8653 *
8654 * This routine converts the iocb command to its Work Queue Entry
8655 * equivalent. The wqe pointer should not have any fields set when
8656 * this routine is called because it will memcpy over them.
8657 * This routine does not set the CQ_ID or the WQEC bits in the
8658 * wqe.
8659 *
8660 * Returns: 0 = Success, IOCB_ERROR = Failure.
8661 **/
8662static int
8663lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8664 union lpfc_wqe *wqe)
8665{
James Smart5ffc2662009-11-18 15:39:44 -05008666 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008667 uint8_t ct = 0;
8668 uint32_t fip;
8669 uint32_t abort_tag;
8670 uint8_t command_type = ELS_COMMAND_NON_FIP;
8671 uint8_t cmnd;
8672 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008673 uint16_t abrt_iotag;
8674 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008675 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008676 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008677 int numBdes, i;
8678 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008679 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008680 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008681 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008682
James Smart45ed1192009-10-02 15:17:02 -04008683 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008684 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008685 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008686 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008687 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008688 command_type = ELS_COMMAND_FIP;
8689 else
8690 command_type = ELS_COMMAND_NON_FIP;
8691
James Smartb5c53952016-03-31 14:12:30 -07008692 if (phba->fcp_embed_io)
8693 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008694 /* Some of the fields are in the right position already */
8695 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008696 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8697 /* The ct field has moved so reset */
8698 wqe->generic.wqe_com.word7 = 0;
8699 wqe->generic.wqe_com.word10 = 0;
8700 }
James Smartb5c53952016-03-31 14:12:30 -07008701
8702 abort_tag = (uint32_t) iocbq->iotag;
8703 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008704 /* words0-2 bpl convert bde */
8705 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008706 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8707 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008708 bpl = (struct ulp_bde64 *)
8709 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8710 if (!bpl)
8711 return IOCB_ERROR;
8712
8713 /* Should already be byte swapped. */
8714 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8715 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8716 /* swap the size field back to the cpu so we
8717 * can assign it to the sgl.
8718 */
8719 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008720 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8721 total_len = 0;
8722 for (i = 0; i < numBdes; i++) {
8723 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8724 total_len += bde.tus.f.bdeSize;
8725 }
James Smart4f774512009-05-22 14:52:35 -04008726 } else
James Smart5ffc2662009-11-18 15:39:44 -05008727 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008728
8729 iocbq->iocb.ulpIoTag = iocbq->iotag;
8730 cmnd = iocbq->iocb.ulpCommand;
8731
8732 switch (iocbq->iocb.ulpCommand) {
8733 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008734 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8735 ndlp = iocbq->context_un.ndlp;
8736 else
8737 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008738 if (!iocbq->iocb.ulpLe) {
8739 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8740 "2007 Only Limited Edition cmd Format"
8741 " supported 0x%x\n",
8742 iocbq->iocb.ulpCommand);
8743 return IOCB_ERROR;
8744 }
James Smartff78d8f2011-12-13 13:21:35 -05008745
James Smart5ffc2662009-11-18 15:39:44 -05008746 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008747 /* Els_reguest64 has a TMO */
8748 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8749 iocbq->iocb.ulpTimeout);
8750 /* Need a VF for word 4 set the vf bit*/
8751 bf_set(els_req64_vf, &wqe->els_req, 0);
8752 /* And a VFID for word 12 */
8753 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008754 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008755 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8756 iocbq->iocb.ulpContext);
8757 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8758 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008759 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008760 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008761 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8762 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008763 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8764 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008765 if_type = bf_get(lpfc_sli_intf_if_type,
8766 &phba->sli4_hba.sli_intf);
8767 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008768 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008769 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008770 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008771 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008772 *pcmd == ELS_CMD_PLOGI)) {
8773 bf_set(els_req64_sp, &wqe->els_req, 1);
8774 bf_set(els_req64_sid, &wqe->els_req,
8775 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008776 if ((*pcmd == ELS_CMD_FLOGI) &&
8777 !(phba->fc_topology ==
8778 LPFC_TOPOLOGY_LOOP))
8779 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008780 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8781 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008782 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008783 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008784 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8785 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8786 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8787 }
James Smartc8685952009-11-18 15:39:16 -05008788 }
James Smart6d368e52011-05-24 11:44:12 -04008789 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8790 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008791 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8792 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8793 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8794 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8795 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8796 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008797 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008798 break;
James Smart5ffc2662009-11-18 15:39:44 -05008799 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008800 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8801 iocbq->iocb.un.ulpWord[3]);
8802 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008803 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008804 /* The entire sequence is transmitted for this IOCB */
8805 xmit_len = total_len;
8806 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008807 if (phba->link_flag & LS_LOOPBACK_MODE)
8808 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008809 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008810 /* word3 iocb=io_tag32 wqe=reserved */
8811 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008812 /* word4 relative_offset memcpy */
8813 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008814 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8815 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8816 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8817 LPFC_WQE_IOD_WRITE);
8818 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8819 LPFC_WQE_LENLOC_WORD12);
8820 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008821 wqe->xmit_sequence.xmit_len = xmit_len;
8822 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008823 break;
James Smart4f774512009-05-22 14:52:35 -04008824 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008825 /* word3 iocb=iotag32 wqe=seq_payload_len */
8826 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008827 /* word4 iocb=rsvd wqe=rsvd */
8828 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8829 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008830 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008831 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008832 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8833 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8834 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8835 LPFC_WQE_LENLOC_WORD3);
8836 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008837 break;
James Smart4f774512009-05-22 14:52:35 -04008838 case CMD_FCP_IWRITE64_CR:
8839 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008840 /* word3 iocb=iotag wqe=payload_offset_len */
8841 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008842 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8843 xmit_len + sizeof(struct fcp_rsp));
8844 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8845 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008846 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8847 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8848 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8849 iocbq->iocb.ulpFCP2Rcvy);
8850 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8851 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008852 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8853 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8854 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008855 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008856 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008857 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8858 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008859 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8860 if (iocbq->priority) {
8861 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8862 (iocbq->priority << 1));
8863 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008864 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8865 (phba->cfg_XLanePriority << 1));
8866 }
8867 }
James Smartb5c53952016-03-31 14:12:30 -07008868 /* Note, word 10 is already initialized to 0 */
8869
8870 if (phba->fcp_embed_io) {
8871 struct lpfc_scsi_buf *lpfc_cmd;
8872 struct sli4_sge *sgl;
8873 union lpfc_wqe128 *wqe128;
8874 struct fcp_cmnd *fcp_cmnd;
8875 uint32_t *ptr;
8876
8877 /* 128 byte wqe support here */
8878 wqe128 = (union lpfc_wqe128 *)wqe;
8879
8880 lpfc_cmd = iocbq->context1;
8881 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8882 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8883
8884 /* Word 0-2 - FCP_CMND */
8885 wqe128->generic.bde.tus.f.bdeFlags =
8886 BUFF_TYPE_BDE_IMMED;
8887 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8888 wqe128->generic.bde.addrHigh = 0;
8889 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8890
8891 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8892
8893 /* Word 22-29 FCP CMND Payload */
8894 ptr = &wqe128->words[22];
8895 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8896 }
James Smart7851fe22011-07-22 18:36:52 -04008897 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008898 case CMD_FCP_IREAD64_CR:
8899 /* word3 iocb=iotag wqe=payload_offset_len */
8900 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008901 bf_set(payload_offset_len, &wqe->fcp_iread,
8902 xmit_len + sizeof(struct fcp_rsp));
8903 bf_set(cmd_buff_len, &wqe->fcp_iread,
8904 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008905 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8906 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8907 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8908 iocbq->iocb.ulpFCP2Rcvy);
8909 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008910 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008911 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8912 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8913 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008914 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008915 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008916 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8917 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008918 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8919 if (iocbq->priority) {
8920 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8921 (iocbq->priority << 1));
8922 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008923 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8924 (phba->cfg_XLanePriority << 1));
8925 }
8926 }
James Smartb5c53952016-03-31 14:12:30 -07008927 /* Note, word 10 is already initialized to 0 */
8928
8929 if (phba->fcp_embed_io) {
8930 struct lpfc_scsi_buf *lpfc_cmd;
8931 struct sli4_sge *sgl;
8932 union lpfc_wqe128 *wqe128;
8933 struct fcp_cmnd *fcp_cmnd;
8934 uint32_t *ptr;
8935
8936 /* 128 byte wqe support here */
8937 wqe128 = (union lpfc_wqe128 *)wqe;
8938
8939 lpfc_cmd = iocbq->context1;
8940 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8941 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8942
8943 /* Word 0-2 - FCP_CMND */
8944 wqe128->generic.bde.tus.f.bdeFlags =
8945 BUFF_TYPE_BDE_IMMED;
8946 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8947 wqe128->generic.bde.addrHigh = 0;
8948 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8949
8950 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8951
8952 /* Word 22-29 FCP CMND Payload */
8953 ptr = &wqe128->words[22];
8954 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8955 }
James Smart7851fe22011-07-22 18:36:52 -04008956 break;
James Smartf1126682009-06-10 17:22:44 -04008957 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008958 /* word3 iocb=iotag wqe=payload_offset_len */
8959 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8960 bf_set(payload_offset_len, &wqe->fcp_icmd,
8961 xmit_len + sizeof(struct fcp_rsp));
8962 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8963 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008964 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008965 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008966 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008967 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8968 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8969 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8970 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8971 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008972 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8973 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008974 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8975 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008976 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8977 if (iocbq->priority) {
8978 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8979 (iocbq->priority << 1));
8980 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008981 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8982 (phba->cfg_XLanePriority << 1));
8983 }
8984 }
James Smartb5c53952016-03-31 14:12:30 -07008985 /* Note, word 10 is already initialized to 0 */
8986
8987 if (phba->fcp_embed_io) {
8988 struct lpfc_scsi_buf *lpfc_cmd;
8989 struct sli4_sge *sgl;
8990 union lpfc_wqe128 *wqe128;
8991 struct fcp_cmnd *fcp_cmnd;
8992 uint32_t *ptr;
8993
8994 /* 128 byte wqe support here */
8995 wqe128 = (union lpfc_wqe128 *)wqe;
8996
8997 lpfc_cmd = iocbq->context1;
8998 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8999 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9000
9001 /* Word 0-2 - FCP_CMND */
9002 wqe128->generic.bde.tus.f.bdeFlags =
9003 BUFF_TYPE_BDE_IMMED;
9004 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9005 wqe128->generic.bde.addrHigh = 0;
9006 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9007
9008 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9009
9010 /* Word 22-29 FCP CMND Payload */
9011 ptr = &wqe128->words[22];
9012 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9013 }
James Smart7851fe22011-07-22 18:36:52 -04009014 break;
James Smart4f774512009-05-22 14:52:35 -04009015 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009016 /* For this command calculate the xmit length of the
9017 * request bde.
9018 */
9019 xmit_len = 0;
9020 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9021 sizeof(struct ulp_bde64);
9022 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009023 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009024 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9025 break;
James Smart63e801c2010-11-20 23:14:19 -05009026 xmit_len += bde.tus.f.bdeSize;
9027 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009028 /* word3 iocb=IO_TAG wqe=request_payload_len */
9029 wqe->gen_req.request_payload_len = xmit_len;
9030 /* word4 iocb=parameter wqe=relative_offset memcpy */
9031 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009032 /* word6 context tag copied in memcpy */
9033 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9034 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9036 "2015 Invalid CT %x command 0x%x\n",
9037 ct, iocbq->iocb.ulpCommand);
9038 return IOCB_ERROR;
9039 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009040 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9041 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9042 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9043 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9044 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9045 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9046 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9047 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009048 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009049 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009050 break;
James Smart4f774512009-05-22 14:52:35 -04009051 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009052 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009053 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009054 /* word3 iocb=iotag32 wqe=response_payload_len */
9055 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009056 /* word4 */
9057 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009058 /* word5 iocb=rsvd wge=did */
9059 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009060 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9061
9062 if_type = bf_get(lpfc_sli_intf_if_type,
9063 &phba->sli4_hba.sli_intf);
9064 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9065 if (iocbq->vport->fc_flag & FC_PT2PT) {
9066 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9067 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9068 iocbq->vport->fc_myDID);
9069 if (iocbq->vport->fc_myDID == Fabric_DID) {
9070 bf_set(wqe_els_did,
9071 &wqe->xmit_els_rsp.wqe_dest, 0);
9072 }
9073 }
9074 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009075 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9076 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9077 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9078 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009079 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009080 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009081 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009082 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009083 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9084 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9085 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9086 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9087 LPFC_WQE_LENLOC_WORD3);
9088 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009089 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9090 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009091 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9092 iocbq->context2)->virt);
9093 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009094 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9095 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009096 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009097 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9098 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009099 phba->vpi_ids[phba->pport->vpi]);
9100 }
James Smart4f774512009-05-22 14:52:35 -04009101 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009102 break;
James Smart4f774512009-05-22 14:52:35 -04009103 case CMD_CLOSE_XRI_CN:
9104 case CMD_ABORT_XRI_CN:
9105 case CMD_ABORT_XRI_CX:
9106 /* words 0-2 memcpy should be 0 rserved */
9107 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009108 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9109 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9110 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9111 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9112 } else
9113 fip = 0;
9114
9115 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009116 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009117 * The link is down, or the command was ELS_FIP
9118 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009119 * on the wire.
9120 */
9121 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9122 else
9123 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9124 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009125 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9126 wqe->abort_cmd.rsrvd5 = 0;
9127 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009128 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9129 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009130 /*
9131 * The abort handler will send us CMD_ABORT_XRI_CN or
9132 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9133 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009134 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9135 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9136 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9137 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009138 cmnd = CMD_ABORT_XRI_CX;
9139 command_type = OTHER_COMMAND;
9140 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009141 break;
James Smart6669f9b2009-10-02 15:16:45 -04009142 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009143 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009144 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009145 * we re-construct this WQE here based on information in
9146 * iocbq from scratch.
9147 */
9148 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009149 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009150 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009151 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9152 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009153 LPFC_ABTS_UNSOL_INT) {
9154 /* ABTS sent by initiator to CT exchange, the
9155 * RX_ID field will be filled with the newly
9156 * allocated responder XRI.
9157 */
9158 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9159 iocbq->sli4_xritag);
9160 } else {
9161 /* ABTS sent by responder to CT exchange, the
9162 * RX_ID field will be filled with the responder
9163 * RX_ID from ABTS.
9164 */
9165 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009166 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009167 }
James Smart6669f9b2009-10-02 15:16:45 -04009168 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9169 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009170
9171 /* Use CT=VPI */
9172 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9173 ndlp->nlp_DID);
9174 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9175 iocbq->iocb.ulpContext);
9176 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009177 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009178 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009179 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9180 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9181 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009182 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9183 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009184 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9185 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9186 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9187 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9188 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9189 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9190 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9191 }
9192
James Smart7851fe22011-07-22 18:36:52 -04009193 break;
James Smartae9e28f2017-05-15 15:20:51 -07009194 case CMD_SEND_FRAME:
9195 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9196 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9197 return 0;
James Smart4f774512009-05-22 14:52:35 -04009198 case CMD_XRI_ABORTED_CX:
9199 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009200 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9201 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9202 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9203 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9204 default:
9205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9206 "2014 Invalid command 0x%x\n",
9207 iocbq->iocb.ulpCommand);
9208 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009209 break;
James Smart4f774512009-05-22 14:52:35 -04009210 }
James Smart6d368e52011-05-24 11:44:12 -04009211
James Smart8012cc32012-10-31 14:44:49 -04009212 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9213 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9214 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9215 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9216 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9217 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9218 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9219 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009220 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9221 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9222 wqe->generic.wqe_com.abort_tag = abort_tag;
9223 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9224 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9225 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9226 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009227 return 0;
9228}
9229
9230/**
9231 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9232 * @phba: Pointer to HBA context object.
9233 * @ring_number: SLI ring number to issue iocb on.
9234 * @piocb: Pointer to command iocb.
9235 * @flag: Flag indicating if this command can be put into txq.
9236 *
9237 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9238 * an iocb command to an HBA with SLI-4 interface spec.
9239 *
9240 * This function is called with hbalock held. The function will return success
9241 * after it successfully submit the iocb to firmware or after adding to the
9242 * txq.
9243 **/
9244static int
9245__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9246 struct lpfc_iocbq *piocb, uint32_t flag)
9247{
9248 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009249 union lpfc_wqe *wqe;
9250 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009251 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009252 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009253
James Smart895427b2017-02-12 13:52:30 -08009254 /* Get the WQ */
9255 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9256 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9257 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9258 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9259 else
9260 wq = phba->sli4_hba.oas_wq;
9261 } else {
9262 wq = phba->sli4_hba.els_wq;
9263 }
9264
9265 /* Get corresponding ring */
9266 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009267
James Smartb5c53952016-03-31 14:12:30 -07009268 /*
9269 * The WQE can be either 64 or 128 bytes,
9270 * so allocate space on the stack assuming the largest.
9271 */
9272 wqe = (union lpfc_wqe *)&wqe128;
9273
James Smart895427b2017-02-12 13:52:30 -08009274 lockdep_assert_held(&phba->hbalock);
9275
James Smart4f774512009-05-22 14:52:35 -04009276 if (piocb->sli4_xritag == NO_XRI) {
9277 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009278 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009279 sglq = NULL;
9280 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009281 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009282 if (!(flag & SLI_IOCB_RET_IOCB)) {
9283 __lpfc_sli_ringtx_put(phba,
9284 pring, piocb);
9285 return IOCB_SUCCESS;
9286 } else {
9287 return IOCB_BUSY;
9288 }
9289 } else {
James Smart895427b2017-02-12 13:52:30 -08009290 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009291 if (!sglq) {
9292 if (!(flag & SLI_IOCB_RET_IOCB)) {
9293 __lpfc_sli_ringtx_put(phba,
9294 pring,
9295 piocb);
9296 return IOCB_SUCCESS;
9297 } else
9298 return IOCB_BUSY;
9299 }
9300 }
James Smart4f774512009-05-22 14:52:35 -04009301 }
James Smart2ea259e2017-02-12 13:52:27 -08009302 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009303 /* These IO's already have an XRI and a mapped sgl. */
9304 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009305 else {
James Smart6d368e52011-05-24 11:44:12 -04009306 /*
9307 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009308 * sglq is on the active list
9309 */
James Smartedccdc12013-01-03 15:43:45 -05009310 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009311 if (!sglq)
9312 return IOCB_ERROR;
9313 }
9314
9315 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009316 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009317 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009318 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009319 return IOCB_ERROR;
9320 }
9321
James Smartb5c53952016-03-31 14:12:30 -07009322 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009323 return IOCB_ERROR;
9324
James Smart895427b2017-02-12 13:52:30 -08009325 if (lpfc_sli4_wq_put(wq, wqe))
9326 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009327 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9328
9329 return 0;
9330}
9331
9332/**
James Smart3772a992009-05-22 14:50:54 -04009333 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9334 *
9335 * This routine wraps the actual lockless version for issusing IOCB function
9336 * pointer from the lpfc_hba struct.
9337 *
9338 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009339 * IOCB_ERROR - Error
9340 * IOCB_SUCCESS - Success
9341 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009342 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009343int
James Smart3772a992009-05-22 14:50:54 -04009344__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9345 struct lpfc_iocbq *piocb, uint32_t flag)
9346{
9347 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9348}
9349
9350/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009351 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009352 * @phba: The hba struct for which this call is being executed.
9353 * @dev_grp: The HBA PCI-Device group number.
9354 *
9355 * This routine sets up the SLI interface API function jump table in @phba
9356 * struct.
9357 * Returns: 0 - success, -ENODEV - failure.
9358 **/
9359int
9360lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9361{
9362
9363 switch (dev_grp) {
9364 case LPFC_PCI_DEV_LP:
9365 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9366 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9367 break;
James Smart4f774512009-05-22 14:52:35 -04009368 case LPFC_PCI_DEV_OC:
9369 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9370 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9371 break;
James Smart3772a992009-05-22 14:50:54 -04009372 default:
9373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9374 "1419 Invalid HBA PCI-device group: 0x%x\n",
9375 dev_grp);
9376 return -ENODEV;
9377 break;
9378 }
9379 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9380 return 0;
9381}
James Smart92d7f7b2007-06-17 19:56:38 -05009382
James Smarta1efe162015-05-21 13:55:20 -04009383/**
James Smart895427b2017-02-12 13:52:30 -08009384 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009385 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009386 * @piocb: Pointer to command iocb.
9387 *
James Smart895427b2017-02-12 13:52:30 -08009388 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9389 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009390 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009391 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009392 */
James Smart895427b2017-02-12 13:52:30 -08009393struct lpfc_sli_ring *
9394lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009395{
James Smart895427b2017-02-12 13:52:30 -08009396 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009397 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009398 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009399 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009400 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009401 /*
James Smart895427b2017-02-12 13:52:30 -08009402 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009403 * be setup based on what work queue we used.
9404 */
9405 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
James Smart895427b2017-02-12 13:52:30 -08009406 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009407 lpfc_sli4_scmd_to_wqidx_distr(phba,
9408 piocb->context1);
James Smart895427b2017-02-12 13:52:30 -08009409 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009410 } else {
9411 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009412 return NULL;
9413 piocb->hba_wqidx = 0;
9414 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009415 }
James Smart895427b2017-02-12 13:52:30 -08009416 } else {
9417 if (unlikely(!phba->sli4_hba.els_wq))
9418 return NULL;
9419 piocb->hba_wqidx = 0;
9420 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009421 }
James Smart9bd2bff52014-09-03 12:57:30 -04009422}
9423
James Smarte59058c2008-08-24 21:49:00 -04009424/**
James Smart3621a712009-04-06 18:47:14 -04009425 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009426 * @phba: Pointer to HBA context object.
9427 * @pring: Pointer to driver SLI ring object.
9428 * @piocb: Pointer to command iocb.
9429 * @flag: Flag indicating if this command can be put into txq.
9430 *
9431 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9432 * function. This function gets the hbalock and calls
9433 * __lpfc_sli_issue_iocb function and will return the error returned
9434 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9435 * functions which do not hold hbalock.
9436 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009437int
James Smart3772a992009-05-22 14:50:54 -04009438lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009439 struct lpfc_iocbq *piocb, uint32_t flag)
9440{
James Smart895427b2017-02-12 13:52:30 -08009441 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009442 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009443 struct lpfc_queue *fpeq;
9444 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009445 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009446 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009447
James Smart7e56aa22012-08-03 12:35:34 -04009448 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009449 pring = lpfc_sli4_calc_ring(phba, piocb);
9450 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009451 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009452
James Smart9bd2bff52014-09-03 12:57:30 -04009453 spin_lock_irqsave(&pring->ring_lock, iflags);
9454 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9455 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009456
James Smart9bd2bff52014-09-03 12:57:30 -04009457 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009458 idx = piocb->hba_wqidx;
9459 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009460
James Smart895427b2017-02-12 13:52:30 -08009461 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009462
James Smart9bd2bff52014-09-03 12:57:30 -04009463 /* Get associated EQ with this index */
9464 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009465
James Smart9bd2bff52014-09-03 12:57:30 -04009466 /* Turn off interrupts from this EQ */
9467 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009468
James Smart9bd2bff52014-09-03 12:57:30 -04009469 /*
9470 * Process all the events on FCP EQ
9471 */
9472 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9473 lpfc_sli4_hba_handle_eqe(phba,
9474 eqe, idx);
9475 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009476 }
James Smartba20c852012-08-03 12:36:52 -04009477
James Smart9bd2bff52014-09-03 12:57:30 -04009478 /* Always clear and re-arm the EQ */
9479 lpfc_sli4_eq_release(fpeq,
9480 LPFC_QUEUE_REARM);
9481 }
James Smart895427b2017-02-12 13:52:30 -08009482 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009483 }
James Smart7e56aa22012-08-03 12:35:34 -04009484 } else {
9485 /* For now, SLI2/3 will still use hbalock */
9486 spin_lock_irqsave(&phba->hbalock, iflags);
9487 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9488 spin_unlock_irqrestore(&phba->hbalock, iflags);
9489 }
James Smart92d7f7b2007-06-17 19:56:38 -05009490 return rc;
9491}
9492
James Smarte59058c2008-08-24 21:49:00 -04009493/**
James Smart3621a712009-04-06 18:47:14 -04009494 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009495 * @phba: Pointer to HBA context object.
9496 *
9497 * This function is called while driver attaches with the
9498 * HBA to setup the extra ring. The extra ring is used
9499 * only when driver needs to support target mode functionality
9500 * or IP over FC functionalities.
9501 *
James Smart895427b2017-02-12 13:52:30 -08009502 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009503 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009504static int
9505lpfc_extra_ring_setup( struct lpfc_hba *phba)
9506{
9507 struct lpfc_sli *psli;
9508 struct lpfc_sli_ring *pring;
9509
9510 psli = &phba->sli;
9511
9512 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009513
9514 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009515 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009516 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9517 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9518 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9519 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009520
James Smarta4bc3372006-12-02 13:34:16 -05009521 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009522 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009523
James Smart7e56aa22012-08-03 12:35:34 -04009524 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9525 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9526 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9527 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009528
9529 /* Setup default profile for this ring */
9530 pring->iotag_max = 4096;
9531 pring->num_mask = 1;
9532 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009533 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9534 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009535 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9536 return 0;
9537}
9538
James Smartcb69f7d2011-12-13 13:21:57 -05009539/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9540 * @phba: Pointer to HBA context object.
9541 * @iocbq: Pointer to iocb object.
9542 *
9543 * The async_event handler calls this routine when it receives
9544 * an ASYNC_STATUS_CN event from the port. The port generates
9545 * this event when an Abort Sequence request to an rport fails
9546 * twice in succession. The abort could be originated by the
9547 * driver or by the port. The ABTS could have been for an ELS
9548 * or FCP IO. The port only generates this event when an ABTS
9549 * fails to complete after one retry.
9550 */
9551static void
9552lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9553 struct lpfc_iocbq *iocbq)
9554{
9555 struct lpfc_nodelist *ndlp = NULL;
9556 uint16_t rpi = 0, vpi = 0;
9557 struct lpfc_vport *vport = NULL;
9558
9559 /* The rpi in the ulpContext is vport-sensitive. */
9560 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9561 rpi = iocbq->iocb.ulpContext;
9562
9563 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9564 "3092 Port generated ABTS async event "
9565 "on vpi %d rpi %d status 0x%x\n",
9566 vpi, rpi, iocbq->iocb.ulpStatus);
9567
9568 vport = lpfc_find_vport_by_vpid(phba, vpi);
9569 if (!vport)
9570 goto err_exit;
9571 ndlp = lpfc_findnode_rpi(vport, rpi);
9572 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9573 goto err_exit;
9574
9575 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9576 lpfc_sli_abts_recover_port(vport, ndlp);
9577 return;
9578
9579 err_exit:
9580 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9581 "3095 Event Context not found, no "
9582 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9583 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9584 vpi, rpi);
9585}
9586
9587/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9588 * @phba: pointer to HBA context object.
9589 * @ndlp: nodelist pointer for the impacted rport.
9590 * @axri: pointer to the wcqe containing the failed exchange.
9591 *
9592 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9593 * port. The port generates this event when an abort exchange request to an
9594 * rport fails twice in succession with no reply. The abort could be originated
9595 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9596 */
9597void
9598lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9599 struct lpfc_nodelist *ndlp,
9600 struct sli4_wcqe_xri_aborted *axri)
9601{
9602 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009603 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009604
James Smart6b5151f2012-01-18 16:24:06 -05009605 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009606 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9607 "3115 Node Context not found, driver "
9608 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009609 return;
9610 }
9611
James Smartcb69f7d2011-12-13 13:21:57 -05009612 vport = ndlp->vport;
9613 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9614 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009615 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009616 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009617 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009618 bf_get(lpfc_wcqe_xa_status, axri),
9619 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009620
James Smart5c1db2a2012-03-01 22:34:36 -05009621 /*
9622 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9623 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9624 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9625 */
James Smarte3d2b802012-08-14 14:25:43 -04009626 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009627 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9628 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009629 lpfc_sli_abts_recover_port(vport, ndlp);
9630}
9631
James Smarte59058c2008-08-24 21:49:00 -04009632/**
James Smart3621a712009-04-06 18:47:14 -04009633 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009634 * @phba: Pointer to HBA context object.
9635 * @pring: Pointer to driver SLI ring object.
9636 * @iocbq: Pointer to iocb object.
9637 *
9638 * This function is called by the slow ring event handler
9639 * function when there is an ASYNC event iocb in the ring.
9640 * This function is called with no lock held.
9641 * Currently this function handles only temperature related
9642 * ASYNC events. The function decodes the temperature sensor
9643 * event message and posts events for the management applications.
9644 **/
James Smart98c9ea52007-10-27 13:37:33 -04009645static void
James Smart57127f12007-10-27 13:37:05 -04009646lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9647 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9648{
9649 IOCB_t *icmd;
9650 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009651 struct temp_event temp_event_data;
9652 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009653 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009654
9655 icmd = &iocbq->iocb;
9656 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009657
James Smartcb69f7d2011-12-13 13:21:57 -05009658 switch (evt_code) {
9659 case ASYNC_TEMP_WARN:
9660 case ASYNC_TEMP_SAFE:
9661 temp_event_data.data = (uint32_t) icmd->ulpContext;
9662 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9663 if (evt_code == ASYNC_TEMP_WARN) {
9664 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9665 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9666 "0347 Adapter is very hot, please take "
9667 "corrective action. temperature : %d Celsius\n",
9668 (uint32_t) icmd->ulpContext);
9669 } else {
9670 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9671 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9672 "0340 Adapter temperature is OK now. "
9673 "temperature : %d Celsius\n",
9674 (uint32_t) icmd->ulpContext);
9675 }
9676
9677 /* Send temperature change event to applications */
9678 shost = lpfc_shost_from_vport(phba->pport);
9679 fc_host_post_vendor_event(shost, fc_get_event_number(),
9680 sizeof(temp_event_data), (char *) &temp_event_data,
9681 LPFC_NL_VENDOR_ID);
9682 break;
9683 case ASYNC_STATUS_CN:
9684 lpfc_sli_abts_err_handler(phba, iocbq);
9685 break;
9686 default:
James Smarta257bf92009-04-06 18:48:10 -04009687 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009688 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009689 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009690 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009691 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9692 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9693 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9694 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009695 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009696 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9697 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9698 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9699 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9700
James Smartcb69f7d2011-12-13 13:21:57 -05009701 break;
James Smart57127f12007-10-27 13:37:05 -04009702 }
James Smart57127f12007-10-27 13:37:05 -04009703}
9704
9705
James Smarte59058c2008-08-24 21:49:00 -04009706/**
James Smart895427b2017-02-12 13:52:30 -08009707 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009708 * @phba: Pointer to HBA context object.
9709 *
9710 * lpfc_sli_setup sets up rings of the SLI interface with
9711 * number of iocbs per ring and iotags. This function is
9712 * called while driver attach to the HBA and before the
9713 * interrupts are enabled. So there is no need for locking.
9714 *
9715 * This function always returns 0.
9716 **/
dea31012005-04-17 16:05:31 -05009717int
James Smart895427b2017-02-12 13:52:30 -08009718lpfc_sli4_setup(struct lpfc_hba *phba)
9719{
9720 struct lpfc_sli_ring *pring;
9721
9722 pring = phba->sli4_hba.els_wq->pring;
9723 pring->num_mask = LPFC_MAX_RING_MASK;
9724 pring->prt[0].profile = 0; /* Mask 0 */
9725 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9726 pring->prt[0].type = FC_TYPE_ELS;
9727 pring->prt[0].lpfc_sli_rcv_unsol_event =
9728 lpfc_els_unsol_event;
9729 pring->prt[1].profile = 0; /* Mask 1 */
9730 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9731 pring->prt[1].type = FC_TYPE_ELS;
9732 pring->prt[1].lpfc_sli_rcv_unsol_event =
9733 lpfc_els_unsol_event;
9734 pring->prt[2].profile = 0; /* Mask 2 */
9735 /* NameServer Inquiry */
9736 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9737 /* NameServer */
9738 pring->prt[2].type = FC_TYPE_CT;
9739 pring->prt[2].lpfc_sli_rcv_unsol_event =
9740 lpfc_ct_unsol_event;
9741 pring->prt[3].profile = 0; /* Mask 3 */
9742 /* NameServer response */
9743 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9744 /* NameServer */
9745 pring->prt[3].type = FC_TYPE_CT;
9746 pring->prt[3].lpfc_sli_rcv_unsol_event =
9747 lpfc_ct_unsol_event;
9748 return 0;
9749}
9750
9751/**
9752 * lpfc_sli_setup - SLI ring setup function
9753 * @phba: Pointer to HBA context object.
9754 *
9755 * lpfc_sli_setup sets up rings of the SLI interface with
9756 * number of iocbs per ring and iotags. This function is
9757 * called while driver attach to the HBA and before the
9758 * interrupts are enabled. So there is no need for locking.
9759 *
9760 * This function always returns 0. SLI3 only.
9761 **/
9762int
dea31012005-04-17 16:05:31 -05009763lpfc_sli_setup(struct lpfc_hba *phba)
9764{
James Smarted957682007-06-17 19:56:37 -05009765 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009766 struct lpfc_sli *psli = &phba->sli;
9767 struct lpfc_sli_ring *pring;
9768
James Smart2a76a282012-08-03 12:35:54 -04009769 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009770 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009771
James Bottomley604a3e32005-10-29 10:28:33 -05009772 psli->iocbq_lookup = NULL;
9773 psli->iocbq_lookup_len = 0;
9774 psli->last_iotag = 0;
9775
dea31012005-04-17 16:05:31 -05009776 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009777 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009778 switch (i) {
9779 case LPFC_FCP_RING: /* ring 0 - FCP */
9780 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009781 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9782 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9783 pring->sli.sli3.numCiocb +=
9784 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9785 pring->sli.sli3.numRiocb +=
9786 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9787 pring->sli.sli3.numCiocb +=
9788 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9789 pring->sli.sli3.numRiocb +=
9790 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9791 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009792 SLI3_IOCB_CMD_SIZE :
9793 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009794 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009795 SLI3_IOCB_RSP_SIZE :
9796 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009797 pring->iotag_ctr = 0;
9798 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009799 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009800 pring->fast_iotag = pring->iotag_max;
9801 pring->num_mask = 0;
9802 break;
James Smarta4bc3372006-12-02 13:34:16 -05009803 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009804 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009805 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9806 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9807 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009808 SLI3_IOCB_CMD_SIZE :
9809 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009810 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009811 SLI3_IOCB_RSP_SIZE :
9812 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009813 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009814 pring->num_mask = 0;
9815 break;
9816 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9817 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009818 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9819 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9820 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009821 SLI3_IOCB_CMD_SIZE :
9822 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009823 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009824 SLI3_IOCB_RSP_SIZE :
9825 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009826 pring->fast_iotag = 0;
9827 pring->iotag_ctr = 0;
9828 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009829 pring->lpfc_sli_rcv_async_status =
9830 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009831 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009832 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009833 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9834 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009835 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009836 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009837 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009838 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9839 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009840 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009841 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009842 pring->prt[2].profile = 0; /* Mask 2 */
9843 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009844 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009845 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009846 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009847 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009848 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009849 pring->prt[3].profile = 0; /* Mask 3 */
9850 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009851 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009852 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009853 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009854 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009855 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009856 break;
9857 }
James Smart7e56aa22012-08-03 12:35:34 -04009858 totiocbsize += (pring->sli.sli3.numCiocb *
9859 pring->sli.sli3.sizeCiocb) +
9860 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009861 }
James Smarted957682007-06-17 19:56:37 -05009862 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009863 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009864 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9865 "SLI2 SLIM Data: x%x x%lx\n",
9866 phba->brd_no, totiocbsize,
9867 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009868 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009869 if (phba->cfg_multi_ring_support == 2)
9870 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009871
9872 return 0;
9873}
9874
James Smarte59058c2008-08-24 21:49:00 -04009875/**
James Smart895427b2017-02-12 13:52:30 -08009876 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009877 * @phba: Pointer to HBA context object.
9878 *
James Smart895427b2017-02-12 13:52:30 -08009879 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009880 * ring. This function also initializes ring indices of each ring.
9881 * This function is called during the initialization of the SLI
9882 * interface of an HBA.
9883 * This function is called with no lock held and always returns
9884 * 1.
9885 **/
James Smart895427b2017-02-12 13:52:30 -08009886void
9887lpfc_sli4_queue_init(struct lpfc_hba *phba)
9888{
9889 struct lpfc_sli *psli;
9890 struct lpfc_sli_ring *pring;
9891 int i;
9892
9893 psli = &phba->sli;
9894 spin_lock_irq(&phba->hbalock);
9895 INIT_LIST_HEAD(&psli->mboxq);
9896 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9897 /* Initialize list headers for txq and txcmplq as double linked lists */
9898 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9899 pring = phba->sli4_hba.fcp_wq[i]->pring;
9900 pring->flag = 0;
9901 pring->ringno = LPFC_FCP_RING;
9902 INIT_LIST_HEAD(&pring->txq);
9903 INIT_LIST_HEAD(&pring->txcmplq);
9904 INIT_LIST_HEAD(&pring->iocb_continueq);
9905 spin_lock_init(&pring->ring_lock);
9906 }
9907 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9908 pring = phba->sli4_hba.nvme_wq[i]->pring;
9909 pring->flag = 0;
9910 pring->ringno = LPFC_FCP_RING;
9911 INIT_LIST_HEAD(&pring->txq);
9912 INIT_LIST_HEAD(&pring->txcmplq);
9913 INIT_LIST_HEAD(&pring->iocb_continueq);
9914 spin_lock_init(&pring->ring_lock);
9915 }
9916 pring = phba->sli4_hba.els_wq->pring;
9917 pring->flag = 0;
9918 pring->ringno = LPFC_ELS_RING;
9919 INIT_LIST_HEAD(&pring->txq);
9920 INIT_LIST_HEAD(&pring->txcmplq);
9921 INIT_LIST_HEAD(&pring->iocb_continueq);
9922 spin_lock_init(&pring->ring_lock);
9923
9924 if (phba->cfg_nvme_io_channel) {
9925 pring = phba->sli4_hba.nvmels_wq->pring;
9926 pring->flag = 0;
9927 pring->ringno = LPFC_ELS_RING;
9928 INIT_LIST_HEAD(&pring->txq);
9929 INIT_LIST_HEAD(&pring->txcmplq);
9930 INIT_LIST_HEAD(&pring->iocb_continueq);
9931 spin_lock_init(&pring->ring_lock);
9932 }
9933
9934 if (phba->cfg_fof) {
9935 pring = phba->sli4_hba.oas_wq->pring;
9936 pring->flag = 0;
9937 pring->ringno = LPFC_FCP_RING;
9938 INIT_LIST_HEAD(&pring->txq);
9939 INIT_LIST_HEAD(&pring->txcmplq);
9940 INIT_LIST_HEAD(&pring->iocb_continueq);
9941 spin_lock_init(&pring->ring_lock);
9942 }
9943
9944 spin_unlock_irq(&phba->hbalock);
9945}
9946
9947/**
9948 * lpfc_sli_queue_init - Queue initialization function
9949 * @phba: Pointer to HBA context object.
9950 *
9951 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9952 * ring. This function also initializes ring indices of each ring.
9953 * This function is called during the initialization of the SLI
9954 * interface of an HBA.
9955 * This function is called with no lock held and always returns
9956 * 1.
9957 **/
9958void
9959lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009960{
9961 struct lpfc_sli *psli;
9962 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009963 int i;
dea31012005-04-17 16:05:31 -05009964
9965 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009966 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009967 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009968 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009969 /* Initialize list headers for txq and txcmplq as double linked lists */
9970 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009971 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009972 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009973 pring->sli.sli3.next_cmdidx = 0;
9974 pring->sli.sli3.local_getidx = 0;
9975 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009976 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009977 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009978 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -08009979 pring->flag = 0;
9980 INIT_LIST_HEAD(&pring->txq);
9981 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -04009982 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009983 }
James Smart2e0fef82007-06-17 19:56:36 -05009984 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009985}
9986
James Smarte59058c2008-08-24 21:49:00 -04009987/**
James Smart04c68492009-05-22 14:52:52 -04009988 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9989 * @phba: Pointer to HBA context object.
9990 *
9991 * This routine flushes the mailbox command subsystem. It will unconditionally
9992 * flush all the mailbox commands in the three possible stages in the mailbox
9993 * command sub-system: pending mailbox command queue; the outstanding mailbox
9994 * command; and completed mailbox command queue. It is caller's responsibility
9995 * to make sure that the driver is in the proper state to flush the mailbox
9996 * command sub-system. Namely, the posting of mailbox commands into the
9997 * pending mailbox command queue from the various clients must be stopped;
9998 * either the HBA is in a state that it will never works on the outstanding
9999 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10000 * mailbox command has been completed.
10001 **/
10002static void
10003lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10004{
10005 LIST_HEAD(completions);
10006 struct lpfc_sli *psli = &phba->sli;
10007 LPFC_MBOXQ_t *pmb;
10008 unsigned long iflag;
10009
10010 /* Flush all the mailbox commands in the mbox system */
10011 spin_lock_irqsave(&phba->hbalock, iflag);
10012 /* The pending mailbox command queue */
10013 list_splice_init(&phba->sli.mboxq, &completions);
10014 /* The outstanding active mailbox command */
10015 if (psli->mbox_active) {
10016 list_add_tail(&psli->mbox_active->list, &completions);
10017 psli->mbox_active = NULL;
10018 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10019 }
10020 /* The completed mailbox command queue */
10021 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10022 spin_unlock_irqrestore(&phba->hbalock, iflag);
10023
10024 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10025 while (!list_empty(&completions)) {
10026 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10027 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10028 if (pmb->mbox_cmpl)
10029 pmb->mbox_cmpl(phba, pmb);
10030 }
10031}
10032
10033/**
James Smart3621a712009-04-06 18:47:14 -040010034 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010035 * @vport: Pointer to virtual port object.
10036 *
10037 * lpfc_sli_host_down is called to clean up the resources
10038 * associated with a vport before destroying virtual
10039 * port data structures.
10040 * This function does following operations:
10041 * - Free discovery resources associated with this virtual
10042 * port.
10043 * - Free iocbs associated with this virtual port in
10044 * the txq.
10045 * - Send abort for all iocb commands associated with this
10046 * vport in txcmplq.
10047 *
10048 * This function is called with no lock held and always returns 1.
10049 **/
dea31012005-04-17 16:05:31 -050010050int
James Smart92d7f7b2007-06-17 19:56:38 -050010051lpfc_sli_host_down(struct lpfc_vport *vport)
10052{
James Smart858c9f62007-06-17 19:56:39 -050010053 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010054 struct lpfc_hba *phba = vport->phba;
10055 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010056 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010057 struct lpfc_sli_ring *pring;
10058 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010059 int i;
10060 unsigned long flags = 0;
10061 uint16_t prev_pring_flag;
10062
10063 lpfc_cleanup_discovery_resources(vport);
10064
10065 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010066
James Smart895427b2017-02-12 13:52:30 -080010067 /*
10068 * Error everything on the txq since these iocbs
10069 * have not been given to the FW yet.
10070 * Also issue ABTS for everything on the txcmplq
10071 */
10072 if (phba->sli_rev != LPFC_SLI_REV4) {
10073 for (i = 0; i < psli->num_rings; i++) {
10074 pring = &psli->sli3_ring[i];
10075 prev_pring_flag = pring->flag;
10076 /* Only slow rings */
10077 if (pring->ringno == LPFC_ELS_RING) {
10078 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10079 /* Set the lpfc data pending flag */
10080 set_bit(LPFC_DATA_READY, &phba->data_flags);
10081 }
10082 list_for_each_entry_safe(iocb, next_iocb,
10083 &pring->txq, list) {
10084 if (iocb->vport != vport)
10085 continue;
10086 list_move_tail(&iocb->list, &completions);
10087 }
10088 list_for_each_entry_safe(iocb, next_iocb,
10089 &pring->txcmplq, list) {
10090 if (iocb->vport != vport)
10091 continue;
10092 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10093 }
10094 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010095 }
James Smart895427b2017-02-12 13:52:30 -080010096 } else {
10097 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10098 pring = qp->pring;
10099 if (!pring)
10100 continue;
10101 if (pring == phba->sli4_hba.els_wq->pring) {
10102 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10103 /* Set the lpfc data pending flag */
10104 set_bit(LPFC_DATA_READY, &phba->data_flags);
10105 }
10106 prev_pring_flag = pring->flag;
10107 spin_lock_irq(&pring->ring_lock);
10108 list_for_each_entry_safe(iocb, next_iocb,
10109 &pring->txq, list) {
10110 if (iocb->vport != vport)
10111 continue;
10112 list_move_tail(&iocb->list, &completions);
10113 }
10114 spin_unlock_irq(&pring->ring_lock);
10115 list_for_each_entry_safe(iocb, next_iocb,
10116 &pring->txcmplq, list) {
10117 if (iocb->vport != vport)
10118 continue;
10119 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10120 }
10121 pring->flag = prev_pring_flag;
10122 }
James Smart92d7f7b2007-06-17 19:56:38 -050010123 }
James Smart92d7f7b2007-06-17 19:56:38 -050010124 spin_unlock_irqrestore(&phba->hbalock, flags);
10125
James Smarta257bf92009-04-06 18:48:10 -040010126 /* Cancel all the IOCBs from the completions list */
10127 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10128 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010129 return 1;
10130}
10131
James Smarte59058c2008-08-24 21:49:00 -040010132/**
James Smart3621a712009-04-06 18:47:14 -040010133 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010134 * @phba: Pointer to HBA context object.
10135 *
10136 * This function cleans up all iocb, buffers, mailbox commands
10137 * while shutting down the HBA. This function is called with no
10138 * lock held and always returns 1.
10139 * This function does the following to cleanup driver resources:
10140 * - Free discovery resources for each virtual port
10141 * - Cleanup any pending fabric iocbs
10142 * - Iterate through the iocb txq and free each entry
10143 * in the list.
10144 * - Free up any buffer posted to the HBA
10145 * - Free mailbox commands in the mailbox queue.
10146 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010147int
James Smart2e0fef82007-06-17 19:56:36 -050010148lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010149{
James Smart2534ba72007-04-25 09:52:20 -040010150 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010151 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010152 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010153 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010154 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010155 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010156 int i;
10157
10158 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010159 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010160
dea31012005-04-17 16:05:31 -050010161 lpfc_hba_down_prep(phba);
10162
James Smart92d7f7b2007-06-17 19:56:38 -050010163 lpfc_fabric_abort_hba(phba);
10164
James Smart2e0fef82007-06-17 19:56:36 -050010165 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010166
James Smart895427b2017-02-12 13:52:30 -080010167 /*
10168 * Error everything on the txq since these iocbs
10169 * have not been given to the FW yet.
10170 */
10171 if (phba->sli_rev != LPFC_SLI_REV4) {
10172 for (i = 0; i < psli->num_rings; i++) {
10173 pring = &psli->sli3_ring[i];
10174 /* Only slow rings */
10175 if (pring->ringno == LPFC_ELS_RING) {
10176 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10177 /* Set the lpfc data pending flag */
10178 set_bit(LPFC_DATA_READY, &phba->data_flags);
10179 }
10180 list_splice_init(&pring->txq, &completions);
10181 }
10182 } else {
10183 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10184 pring = qp->pring;
10185 if (!pring)
10186 continue;
10187 spin_lock_irq(&pring->ring_lock);
10188 list_splice_init(&pring->txq, &completions);
10189 spin_unlock_irq(&pring->ring_lock);
10190 if (pring == phba->sli4_hba.els_wq->pring) {
10191 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10192 /* Set the lpfc data pending flag */
10193 set_bit(LPFC_DATA_READY, &phba->data_flags);
10194 }
10195 }
dea31012005-04-17 16:05:31 -050010196 }
James Smart2e0fef82007-06-17 19:56:36 -050010197 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010198
James Smarta257bf92009-04-06 18:48:10 -040010199 /* Cancel all the IOCBs from the completions list */
10200 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10201 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010202
James Smart0ff10d42008-01-11 01:52:36 -050010203 spin_lock_irqsave(&phba->hbalock, flags);
10204 list_splice_init(&phba->elsbuf, &completions);
10205 phba->elsbuf_cnt = 0;
10206 phba->elsbuf_prev_cnt = 0;
10207 spin_unlock_irqrestore(&phba->hbalock, flags);
10208
10209 while (!list_empty(&completions)) {
10210 list_remove_head(&completions, buf_ptr,
10211 struct lpfc_dmabuf, list);
10212 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10213 kfree(buf_ptr);
10214 }
10215
dea31012005-04-17 16:05:31 -050010216 /* Return any active mbox cmds */
10217 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010218
James Smartda0436e2009-05-22 14:51:39 -040010219 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010220 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010221 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010222
James Smartda0436e2009-05-22 14:51:39 -040010223 return 1;
10224}
James Smart92d7f7b2007-06-17 19:56:38 -050010225
James Smartda0436e2009-05-22 14:51:39 -040010226/**
James Smart3621a712009-04-06 18:47:14 -040010227 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010228 * @srcp: Source memory pointer.
10229 * @destp: Destination memory pointer.
10230 * @cnt: Number of words required to be copied.
10231 *
10232 * This function is used for copying data between driver memory
10233 * and the SLI memory. This function also changes the endianness
10234 * of each word if native endianness is different from SLI
10235 * endianness. This function can be called with or without
10236 * lock.
10237 **/
dea31012005-04-17 16:05:31 -050010238void
10239lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10240{
10241 uint32_t *src = srcp;
10242 uint32_t *dest = destp;
10243 uint32_t ldata;
10244 int i;
10245
10246 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10247 ldata = *src;
10248 ldata = le32_to_cpu(ldata);
10249 *dest = ldata;
10250 src++;
10251 dest++;
10252 }
10253}
10254
James Smarte59058c2008-08-24 21:49:00 -040010255
10256/**
James Smarta0c87cb2009-07-19 10:01:10 -040010257 * lpfc_sli_bemem_bcopy - SLI memory copy function
10258 * @srcp: Source memory pointer.
10259 * @destp: Destination memory pointer.
10260 * @cnt: Number of words required to be copied.
10261 *
10262 * This function is used for copying data between a data structure
10263 * with big endian representation to local endianness.
10264 * This function can be called with or without lock.
10265 **/
10266void
10267lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10268{
10269 uint32_t *src = srcp;
10270 uint32_t *dest = destp;
10271 uint32_t ldata;
10272 int i;
10273
10274 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10275 ldata = *src;
10276 ldata = be32_to_cpu(ldata);
10277 *dest = ldata;
10278 src++;
10279 dest++;
10280 }
10281}
10282
10283/**
James Smart3621a712009-04-06 18:47:14 -040010284 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010285 * @phba: Pointer to HBA context object.
10286 * @pring: Pointer to driver SLI ring object.
10287 * @mp: Pointer to driver buffer object.
10288 *
10289 * This function is called with no lock held.
10290 * It always return zero after adding the buffer to the postbufq
10291 * buffer list.
10292 **/
dea31012005-04-17 16:05:31 -050010293int
James Smart2e0fef82007-06-17 19:56:36 -050010294lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10295 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010296{
10297 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10298 later */
James Smart2e0fef82007-06-17 19:56:36 -050010299 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010300 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010301 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010302 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010303 return 0;
10304}
10305
James Smarte59058c2008-08-24 21:49:00 -040010306/**
James Smart3621a712009-04-06 18:47:14 -040010307 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010308 * @phba: Pointer to HBA context object.
10309 *
10310 * When HBQ is enabled, buffers are searched based on tags. This function
10311 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10312 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10313 * does not conflict with tags of buffer posted for unsolicited events.
10314 * The function returns the allocated tag. The function is called with
10315 * no locks held.
10316 **/
James Smart76bb24e2007-10-27 13:38:00 -040010317uint32_t
10318lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10319{
10320 spin_lock_irq(&phba->hbalock);
10321 phba->buffer_tag_count++;
10322 /*
10323 * Always set the QUE_BUFTAG_BIT to distiguish between
10324 * a tag assigned by HBQ.
10325 */
10326 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10327 spin_unlock_irq(&phba->hbalock);
10328 return phba->buffer_tag_count;
10329}
10330
James Smarte59058c2008-08-24 21:49:00 -040010331/**
James Smart3621a712009-04-06 18:47:14 -040010332 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010333 * @phba: Pointer to HBA context object.
10334 * @pring: Pointer to driver SLI ring object.
10335 * @tag: Buffer tag.
10336 *
10337 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10338 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10339 * iocb is posted to the response ring with the tag of the buffer.
10340 * This function searches the pring->postbufq list using the tag
10341 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10342 * iocb. If the buffer is found then lpfc_dmabuf object of the
10343 * buffer is returned to the caller else NULL is returned.
10344 * This function is called with no lock held.
10345 **/
James Smart76bb24e2007-10-27 13:38:00 -040010346struct lpfc_dmabuf *
10347lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10348 uint32_t tag)
10349{
10350 struct lpfc_dmabuf *mp, *next_mp;
10351 struct list_head *slp = &pring->postbufq;
10352
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010353 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010354 spin_lock_irq(&phba->hbalock);
10355 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10356 if (mp->buffer_tag == tag) {
10357 list_del_init(&mp->list);
10358 pring->postbufq_cnt--;
10359 spin_unlock_irq(&phba->hbalock);
10360 return mp;
10361 }
10362 }
10363
10364 spin_unlock_irq(&phba->hbalock);
10365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010366 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010367 "ring %d Data x%lx x%p x%p x%x\n",
10368 pring->ringno, (unsigned long) tag,
10369 slp->next, slp->prev, pring->postbufq_cnt);
10370
10371 return NULL;
10372}
dea31012005-04-17 16:05:31 -050010373
James Smarte59058c2008-08-24 21:49:00 -040010374/**
James Smart3621a712009-04-06 18:47:14 -040010375 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010376 * @phba: Pointer to HBA context object.
10377 * @pring: Pointer to driver SLI ring object.
10378 * @phys: DMA address of the buffer.
10379 *
10380 * This function searches the buffer list using the dma_address
10381 * of unsolicited event to find the driver's lpfc_dmabuf object
10382 * corresponding to the dma_address. The function returns the
10383 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10384 * This function is called by the ct and els unsolicited event
10385 * handlers to get the buffer associated with the unsolicited
10386 * event.
10387 *
10388 * This function is called with no lock held.
10389 **/
dea31012005-04-17 16:05:31 -050010390struct lpfc_dmabuf *
10391lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10392 dma_addr_t phys)
10393{
10394 struct lpfc_dmabuf *mp, *next_mp;
10395 struct list_head *slp = &pring->postbufq;
10396
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010397 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010398 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010399 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10400 if (mp->phys == phys) {
10401 list_del_init(&mp->list);
10402 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010403 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010404 return mp;
10405 }
10406 }
10407
James Smart2e0fef82007-06-17 19:56:36 -050010408 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010410 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010411 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010412 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010413 slp->next, slp->prev, pring->postbufq_cnt);
10414 return NULL;
10415}
10416
James Smarte59058c2008-08-24 21:49:00 -040010417/**
James Smart3621a712009-04-06 18:47:14 -040010418 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010419 * @phba: Pointer to HBA context object.
10420 * @cmdiocb: Pointer to driver command iocb object.
10421 * @rspiocb: Pointer to driver response iocb object.
10422 *
10423 * This function is the completion handler for the abort iocbs for
10424 * ELS commands. This function is called from the ELS ring event
10425 * handler with no lock held. This function frees memory resources
10426 * associated with the abort iocb.
10427 **/
dea31012005-04-17 16:05:31 -050010428static void
James Smart2e0fef82007-06-17 19:56:36 -050010429lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10430 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010431{
James Smart2e0fef82007-06-17 19:56:36 -050010432 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010433 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010434 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010435
10436 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010437
10438 /*
10439 * Assume that the port already completed and returned, or
10440 * will return the iocb. Just Log the message.
10441 */
James Smart2680eea2007-04-25 09:52:55 -040010442 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10443 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10444
James Smart2e0fef82007-06-17 19:56:36 -050010445 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010446 if (phba->sli_rev < LPFC_SLI_REV4) {
10447 if (abort_iotag != 0 &&
10448 abort_iotag <= phba->sli.last_iotag)
10449 abort_iocb =
10450 phba->sli.iocbq_lookup[abort_iotag];
10451 } else
10452 /* For sli4 the abort_tag is the XRI,
10453 * so the abort routine puts the iotag of the iocb
10454 * being aborted in the context field of the abort
10455 * IOCB.
10456 */
10457 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010458
James Smart2a9bf3d2010-06-07 15:24:45 -040010459 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10460 "0327 Cannot abort els iocb %p "
10461 "with tag %x context %x, abort status %x, "
10462 "abort code %x\n",
10463 abort_iocb, abort_iotag, abort_context,
10464 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010465
James Smartff78d8f2011-12-13 13:21:35 -050010466 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010467 }
James Bottomley604a3e32005-10-29 10:28:33 -050010468 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010469 return;
10470}
10471
James Smarte59058c2008-08-24 21:49:00 -040010472/**
James Smart3621a712009-04-06 18:47:14 -040010473 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010474 * @phba: Pointer to HBA context object.
10475 * @cmdiocb: Pointer to driver command iocb object.
10476 * @rspiocb: Pointer to driver response iocb object.
10477 *
10478 * The function is called from SLI ring event handler with no
10479 * lock held. This function is the completion handler for ELS commands
10480 * which are aborted. The function frees memory resources used for
10481 * the aborted ELS commands.
10482 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010483static void
10484lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10485 struct lpfc_iocbq *rspiocb)
10486{
10487 IOCB_t *irsp = &rspiocb->iocb;
10488
10489 /* ELS cmd tag <ulpIoTag> completes */
10490 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010491 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010492 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010493 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010494 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010495 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10496 lpfc_ct_free_iocb(phba, cmdiocb);
10497 else
10498 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010499 return;
10500}
10501
James Smarte59058c2008-08-24 21:49:00 -040010502/**
James Smart5af5eee2010-10-22 11:06:38 -040010503 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010504 * @phba: Pointer to HBA context object.
10505 * @pring: Pointer to driver SLI ring object.
10506 * @cmdiocb: Pointer to driver command iocb object.
10507 *
James Smart5af5eee2010-10-22 11:06:38 -040010508 * This function issues an abort iocb for the provided command iocb down to
10509 * the port. Other than the case the outstanding command iocb is an abort
10510 * request, this function issues abort out unconditionally. This function is
10511 * called with hbalock held. The function returns 0 when it fails due to
10512 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010513 **/
James Smart5af5eee2010-10-22 11:06:38 -040010514static int
10515lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010516 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010517{
James Smart2e0fef82007-06-17 19:56:36 -050010518 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010519 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010520 IOCB_t *icmd = NULL;
10521 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010522 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010523 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010524
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010525 lockdep_assert_held(&phba->hbalock);
10526
James Smart92d7f7b2007-06-17 19:56:38 -050010527 /*
10528 * There are certain command types we don't want to abort. And we
10529 * don't want to abort commands that are already in the process of
10530 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010531 */
10532 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010533 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010534 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10535 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010536 return 0;
10537
dea31012005-04-17 16:05:31 -050010538 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010539 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010540 if (abtsiocbp == NULL)
10541 return 0;
dea31012005-04-17 16:05:31 -050010542
James Smart07951072007-04-25 09:51:38 -040010543 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010544 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010545 */
10546 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10547
dea31012005-04-17 16:05:31 -050010548 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010549 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10550 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010551 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010552 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010553 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10554 }
James Smartda0436e2009-05-22 14:51:39 -040010555 else
10556 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010557 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010558 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010559
James Smart5ffc2662009-11-18 15:39:44 -050010560 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010561 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010562 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10563 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010564 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10565 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010566
James Smart2e0fef82007-06-17 19:56:36 -050010567 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010568 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10569 else
10570 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10571
10572 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010573 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010574
James Smarte8b62012007-08-02 11:10:09 -040010575 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10576 "0339 Abort xri x%x, original iotag x%x, "
10577 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010578 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010579 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010580 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010581
10582 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010583 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10584 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010585 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010586 /* Note: both hbalock and ring_lock need to be set here */
10587 spin_lock_irqsave(&pring->ring_lock, iflags);
10588 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10589 abtsiocbp, 0);
10590 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10591 } else {
10592 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10593 abtsiocbp, 0);
10594 }
James Smart07951072007-04-25 09:51:38 -040010595
James Smartd7c255b2008-08-24 21:50:00 -040010596 if (retval)
10597 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010598
10599 /*
10600 * Caller to this routine should check for IOCB_ERROR
10601 * and handle it properly. This routine no longer removes
10602 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10603 */
10604 return retval;
10605}
10606
10607/**
10608 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10609 * @phba: Pointer to HBA context object.
10610 * @pring: Pointer to driver SLI ring object.
10611 * @cmdiocb: Pointer to driver command iocb object.
10612 *
10613 * This function issues an abort iocb for the provided command iocb. In case
10614 * of unloading, the abort iocb will not be issued to commands on the ELS
10615 * ring. Instead, the callback function shall be changed to those commands
10616 * so that nothing happens when them finishes. This function is called with
10617 * hbalock held. The function returns 0 when the command iocb is an abort
10618 * request.
10619 **/
10620int
10621lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10622 struct lpfc_iocbq *cmdiocb)
10623{
10624 struct lpfc_vport *vport = cmdiocb->vport;
10625 int retval = IOCB_ERROR;
10626 IOCB_t *icmd = NULL;
10627
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010628 lockdep_assert_held(&phba->hbalock);
10629
James Smart5af5eee2010-10-22 11:06:38 -040010630 /*
10631 * There are certain command types we don't want to abort. And we
10632 * don't want to abort commands that are already in the process of
10633 * being aborted.
10634 */
10635 icmd = &cmdiocb->iocb;
10636 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10637 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10638 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10639 return 0;
10640
10641 /*
10642 * If we're unloading, don't abort iocb on the ELS ring, but change
10643 * the callback so that nothing happens when it finishes.
10644 */
10645 if ((vport->load_flag & FC_UNLOADING) &&
10646 (pring->ringno == LPFC_ELS_RING)) {
10647 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10648 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10649 else
10650 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10651 goto abort_iotag_exit;
10652 }
10653
10654 /* Now, we try to issue the abort to the cmdiocb out */
10655 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10656
James Smart07951072007-04-25 09:51:38 -040010657abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010658 /*
10659 * Caller to this routine should check for IOCB_ERROR
10660 * and handle it properly. This routine no longer removes
10661 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010662 */
James Smart2e0fef82007-06-17 19:56:36 -050010663 return retval;
dea31012005-04-17 16:05:31 -050010664}
10665
James Smarte59058c2008-08-24 21:49:00 -040010666/**
James Smart895427b2017-02-12 13:52:30 -080010667 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10668 * @phba: Pointer to HBA context object.
10669 * @pring: Pointer to driver SLI ring object.
10670 * @cmdiocb: Pointer to driver command iocb object.
10671 *
10672 * This function issues an abort iocb for the provided command iocb down to
10673 * the port. Other than the case the outstanding command iocb is an abort
10674 * request, this function issues abort out unconditionally. This function is
10675 * called with hbalock held. The function returns 0 when it fails due to
10676 * memory allocation failure or when the command iocb is an abort request.
10677 **/
10678static int
10679lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10680 struct lpfc_iocbq *cmdiocb)
10681{
10682 struct lpfc_vport *vport = cmdiocb->vport;
10683 struct lpfc_iocbq *abtsiocbp;
10684 union lpfc_wqe *abts_wqe;
10685 int retval;
10686
10687 /*
10688 * There are certain command types we don't want to abort. And we
10689 * don't want to abort commands that are already in the process of
10690 * being aborted.
10691 */
10692 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10693 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10694 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10695 return 0;
10696
10697 /* issue ABTS for this io based on iotag */
10698 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10699 if (abtsiocbp == NULL)
10700 return 0;
10701
10702 /* This signals the response to set the correct status
10703 * before calling the completion handler
10704 */
10705 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10706
10707 /* Complete prepping the abort wqe and issue to the FW. */
10708 abts_wqe = &abtsiocbp->wqe;
10709 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10710 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10711
10712 /* Explicitly set reserved fields to zero.*/
10713 abts_wqe->abort_cmd.rsrvd4 = 0;
10714 abts_wqe->abort_cmd.rsrvd5 = 0;
10715
10716 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10717 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10718 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10719
10720 /* word 7 */
10721 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10722 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10723 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10724 cmdiocb->iocb.ulpClass);
10725
10726 /* word 8 - tell the FW to abort the IO associated with this
10727 * outstanding exchange ID.
10728 */
10729 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10730
10731 /* word 9 - this is the iotag for the abts_wqe completion. */
10732 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10733 abtsiocbp->iotag);
10734
10735 /* word 10 */
10736 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10737 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10738 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10739
10740 /* word 11 */
10741 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10742 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10743 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10744
10745 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10746 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10747 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010748 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010749 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10750 if (retval == IOCB_ERROR) {
10751 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10752 "6147 Failed abts issue_wqe with status x%x "
10753 "for oxid x%x\n",
10754 retval, cmdiocb->sli4_xritag);
10755 lpfc_sli_release_iocbq(phba, abtsiocbp);
10756 return retval;
10757 }
10758
10759 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10760 "6148 Drv Abort NVME Request Issued for "
10761 "ox_id x%x on reqtag x%x\n",
10762 cmdiocb->sli4_xritag,
10763 abtsiocbp->iotag);
10764
10765 return retval;
10766}
10767
10768/**
James Smart5af5eee2010-10-22 11:06:38 -040010769 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10770 * @phba: pointer to lpfc HBA data structure.
10771 *
10772 * This routine will abort all pending and outstanding iocbs to an HBA.
10773 **/
10774void
10775lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10776{
10777 struct lpfc_sli *psli = &phba->sli;
10778 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010779 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010780 int i;
10781
James Smart895427b2017-02-12 13:52:30 -080010782 if (phba->sli_rev != LPFC_SLI_REV4) {
10783 for (i = 0; i < psli->num_rings; i++) {
10784 pring = &psli->sli3_ring[i];
10785 lpfc_sli_abort_iocb_ring(phba, pring);
10786 }
10787 return;
10788 }
10789 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10790 pring = qp->pring;
10791 if (!pring)
10792 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010793 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010794 }
10795}
10796
10797/**
James Smart3621a712009-04-06 18:47:14 -040010798 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010799 * @iocbq: Pointer to driver iocb object.
10800 * @vport: Pointer to driver virtual port object.
10801 * @tgt_id: SCSI ID of the target.
10802 * @lun_id: LUN ID of the scsi device.
10803 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10804 *
James Smart3621a712009-04-06 18:47:14 -040010805 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010806 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10807 * 0 if the filtering criteria is met for the given iocb and will return
10808 * 1 if the filtering criteria is not met.
10809 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10810 * given iocb is for the SCSI device specified by vport, tgt_id and
10811 * lun_id parameter.
10812 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10813 * given iocb is for the SCSI target specified by vport and tgt_id
10814 * parameters.
10815 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10816 * given iocb is for the SCSI host associated with the given vport.
10817 * This function is called with no locks held.
10818 **/
dea31012005-04-17 16:05:31 -050010819static int
James Smart51ef4c22007-08-02 11:10:31 -040010820lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10821 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010822 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010823{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010824 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010825 int rc = 1;
10826
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010827 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10828 return rc;
10829
James Smart51ef4c22007-08-02 11:10:31 -040010830 if (iocbq->vport != vport)
10831 return rc;
10832
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010833 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010834
James Smart495a7142008-06-14 22:52:59 -040010835 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010836 return rc;
10837
10838 switch (ctx_cmd) {
10839 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010840 if ((lpfc_cmd->rdata->pnode) &&
10841 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10842 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010843 rc = 0;
10844 break;
10845 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010846 if ((lpfc_cmd->rdata->pnode) &&
10847 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010848 rc = 0;
10849 break;
dea31012005-04-17 16:05:31 -050010850 case LPFC_CTX_HOST:
10851 rc = 0;
10852 break;
10853 default:
10854 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010855 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010856 break;
10857 }
10858
10859 return rc;
10860}
10861
James Smarte59058c2008-08-24 21:49:00 -040010862/**
James Smart3621a712009-04-06 18:47:14 -040010863 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010864 * @vport: Pointer to virtual port.
10865 * @tgt_id: SCSI ID of the target.
10866 * @lun_id: LUN ID of the scsi device.
10867 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10868 *
10869 * This function returns number of FCP commands pending for the vport.
10870 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10871 * commands pending on the vport associated with SCSI device specified
10872 * by tgt_id and lun_id parameters.
10873 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10874 * commands pending on the vport associated with SCSI target specified
10875 * by tgt_id parameter.
10876 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10877 * commands pending on the vport.
10878 * This function returns the number of iocbs which satisfy the filter.
10879 * This function is called without any lock held.
10880 **/
dea31012005-04-17 16:05:31 -050010881int
James Smart51ef4c22007-08-02 11:10:31 -040010882lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10883 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010884{
James Smart51ef4c22007-08-02 11:10:31 -040010885 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010886 struct lpfc_iocbq *iocbq;
10887 int sum, i;
dea31012005-04-17 16:05:31 -050010888
Johannes Thumshirn31979002016-07-18 16:06:03 +020010889 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010890 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10891 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010892
James Smart51ef4c22007-08-02 11:10:31 -040010893 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10894 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010895 sum++;
dea31012005-04-17 16:05:31 -050010896 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010897 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010898
dea31012005-04-17 16:05:31 -050010899 return sum;
10900}
10901
James Smarte59058c2008-08-24 21:49:00 -040010902/**
James Smart3621a712009-04-06 18:47:14 -040010903 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010904 * @phba: Pointer to HBA context object
10905 * @cmdiocb: Pointer to command iocb object.
10906 * @rspiocb: Pointer to response iocb object.
10907 *
10908 * This function is called when an aborted FCP iocb completes. This
10909 * function is called by the ring event handler with no lock held.
10910 * This function frees the iocb.
10911 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010912void
James Smart2e0fef82007-06-17 19:56:36 -050010913lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10914 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010915{
James Smartcb69f7d2011-12-13 13:21:57 -050010916 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010917 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010918 "original iotag x%x, abort cmd iotag x%x "
10919 "status 0x%x, reason 0x%x\n",
10920 cmdiocb->iocb.un.acxri.abortContextTag,
10921 cmdiocb->iocb.un.acxri.abortIoTag,
10922 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10923 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010924 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010925 return;
10926}
10927
James Smarte59058c2008-08-24 21:49:00 -040010928/**
James Smart3621a712009-04-06 18:47:14 -040010929 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010930 * @vport: Pointer to virtual port.
10931 * @pring: Pointer to driver SLI ring object.
10932 * @tgt_id: SCSI ID of the target.
10933 * @lun_id: LUN ID of the scsi device.
10934 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10935 *
10936 * This function sends an abort command for every SCSI command
10937 * associated with the given virtual port pending on the ring
10938 * filtered by lpfc_sli_validate_fcp_iocb function.
10939 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10940 * FCP iocbs associated with lun specified by tgt_id and lun_id
10941 * parameters
10942 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10943 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10944 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10945 * FCP iocbs associated with virtual port.
10946 * This function returns number of iocbs it failed to abort.
10947 * This function is called with no locks held.
10948 **/
dea31012005-04-17 16:05:31 -050010949int
James Smart51ef4c22007-08-02 11:10:31 -040010950lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10951 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010952{
James Smart51ef4c22007-08-02 11:10:31 -040010953 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010954 struct lpfc_iocbq *iocbq;
10955 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070010956 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050010957 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010958 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010959 int i;
dea31012005-04-17 16:05:31 -050010960
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010961 for (i = 1; i <= phba->sli.last_iotag; i++) {
10962 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010963
James Smart51ef4c22007-08-02 11:10:31 -040010964 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010965 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010966 continue;
10967
James Smartafbd8d82013-09-06 12:22:13 -040010968 /*
10969 * If the iocbq is already being aborted, don't take a second
10970 * action, but do count it.
10971 */
10972 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10973 continue;
10974
dea31012005-04-17 16:05:31 -050010975 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010976 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010977 if (abtsiocb == NULL) {
10978 errcnt++;
10979 continue;
10980 }
dea31012005-04-17 16:05:31 -050010981
James Smartafbd8d82013-09-06 12:22:13 -040010982 /* indicate the IO is being aborted by the driver. */
10983 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10984
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010985 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010986 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10987 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010988 if (phba->sli_rev == LPFC_SLI_REV4)
10989 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10990 else
10991 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010992 abtsiocb->iocb.ulpLe = 1;
10993 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010994 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010995
James Smart5ffc2662009-11-18 15:39:44 -050010996 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010997 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010998 if (iocbq->iocb_flag & LPFC_IO_FCP)
10999 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011000 if (iocbq->iocb_flag & LPFC_IO_FOF)
11001 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011002
James Smart2e0fef82007-06-17 19:56:36 -050011003 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011004 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11005 else
11006 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11007
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011008 /* Setup callback routine and issue the command. */
11009 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011010 if (phba->sli_rev == LPFC_SLI_REV4) {
11011 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11012 if (!pring_s4)
11013 continue;
11014 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11015 abtsiocb, 0);
11016 } else
11017 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11018 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011019 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011020 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011021 errcnt++;
11022 continue;
11023 }
11024 }
11025
11026 return errcnt;
11027}
11028
James Smarte59058c2008-08-24 21:49:00 -040011029/**
James Smart98912dda2014-04-04 13:52:31 -040011030 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11031 * @vport: Pointer to virtual port.
11032 * @pring: Pointer to driver SLI ring object.
11033 * @tgt_id: SCSI ID of the target.
11034 * @lun_id: LUN ID of the scsi device.
11035 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11036 *
11037 * This function sends an abort command for every SCSI command
11038 * associated with the given virtual port pending on the ring
11039 * filtered by lpfc_sli_validate_fcp_iocb function.
11040 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11041 * FCP iocbs associated with lun specified by tgt_id and lun_id
11042 * parameters
11043 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11044 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11045 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11046 * FCP iocbs associated with virtual port.
11047 * This function returns number of iocbs it aborted .
11048 * This function is called with no locks held right after a taskmgmt
11049 * command is sent.
11050 **/
11051int
11052lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11053 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11054{
11055 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011056 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011057 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011058 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011059 struct lpfc_iocbq *iocbq;
11060 IOCB_t *icmd;
11061 int sum, i, ret_val;
11062 unsigned long iflags;
11063 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011064
11065 spin_lock_irq(&phba->hbalock);
11066
11067 /* all I/Os are in process of being flushed */
11068 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11069 spin_unlock_irq(&phba->hbalock);
11070 return 0;
11071 }
11072 sum = 0;
11073
11074 for (i = 1; i <= phba->sli.last_iotag; i++) {
11075 iocbq = phba->sli.iocbq_lookup[i];
11076
11077 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11078 cmd) != 0)
11079 continue;
11080
11081 /*
11082 * If the iocbq is already being aborted, don't take a second
11083 * action, but do count it.
11084 */
11085 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11086 continue;
11087
11088 /* issue ABTS for this IOCB based on iotag */
11089 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11090 if (abtsiocbq == NULL)
11091 continue;
11092
11093 icmd = &iocbq->iocb;
11094 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11095 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11096 if (phba->sli_rev == LPFC_SLI_REV4)
11097 abtsiocbq->iocb.un.acxri.abortIoTag =
11098 iocbq->sli4_xritag;
11099 else
11100 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11101 abtsiocbq->iocb.ulpLe = 1;
11102 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11103 abtsiocbq->vport = vport;
11104
11105 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011106 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011107 if (iocbq->iocb_flag & LPFC_IO_FCP)
11108 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011109 if (iocbq->iocb_flag & LPFC_IO_FOF)
11110 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011111
James Smart8c50d252014-09-03 12:58:16 -040011112 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11113 ndlp = lpfc_cmd->rdata->pnode;
11114
11115 if (lpfc_is_link_up(phba) &&
11116 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011117 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11118 else
11119 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11120
11121 /* Setup callback routine and issue the command. */
11122 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11123
11124 /*
11125 * Indicate the IO is being aborted by the driver and set
11126 * the caller's flag into the aborted IO.
11127 */
11128 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11129
11130 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011131 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11132 if (pring_s4 == NULL)
11133 continue;
James Smart98912dda2014-04-04 13:52:31 -040011134 /* Note: both hbalock and ring_lock must be set here */
11135 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11136 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11137 abtsiocbq, 0);
11138 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11139 } else {
11140 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11141 abtsiocbq, 0);
11142 }
11143
11144
11145 if (ret_val == IOCB_ERROR)
11146 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11147 else
11148 sum++;
11149 }
11150 spin_unlock_irq(&phba->hbalock);
11151 return sum;
11152}
11153
11154/**
James Smart3621a712009-04-06 18:47:14 -040011155 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011156 * @phba: Pointer to HBA context object.
11157 * @cmdiocbq: Pointer to command iocb.
11158 * @rspiocbq: Pointer to response iocb.
11159 *
11160 * This function is the completion handler for iocbs issued using
11161 * lpfc_sli_issue_iocb_wait function. This function is called by the
11162 * ring event handler function without any lock held. This function
11163 * can be called from both worker thread context and interrupt
11164 * context. This function also can be called from other thread which
11165 * cleans up the SLI layer objects.
11166 * This function copy the contents of the response iocb to the
11167 * response iocb memory object provided by the caller of
11168 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11169 * sleeps for the iocb completion.
11170 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011171static void
11172lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11173 struct lpfc_iocbq *cmdiocbq,
11174 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011175{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011176 wait_queue_head_t *pdone_q;
11177 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011178 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011179
James Smart2e0fef82007-06-17 19:56:36 -050011180 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011181 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11182
11183 /*
11184 * A time out has occurred for the iocb. If a time out
11185 * completion handler has been supplied, call it. Otherwise,
11186 * just free the iocbq.
11187 */
11188
11189 spin_unlock_irqrestore(&phba->hbalock, iflags);
11190 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11191 cmdiocbq->wait_iocb_cmpl = NULL;
11192 if (cmdiocbq->iocb_cmpl)
11193 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11194 else
11195 lpfc_sli_release_iocbq(phba, cmdiocbq);
11196 return;
11197 }
11198
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011199 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11200 if (cmdiocbq->context2 && rspiocbq)
11201 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11202 &rspiocbq->iocb, sizeof(IOCB_t));
11203
James Smart0f65ff62010-02-26 14:14:23 -050011204 /* Set the exchange busy flag for task management commands */
11205 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11206 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11207 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11208 cur_iocbq);
11209 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11210 }
11211
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011212 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011213 if (pdone_q)
11214 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011215 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011216 return;
11217}
11218
James Smarte59058c2008-08-24 21:49:00 -040011219/**
James Smartd11e31d2009-06-10 17:23:06 -040011220 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11221 * @phba: Pointer to HBA context object..
11222 * @piocbq: Pointer to command iocb.
11223 * @flag: Flag to test.
11224 *
11225 * This routine grabs the hbalock and then test the iocb_flag to
11226 * see if the passed in flag is set.
11227 * Returns:
11228 * 1 if flag is set.
11229 * 0 if flag is not set.
11230 **/
11231static int
11232lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11233 struct lpfc_iocbq *piocbq, uint32_t flag)
11234{
11235 unsigned long iflags;
11236 int ret;
11237
11238 spin_lock_irqsave(&phba->hbalock, iflags);
11239 ret = piocbq->iocb_flag & flag;
11240 spin_unlock_irqrestore(&phba->hbalock, iflags);
11241 return ret;
11242
11243}
11244
11245/**
James Smart3621a712009-04-06 18:47:14 -040011246 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011247 * @phba: Pointer to HBA context object..
11248 * @pring: Pointer to sli ring.
11249 * @piocb: Pointer to command iocb.
11250 * @prspiocbq: Pointer to response iocb.
11251 * @timeout: Timeout in number of seconds.
11252 *
11253 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011254 * iocb to complete. The iocb_cmpl field of the shall be used
11255 * to handle iocbs which time out. If the field is NULL, the
11256 * function shall free the iocbq structure. If more clean up is
11257 * needed, the caller is expected to provide a completion function
11258 * that will provide the needed clean up. If the iocb command is
11259 * not completed within timeout seconds, the function will either
11260 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11261 * completion function set in the iocb_cmpl field and then return
11262 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11263 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011264 * The function waits for the iocb completion using an
11265 * non-interruptible wait.
11266 * This function will sleep while waiting for iocb completion.
11267 * So, this function should not be called from any context which
11268 * does not allow sleeping. Due to the same reason, this function
11269 * cannot be called with interrupt disabled.
11270 * This function assumes that the iocb completions occur while
11271 * this function sleep. So, this function cannot be called from
11272 * the thread which process iocb completion for this ring.
11273 * This function clears the iocb_flag of the iocb object before
11274 * issuing the iocb and the iocb completion handler sets this
11275 * flag and wakes this thread when the iocb completes.
11276 * The contents of the response iocb will be copied to prspiocbq
11277 * by the completion handler when the command completes.
11278 * This function returns IOCB_SUCCESS when success.
11279 * This function is called with no lock held.
11280 **/
dea31012005-04-17 16:05:31 -050011281int
James Smart2e0fef82007-06-17 19:56:36 -050011282lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011283 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011284 struct lpfc_iocbq *piocb,
11285 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011286 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011287{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011288 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011289 long timeleft, timeout_req = 0;
11290 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011291 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011292 struct lpfc_iocbq *iocb;
11293 int txq_cnt = 0;
11294 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011295 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011296 unsigned long iflags;
11297 bool iocb_completed = true;
11298
James Smart895427b2017-02-12 13:52:30 -080011299 if (phba->sli_rev >= LPFC_SLI_REV4)
11300 pring = lpfc_sli4_calc_ring(phba, piocb);
11301 else
11302 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011303 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011304 * If the caller has provided a response iocbq buffer, then context2
11305 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011306 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011307 if (prspiocbq) {
11308 if (piocb->context2)
11309 return IOCB_ERROR;
11310 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011311 }
11312
James Smart5a0916b2013-07-15 18:31:42 -040011313 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011314 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11315 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011316 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011317
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011318 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011319 if (lpfc_readl(phba->HCregaddr, &creg_val))
11320 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011321 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11322 writel(creg_val, phba->HCregaddr);
11323 readl(phba->HCregaddr); /* flush */
11324 }
11325
James Smart2a9bf3d2010-06-07 15:24:45 -040011326 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11327 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011328 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011329 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011330 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011331 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011332 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011333 spin_lock_irqsave(&phba->hbalock, iflags);
11334 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011335
James Smart5a0916b2013-07-15 18:31:42 -040011336 /*
11337 * IOCB timed out. Inform the wake iocb wait
11338 * completion function and set local status
11339 */
11340
11341 iocb_completed = false;
11342 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11343 }
11344 spin_unlock_irqrestore(&phba->hbalock, iflags);
11345 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011346 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011347 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011348 /* Note: we are not indicating if the IOCB has a success
11349 * status or not - that's for the caller to check.
11350 * IOCB_SUCCESS means just that the command was sent and
11351 * completed. Not that it completed successfully.
11352 * */
James Smart7054a602007-04-25 09:52:34 -040011353 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011354 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011355 "0338 IOCB wait timeout error - no "
11356 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011357 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011358 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011359 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011360 "0330 IOCB wake NOT set, "
11361 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011362 timeout, (timeleft / jiffies));
11363 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011364 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011365 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011366 if (phba->cfg_log_verbose & LOG_SLI) {
11367 list_for_each_entry(iocb, &pring->txq, list) {
11368 txq_cnt++;
11369 }
11370 list_for_each_entry(iocb, &pring->txcmplq, list) {
11371 txcmplq_cnt++;
11372 }
11373 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11374 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11375 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11376 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011377 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011378 } else {
11379 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011380 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011381 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011382 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011383 }
11384
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011385 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011386 if (lpfc_readl(phba->HCregaddr, &creg_val))
11387 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011388 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11389 writel(creg_val, phba->HCregaddr);
11390 readl(phba->HCregaddr); /* flush */
11391 }
11392
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011393 if (prspiocbq)
11394 piocb->context2 = NULL;
11395
11396 piocb->context_un.wait_queue = NULL;
11397 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011398 return retval;
11399}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011400
James Smarte59058c2008-08-24 21:49:00 -040011401/**
James Smart3621a712009-04-06 18:47:14 -040011402 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011403 * @phba: Pointer to HBA context object.
11404 * @pmboxq: Pointer to driver mailbox object.
11405 * @timeout: Timeout in number of seconds.
11406 *
11407 * This function issues the mailbox to firmware and waits for the
11408 * mailbox command to complete. If the mailbox command is not
11409 * completed within timeout seconds, it returns MBX_TIMEOUT.
11410 * The function waits for the mailbox completion using an
11411 * interruptible wait. If the thread is woken up due to a
11412 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11413 * should not free the mailbox resources, if this function returns
11414 * MBX_TIMEOUT.
11415 * This function will sleep while waiting for mailbox completion.
11416 * So, this function should not be called from any context which
11417 * does not allow sleeping. Due to the same reason, this function
11418 * cannot be called with interrupt disabled.
11419 * This function assumes that the mailbox completion occurs while
11420 * this function sleep. So, this function cannot be called from
11421 * the worker thread which processes mailbox completion.
11422 * This function is called in the context of HBA management
11423 * applications.
11424 * This function returns MBX_SUCCESS when successful.
11425 * This function is called with no lock held.
11426 **/
dea31012005-04-17 16:05:31 -050011427int
James Smart2e0fef82007-06-17 19:56:36 -050011428lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011429 uint32_t timeout)
11430{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011431 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a22013-05-31 17:05:27 -040011432 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011433 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011434 unsigned long flag;
dea31012005-04-17 16:05:31 -050011435
James Smartb230b8a22013-05-31 17:05:27 -040011436 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011437 if (pmboxq->context1)
James Smartb230b8a22013-05-31 17:05:27 -040011438 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011439
James Smart495a7142008-06-14 22:52:59 -040011440 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011441 /* setup wake call as IOCB callback */
11442 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11443 /* setup context field to pass wait_queue pointer to wake function */
11444 pmboxq->context1 = &done_q;
11445
dea31012005-04-17 16:05:31 -050011446 /* now issue the command */
11447 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011448 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011449 wait_event_interruptible_timeout(done_q,
11450 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011451 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011452
James Smart858c9f62007-06-17 19:56:39 -050011453 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a22013-05-31 17:05:27 -040011454 /* restore the possible extended buffer for free resource */
11455 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011456 /*
11457 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11458 * else do not free the resources.
11459 */
James Smartd7c47992010-06-08 18:31:54 -040011460 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011461 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011462 } else {
James Smart7054a602007-04-25 09:52:34 -040011463 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011464 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11465 }
11466 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a22013-05-31 17:05:27 -040011467 } else {
11468 /* restore the possible extended buffer for free resource */
11469 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011470 }
11471
dea31012005-04-17 16:05:31 -050011472 return retval;
11473}
11474
James Smarte59058c2008-08-24 21:49:00 -040011475/**
James Smart3772a992009-05-22 14:50:54 -040011476 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011477 * @phba: Pointer to HBA context.
11478 *
James Smart3772a992009-05-22 14:50:54 -040011479 * This function is called to shutdown the driver's mailbox sub-system.
11480 * It first marks the mailbox sub-system is in a block state to prevent
11481 * the asynchronous mailbox command from issued off the pending mailbox
11482 * command queue. If the mailbox command sub-system shutdown is due to
11483 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11484 * the mailbox sub-system flush routine to forcefully bring down the
11485 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11486 * as with offline or HBA function reset), this routine will wait for the
11487 * outstanding mailbox command to complete before invoking the mailbox
11488 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011489 **/
James Smart3772a992009-05-22 14:50:54 -040011490void
James Smart618a5232012-06-12 13:54:36 -040011491lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011492{
James Smart3772a992009-05-22 14:50:54 -040011493 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011494 unsigned long timeout;
11495
James Smart618a5232012-06-12 13:54:36 -040011496 if (mbx_action == LPFC_MBX_NO_WAIT) {
11497 /* delay 100ms for port state */
11498 msleep(100);
11499 lpfc_sli_mbox_sys_flush(phba);
11500 return;
11501 }
James Smarta183a152011-10-10 21:32:43 -040011502 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011503
James Smart3772a992009-05-22 14:50:54 -040011504 spin_lock_irq(&phba->hbalock);
11505 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011506
11507 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011508 /* Determine how long we might wait for the active mailbox
11509 * command to be gracefully completed by firmware.
11510 */
James Smarta183a152011-10-10 21:32:43 -040011511 if (phba->sli.mbox_active)
11512 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11513 phba->sli.mbox_active) *
11514 1000) + jiffies;
11515 spin_unlock_irq(&phba->hbalock);
11516
James Smart3772a992009-05-22 14:50:54 -040011517 while (phba->sli.mbox_active) {
11518 /* Check active mailbox complete status every 2ms */
11519 msleep(2);
11520 if (time_after(jiffies, timeout))
11521 /* Timeout, let the mailbox flush routine to
11522 * forcefully release active mailbox command
11523 */
11524 break;
11525 }
James Smartd7069f02012-03-01 22:36:29 -050011526 } else
11527 spin_unlock_irq(&phba->hbalock);
11528
James Smart3772a992009-05-22 14:50:54 -040011529 lpfc_sli_mbox_sys_flush(phba);
11530}
11531
11532/**
11533 * lpfc_sli_eratt_read - read sli-3 error attention events
11534 * @phba: Pointer to HBA context.
11535 *
11536 * This function is called to read the SLI3 device error attention registers
11537 * for possible error attention events. The caller must hold the hostlock
11538 * with spin_lock_irq().
11539 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011540 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011541 * Register and returns 0 otherwise.
11542 **/
11543static int
11544lpfc_sli_eratt_read(struct lpfc_hba *phba)
11545{
James Smarted957682007-06-17 19:56:37 -050011546 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011547
James Smart3772a992009-05-22 14:50:54 -040011548 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011549 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11550 goto unplug_err;
11551
James Smart3772a992009-05-22 14:50:54 -040011552 if (ha_copy & HA_ERATT) {
11553 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011554 if (lpfc_sli_read_hs(phba))
11555 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011556
James Smart3772a992009-05-22 14:50:54 -040011557 /* Check if there is a deferred error condition is active */
11558 if ((HS_FFER1 & phba->work_hs) &&
11559 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011560 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011561 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011562 /* Clear all interrupt enable conditions */
11563 writel(0, phba->HCregaddr);
11564 readl(phba->HCregaddr);
11565 }
11566
11567 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011568 phba->work_ha |= HA_ERATT;
11569 /* Indicate polling handles this ERATT */
11570 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011571 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011572 }
James Smart3772a992009-05-22 14:50:54 -040011573 return 0;
James Smart9940b972011-03-11 16:06:12 -050011574
11575unplug_err:
11576 /* Set the driver HS work bitmap */
11577 phba->work_hs |= UNPLUG_ERR;
11578 /* Set the driver HA work bitmap */
11579 phba->work_ha |= HA_ERATT;
11580 /* Indicate polling handles this ERATT */
11581 phba->hba_flag |= HBA_ERATT_HANDLED;
11582 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011583}
11584
James Smarte59058c2008-08-24 21:49:00 -040011585/**
James Smartda0436e2009-05-22 14:51:39 -040011586 * lpfc_sli4_eratt_read - read sli-4 error attention events
11587 * @phba: Pointer to HBA context.
11588 *
11589 * This function is called to read the SLI4 device error attention registers
11590 * for possible error attention events. The caller must hold the hostlock
11591 * with spin_lock_irq().
11592 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011593 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011594 * Register and returns 0 otherwise.
11595 **/
11596static int
11597lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11598{
11599 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011600 uint32_t if_type, portsmphr;
11601 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011602
James Smart2fcee4b2010-12-15 17:57:46 -050011603 /*
11604 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011605 * registers for error attention. This can be changed later.
11606 */
James Smart2fcee4b2010-12-15 17:57:46 -050011607 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11608 switch (if_type) {
11609 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011610 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11611 &uerr_sta_lo) ||
11612 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11613 &uerr_sta_hi)) {
11614 phba->work_hs |= UNPLUG_ERR;
11615 phba->work_ha |= HA_ERATT;
11616 phba->hba_flag |= HBA_ERATT_HANDLED;
11617 return 1;
11618 }
James Smart2fcee4b2010-12-15 17:57:46 -050011619 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11620 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11621 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11622 "1423 HBA Unrecoverable error: "
11623 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11624 "ue_mask_lo_reg=0x%x, "
11625 "ue_mask_hi_reg=0x%x\n",
11626 uerr_sta_lo, uerr_sta_hi,
11627 phba->sli4_hba.ue_mask_lo,
11628 phba->sli4_hba.ue_mask_hi);
11629 phba->work_status[0] = uerr_sta_lo;
11630 phba->work_status[1] = uerr_sta_hi;
11631 phba->work_ha |= HA_ERATT;
11632 phba->hba_flag |= HBA_ERATT_HANDLED;
11633 return 1;
11634 }
11635 break;
11636 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011637 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11638 &portstat_reg.word0) ||
11639 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11640 &portsmphr)){
11641 phba->work_hs |= UNPLUG_ERR;
11642 phba->work_ha |= HA_ERATT;
11643 phba->hba_flag |= HBA_ERATT_HANDLED;
11644 return 1;
11645 }
James Smart2fcee4b2010-12-15 17:57:46 -050011646 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11647 phba->work_status[0] =
11648 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11649 phba->work_status[1] =
11650 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11651 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011652 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011653 "port status reg 0x%x, "
11654 "port smphr reg 0x%x, "
11655 "error 1=0x%x, error 2=0x%x\n",
11656 portstat_reg.word0,
11657 portsmphr,
11658 phba->work_status[0],
11659 phba->work_status[1]);
11660 phba->work_ha |= HA_ERATT;
11661 phba->hba_flag |= HBA_ERATT_HANDLED;
11662 return 1;
11663 }
11664 break;
11665 case LPFC_SLI_INTF_IF_TYPE_1:
11666 default:
James Smarta747c9c2009-11-18 15:41:10 -050011667 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011668 "2886 HBA Error Attention on unsupported "
11669 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011670 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011671 }
James Smart2fcee4b2010-12-15 17:57:46 -050011672
James Smartda0436e2009-05-22 14:51:39 -040011673 return 0;
11674}
11675
11676/**
James Smart3621a712009-04-06 18:47:14 -040011677 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011678 * @phba: Pointer to HBA context.
11679 *
James Smart3772a992009-05-22 14:50:54 -040011680 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011681 * error attention register bit for error attention events.
11682 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011683 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011684 * Register and returns 0 otherwise.
11685 **/
11686int
11687lpfc_sli_check_eratt(struct lpfc_hba *phba)
11688{
11689 uint32_t ha_copy;
11690
11691 /* If somebody is waiting to handle an eratt, don't process it
11692 * here. The brdkill function will do this.
11693 */
11694 if (phba->link_flag & LS_IGNORE_ERATT)
11695 return 0;
11696
11697 /* Check if interrupt handler handles this ERATT */
11698 spin_lock_irq(&phba->hbalock);
11699 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11700 /* Interrupt handler has handled ERATT */
11701 spin_unlock_irq(&phba->hbalock);
11702 return 0;
11703 }
11704
James Smarta257bf92009-04-06 18:48:10 -040011705 /*
11706 * If there is deferred error attention, do not check for error
11707 * attention
11708 */
11709 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11710 spin_unlock_irq(&phba->hbalock);
11711 return 0;
11712 }
11713
James Smart3772a992009-05-22 14:50:54 -040011714 /* If PCI channel is offline, don't process it */
11715 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011716 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011717 return 0;
11718 }
11719
11720 switch (phba->sli_rev) {
11721 case LPFC_SLI_REV2:
11722 case LPFC_SLI_REV3:
11723 /* Read chip Host Attention (HA) register */
11724 ha_copy = lpfc_sli_eratt_read(phba);
11725 break;
James Smartda0436e2009-05-22 14:51:39 -040011726 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011727 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011728 ha_copy = lpfc_sli4_eratt_read(phba);
11729 break;
James Smart3772a992009-05-22 14:50:54 -040011730 default:
11731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11732 "0299 Invalid SLI revision (%d)\n",
11733 phba->sli_rev);
11734 ha_copy = 0;
11735 break;
James Smart93996272008-08-24 21:50:30 -040011736 }
11737 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011738
11739 return ha_copy;
11740}
11741
11742/**
11743 * lpfc_intr_state_check - Check device state for interrupt handling
11744 * @phba: Pointer to HBA context.
11745 *
11746 * This inline routine checks whether a device or its PCI slot is in a state
11747 * that the interrupt should be handled.
11748 *
11749 * This function returns 0 if the device or the PCI slot is in a state that
11750 * interrupt should be handled, otherwise -EIO.
11751 */
11752static inline int
11753lpfc_intr_state_check(struct lpfc_hba *phba)
11754{
11755 /* If the pci channel is offline, ignore all the interrupts */
11756 if (unlikely(pci_channel_offline(phba->pcidev)))
11757 return -EIO;
11758
11759 /* Update device level interrupt statistics */
11760 phba->sli.slistat.sli_intr++;
11761
11762 /* Ignore all interrupts during initialization. */
11763 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11764 return -EIO;
11765
James Smart93996272008-08-24 21:50:30 -040011766 return 0;
11767}
11768
11769/**
James Smart3772a992009-05-22 14:50:54 -040011770 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011771 * @irq: Interrupt number.
11772 * @dev_id: The device context pointer.
11773 *
James Smart93996272008-08-24 21:50:30 -040011774 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011775 * service routine when device with SLI-3 interface spec is enabled with
11776 * MSI-X multi-message interrupt mode and there are slow-path events in
11777 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11778 * interrupt mode, this function is called as part of the device-level
11779 * interrupt handler. When the PCI slot is in error recovery or the HBA
11780 * is undergoing initialization, the interrupt handler will not process
11781 * the interrupt. The link attention and ELS ring attention events are
11782 * handled by the worker thread. The interrupt handler signals the worker
11783 * thread and returns for these events. This function is called without
11784 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011785 * structures.
11786 *
11787 * This function returns IRQ_HANDLED when interrupt is handled else it
11788 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011789 **/
dea31012005-04-17 16:05:31 -050011790irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011791lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011792{
James Smart2e0fef82007-06-17 19:56:36 -050011793 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011794 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011795 uint32_t work_ha_copy;
11796 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011797 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011798 uint32_t control;
11799
James Smart92d7f7b2007-06-17 19:56:38 -050011800 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011801 struct lpfc_vport *vport;
11802 struct lpfc_nodelist *ndlp;
11803 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011804 LPFC_MBOXQ_t *pmb;
11805 int rc;
11806
dea31012005-04-17 16:05:31 -050011807 /*
11808 * Get the driver's phba structure from the dev_id and
11809 * assume the HBA is not interrupting.
11810 */
James Smart93996272008-08-24 21:50:30 -040011811 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011812
11813 if (unlikely(!phba))
11814 return IRQ_NONE;
11815
dea31012005-04-17 16:05:31 -050011816 /*
James Smart93996272008-08-24 21:50:30 -040011817 * Stuff needs to be attented to when this function is invoked as an
11818 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011819 */
James Smart93996272008-08-24 21:50:30 -040011820 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011821 /* Check device state for handling interrupt */
11822 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011823 return IRQ_NONE;
11824 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011825 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011826 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11827 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011828 /* If somebody is waiting to handle an eratt don't process it
11829 * here. The brdkill function will do this.
11830 */
11831 if (phba->link_flag & LS_IGNORE_ERATT)
11832 ha_copy &= ~HA_ERATT;
11833 /* Check the need for handling ERATT in interrupt handler */
11834 if (ha_copy & HA_ERATT) {
11835 if (phba->hba_flag & HBA_ERATT_HANDLED)
11836 /* ERATT polling has handled ERATT */
11837 ha_copy &= ~HA_ERATT;
11838 else
11839 /* Indicate interrupt handler handles ERATT */
11840 phba->hba_flag |= HBA_ERATT_HANDLED;
11841 }
James Smarta257bf92009-04-06 18:48:10 -040011842
11843 /*
11844 * If there is deferred error attention, do not check for any
11845 * interrupt.
11846 */
11847 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011848 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011849 return IRQ_NONE;
11850 }
11851
James Smart93996272008-08-24 21:50:30 -040011852 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011853 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11854 goto unplug_error;
11855
James Smarta747c9c2009-11-18 15:41:10 -050011856 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11857 HC_LAINT_ENA | HC_ERINT_ENA),
11858 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011859 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11860 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011861 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011862 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011863 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011864 } else
11865 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011866
dea31012005-04-17 16:05:31 -050011867 work_ha_copy = ha_copy & phba->work_ha_mask;
11868
James Smart93996272008-08-24 21:50:30 -040011869 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011870 if (work_ha_copy & HA_LATT) {
11871 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11872 /*
11873 * Turn off Link Attention interrupts
11874 * until CLEAR_LA done
11875 */
James Smart5b75da22008-12-04 22:39:35 -050011876 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011877 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011878 if (lpfc_readl(phba->HCregaddr, &control))
11879 goto unplug_error;
dea31012005-04-17 16:05:31 -050011880 control &= ~HC_LAINT_ENA;
11881 writel(control, phba->HCregaddr);
11882 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011883 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011884 }
11885 else
11886 work_ha_copy &= ~HA_LATT;
11887 }
11888
James Smart93996272008-08-24 21:50:30 -040011889 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011890 /*
11891 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11892 * the only slow ring.
11893 */
11894 status = (work_ha_copy &
11895 (HA_RXMASK << (4*LPFC_ELS_RING)));
11896 status >>= (4*LPFC_ELS_RING);
11897 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011898 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011899 if (lpfc_readl(phba->HCregaddr, &control))
11900 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011901
11902 lpfc_debugfs_slow_ring_trc(phba,
11903 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11904 control, status,
11905 (uint32_t)phba->sli.slistat.sli_intr);
11906
James Smart858c9f62007-06-17 19:56:39 -050011907 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011908 lpfc_debugfs_slow_ring_trc(phba,
11909 "ISR Disable ring:"
11910 "pwork:x%x hawork:x%x wait:x%x",
11911 phba->work_ha, work_ha_copy,
11912 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011913 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011914
James Smart858c9f62007-06-17 19:56:39 -050011915 control &=
11916 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011917 writel(control, phba->HCregaddr);
11918 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011919 }
James Smarta58cbd52007-08-02 11:09:43 -040011920 else {
11921 lpfc_debugfs_slow_ring_trc(phba,
11922 "ISR slow ring: pwork:"
11923 "x%x hawork:x%x wait:x%x",
11924 phba->work_ha, work_ha_copy,
11925 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011926 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011927 }
James Smart5b75da22008-12-04 22:39:35 -050011928 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011929 }
11930 }
James Smart5b75da22008-12-04 22:39:35 -050011931 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011932 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011933 if (lpfc_sli_read_hs(phba))
11934 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011935 /*
11936 * Check if there is a deferred error condition
11937 * is active
11938 */
11939 if ((HS_FFER1 & phba->work_hs) &&
11940 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011941 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11942 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011943 phba->hba_flag |= DEFER_ERATT;
11944 /* Clear all interrupt enable conditions */
11945 writel(0, phba->HCregaddr);
11946 readl(phba->HCregaddr);
11947 }
11948 }
11949
James Smart93996272008-08-24 21:50:30 -040011950 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011951 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011952 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011953 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011954 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011955
11956 /* First check out the status word */
11957 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11958 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011959 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011960 /*
11961 * Stray Mailbox Interrupt, mbxCommand <cmd>
11962 * mbxStatus <status>
11963 */
James Smart09372822008-01-11 01:52:54 -050011964 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011965 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011966 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011967 "Interrupt mbxCommand x%x "
11968 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011969 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011970 pmbox->mbxCommand,
11971 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011972 /* clear mailbox attention bit */
11973 work_ha_copy &= ~HA_MBATT;
11974 } else {
James Smart97eab632008-04-07 10:16:05 -040011975 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011976 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011977 phba->last_completion_time = jiffies;
11978 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011979 if (pmb->mbox_cmpl) {
11980 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11981 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011982 if (pmb->out_ext_byte_len &&
11983 pmb->context2)
11984 lpfc_sli_pcimem_bcopy(
11985 phba->mbox_ext,
11986 pmb->context2,
11987 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011988 }
James Smart09372822008-01-11 01:52:54 -050011989 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11990 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11991
11992 lpfc_debugfs_disc_trc(vport,
11993 LPFC_DISC_TRC_MBOX_VPORT,
11994 "MBOX dflt rpi: : "
11995 "status:x%x rpi:x%x",
11996 (uint32_t)pmbox->mbxStatus,
11997 pmbox->un.varWords[0], 0);
11998
11999 if (!pmbox->mbxStatus) {
12000 mp = (struct lpfc_dmabuf *)
12001 (pmb->context1);
12002 ndlp = (struct lpfc_nodelist *)
12003 pmb->context2;
12004
12005 /* Reg_LOGIN of dflt RPI was
12006 * successful. new lets get
12007 * rid of the RPI using the
12008 * same mbox buffer.
12009 */
12010 lpfc_unreg_login(phba,
12011 vport->vpi,
12012 pmbox->un.varWords[0],
12013 pmb);
12014 pmb->mbox_cmpl =
12015 lpfc_mbx_cmpl_dflt_rpi;
12016 pmb->context1 = mp;
12017 pmb->context2 = ndlp;
12018 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012019 rc = lpfc_sli_issue_mbox(phba,
12020 pmb,
12021 MBX_NOWAIT);
12022 if (rc != MBX_BUSY)
12023 lpfc_printf_log(phba,
12024 KERN_ERR,
12025 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012026 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012027 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012028 if (rc != MBX_NOT_FINISHED)
12029 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012030 }
12031 }
James Smart5b75da22008-12-04 22:39:35 -050012032 spin_lock_irqsave(
12033 &phba->pport->work_port_lock,
12034 iflag);
James Smart09372822008-01-11 01:52:54 -050012035 phba->pport->work_port_events &=
12036 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012037 spin_unlock_irqrestore(
12038 &phba->pport->work_port_lock,
12039 iflag);
James Smart09372822008-01-11 01:52:54 -050012040 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012041 }
James Smart97eab632008-04-07 10:16:05 -040012042 } else
James Smart5b75da22008-12-04 22:39:35 -050012043 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012044
James Smart92d7f7b2007-06-17 19:56:38 -050012045 if ((work_ha_copy & HA_MBATT) &&
12046 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012047send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012048 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012049 do {
12050 rc = lpfc_sli_issue_mbox(phba, NULL,
12051 MBX_NOWAIT);
12052 } while (rc == MBX_NOT_FINISHED);
12053 if (rc != MBX_SUCCESS)
12054 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12055 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012056 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012057 }
12058
James Smart5b75da22008-12-04 22:39:35 -050012059 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012060 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012061 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012062 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012063 }
James Smart93996272008-08-24 21:50:30 -040012064 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012065unplug_error:
12066 spin_unlock_irqrestore(&phba->hbalock, iflag);
12067 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012068
James Smart3772a992009-05-22 14:50:54 -040012069} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012070
12071/**
James Smart3772a992009-05-22 14:50:54 -040012072 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012073 * @irq: Interrupt number.
12074 * @dev_id: The device context pointer.
12075 *
12076 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012077 * service routine when device with SLI-3 interface spec is enabled with
12078 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12079 * ring event in the HBA. However, when the device is enabled with either
12080 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12081 * device-level interrupt handler. When the PCI slot is in error recovery
12082 * or the HBA is undergoing initialization, the interrupt handler will not
12083 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12084 * the intrrupt context. This function is called without any lock held.
12085 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012086 *
12087 * This function returns IRQ_HANDLED when interrupt is handled else it
12088 * returns IRQ_NONE.
12089 **/
12090irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012091lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012092{
12093 struct lpfc_hba *phba;
12094 uint32_t ha_copy;
12095 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012096 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012097 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012098
12099 /* Get the driver's phba structure from the dev_id and
12100 * assume the HBA is not interrupting.
12101 */
12102 phba = (struct lpfc_hba *) dev_id;
12103
12104 if (unlikely(!phba))
12105 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012106
12107 /*
James Smart93996272008-08-24 21:50:30 -040012108 * Stuff needs to be attented to when this function is invoked as an
12109 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012110 */
James Smart93996272008-08-24 21:50:30 -040012111 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012112 /* Check device state for handling interrupt */
12113 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012114 return IRQ_NONE;
12115 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012116 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12117 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012118 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012119 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012120 /*
12121 * If there is deferred error attention, do not check for
12122 * any interrupt.
12123 */
12124 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012125 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012126 return IRQ_NONE;
12127 }
James Smart93996272008-08-24 21:50:30 -040012128 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12129 phba->HAregaddr);
12130 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012131 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012132 } else
12133 ha_copy = phba->ha_copy;
12134
12135 /*
12136 * Process all events on FCP ring. Take the optimized path for FCP IO.
12137 */
12138 ha_copy &= ~(phba->work_ha_mask);
12139
12140 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012141 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012142 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012143 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012144 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012145
12146 if (phba->cfg_multi_ring_support == 2) {
12147 /*
James Smart93996272008-08-24 21:50:30 -040012148 * Process all events on extra ring. Take the optimized path
12149 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012150 */
James Smart93996272008-08-24 21:50:30 -040012151 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012152 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012153 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012154 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012155 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012156 status);
12157 }
12158 }
dea31012005-04-17 16:05:31 -050012159 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012160} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012161
James Smart93996272008-08-24 21:50:30 -040012162/**
James Smart3772a992009-05-22 14:50:54 -040012163 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012164 * @irq: Interrupt number.
12165 * @dev_id: The device context pointer.
12166 *
James Smart3772a992009-05-22 14:50:54 -040012167 * This function is the HBA device-level interrupt handler to device with
12168 * SLI-3 interface spec, called from the PCI layer when either MSI or
12169 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12170 * requires driver attention. This function invokes the slow-path interrupt
12171 * attention handling function and fast-path interrupt attention handling
12172 * function in turn to process the relevant HBA attention events. This
12173 * function is called without any lock held. It gets the hbalock to access
12174 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012175 *
12176 * This function returns IRQ_HANDLED when interrupt is handled, else it
12177 * returns IRQ_NONE.
12178 **/
12179irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012180lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012181{
12182 struct lpfc_hba *phba;
12183 irqreturn_t sp_irq_rc, fp_irq_rc;
12184 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012185 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012186
12187 /*
12188 * Get the driver's phba structure from the dev_id and
12189 * assume the HBA is not interrupting.
12190 */
12191 phba = (struct lpfc_hba *) dev_id;
12192
12193 if (unlikely(!phba))
12194 return IRQ_NONE;
12195
James Smart3772a992009-05-22 14:50:54 -040012196 /* Check device state for handling interrupt */
12197 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012198 return IRQ_NONE;
12199
12200 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012201 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12202 spin_unlock(&phba->hbalock);
12203 return IRQ_HANDLED;
12204 }
12205
James Smart93996272008-08-24 21:50:30 -040012206 if (unlikely(!phba->ha_copy)) {
12207 spin_unlock(&phba->hbalock);
12208 return IRQ_NONE;
12209 } else if (phba->ha_copy & HA_ERATT) {
12210 if (phba->hba_flag & HBA_ERATT_HANDLED)
12211 /* ERATT polling has handled ERATT */
12212 phba->ha_copy &= ~HA_ERATT;
12213 else
12214 /* Indicate interrupt handler handles ERATT */
12215 phba->hba_flag |= HBA_ERATT_HANDLED;
12216 }
12217
James Smarta257bf92009-04-06 18:48:10 -040012218 /*
12219 * If there is deferred error attention, do not check for any interrupt.
12220 */
12221 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012222 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012223 return IRQ_NONE;
12224 }
12225
James Smart93996272008-08-24 21:50:30 -040012226 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012227 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12228 spin_unlock(&phba->hbalock);
12229 return IRQ_HANDLED;
12230 }
James Smarta747c9c2009-11-18 15:41:10 -050012231 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12232 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12233 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012234 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012235 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012236 readl(phba->HAregaddr); /* flush */
12237 spin_unlock(&phba->hbalock);
12238
12239 /*
12240 * Invokes slow-path host attention interrupt handling as appropriate.
12241 */
12242
12243 /* status of events with mailbox and link attention */
12244 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12245
12246 /* status of events with ELS ring */
12247 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12248 status2 >>= (4*LPFC_ELS_RING);
12249
12250 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012251 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012252 else
12253 sp_irq_rc = IRQ_NONE;
12254
12255 /*
12256 * Invoke fast-path host attention interrupt handling as appropriate.
12257 */
12258
12259 /* status of events with FCP ring */
12260 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12261 status1 >>= (4*LPFC_FCP_RING);
12262
12263 /* status of events with extra ring */
12264 if (phba->cfg_multi_ring_support == 2) {
12265 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12266 status2 >>= (4*LPFC_EXTRA_RING);
12267 } else
12268 status2 = 0;
12269
12270 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012271 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012272 else
12273 fp_irq_rc = IRQ_NONE;
12274
12275 /* Return device-level interrupt handling status */
12276 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012277} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012278
12279/**
12280 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12281 * @phba: pointer to lpfc hba data structure.
12282 *
12283 * This routine is invoked by the worker thread to process all the pending
12284 * SLI4 FCP abort XRI events.
12285 **/
12286void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12287{
12288 struct lpfc_cq_event *cq_event;
12289
12290 /* First, declare the fcp xri abort event has been handled */
12291 spin_lock_irq(&phba->hbalock);
12292 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12293 spin_unlock_irq(&phba->hbalock);
12294 /* Now, handle all the fcp xri abort events */
12295 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12296 /* Get the first event from the head of the event queue */
12297 spin_lock_irq(&phba->hbalock);
12298 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12299 cq_event, struct lpfc_cq_event, list);
12300 spin_unlock_irq(&phba->hbalock);
12301 /* Notify aborted XRI for FCP work queue */
12302 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12303 /* Free the event processed back to the free pool */
12304 lpfc_sli4_cq_event_release(phba, cq_event);
12305 }
12306}
12307
12308/**
James Smart318083a2017-03-04 09:30:30 -080012309 * lpfc_sli4_nvme_xri_abort_event_proc - Process nvme xri abort event
12310 * @phba: pointer to lpfc hba data structure.
12311 *
12312 * This routine is invoked by the worker thread to process all the pending
12313 * SLI4 NVME abort XRI events.
12314 **/
12315void lpfc_sli4_nvme_xri_abort_event_proc(struct lpfc_hba *phba)
12316{
12317 struct lpfc_cq_event *cq_event;
12318
12319 /* First, declare the fcp xri abort event has been handled */
12320 spin_lock_irq(&phba->hbalock);
12321 phba->hba_flag &= ~NVME_XRI_ABORT_EVENT;
12322 spin_unlock_irq(&phba->hbalock);
12323 /* Now, handle all the fcp xri abort events */
12324 while (!list_empty(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue)) {
12325 /* Get the first event from the head of the event queue */
12326 spin_lock_irq(&phba->hbalock);
12327 list_remove_head(&phba->sli4_hba.sp_nvme_xri_aborted_work_queue,
12328 cq_event, struct lpfc_cq_event, list);
12329 spin_unlock_irq(&phba->hbalock);
12330 /* Notify aborted XRI for NVME work queue */
12331 if (phba->nvmet_support) {
12332 lpfc_sli4_nvmet_xri_aborted(phba,
12333 &cq_event->cqe.wcqe_axri);
12334 } else {
12335 lpfc_sli4_nvme_xri_aborted(phba,
12336 &cq_event->cqe.wcqe_axri);
12337 }
12338 /* Free the event processed back to the free pool */
12339 lpfc_sli4_cq_event_release(phba, cq_event);
12340 }
12341}
12342
12343/**
James Smart4f774512009-05-22 14:52:35 -040012344 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12345 * @phba: pointer to lpfc hba data structure.
12346 *
12347 * This routine is invoked by the worker thread to process all the pending
12348 * SLI4 els abort xri events.
12349 **/
12350void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12351{
12352 struct lpfc_cq_event *cq_event;
12353
12354 /* First, declare the els xri abort event has been handled */
12355 spin_lock_irq(&phba->hbalock);
12356 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12357 spin_unlock_irq(&phba->hbalock);
12358 /* Now, handle all the els xri abort events */
12359 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12360 /* Get the first event from the head of the event queue */
12361 spin_lock_irq(&phba->hbalock);
12362 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12363 cq_event, struct lpfc_cq_event, list);
12364 spin_unlock_irq(&phba->hbalock);
12365 /* Notify aborted XRI for ELS work queue */
12366 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12367 /* Free the event processed back to the free pool */
12368 lpfc_sli4_cq_event_release(phba, cq_event);
12369 }
12370}
12371
James Smart341af102010-01-26 23:07:37 -050012372/**
12373 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12374 * @phba: pointer to lpfc hba data structure
12375 * @pIocbIn: pointer to the rspiocbq
12376 * @pIocbOut: pointer to the cmdiocbq
12377 * @wcqe: pointer to the complete wcqe
12378 *
12379 * This routine transfers the fields of a command iocbq to a response iocbq
12380 * by copying all the IOCB fields from command iocbq and transferring the
12381 * completion status information from the complete wcqe.
12382 **/
James Smart4f774512009-05-22 14:52:35 -040012383static void
James Smart341af102010-01-26 23:07:37 -050012384lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12385 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012386 struct lpfc_iocbq *pIocbOut,
12387 struct lpfc_wcqe_complete *wcqe)
12388{
James Smartaf227412013-10-10 12:23:10 -040012389 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012390 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012391 uint32_t status, max_response;
12392 struct lpfc_dmabuf *dmabuf;
12393 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012394 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12395
12396 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12397 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012398 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012399 status = bf_get(lpfc_wcqe_c_status, wcqe);
12400 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012401 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12402 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12403 pIocbIn->iocb.un.fcpi.fcpi_parm =
12404 pIocbOut->iocb.un.fcpi.fcpi_parm -
12405 wcqe->total_data_placed;
12406 else
12407 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012408 else {
James Smart4f774512009-05-22 14:52:35 -040012409 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012410 switch (pIocbOut->iocb.ulpCommand) {
12411 case CMD_ELS_REQUEST64_CR:
12412 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12413 bpl = (struct ulp_bde64 *)dmabuf->virt;
12414 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12415 max_response = bde.tus.f.bdeSize;
12416 break;
12417 case CMD_GEN_REQUEST64_CR:
12418 max_response = 0;
12419 if (!pIocbOut->context3)
12420 break;
12421 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12422 sizeof(struct ulp_bde64);
12423 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12424 bpl = (struct ulp_bde64 *)dmabuf->virt;
12425 for (i = 0; i < numBdes; i++) {
12426 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12427 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12428 max_response += bde.tus.f.bdeSize;
12429 }
12430 break;
12431 default:
12432 max_response = wcqe->total_data_placed;
12433 break;
12434 }
12435 if (max_response < wcqe->total_data_placed)
12436 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12437 else
12438 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12439 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012440 }
James Smart341af102010-01-26 23:07:37 -050012441
James Smartacd68592012-01-18 16:25:09 -050012442 /* Convert BG errors for completion status */
12443 if (status == CQE_STATUS_DI_ERROR) {
12444 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12445
12446 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12447 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12448 else
12449 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12450
12451 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12452 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12453 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12454 BGS_GUARD_ERR_MASK;
12455 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12456 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12457 BGS_APPTAG_ERR_MASK;
12458 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12459 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12460 BGS_REFTAG_ERR_MASK;
12461
12462 /* Check to see if there was any good data before the error */
12463 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12464 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12465 BGS_HI_WATER_MARK_PRESENT_MASK;
12466 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12467 wcqe->total_data_placed;
12468 }
12469
12470 /*
12471 * Set ALL the error bits to indicate we don't know what
12472 * type of error it is.
12473 */
12474 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12475 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12476 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12477 BGS_GUARD_ERR_MASK);
12478 }
12479
James Smart341af102010-01-26 23:07:37 -050012480 /* Pick up HBA exchange busy condition */
12481 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12482 spin_lock_irqsave(&phba->hbalock, iflags);
12483 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12484 spin_unlock_irqrestore(&phba->hbalock, iflags);
12485 }
James Smart4f774512009-05-22 14:52:35 -040012486}
12487
12488/**
James Smart45ed1192009-10-02 15:17:02 -040012489 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12490 * @phba: Pointer to HBA context object.
12491 * @wcqe: Pointer to work-queue completion queue entry.
12492 *
12493 * This routine handles an ELS work-queue completion event and construct
12494 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12495 * discovery engine to handle.
12496 *
12497 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12498 **/
12499static struct lpfc_iocbq *
12500lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12501 struct lpfc_iocbq *irspiocbq)
12502{
James Smart895427b2017-02-12 13:52:30 -080012503 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012504 struct lpfc_iocbq *cmdiocbq;
12505 struct lpfc_wcqe_complete *wcqe;
12506 unsigned long iflags;
12507
James Smart895427b2017-02-12 13:52:30 -080012508 pring = lpfc_phba_elsring(phba);
12509
James Smart45ed1192009-10-02 15:17:02 -040012510 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012511 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012512 pring->stats.iocb_event++;
12513 /* Look up the ELS command IOCB and create pseudo response IOCB */
12514 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12515 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart89533e92016-10-13 15:06:15 -070012516 /* Put the iocb back on the txcmplq */
12517 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
James Smart7e56aa22012-08-03 12:35:34 -040012518 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012519
12520 if (unlikely(!cmdiocbq)) {
12521 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12522 "0386 ELS complete with no corresponding "
12523 "cmdiocb: iotag (%d)\n",
12524 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12525 lpfc_sli_release_iocbq(phba, irspiocbq);
12526 return NULL;
12527 }
12528
12529 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012530 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012531
12532 return irspiocbq;
12533}
12534
12535/**
James Smart04c68492009-05-22 14:52:52 -040012536 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12537 * @phba: Pointer to HBA context object.
12538 * @cqe: Pointer to mailbox completion queue entry.
12539 *
12540 * This routine process a mailbox completion queue entry with asynchrous
12541 * event.
12542 *
12543 * Return: true if work posted to worker thread, otherwise false.
12544 **/
12545static bool
12546lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12547{
12548 struct lpfc_cq_event *cq_event;
12549 unsigned long iflags;
12550
12551 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12552 "0392 Async Event: word0:x%x, word1:x%x, "
12553 "word2:x%x, word3:x%x\n", mcqe->word0,
12554 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12555
12556 /* Allocate a new internal CQ_EVENT entry */
12557 cq_event = lpfc_sli4_cq_event_alloc(phba);
12558 if (!cq_event) {
12559 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12560 "0394 Failed to allocate CQ_EVENT entry\n");
12561 return false;
12562 }
12563
12564 /* Move the CQE into an asynchronous event entry */
12565 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12566 spin_lock_irqsave(&phba->hbalock, iflags);
12567 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12568 /* Set the async event flag */
12569 phba->hba_flag |= ASYNC_EVENT;
12570 spin_unlock_irqrestore(&phba->hbalock, iflags);
12571
12572 return true;
12573}
12574
12575/**
12576 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12577 * @phba: Pointer to HBA context object.
12578 * @cqe: Pointer to mailbox completion queue entry.
12579 *
12580 * This routine process a mailbox completion queue entry with mailbox
12581 * completion event.
12582 *
12583 * Return: true if work posted to worker thread, otherwise false.
12584 **/
12585static bool
12586lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12587{
12588 uint32_t mcqe_status;
12589 MAILBOX_t *mbox, *pmbox;
12590 struct lpfc_mqe *mqe;
12591 struct lpfc_vport *vport;
12592 struct lpfc_nodelist *ndlp;
12593 struct lpfc_dmabuf *mp;
12594 unsigned long iflags;
12595 LPFC_MBOXQ_t *pmb;
12596 bool workposted = false;
12597 int rc;
12598
12599 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12600 if (!bf_get(lpfc_trailer_completed, mcqe))
12601 goto out_no_mqe_complete;
12602
12603 /* Get the reference to the active mbox command */
12604 spin_lock_irqsave(&phba->hbalock, iflags);
12605 pmb = phba->sli.mbox_active;
12606 if (unlikely(!pmb)) {
12607 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12608 "1832 No pending MBOX command to handle\n");
12609 spin_unlock_irqrestore(&phba->hbalock, iflags);
12610 goto out_no_mqe_complete;
12611 }
12612 spin_unlock_irqrestore(&phba->hbalock, iflags);
12613 mqe = &pmb->u.mqe;
12614 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12615 mbox = phba->mbox;
12616 vport = pmb->vport;
12617
12618 /* Reset heartbeat timer */
12619 phba->last_completion_time = jiffies;
12620 del_timer(&phba->sli.mbox_tmo);
12621
12622 /* Move mbox data to caller's mailbox region, do endian swapping */
12623 if (pmb->mbox_cmpl && mbox)
12624 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012625
James Smart73d91e52011-10-10 21:32:10 -040012626 /*
12627 * For mcqe errors, conditionally move a modified error code to
12628 * the mbox so that the error will not be missed.
12629 */
12630 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12631 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12632 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12633 bf_set(lpfc_mqe_status, mqe,
12634 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12635 }
James Smart04c68492009-05-22 14:52:52 -040012636 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12637 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12638 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12639 "MBOX dflt rpi: status:x%x rpi:x%x",
12640 mcqe_status,
12641 pmbox->un.varWords[0], 0);
12642 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12643 mp = (struct lpfc_dmabuf *)(pmb->context1);
12644 ndlp = (struct lpfc_nodelist *)pmb->context2;
12645 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12646 * RID of the PPI using the same mbox buffer.
12647 */
12648 lpfc_unreg_login(phba, vport->vpi,
12649 pmbox->un.varWords[0], pmb);
12650 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12651 pmb->context1 = mp;
12652 pmb->context2 = ndlp;
12653 pmb->vport = vport;
12654 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12655 if (rc != MBX_BUSY)
12656 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12657 LOG_SLI, "0385 rc should "
12658 "have been MBX_BUSY\n");
12659 if (rc != MBX_NOT_FINISHED)
12660 goto send_current_mbox;
12661 }
12662 }
12663 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12664 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12665 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12666
12667 /* There is mailbox completion work to do */
12668 spin_lock_irqsave(&phba->hbalock, iflags);
12669 __lpfc_mbox_cmpl_put(phba, pmb);
12670 phba->work_ha |= HA_MBATT;
12671 spin_unlock_irqrestore(&phba->hbalock, iflags);
12672 workposted = true;
12673
12674send_current_mbox:
12675 spin_lock_irqsave(&phba->hbalock, iflags);
12676 /* Release the mailbox command posting token */
12677 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12678 /* Setting active mailbox pointer need to be in sync to flag clear */
12679 phba->sli.mbox_active = NULL;
12680 spin_unlock_irqrestore(&phba->hbalock, iflags);
12681 /* Wake up worker thread to post the next pending mailbox command */
12682 lpfc_worker_wake_up(phba);
12683out_no_mqe_complete:
12684 if (bf_get(lpfc_trailer_consumed, mcqe))
12685 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12686 return workposted;
12687}
12688
12689/**
12690 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12691 * @phba: Pointer to HBA context object.
12692 * @cqe: Pointer to mailbox completion queue entry.
12693 *
12694 * This routine process a mailbox completion queue entry, it invokes the
12695 * proper mailbox complete handling or asynchrous event handling routine
12696 * according to the MCQE's async bit.
12697 *
12698 * Return: true if work posted to worker thread, otherwise false.
12699 **/
12700static bool
12701lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12702{
12703 struct lpfc_mcqe mcqe;
12704 bool workposted;
12705
12706 /* Copy the mailbox MCQE and convert endian order as needed */
12707 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12708
12709 /* Invoke the proper event handling routine */
12710 if (!bf_get(lpfc_trailer_async, &mcqe))
12711 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12712 else
12713 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12714 return workposted;
12715}
12716
12717/**
James Smart4f774512009-05-22 14:52:35 -040012718 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12719 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012720 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012721 * @wcqe: Pointer to work-queue completion queue entry.
12722 *
12723 * This routine handles an ELS work-queue completion event.
12724 *
12725 * Return: true if work posted to worker thread, otherwise false.
12726 **/
12727static bool
James Smart2a76a282012-08-03 12:35:54 -040012728lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012729 struct lpfc_wcqe_complete *wcqe)
12730{
James Smart4f774512009-05-22 14:52:35 -040012731 struct lpfc_iocbq *irspiocbq;
12732 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012733 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012734 int txq_cnt = 0;
12735 int txcmplq_cnt = 0;
12736 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012737
James Smart45ed1192009-10-02 15:17:02 -040012738 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012739 irspiocbq = lpfc_sli_get_iocbq(phba);
12740 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012741 if (!list_empty(&pring->txq))
12742 txq_cnt++;
12743 if (!list_empty(&pring->txcmplq))
12744 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012745 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012746 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12747 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012748 txq_cnt, phba->iocb_cnt,
12749 fcp_txcmplq_cnt,
12750 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012751 return false;
James Smart4f774512009-05-22 14:52:35 -040012752 }
James Smart4f774512009-05-22 14:52:35 -040012753
James Smart45ed1192009-10-02 15:17:02 -040012754 /* Save off the slow-path queue event for work thread to process */
12755 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012756 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012757 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012758 &phba->sli4_hba.sp_queue_event);
12759 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012760 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012761
James Smart45ed1192009-10-02 15:17:02 -040012762 return true;
James Smart4f774512009-05-22 14:52:35 -040012763}
12764
12765/**
12766 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12767 * @phba: Pointer to HBA context object.
12768 * @wcqe: Pointer to work-queue completion queue entry.
12769 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012770 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012771 * proper WQ release routine to the slow-path WQ.
12772 **/
12773static void
12774lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12775 struct lpfc_wcqe_release *wcqe)
12776{
James Smart2e90f4b2011-12-13 13:22:37 -050012777 /* sanity check on queue memory */
12778 if (unlikely(!phba->sli4_hba.els_wq))
12779 return;
James Smart4f774512009-05-22 14:52:35 -040012780 /* Check for the slow-path ELS work queue */
12781 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12782 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12783 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12784 else
12785 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12786 "2579 Slow-path wqe consume event carries "
12787 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12788 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12789 phba->sli4_hba.els_wq->queue_id);
12790}
12791
12792/**
12793 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12794 * @phba: Pointer to HBA context object.
12795 * @cq: Pointer to a WQ completion queue.
12796 * @wcqe: Pointer to work-queue completion queue entry.
12797 *
12798 * This routine handles an XRI abort event.
12799 *
12800 * Return: true if work posted to worker thread, otherwise false.
12801 **/
12802static bool
12803lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12804 struct lpfc_queue *cq,
12805 struct sli4_wcqe_xri_aborted *wcqe)
12806{
12807 bool workposted = false;
12808 struct lpfc_cq_event *cq_event;
12809 unsigned long iflags;
12810
12811 /* Allocate a new internal CQ_EVENT entry */
12812 cq_event = lpfc_sli4_cq_event_alloc(phba);
12813 if (!cq_event) {
12814 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12815 "0602 Failed to allocate CQ_EVENT entry\n");
12816 return false;
12817 }
12818
12819 /* Move the CQE into the proper xri abort event list */
12820 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12821 switch (cq->subtype) {
12822 case LPFC_FCP:
12823 spin_lock_irqsave(&phba->hbalock, iflags);
12824 list_add_tail(&cq_event->list,
12825 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12826 /* Set the fcp xri abort event flag */
12827 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12828 spin_unlock_irqrestore(&phba->hbalock, iflags);
12829 workposted = true;
12830 break;
12831 case LPFC_ELS:
12832 spin_lock_irqsave(&phba->hbalock, iflags);
12833 list_add_tail(&cq_event->list,
12834 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12835 /* Set the els xri abort event flag */
12836 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12837 spin_unlock_irqrestore(&phba->hbalock, iflags);
12838 workposted = true;
12839 break;
James Smart318083a2017-03-04 09:30:30 -080012840 case LPFC_NVME:
12841 spin_lock_irqsave(&phba->hbalock, iflags);
12842 list_add_tail(&cq_event->list,
12843 &phba->sli4_hba.sp_nvme_xri_aborted_work_queue);
12844 /* Set the nvme xri abort event flag */
12845 phba->hba_flag |= NVME_XRI_ABORT_EVENT;
12846 spin_unlock_irqrestore(&phba->hbalock, iflags);
12847 workposted = true;
12848 break;
James Smart4f774512009-05-22 14:52:35 -040012849 default:
12850 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080012851 "0603 Invalid CQ subtype %d: "
12852 "%08x %08x %08x %08x\n",
12853 cq->subtype, wcqe->word0, wcqe->parameter,
12854 wcqe->word2, wcqe->word3);
12855 lpfc_sli4_cq_event_release(phba, cq_event);
James Smart4f774512009-05-22 14:52:35 -040012856 workposted = false;
12857 break;
12858 }
12859 return workposted;
12860}
12861
12862/**
James Smart4d9ab992009-10-02 15:16:39 -040012863 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012864 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012865 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012866 *
James Smart4d9ab992009-10-02 15:16:39 -040012867 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012868 *
12869 * Return: true if work posted to worker thread, otherwise false.
12870 **/
12871static bool
James Smart4d9ab992009-10-02 15:16:39 -040012872lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12873{
12874 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012875 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012876 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12877 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070012878 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040012879 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012880 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012881 unsigned long iflags;
12882
James Smart2e90f4b2011-12-13 13:22:37 -050012883 /* sanity check on queue memory */
12884 if (unlikely(!hrq) || unlikely(!drq))
12885 return workposted;
12886
James Smart7851fe22011-07-22 18:36:52 -040012887 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12888 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12889 else
12890 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12891 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012892 goto out;
12893
12894 status = bf_get(lpfc_rcqe_status, rcqe);
12895 switch (status) {
12896 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12897 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12898 "2537 Receive Frame Truncated!!\n");
12899 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012900 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012901 spin_lock_irqsave(&phba->hbalock, iflags);
12902 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12903 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012904 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012905 spin_unlock_irqrestore(&phba->hbalock, iflags);
12906 goto out;
12907 }
James Smartb84daac2012-08-03 12:35:13 -040012908 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070012909 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040012910 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012911
12912 /* If a NVME LS event (type 0x28), treat it as Fast path */
12913 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12914
James Smart4d9ab992009-10-02 15:16:39 -040012915 /* save off the frame for the word thread to process */
12916 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012917 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012918 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012919 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012920 spin_unlock_irqrestore(&phba->hbalock, iflags);
12921 workposted = true;
12922 break;
James Smart4d9ab992009-10-02 15:16:39 -040012923 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070012924 if (phba->nvmet_support) {
12925 tgtp = phba->targetport->private;
12926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
12927 "6402 RQE Error x%x, posted %d err_cnt "
12928 "%d: %x %x %x\n",
12929 status, hrq->RQ_buf_posted,
12930 hrq->RQ_no_posted_buf,
12931 atomic_read(&tgtp->rcv_fcp_cmd_in),
12932 atomic_read(&tgtp->rcv_fcp_cmd_out),
12933 atomic_read(&tgtp->xmt_fcp_release));
12934 }
12935 /* fallthrough */
12936
12937 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040012938 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012939 /* Post more buffers if possible */
12940 spin_lock_irqsave(&phba->hbalock, iflags);
12941 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12942 spin_unlock_irqrestore(&phba->hbalock, iflags);
12943 workposted = true;
12944 break;
12945 }
12946out:
12947 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012948}
12949
12950/**
12951 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12952 * @phba: Pointer to HBA context object.
12953 * @cq: Pointer to the completion queue.
12954 * @wcqe: Pointer to a completion queue entry.
12955 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012956 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012957 * entry.
12958 *
12959 * Return: true if work posted to worker thread, otherwise false.
12960 **/
12961static bool
12962lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012963 struct lpfc_cqe *cqe)
12964{
James Smart45ed1192009-10-02 15:17:02 -040012965 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012966 bool workposted = false;
12967
12968 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012969 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012970
12971 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012972 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012973 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012974 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012975 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012976 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012977 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012978 break;
12979 case CQE_CODE_RELEASE_WQE:
12980 /* Process the WQ release event */
12981 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012982 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012983 break;
12984 case CQE_CODE_XRI_ABORTED:
12985 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012986 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012987 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012988 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012989 break;
James Smart4d9ab992009-10-02 15:16:39 -040012990 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012991 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012992 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012993 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012994 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012995 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012996 break;
James Smart4f774512009-05-22 14:52:35 -040012997 default:
12998 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12999 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013000 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013001 break;
13002 }
13003 return workposted;
13004}
13005
13006/**
James Smart4f774512009-05-22 14:52:35 -040013007 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13008 * @phba: Pointer to HBA context object.
13009 * @eqe: Pointer to fast-path event queue entry.
13010 *
13011 * This routine process a event queue entry from the slow-path event queue.
13012 * It will check the MajorCode and MinorCode to determine this is for a
13013 * completion event on a completion queue, if not, an error shall be logged
13014 * and just return. Otherwise, it will get to the corresponding completion
13015 * queue and process all the entries on that completion queue, rearm the
13016 * completion queue, and then return.
13017 *
13018 **/
13019static void
James Smart67d12732012-08-03 12:36:13 -040013020lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13021 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013022{
James Smart67d12732012-08-03 12:36:13 -040013023 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013024 struct lpfc_cqe *cqe;
13025 bool workposted = false;
13026 int ecount = 0;
13027 uint16_t cqid;
13028
James Smart4f774512009-05-22 14:52:35 -040013029 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013030 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013031
James Smart4f774512009-05-22 14:52:35 -040013032 list_for_each_entry(childq, &speq->child_list, list) {
13033 if (childq->queue_id == cqid) {
13034 cq = childq;
13035 break;
13036 }
13037 }
13038 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013039 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13040 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13041 "0365 Slow-path CQ identifier "
13042 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040013043 return;
13044 }
13045
James Smart895427b2017-02-12 13:52:30 -080013046 /* Save EQ associated with this CQ */
13047 cq->assoc_qp = speq;
13048
James Smart4f774512009-05-22 14:52:35 -040013049 /* Process all the entries to the CQ */
13050 switch (cq->type) {
13051 case LPFC_MCQ:
13052 while ((cqe = lpfc_sli4_cq_get(cq))) {
13053 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013054 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013055 break;
James Smartb84daac2012-08-03 12:35:13 -040013056 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013057 }
13058 break;
13059 case LPFC_WCQ:
13060 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013061 if ((cq->subtype == LPFC_FCP) ||
13062 (cq->subtype == LPFC_NVME))
13063 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013064 cqe);
13065 else
13066 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13067 cqe);
James Smart73d91e52011-10-10 21:32:10 -040013068 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013069 break;
James Smart4f774512009-05-22 14:52:35 -040013070 }
James Smartb84daac2012-08-03 12:35:13 -040013071
13072 /* Track the max number of CQEs processed in 1 EQ */
13073 if (ecount > cq->CQ_max_cqe)
13074 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040013075 break;
13076 default:
13077 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13078 "0370 Invalid completion queue type (%d)\n",
13079 cq->type);
13080 return;
13081 }
13082
13083 /* Catch the no cq entry condition, log an error */
13084 if (unlikely(ecount == 0))
13085 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13086 "0371 No entry from the CQ: identifier "
13087 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13088
13089 /* In any case, flash and re-arm the RCQ */
13090 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13091
13092 /* wake up worker thread if there are works to be done */
13093 if (workposted)
13094 lpfc_worker_wake_up(phba);
13095}
13096
13097/**
13098 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013099 * @phba: Pointer to HBA context object.
13100 * @cq: Pointer to associated CQ
13101 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013102 *
13103 * This routine process a fast-path work queue completion entry from fast-path
13104 * event queue for FCP command response completion.
13105 **/
13106static void
James Smart2a76a282012-08-03 12:35:54 -040013107lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013108 struct lpfc_wcqe_complete *wcqe)
13109{
James Smart2a76a282012-08-03 12:35:54 -040013110 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013111 struct lpfc_iocbq *cmdiocbq;
13112 struct lpfc_iocbq irspiocbq;
13113 unsigned long iflags;
13114
James Smart4f774512009-05-22 14:52:35 -040013115 /* Check for response status */
13116 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13117 /* If resource errors reported from HBA, reduce queue
13118 * depth of the SCSI device.
13119 */
James Smarte3d2b802012-08-14 14:25:43 -040013120 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13121 IOSTAT_LOCAL_REJECT)) &&
13122 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13123 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013124 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013125
James Smart4f774512009-05-22 14:52:35 -040013126 /* Log the error status */
13127 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13128 "0373 FCP complete error: status=x%x, "
13129 "hw_status=x%x, total_data_specified=%d, "
13130 "parameter=x%x, word3=x%x\n",
13131 bf_get(lpfc_wcqe_c_status, wcqe),
13132 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13133 wcqe->total_data_placed, wcqe->parameter,
13134 wcqe->word3);
13135 }
13136
13137 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013138 spin_lock_irqsave(&pring->ring_lock, iflags);
13139 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013140 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13141 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013142 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013143 if (unlikely(!cmdiocbq)) {
13144 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13145 "0374 FCP complete with no corresponding "
13146 "cmdiocb: iotag (%d)\n",
13147 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13148 return;
13149 }
James Smart895427b2017-02-12 13:52:30 -080013150
13151 if (cq->assoc_qp)
13152 cmdiocbq->isr_timestamp =
13153 cq->assoc_qp->isr_timestamp;
13154
13155 if (cmdiocbq->iocb_cmpl == NULL) {
13156 if (cmdiocbq->wqe_cmpl) {
13157 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13158 spin_lock_irqsave(&phba->hbalock, iflags);
13159 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13160 spin_unlock_irqrestore(&phba->hbalock, iflags);
13161 }
13162
13163 /* Pass the cmd_iocb and the wcqe to the upper layer */
13164 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13165 return;
13166 }
James Smart4f774512009-05-22 14:52:35 -040013167 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13168 "0375 FCP cmdiocb not callback function "
13169 "iotag: (%d)\n",
13170 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13171 return;
13172 }
13173
13174 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013175 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013176
James Smart0f65ff62010-02-26 14:14:23 -050013177 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13178 spin_lock_irqsave(&phba->hbalock, iflags);
13179 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13180 spin_unlock_irqrestore(&phba->hbalock, iflags);
13181 }
13182
James Smart4f774512009-05-22 14:52:35 -040013183 /* Pass the cmd_iocb and the rsp state to the upper layer */
13184 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13185}
13186
13187/**
13188 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13189 * @phba: Pointer to HBA context object.
13190 * @cq: Pointer to completion queue.
13191 * @wcqe: Pointer to work-queue completion queue entry.
13192 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013193 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013194 * proper WQ release routine to the slow-path WQ.
13195 **/
13196static void
13197lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13198 struct lpfc_wcqe_release *wcqe)
13199{
13200 struct lpfc_queue *childwq;
13201 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013202 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013203
13204 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013205 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013206 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013207 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013208 lpfc_sli4_wq_release(childwq,
13209 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
13210 wqid_matched = true;
13211 break;
13212 }
13213 }
13214 /* Report warning log message if no match found */
13215 if (wqid_matched != true)
13216 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13217 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013218 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013219}
13220
13221/**
James Smart2d7dbc42017-02-12 13:52:35 -080013222 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13223 * @phba: Pointer to HBA context object.
13224 * @rcqe: Pointer to receive-queue completion queue entry.
13225 *
13226 * This routine process a receive-queue completion queue entry.
13227 *
13228 * Return: true if work posted to worker thread, otherwise false.
13229 **/
13230static bool
13231lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13232 struct lpfc_rcqe *rcqe)
13233{
13234 bool workposted = false;
13235 struct lpfc_queue *hrq;
13236 struct lpfc_queue *drq;
13237 struct rqb_dmabuf *dma_buf;
13238 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013239 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013240 uint32_t status, rq_id;
13241 unsigned long iflags;
13242 uint32_t fctl, idx;
13243
13244 if ((phba->nvmet_support == 0) ||
13245 (phba->sli4_hba.nvmet_cqset == NULL))
13246 return workposted;
13247
13248 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13249 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13250 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13251
13252 /* sanity check on queue memory */
13253 if (unlikely(!hrq) || unlikely(!drq))
13254 return workposted;
13255
13256 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13257 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13258 else
13259 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13260
13261 if ((phba->nvmet_support == 0) ||
13262 (rq_id != hrq->queue_id))
13263 return workposted;
13264
13265 status = bf_get(lpfc_rcqe_status, rcqe);
13266 switch (status) {
13267 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13268 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13269 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013270 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013271 case FC_STATUS_RQ_SUCCESS:
13272 lpfc_sli4_rq_release(hrq, drq);
13273 spin_lock_irqsave(&phba->hbalock, iflags);
13274 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13275 if (!dma_buf) {
13276 hrq->RQ_no_buf_found++;
13277 spin_unlock_irqrestore(&phba->hbalock, iflags);
13278 goto out;
13279 }
13280 spin_unlock_irqrestore(&phba->hbalock, iflags);
13281 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013282 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013283 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13284
13285 /* Just some basic sanity checks on FCP Command frame */
13286 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13287 fc_hdr->fh_f_ctl[1] << 8 |
13288 fc_hdr->fh_f_ctl[2]);
13289 if (((fctl &
13290 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13291 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13292 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13293 goto drop;
13294
13295 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13296 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013297 lpfc_nvmet_unsol_fcp_event(
13298 phba, phba->sli4_hba.els_wq->pring, dma_buf,
13299 cq->assoc_qp->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013300 return false;
13301 }
13302drop:
13303 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13304 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013305 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013306 if (phba->nvmet_support) {
13307 tgtp = phba->targetport->private;
13308 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13309 "6401 RQE Error x%x, posted %d err_cnt "
13310 "%d: %x %x %x\n",
13311 status, hrq->RQ_buf_posted,
13312 hrq->RQ_no_posted_buf,
13313 atomic_read(&tgtp->rcv_fcp_cmd_in),
13314 atomic_read(&tgtp->rcv_fcp_cmd_out),
13315 atomic_read(&tgtp->xmt_fcp_release));
13316 }
13317 /* fallthrough */
13318
13319 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013320 hrq->RQ_no_posted_buf++;
13321 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013322 break;
13323 }
13324out:
13325 return workposted;
13326}
13327
13328/**
James Smart895427b2017-02-12 13:52:30 -080013329 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013330 * @cq: Pointer to the completion queue.
13331 * @eqe: Pointer to fast-path completion queue entry.
13332 *
13333 * This routine process a fast-path work queue completion entry from fast-path
13334 * event queue for FCP command response completion.
13335 **/
13336static int
James Smart895427b2017-02-12 13:52:30 -080013337lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013338 struct lpfc_cqe *cqe)
13339{
13340 struct lpfc_wcqe_release wcqe;
13341 bool workposted = false;
13342
13343 /* Copy the work queue CQE and convert endian order if needed */
13344 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13345
13346 /* Check and process for different type of WCQE and dispatch */
13347 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13348 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013349 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013350 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013351 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013352 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013353 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13354 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13355 (struct lpfc_wcqe_complete *)&wcqe);
13356 if (cq->subtype == LPFC_NVME_LS)
13357 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013358 (struct lpfc_wcqe_complete *)&wcqe);
13359 break;
13360 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013361 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013362 /* Process the WQ release event */
13363 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13364 (struct lpfc_wcqe_release *)&wcqe);
13365 break;
13366 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013367 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013368 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013369 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013370 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13371 (struct sli4_wcqe_xri_aborted *)&wcqe);
13372 break;
James Smart895427b2017-02-12 13:52:30 -080013373 case CQE_CODE_RECEIVE_V1:
13374 case CQE_CODE_RECEIVE:
13375 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013376 if (cq->subtype == LPFC_NVMET) {
13377 workposted = lpfc_sli4_nvmet_handle_rcqe(
13378 phba, cq, (struct lpfc_rcqe *)&wcqe);
13379 }
James Smart895427b2017-02-12 13:52:30 -080013380 break;
James Smart4f774512009-05-22 14:52:35 -040013381 default:
13382 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013383 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013384 bf_get(lpfc_wcqe_c_code, &wcqe));
13385 break;
13386 }
13387 return workposted;
13388}
13389
13390/**
James Smart67d12732012-08-03 12:36:13 -040013391 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013392 * @phba: Pointer to HBA context object.
13393 * @eqe: Pointer to fast-path event queue entry.
13394 *
13395 * This routine process a event queue entry from the fast-path event queue.
13396 * It will check the MajorCode and MinorCode to determine this is for a
13397 * completion event on a completion queue, if not, an error shall be logged
13398 * and just return. Otherwise, it will get to the corresponding completion
13399 * queue and process all the entries on the completion queue, rearm the
13400 * completion queue, and then return.
13401 **/
13402static void
James Smart67d12732012-08-03 12:36:13 -040013403lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13404 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013405{
James Smart895427b2017-02-12 13:52:30 -080013406 struct lpfc_queue *cq = NULL;
James Smart4f774512009-05-22 14:52:35 -040013407 struct lpfc_cqe *cqe;
13408 bool workposted = false;
James Smart2d7dbc42017-02-12 13:52:35 -080013409 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013410 int ecount = 0;
13411
James Smartcb5172e2010-03-15 11:25:07 -040013412 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013414 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013415 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013416 bf_get_le32(lpfc_eqe_major_code, eqe),
13417 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040013418 return;
13419 }
13420
James Smart67d12732012-08-03 12:36:13 -040013421 /* Get the reference to the corresponding CQ */
13422 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13423
James Smart2d7dbc42017-02-12 13:52:35 -080013424 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13425 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13426 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13427 /* Process NVMET unsol rcv */
13428 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13429 goto process_cq;
13430 }
13431 }
13432
James Smart895427b2017-02-12 13:52:30 -080013433 if (phba->sli4_hba.nvme_cq_map &&
13434 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013435 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013436 cq = phba->sli4_hba.nvme_cq[qidx];
13437 goto process_cq;
13438 }
13439
13440 if (phba->sli4_hba.fcp_cq_map &&
13441 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13442 /* Process FCP command completion */
13443 cq = phba->sli4_hba.fcp_cq[qidx];
13444 goto process_cq;
13445 }
13446
13447 if (phba->sli4_hba.nvmels_cq &&
13448 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13449 /* Process NVME unsol rcv */
13450 cq = phba->sli4_hba.nvmels_cq;
13451 }
13452
13453 /* Otherwise this is a Slow path event */
13454 if (cq == NULL) {
13455 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013456 return;
13457 }
13458
James Smart895427b2017-02-12 13:52:30 -080013459process_cq:
James Smart4f774512009-05-22 14:52:35 -040013460 if (unlikely(cqid != cq->queue_id)) {
13461 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13462 "0368 Miss-matched fast-path completion "
13463 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13464 cqid, cq->queue_id);
13465 return;
13466 }
13467
James Smart895427b2017-02-12 13:52:30 -080013468 /* Save EQ associated with this CQ */
13469 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13470
James Smart4f774512009-05-22 14:52:35 -040013471 /* Process all the entries to the CQ */
13472 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013473 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013474 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013475 break;
James Smart4f774512009-05-22 14:52:35 -040013476 }
13477
James Smartb84daac2012-08-03 12:35:13 -040013478 /* Track the max number of CQEs processed in 1 EQ */
13479 if (ecount > cq->CQ_max_cqe)
13480 cq->CQ_max_cqe = ecount;
James Smart0cf07f842017-06-01 21:07:10 -070013481 cq->assoc_qp->EQ_cqe_cnt += ecount;
James Smartb84daac2012-08-03 12:35:13 -040013482
James Smart4f774512009-05-22 14:52:35 -040013483 /* Catch the no cq entry condition */
13484 if (unlikely(ecount == 0))
13485 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13486 "0369 No entry from fast-path completion "
13487 "queue fcpcqid=%d\n", cq->queue_id);
13488
13489 /* In any case, flash and re-arm the CQ */
13490 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13491
13492 /* wake up worker thread if there are works to be done */
13493 if (workposted)
13494 lpfc_worker_wake_up(phba);
13495}
13496
13497static void
13498lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13499{
13500 struct lpfc_eqe *eqe;
13501
13502 /* walk all the EQ entries and drop on the floor */
13503 while ((eqe = lpfc_sli4_eq_get(eq)))
13504 ;
13505
13506 /* Clear and re-arm the EQ */
13507 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13508}
13509
James Smart1ba981f2014-02-20 09:56:45 -050013510
13511/**
13512 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13513 * entry
13514 * @phba: Pointer to HBA context object.
13515 * @eqe: Pointer to fast-path event queue entry.
13516 *
13517 * This routine process a event queue entry from the Flash Optimized Fabric
13518 * event queue. It will check the MajorCode and MinorCode to determine this
13519 * is for a completion event on a completion queue, if not, an error shall be
13520 * logged and just return. Otherwise, it will get to the corresponding
13521 * completion queue and process all the entries on the completion queue, rearm
13522 * the completion queue, and then return.
13523 **/
13524static void
13525lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13526{
13527 struct lpfc_queue *cq;
13528 struct lpfc_cqe *cqe;
13529 bool workposted = false;
13530 uint16_t cqid;
13531 int ecount = 0;
13532
13533 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13535 "9147 Not a valid completion "
13536 "event: majorcode=x%x, minorcode=x%x\n",
13537 bf_get_le32(lpfc_eqe_major_code, eqe),
13538 bf_get_le32(lpfc_eqe_minor_code, eqe));
13539 return;
13540 }
13541
13542 /* Get the reference to the corresponding CQ */
13543 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13544
13545 /* Next check for OAS */
13546 cq = phba->sli4_hba.oas_cq;
13547 if (unlikely(!cq)) {
13548 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13549 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13550 "9148 OAS completion queue "
13551 "does not exist\n");
13552 return;
13553 }
13554
13555 if (unlikely(cqid != cq->queue_id)) {
13556 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13557 "9149 Miss-matched fast-path compl "
13558 "queue id: eqcqid=%d, fcpcqid=%d\n",
13559 cqid, cq->queue_id);
13560 return;
13561 }
13562
13563 /* Process all the entries to the OAS CQ */
13564 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013565 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart1ba981f2014-02-20 09:56:45 -050013566 if (!(++ecount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013567 break;
James Smart1ba981f2014-02-20 09:56:45 -050013568 }
13569
13570 /* Track the max number of CQEs processed in 1 EQ */
13571 if (ecount > cq->CQ_max_cqe)
13572 cq->CQ_max_cqe = ecount;
James Smart0cf07f842017-06-01 21:07:10 -070013573 cq->assoc_qp->EQ_cqe_cnt += ecount;
James Smart1ba981f2014-02-20 09:56:45 -050013574
13575 /* Catch the no cq entry condition */
13576 if (unlikely(ecount == 0))
13577 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13578 "9153 No entry from fast-path completion "
13579 "queue fcpcqid=%d\n", cq->queue_id);
13580
13581 /* In any case, flash and re-arm the CQ */
13582 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13583
13584 /* wake up worker thread if there are works to be done */
13585 if (workposted)
13586 lpfc_worker_wake_up(phba);
13587}
13588
13589/**
13590 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13591 * @irq: Interrupt number.
13592 * @dev_id: The device context pointer.
13593 *
13594 * This function is directly called from the PCI layer as an interrupt
13595 * service routine when device with SLI-4 interface spec is enabled with
13596 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13597 * IOCB ring event in the HBA. However, when the device is enabled with either
13598 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13599 * device-level interrupt handler. When the PCI slot is in error recovery
13600 * or the HBA is undergoing initialization, the interrupt handler will not
13601 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13602 * the intrrupt context. This function is called without any lock held.
13603 * It gets the hbalock to access and update SLI data structures. Note that,
13604 * the EQ to CQ are one-to-one map such that the EQ index is
13605 * equal to that of CQ index.
13606 *
13607 * This function returns IRQ_HANDLED when interrupt is handled else it
13608 * returns IRQ_NONE.
13609 **/
13610irqreturn_t
13611lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13612{
13613 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013614 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013615 struct lpfc_queue *eq;
13616 struct lpfc_eqe *eqe;
13617 unsigned long iflag;
13618 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013619
13620 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013621 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13622 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013623
13624 if (unlikely(!phba))
13625 return IRQ_NONE;
13626
13627 /* Get to the EQ struct associated with this vector */
13628 eq = phba->sli4_hba.fof_eq;
13629 if (unlikely(!eq))
13630 return IRQ_NONE;
13631
13632 /* Check device state for handling interrupt */
13633 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013634 /* Check again for link_state with lock held */
13635 spin_lock_irqsave(&phba->hbalock, iflag);
13636 if (phba->link_state < LPFC_LINK_DOWN)
13637 /* Flush, clear interrupt, and rearm the EQ */
13638 lpfc_sli4_eq_flush(phba, eq);
13639 spin_unlock_irqrestore(&phba->hbalock, iflag);
13640 return IRQ_NONE;
13641 }
13642
13643 /*
13644 * Process all the event on FCP fast-path EQ
13645 */
13646 while ((eqe = lpfc_sli4_eq_get(eq))) {
13647 lpfc_sli4_fof_handle_eqe(phba, eqe);
13648 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013649 break;
James Smart1ba981f2014-02-20 09:56:45 -050013650 eq->EQ_processed++;
13651 }
13652
13653 /* Track the max number of EQEs processed in 1 intr */
13654 if (ecount > eq->EQ_max_eqe)
13655 eq->EQ_max_eqe = ecount;
13656
13657
13658 if (unlikely(ecount == 0)) {
13659 eq->EQ_no_entry++;
13660
13661 if (phba->intr_type == MSIX)
13662 /* MSI-X treated interrupt served as no EQ share INT */
13663 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13664 "9145 MSI-X interrupt with no EQE\n");
13665 else {
13666 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13667 "9146 ISR interrupt with no EQE\n");
13668 /* Non MSI-X treated on interrupt as EQ share INT */
13669 return IRQ_NONE;
13670 }
13671 }
13672 /* Always clear and re-arm the fast-path EQ */
13673 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13674 return IRQ_HANDLED;
13675}
13676
James Smart4f774512009-05-22 14:52:35 -040013677/**
James Smart67d12732012-08-03 12:36:13 -040013678 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013679 * @irq: Interrupt number.
13680 * @dev_id: The device context pointer.
13681 *
13682 * This function is directly called from the PCI layer as an interrupt
13683 * service routine when device with SLI-4 interface spec is enabled with
13684 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13685 * ring event in the HBA. However, when the device is enabled with either
13686 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13687 * device-level interrupt handler. When the PCI slot is in error recovery
13688 * or the HBA is undergoing initialization, the interrupt handler will not
13689 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13690 * the intrrupt context. This function is called without any lock held.
13691 * It gets the hbalock to access and update SLI data structures. Note that,
13692 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13693 * equal to that of FCP CQ index.
13694 *
James Smart67d12732012-08-03 12:36:13 -040013695 * The link attention and ELS ring attention events are handled
13696 * by the worker thread. The interrupt handler signals the worker thread
13697 * and returns for these events. This function is called without any lock
13698 * held. It gets the hbalock to access and update SLI data structures.
13699 *
James Smart4f774512009-05-22 14:52:35 -040013700 * This function returns IRQ_HANDLED when interrupt is handled else it
13701 * returns IRQ_NONE.
13702 **/
13703irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013704lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013705{
13706 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013707 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013708 struct lpfc_queue *fpeq;
13709 struct lpfc_eqe *eqe;
13710 unsigned long iflag;
13711 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013712 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013713
13714 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013715 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13716 phba = hba_eq_hdl->phba;
13717 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013718
13719 if (unlikely(!phba))
13720 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013721 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013722 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013723
13724 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013725 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013726 if (unlikely(!fpeq))
13727 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013728
James Smartbd2cdd52017-02-12 13:52:33 -080013729#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13730 if (phba->ktime_on)
13731 fpeq->isr_timestamp = ktime_get_ns();
13732#endif
13733
James Smartba20c852012-08-03 12:36:52 -040013734 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013735 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013736 lpfc_sli4_eq_clr_intr(fpeq);
13737 else {
James Smart895427b2017-02-12 13:52:30 -080013738 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013739 return IRQ_NONE;
13740 }
13741 }
13742
James Smart4f774512009-05-22 14:52:35 -040013743 /* Check device state for handling interrupt */
13744 if (unlikely(lpfc_intr_state_check(phba))) {
13745 /* Check again for link_state with lock held */
13746 spin_lock_irqsave(&phba->hbalock, iflag);
13747 if (phba->link_state < LPFC_LINK_DOWN)
13748 /* Flush, clear interrupt, and rearm the EQ */
13749 lpfc_sli4_eq_flush(phba, fpeq);
13750 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013751 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013752 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013753 return IRQ_NONE;
13754 }
13755
13756 /*
13757 * Process all the event on FCP fast-path EQ
13758 */
13759 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040013760 if (eqe == NULL)
13761 break;
13762
James Smart895427b2017-02-12 13:52:30 -080013763 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040013764 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013765 break;
James Smartb84daac2012-08-03 12:35:13 -040013766 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013767 }
13768
James Smartb84daac2012-08-03 12:35:13 -040013769 /* Track the max number of EQEs processed in 1 intr */
13770 if (ecount > fpeq->EQ_max_eqe)
13771 fpeq->EQ_max_eqe = ecount;
13772
James Smart4f774512009-05-22 14:52:35 -040013773 /* Always clear and re-arm the fast-path EQ */
13774 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13775
13776 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013777 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013778
13779 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013780 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013781 return IRQ_NONE;
13782 }
13783
James Smart4f774512009-05-22 14:52:35 -040013784 if (phba->intr_type == MSIX)
13785 /* MSI-X treated interrupt served as no EQ share INT */
13786 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13787 "0358 MSI-X interrupt with no EQE\n");
13788 else
13789 /* Non MSI-X treated on interrupt as EQ share INT */
13790 return IRQ_NONE;
13791 }
13792
James Smartba20c852012-08-03 12:36:52 -040013793 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013794 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13795
James Smart4f774512009-05-22 14:52:35 -040013796 return IRQ_HANDLED;
13797} /* lpfc_sli4_fp_intr_handler */
13798
13799/**
13800 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13801 * @irq: Interrupt number.
13802 * @dev_id: The device context pointer.
13803 *
13804 * This function is the device-level interrupt handler to device with SLI-4
13805 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13806 * interrupt mode is enabled and there is an event in the HBA which requires
13807 * driver attention. This function invokes the slow-path interrupt attention
13808 * handling function and fast-path interrupt attention handling function in
13809 * turn to process the relevant HBA attention events. This function is called
13810 * without any lock held. It gets the hbalock to access and update SLI data
13811 * structures.
13812 *
13813 * This function returns IRQ_HANDLED when interrupt is handled, else it
13814 * returns IRQ_NONE.
13815 **/
13816irqreturn_t
13817lpfc_sli4_intr_handler(int irq, void *dev_id)
13818{
13819 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013820 irqreturn_t hba_irq_rc;
13821 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013822 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013823
13824 /* Get the driver's phba structure from the dev_id */
13825 phba = (struct lpfc_hba *)dev_id;
13826
13827 if (unlikely(!phba))
13828 return IRQ_NONE;
13829
13830 /*
James Smart4f774512009-05-22 14:52:35 -040013831 * Invoke fast-path host attention interrupt handling as appropriate.
13832 */
James Smart895427b2017-02-12 13:52:30 -080013833 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013834 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013835 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013836 if (hba_irq_rc == IRQ_HANDLED)
13837 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013838 }
13839
James Smart1ba981f2014-02-20 09:56:45 -050013840 if (phba->cfg_fof) {
13841 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013842 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013843 if (hba_irq_rc == IRQ_HANDLED)
13844 hba_handled |= true;
13845 }
13846
James Smart67d12732012-08-03 12:36:13 -040013847 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013848} /* lpfc_sli4_intr_handler */
13849
13850/**
13851 * lpfc_sli4_queue_free - free a queue structure and associated memory
13852 * @queue: The queue structure to free.
13853 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013854 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013855 * the host resident queue. This function must be called after destroying the
13856 * queue on the HBA.
13857 **/
13858void
13859lpfc_sli4_queue_free(struct lpfc_queue *queue)
13860{
13861 struct lpfc_dmabuf *dmabuf;
13862
13863 if (!queue)
13864 return;
13865
13866 while (!list_empty(&queue->page_list)) {
13867 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13868 list);
James Smart49198b32010-04-06 15:04:33 -040013869 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040013870 dmabuf->virt, dmabuf->phys);
13871 kfree(dmabuf);
13872 }
James Smart895427b2017-02-12 13:52:30 -080013873 if (queue->rqbp) {
13874 lpfc_free_rq_buffer(queue->phba, queue);
13875 kfree(queue->rqbp);
13876 }
James Smartd1f525a2017-04-21 16:04:55 -070013877
13878 if (!list_empty(&queue->wq_list))
13879 list_del(&queue->wq_list);
13880
James Smart4f774512009-05-22 14:52:35 -040013881 kfree(queue);
13882 return;
13883}
13884
13885/**
13886 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13887 * @phba: The HBA that this queue is being created on.
13888 * @entry_size: The size of each queue entry for this queue.
13889 * @entry count: The number of entries that this queue will handle.
13890 *
13891 * This function allocates a queue structure and the DMAable memory used for
13892 * the host resident queue. This function must be called before creating the
13893 * queue on the HBA.
13894 **/
13895struct lpfc_queue *
13896lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13897 uint32_t entry_count)
13898{
13899 struct lpfc_queue *queue;
13900 struct lpfc_dmabuf *dmabuf;
13901 int x, total_qe_count;
13902 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013903 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013904
James Smartcb5172e2010-03-15 11:25:07 -040013905 if (!phba->sli4_hba.pc_sli4_params.supported)
13906 hw_page_size = SLI4_PAGE_SIZE;
13907
James Smart4f774512009-05-22 14:52:35 -040013908 queue = kzalloc(sizeof(struct lpfc_queue) +
13909 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13910 if (!queue)
13911 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013912 queue->page_count = (ALIGN(entry_size * entry_count,
13913 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013914
13915 /* If needed, Adjust page count to match the max the adapter supports */
13916 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13917 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13918
James Smart4f774512009-05-22 14:52:35 -040013919 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013920 INIT_LIST_HEAD(&queue->wq_list);
James Smart4f774512009-05-22 14:52:35 -040013921 INIT_LIST_HEAD(&queue->page_list);
13922 INIT_LIST_HEAD(&queue->child_list);
13923 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13924 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13925 if (!dmabuf)
13926 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013927 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13928 hw_page_size, &dmabuf->phys,
13929 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013930 if (!dmabuf->virt) {
13931 kfree(dmabuf);
13932 goto out_fail;
13933 }
13934 dmabuf->buffer_tag = x;
13935 list_add_tail(&dmabuf->list, &queue->page_list);
13936 /* initialize queue's entry array */
13937 dma_pointer = dmabuf->virt;
13938 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013939 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013940 total_qe_count++, dma_pointer += entry_size) {
13941 queue->qe[total_qe_count].address = dma_pointer;
13942 }
13943 }
13944 queue->entry_size = entry_size;
13945 queue->entry_count = entry_count;
13946 queue->phba = phba;
13947
James Smart64eb4dc2017-05-15 15:20:49 -070013948 /* entry_repost will be set during q creation */
13949
James Smart4f774512009-05-22 14:52:35 -040013950 return queue;
13951out_fail:
13952 lpfc_sli4_queue_free(queue);
13953 return NULL;
13954}
13955
13956/**
James Smart962bc512013-01-03 15:44:00 -050013957 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13958 * @phba: HBA structure that indicates port to create a queue on.
13959 * @pci_barset: PCI BAR set flag.
13960 *
13961 * This function shall perform iomap of the specified PCI BAR address to host
13962 * memory address if not already done so and return it. The returned host
13963 * memory address can be NULL.
13964 */
13965static void __iomem *
13966lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13967{
James Smart962bc512013-01-03 15:44:00 -050013968 if (!phba->pcidev)
13969 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013970
13971 switch (pci_barset) {
13972 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013973 return phba->pci_bar0_memmap_p;
13974 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013975 return phba->pci_bar2_memmap_p;
13976 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013977 return phba->pci_bar4_memmap_p;
13978 default:
13979 break;
13980 }
13981 return NULL;
13982}
13983
13984/**
James Smart895427b2017-02-12 13:52:30 -080013985 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040013986 * @phba: HBA structure that indicates port to create a queue on.
13987 * @startq: The starting FCP EQ to modify
13988 *
13989 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080013990 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
13991 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040013992 *
13993 * The @phba struct is used to send mailbox command to HBA. The @startq
13994 * is used to get the starting FCP EQ to change.
13995 * This function is asynchronous and will wait for the mailbox
13996 * command to finish before continuing.
13997 *
13998 * On success this function will return a zero. If unable to allocate enough
13999 * memory this function will return -ENOMEM. If the queue create mailbox command
14000 * fails this function will return -ENXIO.
14001 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014002int
James Smart0cf07f842017-06-01 21:07:10 -070014003lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14004 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014005{
14006 struct lpfc_mbx_modify_eq_delay *eq_delay;
14007 LPFC_MBOXQ_t *mbox;
14008 struct lpfc_queue *eq;
14009 int cnt, rc, length, status = 0;
14010 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014011 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014012 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014013 union lpfc_sli4_cfg_shdr *shdr;
14014 uint16_t dmult;
14015
James Smart895427b2017-02-12 13:52:30 -080014016 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014017 return 0;
14018
14019 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14020 if (!mbox)
14021 return -ENOMEM;
14022 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14023 sizeof(struct lpfc_sli4_cfg_mhdr));
14024 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14025 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14026 length, LPFC_SLI4_MBX_EMBED);
14027 eq_delay = &mbox->u.mqe.un.eq_delay;
14028
14029 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014030 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014031 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014032 dmult = 0;
14033 else
14034 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014035 if (dmult > LPFC_DMULT_MAX)
14036 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014037
14038 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014039 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14040 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014041 if (!eq)
14042 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014043 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014044 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14045 eq_delay->u.request.eq[cnt].phase = 0;
14046 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14047 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014048
14049 /* q_mode is only used for auto_imax */
14050 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14051 /* Use EQ Delay Register method for q_mode */
14052
14053 /* Convert for EQ Delay register */
14054 val = phba->cfg_fcp_imax;
14055 if (val) {
14056 /* First, interrupts per sec per EQ */
14057 val = phba->cfg_fcp_imax /
14058 phba->io_channel_irqs;
14059
14060 /* us delay between each interrupt */
14061 val = LPFC_SEC_TO_USEC / val;
14062 }
14063 eq->q_mode = val;
14064 } else {
14065 eq->q_mode = imax;
14066 }
14067
14068 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014069 break;
14070 }
14071 eq_delay->u.request.num_eq = cnt;
14072
14073 mbox->vport = phba->pport;
14074 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14075 mbox->context1 = NULL;
14076 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14077 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14078 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14079 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14080 if (shdr_status || shdr_add_status || rc) {
14081 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14082 "2512 MODIFY_EQ_DELAY mailbox failed with "
14083 "status x%x add_status x%x, mbx status x%x\n",
14084 shdr_status, shdr_add_status, rc);
14085 status = -ENXIO;
14086 }
14087 mempool_free(mbox, phba->mbox_mem_pool);
14088 return status;
14089}
14090
14091/**
James Smart4f774512009-05-22 14:52:35 -040014092 * lpfc_eq_create - Create an Event Queue on the HBA
14093 * @phba: HBA structure that indicates port to create a queue on.
14094 * @eq: The queue structure to use to create the event queue.
14095 * @imax: The maximum interrupt per second limit.
14096 *
14097 * This function creates an event queue, as detailed in @eq, on a port,
14098 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14099 *
14100 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14101 * is used to get the entry count and entry size that are necessary to
14102 * determine the number of pages to allocate and use for this queue. This
14103 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14104 * event queue. This function is asynchronous and will wait for the mailbox
14105 * command to finish before continuing.
14106 *
14107 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014108 * memory this function will return -ENOMEM. If the queue create mailbox command
14109 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014110 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014111int
James Smartee020062012-09-29 11:28:52 -040014112lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014113{
14114 struct lpfc_mbx_eq_create *eq_create;
14115 LPFC_MBOXQ_t *mbox;
14116 int rc, length, status = 0;
14117 struct lpfc_dmabuf *dmabuf;
14118 uint32_t shdr_status, shdr_add_status;
14119 union lpfc_sli4_cfg_shdr *shdr;
14120 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014121 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14122
James Smart2e90f4b2011-12-13 13:22:37 -050014123 /* sanity check on queue memory */
14124 if (!eq)
14125 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014126 if (!phba->sli4_hba.pc_sli4_params.supported)
14127 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014128
14129 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14130 if (!mbox)
14131 return -ENOMEM;
14132 length = (sizeof(struct lpfc_mbx_eq_create) -
14133 sizeof(struct lpfc_sli4_cfg_mhdr));
14134 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14135 LPFC_MBOX_OPCODE_EQ_CREATE,
14136 length, LPFC_SLI4_MBX_EMBED);
14137 eq_create = &mbox->u.mqe.un.eq_create;
14138 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14139 eq->page_count);
14140 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14141 LPFC_EQE_SIZE);
14142 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040014143 /* don't setup delay multiplier using EQ_CREATE */
14144 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014145 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14146 dmult);
14147 switch (eq->entry_count) {
14148 default:
14149 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14150 "0360 Unsupported EQ count. (%d)\n",
14151 eq->entry_count);
14152 if (eq->entry_count < 256)
14153 return -EINVAL;
14154 /* otherwise default to smallest count (drop through) */
14155 case 256:
14156 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14157 LPFC_EQ_CNT_256);
14158 break;
14159 case 512:
14160 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14161 LPFC_EQ_CNT_512);
14162 break;
14163 case 1024:
14164 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14165 LPFC_EQ_CNT_1024);
14166 break;
14167 case 2048:
14168 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14169 LPFC_EQ_CNT_2048);
14170 break;
14171 case 4096:
14172 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14173 LPFC_EQ_CNT_4096);
14174 break;
14175 }
14176 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014177 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014178 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14179 putPaddrLow(dmabuf->phys);
14180 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14181 putPaddrHigh(dmabuf->phys);
14182 }
14183 mbox->vport = phba->pport;
14184 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14185 mbox->context1 = NULL;
14186 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14187 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14188 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14189 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14190 if (shdr_status || shdr_add_status || rc) {
14191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14192 "2500 EQ_CREATE mailbox failed with "
14193 "status x%x add_status x%x, mbx status x%x\n",
14194 shdr_status, shdr_add_status, rc);
14195 status = -ENXIO;
14196 }
14197 eq->type = LPFC_EQ;
14198 eq->subtype = LPFC_NONE;
14199 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14200 if (eq->queue_id == 0xFFFF)
14201 status = -ENXIO;
14202 eq->host_index = 0;
14203 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014204 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014205
James Smart8fa38512009-07-19 10:01:03 -040014206 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014207 return status;
14208}
14209
14210/**
14211 * lpfc_cq_create - Create a Completion Queue on the HBA
14212 * @phba: HBA structure that indicates port to create a queue on.
14213 * @cq: The queue structure to use to create the completion queue.
14214 * @eq: The event queue to bind this completion queue to.
14215 *
14216 * This function creates a completion queue, as detailed in @wq, on a port,
14217 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14218 *
14219 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14220 * is used to get the entry count and entry size that are necessary to
14221 * determine the number of pages to allocate and use for this queue. The @eq
14222 * is used to indicate which event queue to bind this completion queue to. This
14223 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14224 * completion queue. This function is asynchronous and will wait for the mailbox
14225 * command to finish before continuing.
14226 *
14227 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014228 * memory this function will return -ENOMEM. If the queue create mailbox command
14229 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014230 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014231int
James Smart4f774512009-05-22 14:52:35 -040014232lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14233 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14234{
14235 struct lpfc_mbx_cq_create *cq_create;
14236 struct lpfc_dmabuf *dmabuf;
14237 LPFC_MBOXQ_t *mbox;
14238 int rc, length, status = 0;
14239 uint32_t shdr_status, shdr_add_status;
14240 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014241 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14242
James Smart2e90f4b2011-12-13 13:22:37 -050014243 /* sanity check on queue memory */
14244 if (!cq || !eq)
14245 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014246 if (!phba->sli4_hba.pc_sli4_params.supported)
14247 hw_page_size = SLI4_PAGE_SIZE;
14248
James Smart4f774512009-05-22 14:52:35 -040014249 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14250 if (!mbox)
14251 return -ENOMEM;
14252 length = (sizeof(struct lpfc_mbx_cq_create) -
14253 sizeof(struct lpfc_sli4_cfg_mhdr));
14254 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14255 LPFC_MBOX_OPCODE_CQ_CREATE,
14256 length, LPFC_SLI4_MBX_EMBED);
14257 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014258 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014259 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14260 cq->page_count);
14261 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14262 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014263 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14264 phba->sli4_hba.pc_sli4_params.cqv);
14265 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040014266 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
14267 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014268 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14269 eq->queue_id);
14270 } else {
14271 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14272 eq->queue_id);
14273 }
James Smart4f774512009-05-22 14:52:35 -040014274 switch (cq->entry_count) {
14275 default:
14276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014277 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014278 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014279 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014280 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014281 if (cq->entry_count < 256) {
14282 status = -EINVAL;
14283 goto out;
14284 }
James Smart4f774512009-05-22 14:52:35 -040014285 /* otherwise default to smallest count (drop through) */
14286 case 256:
14287 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14288 LPFC_CQ_CNT_256);
14289 break;
14290 case 512:
14291 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14292 LPFC_CQ_CNT_512);
14293 break;
14294 case 1024:
14295 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14296 LPFC_CQ_CNT_1024);
14297 break;
14298 }
14299 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014300 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014301 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14302 putPaddrLow(dmabuf->phys);
14303 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14304 putPaddrHigh(dmabuf->phys);
14305 }
14306 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14307
14308 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014309 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14310 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14311 if (shdr_status || shdr_add_status || rc) {
14312 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14313 "2501 CQ_CREATE mailbox failed with "
14314 "status x%x add_status x%x, mbx status x%x\n",
14315 shdr_status, shdr_add_status, rc);
14316 status = -ENXIO;
14317 goto out;
14318 }
14319 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14320 if (cq->queue_id == 0xFFFF) {
14321 status = -ENXIO;
14322 goto out;
14323 }
14324 /* link the cq onto the parent eq child list */
14325 list_add_tail(&cq->list, &eq->child_list);
14326 /* Set up completion queue's type and subtype */
14327 cq->type = type;
14328 cq->subtype = subtype;
14329 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014330 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014331 cq->host_index = 0;
14332 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014333 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014334
James Smart8fa38512009-07-19 10:01:03 -040014335out:
14336 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014337 return status;
14338}
14339
14340/**
James Smart2d7dbc42017-02-12 13:52:35 -080014341 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14342 * @phba: HBA structure that indicates port to create a queue on.
14343 * @cqp: The queue structure array to use to create the completion queues.
14344 * @eqp: The event queue array to bind these completion queues to.
14345 *
14346 * This function creates a set of completion queue, s to support MRQ
14347 * as detailed in @cqp, on a port,
14348 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14349 *
14350 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14351 * is used to get the entry count and entry size that are necessary to
14352 * determine the number of pages to allocate and use for this queue. The @eq
14353 * is used to indicate which event queue to bind this completion queue to. This
14354 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14355 * completion queue. This function is asynchronous and will wait for the mailbox
14356 * command to finish before continuing.
14357 *
14358 * On success this function will return a zero. If unable to allocate enough
14359 * memory this function will return -ENOMEM. If the queue create mailbox command
14360 * fails this function will return -ENXIO.
14361 **/
14362int
14363lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14364 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14365{
14366 struct lpfc_queue *cq;
14367 struct lpfc_queue *eq;
14368 struct lpfc_mbx_cq_create_set *cq_set;
14369 struct lpfc_dmabuf *dmabuf;
14370 LPFC_MBOXQ_t *mbox;
14371 int rc, length, alloclen, status = 0;
14372 int cnt, idx, numcq, page_idx = 0;
14373 uint32_t shdr_status, shdr_add_status;
14374 union lpfc_sli4_cfg_shdr *shdr;
14375 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14376
14377 /* sanity check on queue memory */
14378 numcq = phba->cfg_nvmet_mrq;
14379 if (!cqp || !eqp || !numcq)
14380 return -ENODEV;
14381 if (!phba->sli4_hba.pc_sli4_params.supported)
14382 hw_page_size = SLI4_PAGE_SIZE;
14383
14384 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14385 if (!mbox)
14386 return -ENOMEM;
14387
14388 length = sizeof(struct lpfc_mbx_cq_create_set);
14389 length += ((numcq * cqp[0]->page_count) *
14390 sizeof(struct dma_address));
14391 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14392 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14393 LPFC_SLI4_MBX_NEMBED);
14394 if (alloclen < length) {
14395 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14396 "3098 Allocated DMA memory size (%d) is "
14397 "less than the requested DMA memory size "
14398 "(%d)\n", alloclen, length);
14399 status = -ENOMEM;
14400 goto out;
14401 }
14402 cq_set = mbox->sge_array->addr[0];
14403 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14404 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14405
14406 for (idx = 0; idx < numcq; idx++) {
14407 cq = cqp[idx];
14408 eq = eqp[idx];
14409 if (!cq || !eq) {
14410 status = -ENOMEM;
14411 goto out;
14412 }
14413
14414 switch (idx) {
14415 case 0:
14416 bf_set(lpfc_mbx_cq_create_set_page_size,
14417 &cq_set->u.request,
14418 (hw_page_size / SLI4_PAGE_SIZE));
14419 bf_set(lpfc_mbx_cq_create_set_num_pages,
14420 &cq_set->u.request, cq->page_count);
14421 bf_set(lpfc_mbx_cq_create_set_evt,
14422 &cq_set->u.request, 1);
14423 bf_set(lpfc_mbx_cq_create_set_valid,
14424 &cq_set->u.request, 1);
14425 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14426 &cq_set->u.request, 0);
14427 bf_set(lpfc_mbx_cq_create_set_num_cq,
14428 &cq_set->u.request, numcq);
14429 switch (cq->entry_count) {
14430 default:
14431 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14432 "3118 Bad CQ count. (%d)\n",
14433 cq->entry_count);
14434 if (cq->entry_count < 256) {
14435 status = -EINVAL;
14436 goto out;
14437 }
14438 /* otherwise default to smallest (drop thru) */
14439 case 256:
14440 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14441 &cq_set->u.request, LPFC_CQ_CNT_256);
14442 break;
14443 case 512:
14444 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14445 &cq_set->u.request, LPFC_CQ_CNT_512);
14446 break;
14447 case 1024:
14448 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14449 &cq_set->u.request, LPFC_CQ_CNT_1024);
14450 break;
14451 }
14452 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14453 &cq_set->u.request, eq->queue_id);
14454 break;
14455 case 1:
14456 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14457 &cq_set->u.request, eq->queue_id);
14458 break;
14459 case 2:
14460 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14461 &cq_set->u.request, eq->queue_id);
14462 break;
14463 case 3:
14464 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14465 &cq_set->u.request, eq->queue_id);
14466 break;
14467 case 4:
14468 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14469 &cq_set->u.request, eq->queue_id);
14470 break;
14471 case 5:
14472 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14473 &cq_set->u.request, eq->queue_id);
14474 break;
14475 case 6:
14476 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14477 &cq_set->u.request, eq->queue_id);
14478 break;
14479 case 7:
14480 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14481 &cq_set->u.request, eq->queue_id);
14482 break;
14483 case 8:
14484 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14485 &cq_set->u.request, eq->queue_id);
14486 break;
14487 case 9:
14488 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14489 &cq_set->u.request, eq->queue_id);
14490 break;
14491 case 10:
14492 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14493 &cq_set->u.request, eq->queue_id);
14494 break;
14495 case 11:
14496 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14497 &cq_set->u.request, eq->queue_id);
14498 break;
14499 case 12:
14500 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14501 &cq_set->u.request, eq->queue_id);
14502 break;
14503 case 13:
14504 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14505 &cq_set->u.request, eq->queue_id);
14506 break;
14507 case 14:
14508 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14509 &cq_set->u.request, eq->queue_id);
14510 break;
14511 case 15:
14512 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14513 &cq_set->u.request, eq->queue_id);
14514 break;
14515 }
14516
14517 /* link the cq onto the parent eq child list */
14518 list_add_tail(&cq->list, &eq->child_list);
14519 /* Set up completion queue's type and subtype */
14520 cq->type = type;
14521 cq->subtype = subtype;
14522 cq->assoc_qid = eq->queue_id;
14523 cq->host_index = 0;
14524 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014525 cq->entry_repost = LPFC_CQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080014526
14527 rc = 0;
14528 list_for_each_entry(dmabuf, &cq->page_list, list) {
14529 memset(dmabuf->virt, 0, hw_page_size);
14530 cnt = page_idx + dmabuf->buffer_tag;
14531 cq_set->u.request.page[cnt].addr_lo =
14532 putPaddrLow(dmabuf->phys);
14533 cq_set->u.request.page[cnt].addr_hi =
14534 putPaddrHigh(dmabuf->phys);
14535 rc++;
14536 }
14537 page_idx += rc;
14538 }
14539
14540 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14541
14542 /* The IOCTL status is embedded in the mailbox subheader. */
14543 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14544 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14545 if (shdr_status || shdr_add_status || rc) {
14546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14547 "3119 CQ_CREATE_SET mailbox failed with "
14548 "status x%x add_status x%x, mbx status x%x\n",
14549 shdr_status, shdr_add_status, rc);
14550 status = -ENXIO;
14551 goto out;
14552 }
14553 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14554 if (rc == 0xFFFF) {
14555 status = -ENXIO;
14556 goto out;
14557 }
14558
14559 for (idx = 0; idx < numcq; idx++) {
14560 cq = cqp[idx];
14561 cq->queue_id = rc + idx;
14562 }
14563
14564out:
14565 lpfc_sli4_mbox_cmd_free(phba, mbox);
14566 return status;
14567}
14568
14569/**
James Smartb19a0612010-04-06 14:48:51 -040014570 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014571 * @phba: HBA structure that indicates port to create a queue on.
14572 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014573 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14574 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014575 *
James Smartb19a0612010-04-06 14:48:51 -040014576 * This function provides failback (fb) functionality when the
14577 * mq_create_ext fails on older FW generations. It's purpose is identical
14578 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014579 *
James Smartb19a0612010-04-06 14:48:51 -040014580 * This routine cannot fail as all attributes were previously accessed and
14581 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014582 **/
James Smartb19a0612010-04-06 14:48:51 -040014583static void
14584lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14585 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014586{
14587 struct lpfc_mbx_mq_create *mq_create;
14588 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014589 int length;
James Smart04c68492009-05-22 14:52:52 -040014590
James Smart04c68492009-05-22 14:52:52 -040014591 length = (sizeof(struct lpfc_mbx_mq_create) -
14592 sizeof(struct lpfc_sli4_cfg_mhdr));
14593 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14594 LPFC_MBOX_OPCODE_MQ_CREATE,
14595 length, LPFC_SLI4_MBX_EMBED);
14596 mq_create = &mbox->u.mqe.un.mq_create;
14597 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014598 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014599 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014600 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014601 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14602 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014603 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014604 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14605 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014606 break;
14607 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014608 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14609 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014610 break;
14611 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014612 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14613 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014614 break;
14615 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014616 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14617 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014618 break;
14619 }
14620 list_for_each_entry(dmabuf, &mq->page_list, list) {
14621 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014622 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014623 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014624 putPaddrHigh(dmabuf->phys);
14625 }
14626}
14627
14628/**
14629 * lpfc_mq_create - Create a mailbox Queue on the HBA
14630 * @phba: HBA structure that indicates port to create a queue on.
14631 * @mq: The queue structure to use to create the mailbox queue.
14632 * @cq: The completion queue to associate with this cq.
14633 * @subtype: The queue's subtype.
14634 *
14635 * This function creates a mailbox queue, as detailed in @mq, on a port,
14636 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14637 *
14638 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14639 * is used to get the entry count and entry size that are necessary to
14640 * determine the number of pages to allocate and use for this queue. This
14641 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14642 * mailbox queue. This function is asynchronous and will wait for the mailbox
14643 * command to finish before continuing.
14644 *
14645 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014646 * memory this function will return -ENOMEM. If the queue create mailbox command
14647 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014648 **/
14649int32_t
14650lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14651 struct lpfc_queue *cq, uint32_t subtype)
14652{
14653 struct lpfc_mbx_mq_create *mq_create;
14654 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14655 struct lpfc_dmabuf *dmabuf;
14656 LPFC_MBOXQ_t *mbox;
14657 int rc, length, status = 0;
14658 uint32_t shdr_status, shdr_add_status;
14659 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014660 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014661
James Smart2e90f4b2011-12-13 13:22:37 -050014662 /* sanity check on queue memory */
14663 if (!mq || !cq)
14664 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014665 if (!phba->sli4_hba.pc_sli4_params.supported)
14666 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014667
14668 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14669 if (!mbox)
14670 return -ENOMEM;
14671 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14672 sizeof(struct lpfc_sli4_cfg_mhdr));
14673 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14674 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14675 length, LPFC_SLI4_MBX_EMBED);
14676
14677 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014678 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014679 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14680 &mq_create_ext->u.request, mq->page_count);
14681 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14682 &mq_create_ext->u.request, 1);
14683 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014684 &mq_create_ext->u.request, 1);
14685 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14686 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014687 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14688 &mq_create_ext->u.request, 1);
14689 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14690 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014691 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014692 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14693 phba->sli4_hba.pc_sli4_params.mqv);
14694 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14695 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14696 cq->queue_id);
14697 else
14698 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14699 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014700 switch (mq->entry_count) {
14701 default:
14702 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14703 "0362 Unsupported MQ count. (%d)\n",
14704 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014705 if (mq->entry_count < 16) {
14706 status = -EINVAL;
14707 goto out;
14708 }
James Smartb19a0612010-04-06 14:48:51 -040014709 /* otherwise default to smallest count (drop through) */
14710 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014711 bf_set(lpfc_mq_context_ring_size,
14712 &mq_create_ext->u.request.context,
14713 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014714 break;
14715 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014716 bf_set(lpfc_mq_context_ring_size,
14717 &mq_create_ext->u.request.context,
14718 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014719 break;
14720 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014721 bf_set(lpfc_mq_context_ring_size,
14722 &mq_create_ext->u.request.context,
14723 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014724 break;
14725 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014726 bf_set(lpfc_mq_context_ring_size,
14727 &mq_create_ext->u.request.context,
14728 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014729 break;
14730 }
14731 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014732 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014733 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14734 putPaddrLow(dmabuf->phys);
14735 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014736 putPaddrHigh(dmabuf->phys);
14737 }
14738 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014739 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14740 &mq_create_ext->u.response);
14741 if (rc != MBX_SUCCESS) {
14742 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14743 "2795 MQ_CREATE_EXT failed with "
14744 "status x%x. Failback to MQ_CREATE.\n",
14745 rc);
14746 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14747 mq_create = &mbox->u.mqe.un.mq_create;
14748 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14749 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14750 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14751 &mq_create->u.response);
14752 }
14753
James Smart04c68492009-05-22 14:52:52 -040014754 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014755 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14756 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14757 if (shdr_status || shdr_add_status || rc) {
14758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14759 "2502 MQ_CREATE mailbox failed with "
14760 "status x%x add_status x%x, mbx status x%x\n",
14761 shdr_status, shdr_add_status, rc);
14762 status = -ENXIO;
14763 goto out;
14764 }
James Smart04c68492009-05-22 14:52:52 -040014765 if (mq->queue_id == 0xFFFF) {
14766 status = -ENXIO;
14767 goto out;
14768 }
14769 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014770 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014771 mq->subtype = subtype;
14772 mq->host_index = 0;
14773 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014774 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040014775
14776 /* link the mq onto the parent cq child list */
14777 list_add_tail(&mq->list, &cq->child_list);
14778out:
James Smart8fa38512009-07-19 10:01:03 -040014779 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014780 return status;
14781}
14782
14783/**
James Smart4f774512009-05-22 14:52:35 -040014784 * lpfc_wq_create - Create a Work Queue on the HBA
14785 * @phba: HBA structure that indicates port to create a queue on.
14786 * @wq: The queue structure to use to create the work queue.
14787 * @cq: The completion queue to bind this work queue to.
14788 * @subtype: The subtype of the work queue indicating its functionality.
14789 *
14790 * This function creates a work queue, as detailed in @wq, on a port, described
14791 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14792 *
14793 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14794 * is used to get the entry count and entry size that are necessary to
14795 * determine the number of pages to allocate and use for this queue. The @cq
14796 * is used to indicate which completion queue to bind this work queue to. This
14797 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14798 * work queue. This function is asynchronous and will wait for the mailbox
14799 * command to finish before continuing.
14800 *
14801 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014802 * memory this function will return -ENOMEM. If the queue create mailbox command
14803 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014804 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014805int
James Smart4f774512009-05-22 14:52:35 -040014806lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14807 struct lpfc_queue *cq, uint32_t subtype)
14808{
14809 struct lpfc_mbx_wq_create *wq_create;
14810 struct lpfc_dmabuf *dmabuf;
14811 LPFC_MBOXQ_t *mbox;
14812 int rc, length, status = 0;
14813 uint32_t shdr_status, shdr_add_status;
14814 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014815 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014816 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014817 void __iomem *bar_memmap_p;
14818 uint32_t db_offset;
14819 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014820
James Smart2e90f4b2011-12-13 13:22:37 -050014821 /* sanity check on queue memory */
14822 if (!wq || !cq)
14823 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014824 if (!phba->sli4_hba.pc_sli4_params.supported)
14825 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014826
14827 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14828 if (!mbox)
14829 return -ENOMEM;
14830 length = (sizeof(struct lpfc_mbx_wq_create) -
14831 sizeof(struct lpfc_sli4_cfg_mhdr));
14832 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14833 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14834 length, LPFC_SLI4_MBX_EMBED);
14835 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014836 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014837 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14838 wq->page_count);
14839 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14840 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014841
14842 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014843 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14844 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014845
James Smart0c651872013-07-15 18:33:23 -040014846 switch (phba->sli4_hba.pc_sli4_params.wqv) {
14847 case LPFC_Q_CREATE_VERSION_0:
14848 switch (wq->entry_size) {
14849 default:
14850 case 64:
14851 /* Nothing to do, version 0 ONLY supports 64 byte */
14852 page = wq_create->u.request.page;
14853 break;
14854 case 128:
14855 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14856 LPFC_WQ_SZ128_SUPPORT)) {
14857 status = -ERANGE;
14858 goto out;
14859 }
14860 /* If we get here the HBA MUST also support V1 and
14861 * we MUST use it
14862 */
14863 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14864 LPFC_Q_CREATE_VERSION_1);
14865
14866 bf_set(lpfc_mbx_wq_create_wqe_count,
14867 &wq_create->u.request_1, wq->entry_count);
14868 bf_set(lpfc_mbx_wq_create_wqe_size,
14869 &wq_create->u.request_1,
14870 LPFC_WQ_WQE_SIZE_128);
14871 bf_set(lpfc_mbx_wq_create_page_size,
14872 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014873 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014874 page = wq_create->u.request_1.page;
14875 break;
14876 }
14877 break;
14878 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014879 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14880 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070014881 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14882 LPFC_Q_CREATE_VERSION_1);
14883
James Smart5a6f1332011-03-11 16:05:35 -050014884 switch (wq->entry_size) {
14885 default:
14886 case 64:
14887 bf_set(lpfc_mbx_wq_create_wqe_size,
14888 &wq_create->u.request_1,
14889 LPFC_WQ_WQE_SIZE_64);
14890 break;
14891 case 128:
James Smart0c651872013-07-15 18:33:23 -040014892 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14893 LPFC_WQ_SZ128_SUPPORT)) {
14894 status = -ERANGE;
14895 goto out;
14896 }
James Smart5a6f1332011-03-11 16:05:35 -050014897 bf_set(lpfc_mbx_wq_create_wqe_size,
14898 &wq_create->u.request_1,
14899 LPFC_WQ_WQE_SIZE_128);
14900 break;
14901 }
James Smart8ea73db2017-02-12 13:52:25 -080014902 bf_set(lpfc_mbx_wq_create_page_size,
14903 &wq_create->u.request_1,
14904 LPFC_WQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014905 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040014906 break;
14907 default:
14908 status = -ERANGE;
14909 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050014910 }
James Smart0c651872013-07-15 18:33:23 -040014911
James Smart4f774512009-05-22 14:52:35 -040014912 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014913 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050014914 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14915 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040014916 }
James Smart962bc512013-01-03 15:44:00 -050014917
14918 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14919 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14920
James Smart4f774512009-05-22 14:52:35 -040014921 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14922 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014923 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14924 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14925 if (shdr_status || shdr_add_status || rc) {
14926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14927 "2503 WQ_CREATE mailbox failed with "
14928 "status x%x add_status x%x, mbx status x%x\n",
14929 shdr_status, shdr_add_status, rc);
14930 status = -ENXIO;
14931 goto out;
14932 }
14933 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14934 if (wq->queue_id == 0xFFFF) {
14935 status = -ENXIO;
14936 goto out;
14937 }
James Smart962bc512013-01-03 15:44:00 -050014938 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14939 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14940 &wq_create->u.response);
14941 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14942 (wq->db_format != LPFC_DB_RING_FORMAT)) {
14943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14944 "3265 WQ[%d] doorbell format not "
14945 "supported: x%x\n", wq->queue_id,
14946 wq->db_format);
14947 status = -EINVAL;
14948 goto out;
14949 }
14950 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14951 &wq_create->u.response);
14952 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14953 if (!bar_memmap_p) {
14954 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14955 "3263 WQ[%d] failed to memmap pci "
14956 "barset:x%x\n", wq->queue_id,
14957 pci_barset);
14958 status = -ENOMEM;
14959 goto out;
14960 }
14961 db_offset = wq_create->u.response.doorbell_offset;
14962 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14963 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14965 "3252 WQ[%d] doorbell offset not "
14966 "supported: x%x\n", wq->queue_id,
14967 db_offset);
14968 status = -EINVAL;
14969 goto out;
14970 }
14971 wq->db_regaddr = bar_memmap_p + db_offset;
14972 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014973 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14974 "format:x%x\n", wq->queue_id, pci_barset,
14975 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014976 } else {
14977 wq->db_format = LPFC_DB_LIST_FORMAT;
14978 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14979 }
James Smart895427b2017-02-12 13:52:30 -080014980 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14981 if (wq->pring == NULL) {
14982 status = -ENOMEM;
14983 goto out;
14984 }
James Smart4f774512009-05-22 14:52:35 -040014985 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050014986 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014987 wq->subtype = subtype;
14988 wq->host_index = 0;
14989 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050014990 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040014991
14992 /* link the wq onto the parent cq child list */
14993 list_add_tail(&wq->list, &cq->child_list);
14994out:
James Smart8fa38512009-07-19 10:01:03 -040014995 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014996 return status;
14997}
14998
14999/**
15000 * lpfc_rq_create - Create a Receive Queue on the HBA
15001 * @phba: HBA structure that indicates port to create a queue on.
15002 * @hrq: The queue structure to use to create the header receive queue.
15003 * @drq: The queue structure to use to create the data receive queue.
15004 * @cq: The completion queue to bind this work queue to.
15005 *
15006 * This function creates a receive buffer queue pair , as detailed in @hrq and
15007 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15008 * to the HBA.
15009 *
15010 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15011 * struct is used to get the entry count that is necessary to determine the
15012 * number of pages to use for this queue. The @cq is used to indicate which
15013 * completion queue to bind received buffers that are posted to these queues to.
15014 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15015 * receive queue pair. This function is asynchronous and will wait for the
15016 * mailbox command to finish before continuing.
15017 *
15018 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015019 * memory this function will return -ENOMEM. If the queue create mailbox command
15020 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015021 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015022int
James Smart4f774512009-05-22 14:52:35 -040015023lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15024 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15025{
15026 struct lpfc_mbx_rq_create *rq_create;
15027 struct lpfc_dmabuf *dmabuf;
15028 LPFC_MBOXQ_t *mbox;
15029 int rc, length, status = 0;
15030 uint32_t shdr_status, shdr_add_status;
15031 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015032 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015033 void __iomem *bar_memmap_p;
15034 uint32_t db_offset;
15035 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015036
James Smart2e90f4b2011-12-13 13:22:37 -050015037 /* sanity check on queue memory */
15038 if (!hrq || !drq || !cq)
15039 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015040 if (!phba->sli4_hba.pc_sli4_params.supported)
15041 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015042
15043 if (hrq->entry_count != drq->entry_count)
15044 return -EINVAL;
15045 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15046 if (!mbox)
15047 return -ENOMEM;
15048 length = (sizeof(struct lpfc_mbx_rq_create) -
15049 sizeof(struct lpfc_sli4_cfg_mhdr));
15050 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15051 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15052 length, LPFC_SLI4_MBX_EMBED);
15053 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015054 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15055 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15056 phba->sli4_hba.pc_sli4_params.rqv);
15057 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15058 bf_set(lpfc_rq_context_rqe_count_1,
15059 &rq_create->u.request.context,
15060 hrq->entry_count);
15061 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015062 bf_set(lpfc_rq_context_rqe_size,
15063 &rq_create->u.request.context,
15064 LPFC_RQE_SIZE_8);
15065 bf_set(lpfc_rq_context_page_size,
15066 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015067 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015068 } else {
15069 switch (hrq->entry_count) {
15070 default:
15071 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15072 "2535 Unsupported RQ count. (%d)\n",
15073 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015074 if (hrq->entry_count < 512) {
15075 status = -EINVAL;
15076 goto out;
15077 }
James Smart5a6f1332011-03-11 16:05:35 -050015078 /* otherwise default to smallest count (drop through) */
15079 case 512:
15080 bf_set(lpfc_rq_context_rqe_count,
15081 &rq_create->u.request.context,
15082 LPFC_RQ_RING_SIZE_512);
15083 break;
15084 case 1024:
15085 bf_set(lpfc_rq_context_rqe_count,
15086 &rq_create->u.request.context,
15087 LPFC_RQ_RING_SIZE_1024);
15088 break;
15089 case 2048:
15090 bf_set(lpfc_rq_context_rqe_count,
15091 &rq_create->u.request.context,
15092 LPFC_RQ_RING_SIZE_2048);
15093 break;
15094 case 4096:
15095 bf_set(lpfc_rq_context_rqe_count,
15096 &rq_create->u.request.context,
15097 LPFC_RQ_RING_SIZE_4096);
15098 break;
15099 }
15100 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15101 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015102 }
15103 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15104 cq->queue_id);
15105 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15106 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015107 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015108 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015109 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15110 putPaddrLow(dmabuf->phys);
15111 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15112 putPaddrHigh(dmabuf->phys);
15113 }
James Smart962bc512013-01-03 15:44:00 -050015114 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15115 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15116
James Smart4f774512009-05-22 14:52:35 -040015117 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15118 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015119 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15120 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15121 if (shdr_status || shdr_add_status || rc) {
15122 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15123 "2504 RQ_CREATE mailbox failed with "
15124 "status x%x add_status x%x, mbx status x%x\n",
15125 shdr_status, shdr_add_status, rc);
15126 status = -ENXIO;
15127 goto out;
15128 }
15129 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15130 if (hrq->queue_id == 0xFFFF) {
15131 status = -ENXIO;
15132 goto out;
15133 }
James Smart962bc512013-01-03 15:44:00 -050015134
15135 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15136 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15137 &rq_create->u.response);
15138 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15139 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15141 "3262 RQ [%d] doorbell format not "
15142 "supported: x%x\n", hrq->queue_id,
15143 hrq->db_format);
15144 status = -EINVAL;
15145 goto out;
15146 }
15147
15148 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15149 &rq_create->u.response);
15150 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15151 if (!bar_memmap_p) {
15152 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15153 "3269 RQ[%d] failed to memmap pci "
15154 "barset:x%x\n", hrq->queue_id,
15155 pci_barset);
15156 status = -ENOMEM;
15157 goto out;
15158 }
15159
15160 db_offset = rq_create->u.response.doorbell_offset;
15161 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15162 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15163 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15164 "3270 RQ[%d] doorbell offset not "
15165 "supported: x%x\n", hrq->queue_id,
15166 db_offset);
15167 status = -EINVAL;
15168 goto out;
15169 }
15170 hrq->db_regaddr = bar_memmap_p + db_offset;
15171 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015172 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15173 "format:x%x\n", hrq->queue_id, pci_barset,
15174 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015175 } else {
15176 hrq->db_format = LPFC_DB_RING_FORMAT;
15177 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15178 }
James Smart4f774512009-05-22 14:52:35 -040015179 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015180 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015181 hrq->subtype = subtype;
15182 hrq->host_index = 0;
15183 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015184 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015185
15186 /* now create the data queue */
15187 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15188 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15189 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015190 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15191 phba->sli4_hba.pc_sli4_params.rqv);
15192 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15193 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015194 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015195 if (subtype == LPFC_NVMET)
15196 rq_create->u.request.context.buffer_size =
15197 LPFC_NVMET_DATA_BUF_SIZE;
15198 else
15199 rq_create->u.request.context.buffer_size =
15200 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015201 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15202 LPFC_RQE_SIZE_8);
15203 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15204 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015205 } else {
15206 switch (drq->entry_count) {
15207 default:
15208 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15209 "2536 Unsupported RQ count. (%d)\n",
15210 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015211 if (drq->entry_count < 512) {
15212 status = -EINVAL;
15213 goto out;
15214 }
James Smart5a6f1332011-03-11 16:05:35 -050015215 /* otherwise default to smallest count (drop through) */
15216 case 512:
15217 bf_set(lpfc_rq_context_rqe_count,
15218 &rq_create->u.request.context,
15219 LPFC_RQ_RING_SIZE_512);
15220 break;
15221 case 1024:
15222 bf_set(lpfc_rq_context_rqe_count,
15223 &rq_create->u.request.context,
15224 LPFC_RQ_RING_SIZE_1024);
15225 break;
15226 case 2048:
15227 bf_set(lpfc_rq_context_rqe_count,
15228 &rq_create->u.request.context,
15229 LPFC_RQ_RING_SIZE_2048);
15230 break;
15231 case 4096:
15232 bf_set(lpfc_rq_context_rqe_count,
15233 &rq_create->u.request.context,
15234 LPFC_RQ_RING_SIZE_4096);
15235 break;
15236 }
James Smart3c603be2017-05-15 15:20:44 -070015237 if (subtype == LPFC_NVMET)
15238 bf_set(lpfc_rq_context_buf_size,
15239 &rq_create->u.request.context,
15240 LPFC_NVMET_DATA_BUF_SIZE);
15241 else
15242 bf_set(lpfc_rq_context_buf_size,
15243 &rq_create->u.request.context,
15244 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015245 }
15246 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15247 cq->queue_id);
15248 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15249 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015250 list_for_each_entry(dmabuf, &drq->page_list, list) {
15251 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15252 putPaddrLow(dmabuf->phys);
15253 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15254 putPaddrHigh(dmabuf->phys);
15255 }
James Smart962bc512013-01-03 15:44:00 -050015256 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15257 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015258 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15259 /* The IOCTL status is embedded in the mailbox subheader. */
15260 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15261 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15262 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15263 if (shdr_status || shdr_add_status || rc) {
15264 status = -ENXIO;
15265 goto out;
15266 }
15267 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15268 if (drq->queue_id == 0xFFFF) {
15269 status = -ENXIO;
15270 goto out;
15271 }
15272 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015273 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015274 drq->subtype = subtype;
15275 drq->host_index = 0;
15276 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015277 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015278
15279 /* link the header and data RQs onto the parent cq child list */
15280 list_add_tail(&hrq->list, &cq->child_list);
15281 list_add_tail(&drq->list, &cq->child_list);
15282
15283out:
James Smart8fa38512009-07-19 10:01:03 -040015284 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015285 return status;
15286}
15287
15288/**
James Smart2d7dbc42017-02-12 13:52:35 -080015289 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15290 * @phba: HBA structure that indicates port to create a queue on.
15291 * @hrqp: The queue structure array to use to create the header receive queues.
15292 * @drqp: The queue structure array to use to create the data receive queues.
15293 * @cqp: The completion queue array to bind these receive queues to.
15294 *
15295 * This function creates a receive buffer queue pair , as detailed in @hrq and
15296 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15297 * to the HBA.
15298 *
15299 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15300 * struct is used to get the entry count that is necessary to determine the
15301 * number of pages to use for this queue. The @cq is used to indicate which
15302 * completion queue to bind received buffers that are posted to these queues to.
15303 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15304 * receive queue pair. This function is asynchronous and will wait for the
15305 * mailbox command to finish before continuing.
15306 *
15307 * On success this function will return a zero. If unable to allocate enough
15308 * memory this function will return -ENOMEM. If the queue create mailbox command
15309 * fails this function will return -ENXIO.
15310 **/
15311int
15312lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15313 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15314 uint32_t subtype)
15315{
15316 struct lpfc_queue *hrq, *drq, *cq;
15317 struct lpfc_mbx_rq_create_v2 *rq_create;
15318 struct lpfc_dmabuf *dmabuf;
15319 LPFC_MBOXQ_t *mbox;
15320 int rc, length, alloclen, status = 0;
15321 int cnt, idx, numrq, page_idx = 0;
15322 uint32_t shdr_status, shdr_add_status;
15323 union lpfc_sli4_cfg_shdr *shdr;
15324 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15325
15326 numrq = phba->cfg_nvmet_mrq;
15327 /* sanity check on array memory */
15328 if (!hrqp || !drqp || !cqp || !numrq)
15329 return -ENODEV;
15330 if (!phba->sli4_hba.pc_sli4_params.supported)
15331 hw_page_size = SLI4_PAGE_SIZE;
15332
15333 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15334 if (!mbox)
15335 return -ENOMEM;
15336
15337 length = sizeof(struct lpfc_mbx_rq_create_v2);
15338 length += ((2 * numrq * hrqp[0]->page_count) *
15339 sizeof(struct dma_address));
15340
15341 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15342 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15343 LPFC_SLI4_MBX_NEMBED);
15344 if (alloclen < length) {
15345 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15346 "3099 Allocated DMA memory size (%d) is "
15347 "less than the requested DMA memory size "
15348 "(%d)\n", alloclen, length);
15349 status = -ENOMEM;
15350 goto out;
15351 }
15352
15353
15354
15355 rq_create = mbox->sge_array->addr[0];
15356 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15357
15358 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15359 cnt = 0;
15360
15361 for (idx = 0; idx < numrq; idx++) {
15362 hrq = hrqp[idx];
15363 drq = drqp[idx];
15364 cq = cqp[idx];
15365
James Smart2d7dbc42017-02-12 13:52:35 -080015366 /* sanity check on queue memory */
15367 if (!hrq || !drq || !cq) {
15368 status = -ENODEV;
15369 goto out;
15370 }
15371
James Smart7aabe842017-03-04 09:30:22 -080015372 if (hrq->entry_count != drq->entry_count) {
15373 status = -EINVAL;
15374 goto out;
15375 }
15376
James Smart2d7dbc42017-02-12 13:52:35 -080015377 if (idx == 0) {
15378 bf_set(lpfc_mbx_rq_create_num_pages,
15379 &rq_create->u.request,
15380 hrq->page_count);
15381 bf_set(lpfc_mbx_rq_create_rq_cnt,
15382 &rq_create->u.request, (numrq * 2));
15383 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15384 1);
15385 bf_set(lpfc_rq_context_base_cq,
15386 &rq_create->u.request.context,
15387 cq->queue_id);
15388 bf_set(lpfc_rq_context_data_size,
15389 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015390 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015391 bf_set(lpfc_rq_context_hdr_size,
15392 &rq_create->u.request.context,
15393 LPFC_HDR_BUF_SIZE);
15394 bf_set(lpfc_rq_context_rqe_count_1,
15395 &rq_create->u.request.context,
15396 hrq->entry_count);
15397 bf_set(lpfc_rq_context_rqe_size,
15398 &rq_create->u.request.context,
15399 LPFC_RQE_SIZE_8);
15400 bf_set(lpfc_rq_context_page_size,
15401 &rq_create->u.request.context,
15402 (PAGE_SIZE/SLI4_PAGE_SIZE));
15403 }
15404 rc = 0;
15405 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15406 memset(dmabuf->virt, 0, hw_page_size);
15407 cnt = page_idx + dmabuf->buffer_tag;
15408 rq_create->u.request.page[cnt].addr_lo =
15409 putPaddrLow(dmabuf->phys);
15410 rq_create->u.request.page[cnt].addr_hi =
15411 putPaddrHigh(dmabuf->phys);
15412 rc++;
15413 }
15414 page_idx += rc;
15415
15416 rc = 0;
15417 list_for_each_entry(dmabuf, &drq->page_list, list) {
15418 memset(dmabuf->virt, 0, hw_page_size);
15419 cnt = page_idx + dmabuf->buffer_tag;
15420 rq_create->u.request.page[cnt].addr_lo =
15421 putPaddrLow(dmabuf->phys);
15422 rq_create->u.request.page[cnt].addr_hi =
15423 putPaddrHigh(dmabuf->phys);
15424 rc++;
15425 }
15426 page_idx += rc;
15427
15428 hrq->db_format = LPFC_DB_RING_FORMAT;
15429 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15430 hrq->type = LPFC_HRQ;
15431 hrq->assoc_qid = cq->queue_id;
15432 hrq->subtype = subtype;
15433 hrq->host_index = 0;
15434 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015435 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015436
15437 drq->db_format = LPFC_DB_RING_FORMAT;
15438 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15439 drq->type = LPFC_DRQ;
15440 drq->assoc_qid = cq->queue_id;
15441 drq->subtype = subtype;
15442 drq->host_index = 0;
15443 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015444 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015445
15446 list_add_tail(&hrq->list, &cq->child_list);
15447 list_add_tail(&drq->list, &cq->child_list);
15448 }
15449
15450 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15451 /* The IOCTL status is embedded in the mailbox subheader. */
15452 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15453 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15454 if (shdr_status || shdr_add_status || rc) {
15455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15456 "3120 RQ_CREATE mailbox failed with "
15457 "status x%x add_status x%x, mbx status x%x\n",
15458 shdr_status, shdr_add_status, rc);
15459 status = -ENXIO;
15460 goto out;
15461 }
15462 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15463 if (rc == 0xFFFF) {
15464 status = -ENXIO;
15465 goto out;
15466 }
15467
15468 /* Initialize all RQs with associated queue id */
15469 for (idx = 0; idx < numrq; idx++) {
15470 hrq = hrqp[idx];
15471 hrq->queue_id = rc + (2 * idx);
15472 drq = drqp[idx];
15473 drq->queue_id = rc + (2 * idx) + 1;
15474 }
15475
15476out:
15477 lpfc_sli4_mbox_cmd_free(phba, mbox);
15478 return status;
15479}
15480
15481/**
James Smart4f774512009-05-22 14:52:35 -040015482 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15483 * @eq: The queue structure associated with the queue to destroy.
15484 *
15485 * This function destroys a queue, as detailed in @eq by sending an mailbox
15486 * command, specific to the type of queue, to the HBA.
15487 *
15488 * The @eq struct is used to get the queue ID of the queue to destroy.
15489 *
15490 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015491 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015492 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015493int
James Smart4f774512009-05-22 14:52:35 -040015494lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15495{
15496 LPFC_MBOXQ_t *mbox;
15497 int rc, length, status = 0;
15498 uint32_t shdr_status, shdr_add_status;
15499 union lpfc_sli4_cfg_shdr *shdr;
15500
James Smart2e90f4b2011-12-13 13:22:37 -050015501 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015502 if (!eq)
15503 return -ENODEV;
15504 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15505 if (!mbox)
15506 return -ENOMEM;
15507 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15508 sizeof(struct lpfc_sli4_cfg_mhdr));
15509 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15510 LPFC_MBOX_OPCODE_EQ_DESTROY,
15511 length, LPFC_SLI4_MBX_EMBED);
15512 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15513 eq->queue_id);
15514 mbox->vport = eq->phba->pport;
15515 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15516
15517 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15518 /* The IOCTL status is embedded in the mailbox subheader. */
15519 shdr = (union lpfc_sli4_cfg_shdr *)
15520 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15521 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15522 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15523 if (shdr_status || shdr_add_status || rc) {
15524 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15525 "2505 EQ_DESTROY mailbox failed with "
15526 "status x%x add_status x%x, mbx status x%x\n",
15527 shdr_status, shdr_add_status, rc);
15528 status = -ENXIO;
15529 }
15530
15531 /* Remove eq from any list */
15532 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015533 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015534 return status;
15535}
15536
15537/**
15538 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15539 * @cq: The queue structure associated with the queue to destroy.
15540 *
15541 * This function destroys a queue, as detailed in @cq by sending an mailbox
15542 * command, specific to the type of queue, to the HBA.
15543 *
15544 * The @cq struct is used to get the queue ID of the queue to destroy.
15545 *
15546 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015547 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015548 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015549int
James Smart4f774512009-05-22 14:52:35 -040015550lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15551{
15552 LPFC_MBOXQ_t *mbox;
15553 int rc, length, status = 0;
15554 uint32_t shdr_status, shdr_add_status;
15555 union lpfc_sli4_cfg_shdr *shdr;
15556
James Smart2e90f4b2011-12-13 13:22:37 -050015557 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015558 if (!cq)
15559 return -ENODEV;
15560 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15561 if (!mbox)
15562 return -ENOMEM;
15563 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15564 sizeof(struct lpfc_sli4_cfg_mhdr));
15565 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15566 LPFC_MBOX_OPCODE_CQ_DESTROY,
15567 length, LPFC_SLI4_MBX_EMBED);
15568 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15569 cq->queue_id);
15570 mbox->vport = cq->phba->pport;
15571 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15572 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15573 /* The IOCTL status is embedded in the mailbox subheader. */
15574 shdr = (union lpfc_sli4_cfg_shdr *)
15575 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15576 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15577 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15578 if (shdr_status || shdr_add_status || rc) {
15579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15580 "2506 CQ_DESTROY mailbox failed with "
15581 "status x%x add_status x%x, mbx status x%x\n",
15582 shdr_status, shdr_add_status, rc);
15583 status = -ENXIO;
15584 }
15585 /* Remove cq from any list */
15586 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015587 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015588 return status;
15589}
15590
15591/**
James Smart04c68492009-05-22 14:52:52 -040015592 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15593 * @qm: The queue structure associated with the queue to destroy.
15594 *
15595 * This function destroys a queue, as detailed in @mq by sending an mailbox
15596 * command, specific to the type of queue, to the HBA.
15597 *
15598 * The @mq struct is used to get the queue ID of the queue to destroy.
15599 *
15600 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015601 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015602 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015603int
James Smart04c68492009-05-22 14:52:52 -040015604lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15605{
15606 LPFC_MBOXQ_t *mbox;
15607 int rc, length, status = 0;
15608 uint32_t shdr_status, shdr_add_status;
15609 union lpfc_sli4_cfg_shdr *shdr;
15610
James Smart2e90f4b2011-12-13 13:22:37 -050015611 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015612 if (!mq)
15613 return -ENODEV;
15614 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15615 if (!mbox)
15616 return -ENOMEM;
15617 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15618 sizeof(struct lpfc_sli4_cfg_mhdr));
15619 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15620 LPFC_MBOX_OPCODE_MQ_DESTROY,
15621 length, LPFC_SLI4_MBX_EMBED);
15622 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15623 mq->queue_id);
15624 mbox->vport = mq->phba->pport;
15625 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15626 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15627 /* The IOCTL status is embedded in the mailbox subheader. */
15628 shdr = (union lpfc_sli4_cfg_shdr *)
15629 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15630 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15631 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15632 if (shdr_status || shdr_add_status || rc) {
15633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15634 "2507 MQ_DESTROY mailbox failed with "
15635 "status x%x add_status x%x, mbx status x%x\n",
15636 shdr_status, shdr_add_status, rc);
15637 status = -ENXIO;
15638 }
15639 /* Remove mq from any list */
15640 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015641 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015642 return status;
15643}
15644
15645/**
James Smart4f774512009-05-22 14:52:35 -040015646 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15647 * @wq: The queue structure associated with the queue to destroy.
15648 *
15649 * This function destroys a queue, as detailed in @wq by sending an mailbox
15650 * command, specific to the type of queue, to the HBA.
15651 *
15652 * The @wq struct is used to get the queue ID of the queue to destroy.
15653 *
15654 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015655 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015656 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015657int
James Smart4f774512009-05-22 14:52:35 -040015658lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15659{
15660 LPFC_MBOXQ_t *mbox;
15661 int rc, length, status = 0;
15662 uint32_t shdr_status, shdr_add_status;
15663 union lpfc_sli4_cfg_shdr *shdr;
15664
James Smart2e90f4b2011-12-13 13:22:37 -050015665 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015666 if (!wq)
15667 return -ENODEV;
15668 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15669 if (!mbox)
15670 return -ENOMEM;
15671 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15672 sizeof(struct lpfc_sli4_cfg_mhdr));
15673 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15674 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15675 length, LPFC_SLI4_MBX_EMBED);
15676 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15677 wq->queue_id);
15678 mbox->vport = wq->phba->pport;
15679 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15680 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15681 shdr = (union lpfc_sli4_cfg_shdr *)
15682 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15683 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15684 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15685 if (shdr_status || shdr_add_status || rc) {
15686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15687 "2508 WQ_DESTROY mailbox failed with "
15688 "status x%x add_status x%x, mbx status x%x\n",
15689 shdr_status, shdr_add_status, rc);
15690 status = -ENXIO;
15691 }
15692 /* Remove wq from any list */
15693 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070015694 kfree(wq->pring);
15695 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040015696 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015697 return status;
15698}
15699
15700/**
15701 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15702 * @rq: The queue structure associated with the queue to destroy.
15703 *
15704 * This function destroys a queue, as detailed in @rq by sending an mailbox
15705 * command, specific to the type of queue, to the HBA.
15706 *
15707 * The @rq struct is used to get the queue ID of the queue to destroy.
15708 *
15709 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015710 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015711 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015712int
James Smart4f774512009-05-22 14:52:35 -040015713lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15714 struct lpfc_queue *drq)
15715{
15716 LPFC_MBOXQ_t *mbox;
15717 int rc, length, status = 0;
15718 uint32_t shdr_status, shdr_add_status;
15719 union lpfc_sli4_cfg_shdr *shdr;
15720
James Smart2e90f4b2011-12-13 13:22:37 -050015721 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015722 if (!hrq || !drq)
15723 return -ENODEV;
15724 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15725 if (!mbox)
15726 return -ENOMEM;
15727 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050015728 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040015729 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15730 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15731 length, LPFC_SLI4_MBX_EMBED);
15732 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15733 hrq->queue_id);
15734 mbox->vport = hrq->phba->pport;
15735 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15736 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15737 /* The IOCTL status is embedded in the mailbox subheader. */
15738 shdr = (union lpfc_sli4_cfg_shdr *)
15739 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15740 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15741 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15742 if (shdr_status || shdr_add_status || rc) {
15743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15744 "2509 RQ_DESTROY mailbox failed with "
15745 "status x%x add_status x%x, mbx status x%x\n",
15746 shdr_status, shdr_add_status, rc);
15747 if (rc != MBX_TIMEOUT)
15748 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15749 return -ENXIO;
15750 }
15751 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15752 drq->queue_id);
15753 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15754 shdr = (union lpfc_sli4_cfg_shdr *)
15755 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15756 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15757 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15758 if (shdr_status || shdr_add_status || rc) {
15759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15760 "2510 RQ_DESTROY mailbox failed with "
15761 "status x%x add_status x%x, mbx status x%x\n",
15762 shdr_status, shdr_add_status, rc);
15763 status = -ENXIO;
15764 }
15765 list_del_init(&hrq->list);
15766 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040015767 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015768 return status;
15769}
15770
15771/**
15772 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15773 * @phba: The virtual port for which this call being executed.
15774 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15775 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15776 * @xritag: the xritag that ties this io to the SGL pages.
15777 *
15778 * This routine will post the sgl pages for the IO that has the xritag
15779 * that is in the iocbq structure. The xritag is assigned during iocbq
15780 * creation and persists for as long as the driver is loaded.
15781 * if the caller has fewer than 256 scatter gather segments to map then
15782 * pdma_phys_addr1 should be 0.
15783 * If the caller needs to map more than 256 scatter gather segment then
15784 * pdma_phys_addr1 should be a valid physical address.
15785 * physical address for SGLs must be 64 byte aligned.
15786 * If you are going to map 2 SGL's then the first one must have 256 entries
15787 * the second sgl can have between 1 and 256 entries.
15788 *
15789 * Return codes:
15790 * 0 - Success
15791 * -ENXIO, -ENOMEM - Failure
15792 **/
15793int
15794lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15795 dma_addr_t pdma_phys_addr0,
15796 dma_addr_t pdma_phys_addr1,
15797 uint16_t xritag)
15798{
15799 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15800 LPFC_MBOXQ_t *mbox;
15801 int rc;
15802 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040015803 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040015804 union lpfc_sli4_cfg_shdr *shdr;
15805
15806 if (xritag == NO_XRI) {
15807 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15808 "0364 Invalid param:\n");
15809 return -EINVAL;
15810 }
15811
15812 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15813 if (!mbox)
15814 return -ENOMEM;
15815
15816 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15817 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15818 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050015819 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040015820
15821 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15822 &mbox->u.mqe.un.post_sgl_pages;
15823 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15824 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15825
15826 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15827 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15828 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15829 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15830
15831 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15832 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15833 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15834 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15835 if (!phba->sli4_hba.intr_enable)
15836 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040015837 else {
James Smarta183a152011-10-10 21:32:43 -040015838 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040015839 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15840 }
James Smart4f774512009-05-22 14:52:35 -040015841 /* The IOCTL status is embedded in the mailbox subheader. */
15842 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15843 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15844 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15845 if (rc != MBX_TIMEOUT)
15846 mempool_free(mbox, phba->mbox_mem_pool);
15847 if (shdr_status || shdr_add_status || rc) {
15848 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15849 "2511 POST_SGL mailbox failed with "
15850 "status x%x add_status x%x, mbx status x%x\n",
15851 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040015852 }
15853 return 0;
15854}
James Smart4f774512009-05-22 14:52:35 -040015855
15856/**
James Smart88a2cfb2011-07-22 18:36:33 -040015857 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040015858 * @phba: pointer to lpfc hba data structure.
15859 *
15860 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040015861 * HBA consistent with the SLI-4 interface spec. This routine
15862 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15863 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040015864 *
James Smart88a2cfb2011-07-22 18:36:33 -040015865 * Returns
15866 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15867 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15868 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015869static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015870lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15871{
15872 unsigned long xri;
15873
15874 /*
15875 * Fetch the next logical xri. Because this index is logical,
15876 * the driver starts at 0 each time.
15877 */
15878 spin_lock_irq(&phba->hbalock);
15879 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15880 phba->sli4_hba.max_cfg_param.max_xri, 0);
15881 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15882 spin_unlock_irq(&phba->hbalock);
15883 return NO_XRI;
15884 } else {
15885 set_bit(xri, phba->sli4_hba.xri_bmask);
15886 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015887 }
James Smart6d368e52011-05-24 11:44:12 -040015888 spin_unlock_irq(&phba->hbalock);
15889 return xri;
15890}
15891
15892/**
15893 * lpfc_sli4_free_xri - Release an xri for reuse.
15894 * @phba: pointer to lpfc hba data structure.
15895 *
15896 * This routine is invoked to release an xri to the pool of
15897 * available rpis maintained by the driver.
15898 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015899static void
James Smart6d368e52011-05-24 11:44:12 -040015900__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15901{
15902 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040015903 phba->sli4_hba.max_cfg_param.xri_used--;
15904 }
15905}
15906
15907/**
15908 * lpfc_sli4_free_xri - Release an xri for reuse.
15909 * @phba: pointer to lpfc hba data structure.
15910 *
15911 * This routine is invoked to release an xri to the pool of
15912 * available rpis maintained by the driver.
15913 **/
15914void
15915lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15916{
15917 spin_lock_irq(&phba->hbalock);
15918 __lpfc_sli4_free_xri(phba, xri);
15919 spin_unlock_irq(&phba->hbalock);
15920}
15921
15922/**
James Smart4f774512009-05-22 14:52:35 -040015923 * lpfc_sli4_next_xritag - Get an xritag for the io
15924 * @phba: Pointer to HBA context object.
15925 *
15926 * This function gets an xritag for the iocb. If there is no unused xritag
15927 * it will return 0xffff.
15928 * The function returns the allocated xritag if successful, else returns zero.
15929 * Zero is not a valid xritag.
15930 * The caller is not required to hold any lock.
15931 **/
15932uint16_t
15933lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15934{
James Smart6d368e52011-05-24 11:44:12 -040015935 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040015936
James Smart6d368e52011-05-24 11:44:12 -040015937 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040015938 if (xri_index == NO_XRI)
15939 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15940 "2004 Failed to allocate XRI.last XRITAG is %d"
15941 " Max XRI is %d, Used XRI is %d\n",
15942 xri_index,
15943 phba->sli4_hba.max_cfg_param.max_xri,
15944 phba->sli4_hba.max_cfg_param.xri_used);
15945 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040015946}
15947
15948/**
James Smart895427b2017-02-12 13:52:30 -080015949 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040015950 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040015951 * @post_sgl_list: pointer to els sgl entry list.
15952 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040015953 *
15954 * This routine is invoked to post a block of driver's sgl pages to the
15955 * HBA using non-embedded mailbox command. No Lock is held. This routine
15956 * is only called when the driver is loading and after all IO has been
15957 * stopped.
15958 **/
James Smart8a9d2e82012-05-09 21:16:12 -040015959static int
James Smart895427b2017-02-12 13:52:30 -080015960lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040015961 struct list_head *post_sgl_list,
15962 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040015963{
James Smart8a9d2e82012-05-09 21:16:12 -040015964 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040015965 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15966 struct sgl_page_pairs *sgl_pg_pairs;
15967 void *viraddr;
15968 LPFC_MBOXQ_t *mbox;
15969 uint32_t reqlen, alloclen, pg_pairs;
15970 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040015971 uint16_t xritag_start = 0;
15972 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040015973 uint32_t shdr_status, shdr_add_status;
15974 union lpfc_sli4_cfg_shdr *shdr;
15975
James Smart895427b2017-02-12 13:52:30 -080015976 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015977 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015978 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080015979 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040015980 "2559 Block sgl registration required DMA "
15981 "size (%d) great than a page\n", reqlen);
15982 return -ENOMEM;
15983 }
James Smart895427b2017-02-12 13:52:30 -080015984
James Smart4f774512009-05-22 14:52:35 -040015985 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040015986 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040015987 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040015988
15989 /* Allocate DMA memory and set up the non-embedded mailbox command */
15990 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15991 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15992 LPFC_SLI4_MBX_NEMBED);
15993
15994 if (alloclen < reqlen) {
15995 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15996 "0285 Allocated DMA memory size (%d) is "
15997 "less than the requested DMA memory "
15998 "size (%d)\n", alloclen, reqlen);
15999 lpfc_sli4_mbox_cmd_free(phba, mbox);
16000 return -ENOMEM;
16001 }
James Smart4f774512009-05-22 14:52:35 -040016002 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016003 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016004 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16005 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16006
James Smart8a9d2e82012-05-09 21:16:12 -040016007 pg_pairs = 0;
16008 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016009 /* Set up the sge entry */
16010 sgl_pg_pairs->sgl_pg0_addr_lo =
16011 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16012 sgl_pg_pairs->sgl_pg0_addr_hi =
16013 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16014 sgl_pg_pairs->sgl_pg1_addr_lo =
16015 cpu_to_le32(putPaddrLow(0));
16016 sgl_pg_pairs->sgl_pg1_addr_hi =
16017 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016018
James Smart4f774512009-05-22 14:52:35 -040016019 /* Keep the first xritag on the list */
16020 if (pg_pairs == 0)
16021 xritag_start = sglq_entry->sli4_xritag;
16022 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016023 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016024 }
James Smart6d368e52011-05-24 11:44:12 -040016025
16026 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016027 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016028 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016029 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016030
James Smart4f774512009-05-22 14:52:35 -040016031 if (!phba->sli4_hba.intr_enable)
16032 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16033 else {
James Smarta183a152011-10-10 21:32:43 -040016034 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016035 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16036 }
16037 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16038 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16039 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16040 if (rc != MBX_TIMEOUT)
16041 lpfc_sli4_mbox_cmd_free(phba, mbox);
16042 if (shdr_status || shdr_add_status || rc) {
16043 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16044 "2513 POST_SGL_BLOCK mailbox command failed "
16045 "status x%x add_status x%x mbx status x%x\n",
16046 shdr_status, shdr_add_status, rc);
16047 rc = -ENXIO;
16048 }
16049 return rc;
16050}
16051
16052/**
16053 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16054 * @phba: pointer to lpfc hba data structure.
16055 * @sblist: pointer to scsi buffer list.
16056 * @count: number of scsi buffers on the list.
16057 *
16058 * This routine is invoked to post a block of @count scsi sgl pages from a
16059 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16060 * No Lock is held.
16061 *
16062 **/
16063int
James Smart8a9d2e82012-05-09 21:16:12 -040016064lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16065 struct list_head *sblist,
16066 int count)
James Smart4f774512009-05-22 14:52:35 -040016067{
16068 struct lpfc_scsi_buf *psb;
16069 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16070 struct sgl_page_pairs *sgl_pg_pairs;
16071 void *viraddr;
16072 LPFC_MBOXQ_t *mbox;
16073 uint32_t reqlen, alloclen, pg_pairs;
16074 uint32_t mbox_tmo;
16075 uint16_t xritag_start = 0;
16076 int rc = 0;
16077 uint32_t shdr_status, shdr_add_status;
16078 dma_addr_t pdma_phys_bpl1;
16079 union lpfc_sli4_cfg_shdr *shdr;
16080
16081 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016082 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016083 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016084 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016085 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16086 "0217 Block sgl registration required DMA "
16087 "size (%d) great than a page\n", reqlen);
16088 return -ENOMEM;
16089 }
16090 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16091 if (!mbox) {
16092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16093 "0283 Failed to allocate mbox cmd memory\n");
16094 return -ENOMEM;
16095 }
16096
16097 /* Allocate DMA memory and set up the non-embedded mailbox command */
16098 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16099 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16100 LPFC_SLI4_MBX_NEMBED);
16101
16102 if (alloclen < reqlen) {
16103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16104 "2561 Allocated DMA memory size (%d) is "
16105 "less than the requested DMA memory "
16106 "size (%d)\n", alloclen, reqlen);
16107 lpfc_sli4_mbox_cmd_free(phba, mbox);
16108 return -ENOMEM;
16109 }
James Smart6d368e52011-05-24 11:44:12 -040016110
James Smart4f774512009-05-22 14:52:35 -040016111 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016112 viraddr = mbox->sge_array->addr[0];
16113
16114 /* Set up the SGL pages in the non-embedded DMA pages */
16115 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16116 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16117
16118 pg_pairs = 0;
16119 list_for_each_entry(psb, sblist, list) {
16120 /* Set up the sge entry */
16121 sgl_pg_pairs->sgl_pg0_addr_lo =
16122 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16123 sgl_pg_pairs->sgl_pg0_addr_hi =
16124 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16125 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16126 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16127 else
16128 pdma_phys_bpl1 = 0;
16129 sgl_pg_pairs->sgl_pg1_addr_lo =
16130 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16131 sgl_pg_pairs->sgl_pg1_addr_hi =
16132 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16133 /* Keep the first xritag on the list */
16134 if (pg_pairs == 0)
16135 xritag_start = psb->cur_iocbq.sli4_xritag;
16136 sgl_pg_pairs++;
16137 pg_pairs++;
16138 }
16139 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16140 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16141 /* Perform endian conversion if necessary */
16142 sgl->word0 = cpu_to_le32(sgl->word0);
16143
16144 if (!phba->sli4_hba.intr_enable)
16145 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16146 else {
James Smarta183a152011-10-10 21:32:43 -040016147 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016148 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16149 }
16150 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16151 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16152 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16153 if (rc != MBX_TIMEOUT)
16154 lpfc_sli4_mbox_cmd_free(phba, mbox);
16155 if (shdr_status || shdr_add_status || rc) {
16156 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16157 "2564 POST_SGL_BLOCK mailbox command failed "
16158 "status x%x add_status x%x mbx status x%x\n",
16159 shdr_status, shdr_add_status, rc);
16160 rc = -ENXIO;
16161 }
16162 return rc;
16163}
16164
16165/**
16166 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16167 * @phba: pointer to lpfc_hba struct that the frame was received on
16168 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16169 *
16170 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16171 * valid type of frame that the LPFC driver will handle. This function will
16172 * return a zero if the frame is a valid frame or a non zero value when the
16173 * frame does not pass the check.
16174 **/
16175static int
16176lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16177{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016178 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016179 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016180 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016181
James Smartae9e28f2017-05-15 15:20:51 -070016182#define FC_RCTL_MDS_DIAGS 0xF4
16183
James Smart4f774512009-05-22 14:52:35 -040016184 switch (fc_hdr->fh_r_ctl) {
16185 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16186 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16187 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16188 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16189 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16190 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16191 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16192 case FC_RCTL_DD_CMD_STATUS: /* command status */
16193 case FC_RCTL_ELS_REQ: /* extended link services request */
16194 case FC_RCTL_ELS_REP: /* extended link services reply */
16195 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16196 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16197 case FC_RCTL_BA_NOP: /* basic link service NOP */
16198 case FC_RCTL_BA_ABTS: /* basic link service abort */
16199 case FC_RCTL_BA_RMC: /* remove connection */
16200 case FC_RCTL_BA_ACC: /* basic accept */
16201 case FC_RCTL_BA_RJT: /* basic reject */
16202 case FC_RCTL_BA_PRMT:
16203 case FC_RCTL_ACK_1: /* acknowledge_1 */
16204 case FC_RCTL_ACK_0: /* acknowledge_0 */
16205 case FC_RCTL_P_RJT: /* port reject */
16206 case FC_RCTL_F_RJT: /* fabric reject */
16207 case FC_RCTL_P_BSY: /* port busy */
16208 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16209 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16210 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016211 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016212 case FC_RCTL_END: /* end */
16213 break;
16214 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16215 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16216 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16217 return lpfc_fc_frame_check(phba, fc_hdr);
16218 default:
16219 goto drop;
16220 }
James Smartae9e28f2017-05-15 15:20:51 -070016221
16222#define FC_TYPE_VENDOR_UNIQUE 0xFF
16223
James Smart4f774512009-05-22 14:52:35 -040016224 switch (fc_hdr->fh_type) {
16225 case FC_TYPE_BLS:
16226 case FC_TYPE_ELS:
16227 case FC_TYPE_FCP:
16228 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016229 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016230 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016231 break;
16232 case FC_TYPE_IP:
16233 case FC_TYPE_ILS:
16234 default:
16235 goto drop;
16236 }
James Smart546fc852011-03-11 16:06:29 -050016237
James Smart4f774512009-05-22 14:52:35 -040016238 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016239 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016240 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016241 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16242 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16243 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16244 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16245 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016246 return 0;
16247drop:
16248 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016249 "2539 Dropped frame rctl:x%x type:x%x\n",
16250 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016251 return 1;
16252}
16253
16254/**
16255 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16256 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16257 *
16258 * This function processes the FC header to retrieve the VFI from the VF
16259 * header, if one exists. This function will return the VFI if one exists
16260 * or 0 if no VSAN Header exists.
16261 **/
16262static uint32_t
16263lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16264{
16265 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16266
16267 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16268 return 0;
16269 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16270}
16271
16272/**
16273 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16274 * @phba: Pointer to the HBA structure to search for the vport on
16275 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16276 * @fcfi: The FC Fabric ID that the frame came from
16277 *
16278 * This function searches the @phba for a vport that matches the content of the
16279 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16280 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16281 * returns the matching vport pointer or NULL if unable to match frame to a
16282 * vport.
16283 **/
16284static struct lpfc_vport *
16285lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016286 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016287{
16288 struct lpfc_vport **vports;
16289 struct lpfc_vport *vport = NULL;
16290 int i;
James Smart939723a2012-05-09 21:19:03 -040016291
James Smartbf086112011-08-21 21:48:13 -040016292 if (did == Fabric_DID)
16293 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016294 if ((phba->pport->fc_flag & FC_PT2PT) &&
16295 !(phba->link_state == LPFC_HBA_READY))
16296 return phba->pport;
16297
James Smart4f774512009-05-22 14:52:35 -040016298 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016299 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016300 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16301 if (phba->fcf.fcfi == fcfi &&
16302 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16303 vports[i]->fc_myDID == did) {
16304 vport = vports[i];
16305 break;
16306 }
16307 }
James Smart895427b2017-02-12 13:52:30 -080016308 }
James Smart4f774512009-05-22 14:52:35 -040016309 lpfc_destroy_vport_work_array(phba, vports);
16310 return vport;
16311}
16312
16313/**
James Smart45ed1192009-10-02 15:17:02 -040016314 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16315 * @vport: The vport to work on.
16316 *
16317 * This function updates the receive sequence time stamp for this vport. The
16318 * receive sequence time stamp indicates the time that the last frame of the
16319 * the sequence that has been idle for the longest amount of time was received.
16320 * the driver uses this time stamp to indicate if any received sequences have
16321 * timed out.
16322 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016323static void
James Smart45ed1192009-10-02 15:17:02 -040016324lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16325{
16326 struct lpfc_dmabuf *h_buf;
16327 struct hbq_dmabuf *dmabuf = NULL;
16328
16329 /* get the oldest sequence on the rcv list */
16330 h_buf = list_get_first(&vport->rcv_buffer_list,
16331 struct lpfc_dmabuf, list);
16332 if (!h_buf)
16333 return;
16334 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16335 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16336}
16337
16338/**
16339 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16340 * @vport: The vport that the received sequences were sent to.
16341 *
16342 * This function cleans up all outstanding received sequences. This is called
16343 * by the driver when a link event or user action invalidates all the received
16344 * sequences.
16345 **/
16346void
16347lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16348{
16349 struct lpfc_dmabuf *h_buf, *hnext;
16350 struct lpfc_dmabuf *d_buf, *dnext;
16351 struct hbq_dmabuf *dmabuf = NULL;
16352
16353 /* start with the oldest sequence on the rcv list */
16354 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16355 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16356 list_del_init(&dmabuf->hbuf.list);
16357 list_for_each_entry_safe(d_buf, dnext,
16358 &dmabuf->dbuf.list, list) {
16359 list_del_init(&d_buf->list);
16360 lpfc_in_buf_free(vport->phba, d_buf);
16361 }
16362 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16363 }
16364}
16365
16366/**
16367 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16368 * @vport: The vport that the received sequences were sent to.
16369 *
16370 * This function determines whether any received sequences have timed out by
16371 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16372 * indicates that there is at least one timed out sequence this routine will
16373 * go through the received sequences one at a time from most inactive to most
16374 * active to determine which ones need to be cleaned up. Once it has determined
16375 * that a sequence needs to be cleaned up it will simply free up the resources
16376 * without sending an abort.
16377 **/
16378void
16379lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16380{
16381 struct lpfc_dmabuf *h_buf, *hnext;
16382 struct lpfc_dmabuf *d_buf, *dnext;
16383 struct hbq_dmabuf *dmabuf = NULL;
16384 unsigned long timeout;
16385 int abort_count = 0;
16386
16387 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16388 vport->rcv_buffer_time_stamp);
16389 if (list_empty(&vport->rcv_buffer_list) ||
16390 time_before(jiffies, timeout))
16391 return;
16392 /* start with the oldest sequence on the rcv list */
16393 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16394 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16395 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16396 dmabuf->time_stamp);
16397 if (time_before(jiffies, timeout))
16398 break;
16399 abort_count++;
16400 list_del_init(&dmabuf->hbuf.list);
16401 list_for_each_entry_safe(d_buf, dnext,
16402 &dmabuf->dbuf.list, list) {
16403 list_del_init(&d_buf->list);
16404 lpfc_in_buf_free(vport->phba, d_buf);
16405 }
16406 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16407 }
16408 if (abort_count)
16409 lpfc_update_rcv_time_stamp(vport);
16410}
16411
16412/**
James Smart4f774512009-05-22 14:52:35 -040016413 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16414 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16415 *
16416 * This function searches through the existing incomplete sequences that have
16417 * been sent to this @vport. If the frame matches one of the incomplete
16418 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16419 * make up that sequence. If no sequence is found that matches this frame then
16420 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16421 * This function returns a pointer to the first dmabuf in the sequence list that
16422 * the frame was linked to.
16423 **/
16424static struct hbq_dmabuf *
16425lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16426{
16427 struct fc_frame_header *new_hdr;
16428 struct fc_frame_header *temp_hdr;
16429 struct lpfc_dmabuf *d_buf;
16430 struct lpfc_dmabuf *h_buf;
16431 struct hbq_dmabuf *seq_dmabuf = NULL;
16432 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016433 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016434
James Smart4d9ab992009-10-02 15:16:39 -040016435 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016436 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016437 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016438
James Smart4f774512009-05-22 14:52:35 -040016439 /* Use the hdr_buf to find the sequence that this frame belongs to */
16440 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16441 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16442 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16443 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16444 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16445 continue;
16446 /* found a pending sequence that matches this frame */
16447 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16448 break;
16449 }
16450 if (!seq_dmabuf) {
16451 /*
16452 * This indicates first frame received for this sequence.
16453 * Queue the buffer on the vport's rcv_buffer_list.
16454 */
16455 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016456 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016457 return dmabuf;
16458 }
16459 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016460 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16461 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016462 list_del_init(&seq_dmabuf->hbuf.list);
16463 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16464 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016465 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016466 return dmabuf;
16467 }
James Smart45ed1192009-10-02 15:17:02 -040016468 /* move this sequence to the tail to indicate a young sequence */
16469 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16470 seq_dmabuf->time_stamp = jiffies;
16471 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016472 if (list_empty(&seq_dmabuf->dbuf.list)) {
16473 temp_hdr = dmabuf->hbuf.virt;
16474 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16475 return seq_dmabuf;
16476 }
James Smart4f774512009-05-22 14:52:35 -040016477 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016478 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16479 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016480 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16481 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16482 /*
16483 * If the frame's sequence count is greater than the frame on
16484 * the list then insert the frame right after this frame
16485 */
James Smarteeead812009-12-21 17:01:23 -050016486 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16487 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016488 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016489 found = 1;
16490 break;
James Smart4f774512009-05-22 14:52:35 -040016491 }
James Smart4360ca92015-12-16 18:12:04 -050016492
16493 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16494 break;
16495 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016496 }
James Smart4360ca92015-12-16 18:12:04 -050016497
16498 if (found)
16499 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016500 return NULL;
16501}
16502
16503/**
James Smart6669f9b2009-10-02 15:16:45 -040016504 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16505 * @vport: pointer to a vitural port
16506 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16507 *
16508 * This function tries to abort from the partially assembed sequence, described
16509 * by the information from basic abbort @dmabuf. It checks to see whether such
16510 * partially assembled sequence held by the driver. If so, it shall free up all
16511 * the frames from the partially assembled sequence.
16512 *
16513 * Return
16514 * true -- if there is matching partially assembled sequence present and all
16515 * the frames freed with the sequence;
16516 * false -- if there is no matching partially assembled sequence present so
16517 * nothing got aborted in the lower layer driver
16518 **/
16519static bool
16520lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16521 struct hbq_dmabuf *dmabuf)
16522{
16523 struct fc_frame_header *new_hdr;
16524 struct fc_frame_header *temp_hdr;
16525 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16526 struct hbq_dmabuf *seq_dmabuf = NULL;
16527
16528 /* Use the hdr_buf to find the sequence that matches this frame */
16529 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16530 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16531 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16532 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16533 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16534 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16535 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16536 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16537 continue;
16538 /* found a pending sequence that matches this frame */
16539 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16540 break;
16541 }
16542
16543 /* Free up all the frames from the partially assembled sequence */
16544 if (seq_dmabuf) {
16545 list_for_each_entry_safe(d_buf, n_buf,
16546 &seq_dmabuf->dbuf.list, list) {
16547 list_del_init(&d_buf->list);
16548 lpfc_in_buf_free(vport->phba, d_buf);
16549 }
16550 return true;
16551 }
16552 return false;
16553}
16554
16555/**
James Smart6dd9e312013-01-03 15:43:37 -050016556 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16557 * @vport: pointer to a vitural port
16558 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16559 *
16560 * This function tries to abort from the assembed sequence from upper level
16561 * protocol, described by the information from basic abbort @dmabuf. It
16562 * checks to see whether such pending context exists at upper level protocol.
16563 * If so, it shall clean up the pending context.
16564 *
16565 * Return
16566 * true -- if there is matching pending context of the sequence cleaned
16567 * at ulp;
16568 * false -- if there is no matching pending context of the sequence present
16569 * at ulp.
16570 **/
16571static bool
16572lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16573{
16574 struct lpfc_hba *phba = vport->phba;
16575 int handled;
16576
16577 /* Accepting abort at ulp with SLI4 only */
16578 if (phba->sli_rev < LPFC_SLI_REV4)
16579 return false;
16580
16581 /* Register all caring upper level protocols to attend abort */
16582 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16583 if (handled)
16584 return true;
16585
16586 return false;
16587}
16588
16589/**
James Smart546fc852011-03-11 16:06:29 -050016590 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016591 * @phba: Pointer to HBA context object.
16592 * @cmd_iocbq: pointer to the command iocbq structure.
16593 * @rsp_iocbq: pointer to the response iocbq structure.
16594 *
James Smart546fc852011-03-11 16:06:29 -050016595 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016596 * event. It properly releases the memory allocated to the sequence abort
16597 * accept iocb.
16598 **/
16599static void
James Smart546fc852011-03-11 16:06:29 -050016600lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016601 struct lpfc_iocbq *cmd_iocbq,
16602 struct lpfc_iocbq *rsp_iocbq)
16603{
James Smart6dd9e312013-01-03 15:43:37 -050016604 struct lpfc_nodelist *ndlp;
16605
16606 if (cmd_iocbq) {
16607 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16608 lpfc_nlp_put(ndlp);
16609 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016610 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016611 }
James Smart6b5151f2012-01-18 16:24:06 -050016612
16613 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16614 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16615 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16616 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16617 rsp_iocbq->iocb.ulpStatus,
16618 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016619}
16620
16621/**
James Smart6d368e52011-05-24 11:44:12 -040016622 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16623 * @phba: Pointer to HBA context object.
16624 * @xri: xri id in transaction.
16625 *
16626 * This function validates the xri maps to the known range of XRIs allocated an
16627 * used by the driver.
16628 **/
James Smart7851fe22011-07-22 18:36:52 -040016629uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016630lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16631 uint16_t xri)
16632{
James Smarta2fc4aef2014-09-03 12:57:55 -040016633 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016634
16635 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16636 if (xri == phba->sli4_hba.xri_ids[i])
16637 return i;
16638 }
16639 return NO_XRI;
16640}
16641
James Smart6d368e52011-05-24 11:44:12 -040016642/**
James Smart546fc852011-03-11 16:06:29 -050016643 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016644 * @phba: Pointer to HBA context object.
16645 * @fc_hdr: pointer to a FC frame header.
16646 *
James Smart546fc852011-03-11 16:06:29 -050016647 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016648 * event after aborting the sequence handling.
16649 **/
James Smart86c67372017-04-21 16:05:04 -070016650void
James Smart6dd9e312013-01-03 15:43:37 -050016651lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16652 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016653{
James Smart6dd9e312013-01-03 15:43:37 -050016654 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040016655 struct lpfc_iocbq *ctiocb = NULL;
16656 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040016657 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050016658 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040016659 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050016660 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040016661
16662 if (!lpfc_is_link_up(phba))
16663 return;
16664
16665 sid = sli4_sid_from_fc_hdr(fc_hdr);
16666 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050016667 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040016668
James Smart6dd9e312013-01-03 15:43:37 -050016669 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040016670 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070016671 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050016672 if (!ndlp) {
16673 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16674 "1268 Failed to allocate ndlp for "
16675 "oxid:x%x SID:x%x\n", oxid, sid);
16676 return;
16677 }
James Smart6dd9e312013-01-03 15:43:37 -050016678 /* Put ndlp onto pport node list */
16679 lpfc_enqueue_node(vport, ndlp);
16680 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16681 /* re-setup ndlp without removing from node list */
16682 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16683 if (!ndlp) {
16684 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16685 "3275 Failed to active ndlp found "
16686 "for oxid:x%x SID:x%x\n", oxid, sid);
16687 return;
16688 }
James Smart6669f9b2009-10-02 15:16:45 -040016689 }
16690
James Smart546fc852011-03-11 16:06:29 -050016691 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040016692 ctiocb = lpfc_sli_get_iocbq(phba);
16693 if (!ctiocb)
16694 return;
16695
James Smart5ffc2662009-11-18 15:39:44 -050016696 /* Extract the F_CTL field from FC_HDR */
16697 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16698
James Smart6669f9b2009-10-02 15:16:45 -040016699 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040016700 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050016701 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040016702 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16703 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16704 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16705
16706 /* Fill in the rest of iocb fields */
16707 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16708 icmd->ulpBdeCount = 0;
16709 icmd->ulpLe = 1;
16710 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040016711 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050016712 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016713
James Smart6669f9b2009-10-02 15:16:45 -040016714 ctiocb->iocb_cmpl = NULL;
16715 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050016716 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040016717 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050016718 ctiocb->sli4_xritag = NO_XRI;
16719
James Smartee0f4fe2012-05-09 21:19:14 -040016720 if (fctl & FC_FC_EX_CTX)
16721 /* Exchange responder sent the abort so we
16722 * own the oxid.
16723 */
16724 xri = oxid;
16725 else
16726 xri = rxid;
16727 lxri = lpfc_sli4_xri_inrange(phba, xri);
16728 if (lxri != NO_XRI)
16729 lpfc_set_rrq_active(phba, ndlp, lxri,
16730 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050016731 /* For BA_ABTS from exchange responder, if the logical xri with
16732 * the oxid maps to the FCP XRI range, the port no longer has
16733 * that exchange context, send a BLS_RJT. Override the IOCB for
16734 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050016735 */
James Smart6dd9e312013-01-03 15:43:37 -050016736 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080016737 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050016738 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16739 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16740 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16741 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16742 }
16743
16744 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16745 * the driver no longer has that exchange, send a BLS_RJT. Override
16746 * the IOCB for a BA_RJT.
16747 */
16748 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050016749 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16750 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16751 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16752 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16753 }
James Smart6669f9b2009-10-02 15:16:45 -040016754
James Smart5ffc2662009-11-18 15:39:44 -050016755 if (fctl & FC_FC_EX_CTX) {
16756 /* ABTS sent by responder to CT exchange, construction
16757 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16758 * field and RX_ID from ABTS for RX_ID field.
16759 */
James Smart546fc852011-03-11 16:06:29 -050016760 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050016761 } else {
16762 /* ABTS sent by initiator to CT exchange, construction
16763 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050016764 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050016765 */
James Smart546fc852011-03-11 16:06:29 -050016766 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050016767 }
James Smartf09c3ac2012-03-01 22:33:29 -050016768 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050016769 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050016770
James Smart546fc852011-03-11 16:06:29 -050016771 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050016772 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16773 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16774 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050016775
16776 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16777 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050016778 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16779 "2925 Failed to issue CT ABTS RSP x%x on "
16780 "xri x%x, Data x%x\n",
16781 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16782 phba->link_state);
16783 lpfc_nlp_put(ndlp);
16784 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050016785 lpfc_sli_release_iocbq(phba, ctiocb);
16786 }
James Smart6669f9b2009-10-02 15:16:45 -040016787}
16788
16789/**
16790 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16791 * @vport: Pointer to the vport on which this sequence was received
16792 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16793 *
16794 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16795 * receive sequence is only partially assembed by the driver, it shall abort
16796 * the partially assembled frames for the sequence. Otherwise, if the
16797 * unsolicited receive sequence has been completely assembled and passed to
16798 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16799 * unsolicited sequence has been aborted. After that, it will issue a basic
16800 * accept to accept the abort.
16801 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016802static void
James Smart6669f9b2009-10-02 15:16:45 -040016803lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16804 struct hbq_dmabuf *dmabuf)
16805{
16806 struct lpfc_hba *phba = vport->phba;
16807 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050016808 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050016809 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040016810
James Smart6669f9b2009-10-02 15:16:45 -040016811 /* Make a copy of fc_hdr before the dmabuf being released */
16812 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050016813 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040016814
James Smart5ffc2662009-11-18 15:39:44 -050016815 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050016816 /* ABTS by responder to exchange, no cleanup needed */
16817 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050016818 } else {
James Smart6dd9e312013-01-03 15:43:37 -050016819 /* ABTS by initiator to exchange, need to do cleanup */
16820 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16821 if (aborted == false)
16822 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050016823 }
James Smart6dd9e312013-01-03 15:43:37 -050016824 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16825
James Smart86c67372017-04-21 16:05:04 -070016826 if (phba->nvmet_support) {
16827 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16828 return;
16829 }
16830
James Smart6dd9e312013-01-03 15:43:37 -050016831 /* Respond with BA_ACC or BA_RJT accordingly */
16832 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040016833}
16834
16835/**
James Smart4f774512009-05-22 14:52:35 -040016836 * lpfc_seq_complete - Indicates if a sequence is complete
16837 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16838 *
16839 * This function checks the sequence, starting with the frame described by
16840 * @dmabuf, to see if all the frames associated with this sequence are present.
16841 * the frames associated with this sequence are linked to the @dmabuf using the
16842 * dbuf list. This function looks for two major things. 1) That the first frame
16843 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16844 * set. 3) That there are no holes in the sequence count. The function will
16845 * return 1 when the sequence is complete, otherwise it will return 0.
16846 **/
16847static int
16848lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16849{
16850 struct fc_frame_header *hdr;
16851 struct lpfc_dmabuf *d_buf;
16852 struct hbq_dmabuf *seq_dmabuf;
16853 uint32_t fctl;
16854 int seq_count = 0;
16855
16856 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16857 /* make sure first fame of sequence has a sequence count of zero */
16858 if (hdr->fh_seq_cnt != seq_count)
16859 return 0;
16860 fctl = (hdr->fh_f_ctl[0] << 16 |
16861 hdr->fh_f_ctl[1] << 8 |
16862 hdr->fh_f_ctl[2]);
16863 /* If last frame of sequence we can return success. */
16864 if (fctl & FC_FC_END_SEQ)
16865 return 1;
16866 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16867 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16868 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16869 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050016870 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040016871 return 0;
16872 fctl = (hdr->fh_f_ctl[0] << 16 |
16873 hdr->fh_f_ctl[1] << 8 |
16874 hdr->fh_f_ctl[2]);
16875 /* If last frame of sequence we can return success. */
16876 if (fctl & FC_FC_END_SEQ)
16877 return 1;
16878 }
16879 return 0;
16880}
16881
16882/**
16883 * lpfc_prep_seq - Prep sequence for ULP processing
16884 * @vport: Pointer to the vport on which this sequence was received
16885 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16886 *
16887 * This function takes a sequence, described by a list of frames, and creates
16888 * a list of iocbq structures to describe the sequence. This iocbq list will be
16889 * used to issue to the generic unsolicited sequence handler. This routine
16890 * returns a pointer to the first iocbq in the list. If the function is unable
16891 * to allocate an iocbq then it throw out the received frames that were not
16892 * able to be described and return a pointer to the first iocbq. If unable to
16893 * allocate any iocbqs (including the first) this function will return NULL.
16894 **/
16895static struct lpfc_iocbq *
16896lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16897{
James Smart7851fe22011-07-22 18:36:52 -040016898 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016899 struct lpfc_dmabuf *d_buf, *n_buf;
16900 struct lpfc_iocbq *first_iocbq, *iocbq;
16901 struct fc_frame_header *fc_hdr;
16902 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040016903 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050016904 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040016905
16906 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16907 /* remove from receive buffer list */
16908 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016909 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016910 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040016911 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040016912 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016913 /* Get an iocbq struct to fill in. */
16914 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16915 if (first_iocbq) {
16916 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040016917 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016918 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080016919 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040016920
16921 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16922 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16923 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16924 first_iocbq->iocb.un.rcvels.parmRo =
16925 sli4_did_from_fc_hdr(fc_hdr);
16926 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16927 } else
16928 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040016929 first_iocbq->iocb.ulpContext = NO_XRI;
16930 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16931 be16_to_cpu(fc_hdr->fh_ox_id);
16932 /* iocbq is prepped for internal consumption. Physical vpi. */
16933 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16934 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040016935 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040016936 tot_len = bf_get(lpfc_rcqe_length,
16937 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16938
James Smart4f774512009-05-22 14:52:35 -040016939 first_iocbq->context2 = &seq_dmabuf->dbuf;
16940 first_iocbq->context3 = NULL;
16941 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040016942 if (tot_len > LPFC_DATA_BUF_SIZE)
16943 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040016944 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040016945 else
16946 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16947
James Smart4f774512009-05-22 14:52:35 -040016948 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040016949
James Smart7851fe22011-07-22 18:36:52 -040016950 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040016951 }
16952 iocbq = first_iocbq;
16953 /*
16954 * Each IOCBq can have two Buffers assigned, so go through the list
16955 * of buffers for this sequence and save two buffers in each IOCBq
16956 */
16957 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16958 if (!iocbq) {
16959 lpfc_in_buf_free(vport->phba, d_buf);
16960 continue;
16961 }
16962 if (!iocbq->context3) {
16963 iocbq->context3 = d_buf;
16964 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040016965 /* We need to get the size out of the right CQE */
16966 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16967 len = bf_get(lpfc_rcqe_length,
16968 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016969 pbde = (struct ulp_bde64 *)
16970 &iocbq->iocb.unsli3.sli3Words[4];
16971 if (len > LPFC_DATA_BUF_SIZE)
16972 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16973 else
16974 pbde->tus.f.bdeSize = len;
16975
James Smart7851fe22011-07-22 18:36:52 -040016976 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16977 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040016978 } else {
16979 iocbq = lpfc_sli_get_iocbq(vport->phba);
16980 if (!iocbq) {
16981 if (first_iocbq) {
16982 first_iocbq->iocb.ulpStatus =
16983 IOSTAT_FCP_RSP_ERROR;
16984 first_iocbq->iocb.un.ulpWord[4] =
16985 IOERR_NO_RESOURCES;
16986 }
16987 lpfc_in_buf_free(vport->phba, d_buf);
16988 continue;
16989 }
James Smart7851fe22011-07-22 18:36:52 -040016990 /* We need to get the size out of the right CQE */
16991 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16992 len = bf_get(lpfc_rcqe_length,
16993 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016994 iocbq->context2 = d_buf;
16995 iocbq->context3 = NULL;
16996 iocbq->iocb.ulpBdeCount = 1;
16997 if (len > LPFC_DATA_BUF_SIZE)
16998 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16999 LPFC_DATA_BUF_SIZE;
17000 else
17001 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17002
James Smart7851fe22011-07-22 18:36:52 -040017003 tot_len += len;
17004 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17005
James Smart4f774512009-05-22 14:52:35 -040017006 iocbq->iocb.un.rcvels.remoteID = sid;
17007 list_add_tail(&iocbq->list, &first_iocbq->list);
17008 }
17009 }
17010 return first_iocbq;
17011}
17012
James Smart6669f9b2009-10-02 15:16:45 -040017013static void
17014lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17015 struct hbq_dmabuf *seq_dmabuf)
17016{
17017 struct fc_frame_header *fc_hdr;
17018 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17019 struct lpfc_hba *phba = vport->phba;
17020
17021 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17022 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17023 if (!iocbq) {
17024 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17025 "2707 Ring %d handler: Failed to allocate "
17026 "iocb Rctl x%x Type x%x received\n",
17027 LPFC_ELS_RING,
17028 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17029 return;
17030 }
17031 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017032 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017033 iocbq, fc_hdr->fh_r_ctl,
17034 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017036 "2540 Ring %d handler: unexpected Rctl "
17037 "x%x Type x%x received\n",
17038 LPFC_ELS_RING,
17039 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17040
17041 /* Free iocb created in lpfc_prep_seq */
17042 list_for_each_entry_safe(curr_iocb, next_iocb,
17043 &iocbq->list, list) {
17044 list_del_init(&curr_iocb->list);
17045 lpfc_sli_release_iocbq(phba, curr_iocb);
17046 }
17047 lpfc_sli_release_iocbq(phba, iocbq);
17048}
17049
James Smartae9e28f2017-05-15 15:20:51 -070017050static void
17051lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17052 struct lpfc_iocbq *rspiocb)
17053{
17054 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17055
17056 if (pcmd && pcmd->virt)
17057 pci_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17058 kfree(pcmd);
17059 lpfc_sli_release_iocbq(phba, cmdiocb);
17060}
17061
17062static void
17063lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17064 struct hbq_dmabuf *dmabuf)
17065{
17066 struct fc_frame_header *fc_hdr;
17067 struct lpfc_hba *phba = vport->phba;
17068 struct lpfc_iocbq *iocbq = NULL;
17069 union lpfc_wqe *wqe;
17070 struct lpfc_dmabuf *pcmd = NULL;
17071 uint32_t frame_len;
17072 int rc;
17073
17074 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17075 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17076
17077 /* Send the received frame back */
17078 iocbq = lpfc_sli_get_iocbq(phba);
17079 if (!iocbq)
17080 goto exit;
17081
17082 /* Allocate buffer for command payload */
17083 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17084 if (pcmd)
17085 pcmd->virt = pci_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
17086 &pcmd->phys);
17087 if (!pcmd || !pcmd->virt)
17088 goto exit;
17089
17090 INIT_LIST_HEAD(&pcmd->list);
17091
17092 /* copyin the payload */
17093 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17094
17095 /* fill in BDE's for command */
17096 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17097 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17098 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17099 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17100
17101 iocbq->context2 = pcmd;
17102 iocbq->vport = vport;
17103 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17104 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17105
17106 /*
17107 * Setup rest of the iocb as though it were a WQE
17108 * Build the SEND_FRAME WQE
17109 */
17110 wqe = (union lpfc_wqe *)&iocbq->iocb;
17111
17112 wqe->send_frame.frame_len = frame_len;
17113 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17114 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17115 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17116 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17117 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17118 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17119
17120 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17121 iocbq->iocb.ulpLe = 1;
17122 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17123 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17124 if (rc == IOCB_ERROR)
17125 goto exit;
17126
17127 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17128 return;
17129
17130exit:
17131 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17132 "2023 Unable to process MDS loopback frame\n");
17133 if (pcmd && pcmd->virt)
17134 pci_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
17135 kfree(pcmd);
17136 lpfc_sli_release_iocbq(phba, iocbq);
17137 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17138}
17139
James Smart4f774512009-05-22 14:52:35 -040017140/**
17141 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17142 * @phba: Pointer to HBA context object.
17143 *
17144 * This function is called with no lock held. This function processes all
17145 * the received buffers and gives it to upper layers when a received buffer
17146 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017147 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017148 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17149 * appropriate receive function when the final frame in a sequence is received.
17150 **/
James Smart4d9ab992009-10-02 15:16:39 -040017151void
17152lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17153 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017154{
James Smart4d9ab992009-10-02 15:16:39 -040017155 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017156 struct fc_frame_header *fc_hdr;
17157 struct lpfc_vport *vport;
17158 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017159 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017160
James Smart4f774512009-05-22 14:52:35 -040017161 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017162 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017163
James Smart4d9ab992009-10-02 15:16:39 -040017164 /* check to see if this a valid type of frame */
17165 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17166 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17167 return;
17168 }
James Smart2ea259e2017-02-12 13:52:27 -080017169
James Smart7851fe22011-07-22 18:36:52 -040017170 if ((bf_get(lpfc_cqe_code,
17171 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17172 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17173 &dmabuf->cq_event.cqe.rcqe_cmpl);
17174 else
17175 fcfi = bf_get(lpfc_rcqe_fcf_id,
17176 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017177
James Smartae9e28f2017-05-15 15:20:51 -070017178 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17179 vport = phba->pport;
17180 /* Handle MDS Loopback frames */
17181 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17182 return;
17183 }
17184
James Smart895427b2017-02-12 13:52:30 -080017185 /* d_id this frame is directed to */
17186 did = sli4_did_from_fc_hdr(fc_hdr);
17187
17188 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017189 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017190 /* throw out the frame */
17191 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17192 return;
17193 }
James Smart939723a2012-05-09 21:19:03 -040017194
James Smart939723a2012-05-09 21:19:03 -040017195 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17196 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17197 (did != Fabric_DID)) {
17198 /*
17199 * Throw out the frame if we are not pt2pt.
17200 * The pt2pt protocol allows for discovery frames
17201 * to be received without a registered VPI.
17202 */
17203 if (!(vport->fc_flag & FC_PT2PT) ||
17204 (phba->link_state == LPFC_HBA_READY)) {
17205 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17206 return;
17207 }
17208 }
17209
James Smart6669f9b2009-10-02 15:16:45 -040017210 /* Handle the basic abort sequence (BA_ABTS) event */
17211 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17212 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17213 return;
17214 }
17215
James Smart4d9ab992009-10-02 15:16:39 -040017216 /* Link this frame */
17217 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17218 if (!seq_dmabuf) {
17219 /* unable to add frame to vport - throw it out */
17220 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17221 return;
17222 }
17223 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017224 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017225 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017226
James Smart6669f9b2009-10-02 15:16:45 -040017227 /* Send the complete sequence to the upper layer protocol */
17228 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017229}
James Smart6fb120a2009-05-22 14:52:59 -040017230
17231/**
17232 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17233 * @phba: pointer to lpfc hba data structure.
17234 *
17235 * This routine is invoked to post rpi header templates to the
17236 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017237 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17238 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017239 *
17240 * This routine does not require any locks. It's usage is expected
17241 * to be driver load or reset recovery when the driver is
17242 * sequential.
17243 *
17244 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017245 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017246 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017247 * When this error occurs, the driver is not guaranteed
17248 * to have any rpi regions posted to the device and
17249 * must either attempt to repost the regions or take a
17250 * fatal error.
17251 **/
17252int
17253lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17254{
17255 struct lpfc_rpi_hdr *rpi_page;
17256 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017257 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017258
James Smart6d368e52011-05-24 11:44:12 -040017259 /* SLI4 ports that support extents do not require RPI headers. */
17260 if (!phba->sli4_hba.rpi_hdrs_in_use)
17261 goto exit;
17262 if (phba->sli4_hba.extents_in_use)
17263 return -EIO;
17264
James Smart6fb120a2009-05-22 14:52:59 -040017265 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017266 /*
17267 * Assign the rpi headers a physical rpi only if the driver
17268 * has not initialized those resources. A port reset only
17269 * needs the headers posted.
17270 */
17271 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17272 LPFC_RPI_RSRC_RDY)
17273 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17274
James Smart6fb120a2009-05-22 14:52:59 -040017275 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17276 if (rc != MBX_SUCCESS) {
17277 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17278 "2008 Error %d posting all rpi "
17279 "headers\n", rc);
17280 rc = -EIO;
17281 break;
17282 }
17283 }
17284
James Smart6d368e52011-05-24 11:44:12 -040017285 exit:
17286 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17287 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017288 return rc;
17289}
17290
17291/**
17292 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17293 * @phba: pointer to lpfc hba data structure.
17294 * @rpi_page: pointer to the rpi memory region.
17295 *
17296 * This routine is invoked to post a single rpi header to the
17297 * HBA consistent with the SLI-4 interface spec. This memory region
17298 * maps up to 64 rpi context regions.
17299 *
17300 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017301 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017302 * -ENOMEM - No available memory
17303 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017304 **/
17305int
17306lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17307{
17308 LPFC_MBOXQ_t *mboxq;
17309 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17310 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017311 uint32_t shdr_status, shdr_add_status;
17312 union lpfc_sli4_cfg_shdr *shdr;
17313
James Smart6d368e52011-05-24 11:44:12 -040017314 /* SLI4 ports that support extents do not require RPI headers. */
17315 if (!phba->sli4_hba.rpi_hdrs_in_use)
17316 return rc;
17317 if (phba->sli4_hba.extents_in_use)
17318 return -EIO;
17319
James Smart6fb120a2009-05-22 14:52:59 -040017320 /* The port is notified of the header region via a mailbox command. */
17321 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17322 if (!mboxq) {
17323 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17324 "2001 Unable to allocate memory for issuing "
17325 "SLI_CONFIG_SPECIAL mailbox command\n");
17326 return -ENOMEM;
17327 }
17328
17329 /* Post all rpi memory regions to the port. */
17330 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017331 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17332 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17333 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017334 sizeof(struct lpfc_sli4_cfg_mhdr),
17335 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017336
17337
17338 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017339 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17340 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017341 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17342 hdr_tmpl, rpi_page->page_count);
17343
James Smart6fb120a2009-05-22 14:52:59 -040017344 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17345 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017346 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017347 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17348 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17349 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17350 if (rc != MBX_TIMEOUT)
17351 mempool_free(mboxq, phba->mbox_mem_pool);
17352 if (shdr_status || shdr_add_status || rc) {
17353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17354 "2514 POST_RPI_HDR mailbox failed with "
17355 "status x%x add_status x%x, mbx status x%x\n",
17356 shdr_status, shdr_add_status, rc);
17357 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017358 } else {
17359 /*
17360 * The next_rpi stores the next logical module-64 rpi value used
17361 * to post physical rpis in subsequent rpi postings.
17362 */
17363 spin_lock_irq(&phba->hbalock);
17364 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17365 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017366 }
17367 return rc;
17368}
17369
17370/**
17371 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17372 * @phba: pointer to lpfc hba data structure.
17373 *
17374 * This routine is invoked to post rpi header templates to the
17375 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017376 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17377 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017378 *
17379 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017380 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017381 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17382 **/
17383int
17384lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17385{
James Smart6d368e52011-05-24 11:44:12 -040017386 unsigned long rpi;
17387 uint16_t max_rpi, rpi_limit;
17388 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017389 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017390 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017391
James Smart6fb120a2009-05-22 14:52:59 -040017392 /*
James Smart6d368e52011-05-24 11:44:12 -040017393 * Fetch the next logical rpi. Because this index is logical,
17394 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017395 */
James Smart4902b382013-10-10 12:20:35 -040017396 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017397 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17398 rpi_limit = phba->sli4_hba.next_rpi;
17399
James Smart6d368e52011-05-24 11:44:12 -040017400 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17401 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017402 rpi = LPFC_RPI_ALLOC_ERROR;
17403 else {
17404 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17405 phba->sli4_hba.max_cfg_param.rpi_used++;
17406 phba->sli4_hba.rpi_count++;
17407 }
James Smartbe6bb942015-04-07 15:07:22 -040017408 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17409 "0001 rpi:%x max:%x lim:%x\n",
17410 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017411
17412 /*
17413 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017414 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017415 */
17416 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17417 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017418 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017419 return rpi;
17420 }
17421
17422 /*
James Smart6d368e52011-05-24 11:44:12 -040017423 * RPI header postings are not required for SLI4 ports capable of
17424 * extents.
17425 */
17426 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017427 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017428 return rpi;
17429 }
17430
17431 /*
James Smart6fb120a2009-05-22 14:52:59 -040017432 * If the driver is running low on rpi resources, allocate another
17433 * page now. Note that the next_rpi value is used because
17434 * it represents how many are actually in use whereas max_rpi notes
17435 * how many are supported max by the device.
17436 */
James Smart6d368e52011-05-24 11:44:12 -040017437 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017438 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017439 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17440 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17441 if (!rpi_hdr) {
17442 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17443 "2002 Error Could not grow rpi "
17444 "count\n");
17445 } else {
James Smart6d368e52011-05-24 11:44:12 -040017446 lrpi = rpi_hdr->start_rpi;
17447 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017448 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17449 }
17450 }
17451
17452 return rpi;
17453}
17454
17455/**
17456 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17457 * @phba: pointer to lpfc hba data structure.
17458 *
17459 * This routine is invoked to release an rpi to the pool of
17460 * available rpis maintained by the driver.
17461 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017462static void
James Smartd7c47992010-06-08 18:31:54 -040017463__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17464{
17465 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17466 phba->sli4_hba.rpi_count--;
17467 phba->sli4_hba.max_cfg_param.rpi_used--;
17468 }
17469}
17470
17471/**
17472 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17473 * @phba: pointer to lpfc hba data structure.
17474 *
17475 * This routine is invoked to release an rpi to the pool of
17476 * available rpis maintained by the driver.
17477 **/
17478void
James Smart6fb120a2009-05-22 14:52:59 -040017479lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17480{
17481 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017482 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017483 spin_unlock_irq(&phba->hbalock);
17484}
17485
17486/**
17487 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17488 * @phba: pointer to lpfc hba data structure.
17489 *
17490 * This routine is invoked to remove the memory region that
17491 * provided rpi via a bitmask.
17492 **/
17493void
17494lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17495{
17496 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017497 kfree(phba->sli4_hba.rpi_ids);
17498 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017499}
17500
17501/**
17502 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17503 * @phba: pointer to lpfc hba data structure.
17504 *
17505 * This routine is invoked to remove the memory region that
17506 * provided rpi via a bitmask.
17507 **/
17508int
James Smart6b5151f2012-01-18 16:24:06 -050017509lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17510 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017511{
17512 LPFC_MBOXQ_t *mboxq;
17513 struct lpfc_hba *phba = ndlp->phba;
17514 int rc;
17515
17516 /* The port is notified of the header region via a mailbox command. */
17517 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17518 if (!mboxq)
17519 return -ENOMEM;
17520
17521 /* Post all rpi memory regions to the port. */
17522 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017523 if (cmpl) {
17524 mboxq->mbox_cmpl = cmpl;
17525 mboxq->context1 = arg;
17526 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017527 } else
17528 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017529 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017530 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17531 if (rc == MBX_NOT_FINISHED) {
17532 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17533 "2010 Resume RPI Mailbox failed "
17534 "status %d, mbxStatus x%x\n", rc,
17535 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17536 mempool_free(mboxq, phba->mbox_mem_pool);
17537 return -EIO;
17538 }
17539 return 0;
17540}
17541
17542/**
17543 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017544 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017545 *
James Smart76a95d72010-11-20 23:11:48 -050017546 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017547 *
17548 * Returns:
17549 * 0 success
17550 * -Evalue otherwise
17551 **/
17552int
James Smart76a95d72010-11-20 23:11:48 -050017553lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017554{
17555 LPFC_MBOXQ_t *mboxq;
17556 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017557 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017558 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017559 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017560 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17561 if (!mboxq)
17562 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017563 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017564 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017565 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017566 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017567 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017568 "2022 INIT VPI Mailbox failed "
17569 "status %d, mbxStatus x%x\n", rc,
17570 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017571 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017572 }
James Smart6a9c52c2009-10-02 15:16:51 -040017573 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017574 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017575
17576 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017577}
17578
17579/**
17580 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17581 * @phba: pointer to lpfc hba data structure.
17582 * @mboxq: Pointer to mailbox object.
17583 *
17584 * This routine is invoked to manually add a single FCF record. The caller
17585 * must pass a completely initialized FCF_Record. This routine takes
17586 * care of the nonembedded mailbox operations.
17587 **/
17588static void
17589lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17590{
17591 void *virt_addr;
17592 union lpfc_sli4_cfg_shdr *shdr;
17593 uint32_t shdr_status, shdr_add_status;
17594
17595 virt_addr = mboxq->sge_array->addr[0];
17596 /* The IOCTL status is embedded in the mailbox subheader. */
17597 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17598 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17599 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17600
17601 if ((shdr_status || shdr_add_status) &&
17602 (shdr_status != STATUS_FCF_IN_USE))
17603 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17604 "2558 ADD_FCF_RECORD mailbox failed with "
17605 "status x%x add_status x%x\n",
17606 shdr_status, shdr_add_status);
17607
17608 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17609}
17610
17611/**
17612 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17613 * @phba: pointer to lpfc hba data structure.
17614 * @fcf_record: pointer to the initialized fcf record to add.
17615 *
17616 * This routine is invoked to manually add a single FCF record. The caller
17617 * must pass a completely initialized FCF_Record. This routine takes
17618 * care of the nonembedded mailbox operations.
17619 **/
17620int
17621lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17622{
17623 int rc = 0;
17624 LPFC_MBOXQ_t *mboxq;
17625 uint8_t *bytep;
17626 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017627 struct lpfc_mbx_sge sge;
17628 uint32_t alloc_len, req_len;
17629 uint32_t fcfindex;
17630
17631 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17632 if (!mboxq) {
17633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17634 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17635 return -ENOMEM;
17636 }
17637
17638 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17639 sizeof(uint32_t);
17640
17641 /* Allocate DMA memory and set up the non-embedded mailbox command */
17642 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17643 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17644 req_len, LPFC_SLI4_MBX_NEMBED);
17645 if (alloc_len < req_len) {
17646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17647 "2523 Allocated DMA memory size (x%x) is "
17648 "less than the requested DMA memory "
17649 "size (x%x)\n", alloc_len, req_len);
17650 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17651 return -ENOMEM;
17652 }
17653
17654 /*
17655 * Get the first SGE entry from the non-embedded DMA memory. This
17656 * routine only uses a single SGE.
17657 */
17658 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040017659 virt_addr = mboxq->sge_array->addr[0];
17660 /*
17661 * Configure the FCF record for FCFI 0. This is the driver's
17662 * hardcoded default and gets used in nonFIP mode.
17663 */
17664 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17665 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17666 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17667
17668 /*
17669 * Copy the fcf_index and the FCF Record Data. The data starts after
17670 * the FCoE header plus word10. The data copy needs to be endian
17671 * correct.
17672 */
17673 bytep += sizeof(uint32_t);
17674 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17675 mboxq->vport = phba->pport;
17676 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17677 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17678 if (rc == MBX_NOT_FINISHED) {
17679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17680 "2515 ADD_FCF_RECORD mailbox failed with "
17681 "status 0x%x\n", rc);
17682 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17683 rc = -EIO;
17684 } else
17685 rc = 0;
17686
17687 return rc;
17688}
17689
17690/**
17691 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17692 * @phba: pointer to lpfc hba data structure.
17693 * @fcf_record: pointer to the fcf record to write the default data.
17694 * @fcf_index: FCF table entry index.
17695 *
17696 * This routine is invoked to build the driver's default FCF record. The
17697 * values used are hardcoded. This routine handles memory initialization.
17698 *
17699 **/
17700void
17701lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17702 struct fcf_record *fcf_record,
17703 uint16_t fcf_index)
17704{
17705 memset(fcf_record, 0, sizeof(struct fcf_record));
17706 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17707 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17708 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17709 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17710 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17711 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17712 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17713 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17714 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17715 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17716 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17717 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17718 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040017719 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040017720 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17721 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17722 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17723 /* Set the VLAN bit map */
17724 if (phba->valid_vlan) {
17725 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17726 = 1 << (phba->vlan_id % 8);
17727 }
17728}
17729
17730/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017731 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040017732 * @phba: pointer to lpfc hba data structure.
17733 * @fcf_index: FCF table entry offset.
17734 *
James Smart0c9ab6f2010-02-26 14:15:57 -050017735 * This routine is invoked to scan the entire FCF table by reading FCF
17736 * record and processing it one at a time starting from the @fcf_index
17737 * for initial FCF discovery or fast FCF failover rediscovery.
17738 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017739 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017740 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040017741 **/
17742int
James Smart0c9ab6f2010-02-26 14:15:57 -050017743lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040017744{
17745 int rc = 0, error;
17746 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040017747
James Smart32b97932009-07-19 10:01:21 -040017748 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050017749 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040017750 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17751 if (!mboxq) {
17752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17753 "2000 Failed to allocate mbox for "
17754 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040017755 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050017756 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017757 }
James Smartecfd03c2010-02-12 14:41:27 -050017758 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050017759 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050017760 if (rc) {
17761 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050017762 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017763 }
James Smartecfd03c2010-02-12 14:41:27 -050017764 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040017765 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050017766 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040017767
17768 spin_lock_irq(&phba->hbalock);
17769 phba->hba_flag |= FCF_TS_INPROG;
17770 spin_unlock_irq(&phba->hbalock);
17771
James Smart6fb120a2009-05-22 14:52:59 -040017772 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050017773 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040017774 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050017775 else {
James Smart38b92ef2010-08-04 16:11:39 -040017776 /* Reset eligible FCF count for new scan */
17777 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040017778 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017779 error = 0;
James Smart32b97932009-07-19 10:01:21 -040017780 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017781fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040017782 if (error) {
17783 if (mboxq)
17784 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040017785 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040017786 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040017787 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040017788 spin_unlock_irq(&phba->hbalock);
17789 }
James Smart6fb120a2009-05-22 14:52:59 -040017790 return error;
17791}
James Smarta0c87cb2009-07-19 10:01:10 -040017792
17793/**
James Smarta93ff372010-10-22 11:06:08 -040017794 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050017795 * @phba: pointer to lpfc hba data structure.
17796 * @fcf_index: FCF table entry offset.
17797 *
17798 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040017799 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050017800 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017801 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017802 * otherwise.
17803 **/
17804int
17805lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17806{
17807 int rc = 0, error;
17808 LPFC_MBOXQ_t *mboxq;
17809
17810 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17811 if (!mboxq) {
17812 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17813 "2763 Failed to allocate mbox for "
17814 "READ_FCF cmd\n");
17815 error = -ENOMEM;
17816 goto fail_fcf_read;
17817 }
17818 /* Construct the read FCF record mailbox command */
17819 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17820 if (rc) {
17821 error = -EINVAL;
17822 goto fail_fcf_read;
17823 }
17824 /* Issue the mailbox command asynchronously */
17825 mboxq->vport = phba->pport;
17826 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17827 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17828 if (rc == MBX_NOT_FINISHED)
17829 error = -EIO;
17830 else
17831 error = 0;
17832
17833fail_fcf_read:
17834 if (error && mboxq)
17835 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17836 return error;
17837}
17838
17839/**
17840 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17841 * @phba: pointer to lpfc hba data structure.
17842 * @fcf_index: FCF table entry offset.
17843 *
17844 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040017845 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050017846 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017847 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017848 * otherwise.
17849 **/
17850int
17851lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17852{
17853 int rc = 0, error;
17854 LPFC_MBOXQ_t *mboxq;
17855
17856 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17857 if (!mboxq) {
17858 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17859 "2758 Failed to allocate mbox for "
17860 "READ_FCF cmd\n");
17861 error = -ENOMEM;
17862 goto fail_fcf_read;
17863 }
17864 /* Construct the read FCF record mailbox command */
17865 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17866 if (rc) {
17867 error = -EINVAL;
17868 goto fail_fcf_read;
17869 }
17870 /* Issue the mailbox command asynchronously */
17871 mboxq->vport = phba->pport;
17872 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17873 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17874 if (rc == MBX_NOT_FINISHED)
17875 error = -EIO;
17876 else
17877 error = 0;
17878
17879fail_fcf_read:
17880 if (error && mboxq)
17881 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17882 return error;
17883}
17884
17885/**
James Smartf5cb5302015-12-16 18:11:52 -050017886 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040017887 * phba pointer to the lpfc_hba struct for this port.
17888 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17889 * routine when the rr_bmask is empty. The FCF indecies are put into the
17890 * rr_bmask based on their priority level. Starting from the highest priority
17891 * to the lowest. The most likely FCF candidate will be in the highest
17892 * priority group. When this routine is called it searches the fcf_pri list for
17893 * next lowest priority group and repopulates the rr_bmask with only those
17894 * fcf_indexes.
17895 * returns:
17896 * 1=success 0=failure
17897 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017898static int
James Smart7d791df2011-07-22 18:37:52 -040017899lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17900{
17901 uint16_t next_fcf_pri;
17902 uint16_t last_index;
17903 struct lpfc_fcf_pri *fcf_pri;
17904 int rc;
17905 int ret = 0;
17906
17907 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
17908 LPFC_SLI4_FCF_TBL_INDX_MAX);
17909 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
17910 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050017911
17912 /* Verify the priority list has 2 or more entries */
17913 spin_lock_irq(&phba->hbalock);
17914 if (list_empty(&phba->fcf.fcf_pri_list) ||
17915 list_is_singular(&phba->fcf.fcf_pri_list)) {
17916 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040017917 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
17918 "3061 Last IDX %d\n", last_index);
17919 return 0; /* Empty rr list */
17920 }
James Smart25626692013-03-01 16:36:54 -050017921 spin_unlock_irq(&phba->hbalock);
17922
James Smart7d791df2011-07-22 18:37:52 -040017923 next_fcf_pri = 0;
17924 /*
17925 * Clear the rr_bmask and set all of the bits that are at this
17926 * priority.
17927 */
17928 memset(phba->fcf.fcf_rr_bmask, 0,
17929 sizeof(*phba->fcf.fcf_rr_bmask));
17930 spin_lock_irq(&phba->hbalock);
17931 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17932 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
17933 continue;
17934 /*
17935 * the 1st priority that has not FLOGI failed
17936 * will be the highest.
17937 */
17938 if (!next_fcf_pri)
17939 next_fcf_pri = fcf_pri->fcf_rec.priority;
17940 spin_unlock_irq(&phba->hbalock);
17941 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17942 rc = lpfc_sli4_fcf_rr_index_set(phba,
17943 fcf_pri->fcf_rec.fcf_index);
17944 if (rc)
17945 return 0;
17946 }
17947 spin_lock_irq(&phba->hbalock);
17948 }
17949 /*
17950 * if next_fcf_pri was not set above and the list is not empty then
17951 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070017952 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040017953 */
17954 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
17955 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
17956 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
17957 /*
17958 * the 1st priority that has not FLOGI failed
17959 * will be the highest.
17960 */
17961 if (!next_fcf_pri)
17962 next_fcf_pri = fcf_pri->fcf_rec.priority;
17963 spin_unlock_irq(&phba->hbalock);
17964 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
17965 rc = lpfc_sli4_fcf_rr_index_set(phba,
17966 fcf_pri->fcf_rec.fcf_index);
17967 if (rc)
17968 return 0;
17969 }
17970 spin_lock_irq(&phba->hbalock);
17971 }
17972 } else
17973 ret = 1;
17974 spin_unlock_irq(&phba->hbalock);
17975
17976 return ret;
17977}
17978/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017979 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17980 * @phba: pointer to lpfc hba data structure.
17981 *
17982 * This routine is to get the next eligible FCF record index in a round
17983 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040017984 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050017985 * shall be returned, otherwise, the next eligible FCF record's index
17986 * shall be returned.
17987 **/
17988uint16_t
17989lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17990{
17991 uint16_t next_fcf_index;
17992
James Smart421c6622013-01-03 15:44:16 -050017993initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040017994 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050017995 next_fcf_index = phba->fcf.current_rec.fcf_indx;
17996
James Smart7d791df2011-07-22 18:37:52 -040017997next_priority:
James Smart421c6622013-01-03 15:44:16 -050017998 /* Determine the next fcf index to check */
17999 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018000 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18001 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018002 next_fcf_index);
18003
James Smart0c9ab6f2010-02-26 14:15:57 -050018004 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018005 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18006 /*
18007 * If we have wrapped then we need to clear the bits that
18008 * have been tested so that we can detect when we should
18009 * change the priority level.
18010 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018011 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18012 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018013 }
18014
James Smart0c9ab6f2010-02-26 14:15:57 -050018015
James Smart3804dc82010-07-14 15:31:37 -040018016 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018017 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18018 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18019 /*
18020 * If next fcf index is not found check if there are lower
18021 * Priority level fcf's in the fcf_priority list.
18022 * Set up the rr_bmask with all of the avaiable fcf bits
18023 * at that level and continue the selection process.
18024 */
18025 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018026 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018027 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18028 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018029 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18030 return LPFC_FCOE_FCF_NEXT_NONE;
18031 else {
18032 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18033 "3063 Only FCF available idx %d, flag %x\n",
18034 next_fcf_index,
18035 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18036 return next_fcf_index;
18037 }
James Smart3804dc82010-07-14 15:31:37 -040018038 }
18039
James Smart7d791df2011-07-22 18:37:52 -040018040 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18041 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018042 LPFC_FCF_FLOGI_FAILED) {
18043 if (list_is_singular(&phba->fcf.fcf_pri_list))
18044 return LPFC_FCOE_FCF_NEXT_NONE;
18045
James Smart7d791df2011-07-22 18:37:52 -040018046 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018047 }
James Smart7d791df2011-07-22 18:37:52 -040018048
James Smart3804dc82010-07-14 15:31:37 -040018049 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018050 "2845 Get next roundrobin failover FCF (x%x)\n",
18051 next_fcf_index);
18052
James Smart0c9ab6f2010-02-26 14:15:57 -050018053 return next_fcf_index;
18054}
18055
18056/**
18057 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18058 * @phba: pointer to lpfc hba data structure.
18059 *
18060 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018061 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018062 * does not go beyond the range of the driver allocated bmask dimension
18063 * before setting the bit.
18064 *
18065 * Returns 0 if the index bit successfully set, otherwise, it returns
18066 * -EINVAL.
18067 **/
18068int
18069lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18070{
18071 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18072 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018073 "2610 FCF (x%x) reached driver's book "
18074 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018075 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18076 return -EINVAL;
18077 }
18078 /* Set the eligible FCF record index bmask */
18079 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18080
James Smart3804dc82010-07-14 15:31:37 -040018081 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018082 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018083 "bmask\n", fcf_index);
18084
James Smart0c9ab6f2010-02-26 14:15:57 -050018085 return 0;
18086}
18087
18088/**
James Smart3804dc82010-07-14 15:31:37 -040018089 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018090 * @phba: pointer to lpfc hba data structure.
18091 *
18092 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018093 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018094 * does not go beyond the range of the driver allocated bmask dimension
18095 * before clearing the bit.
18096 **/
18097void
18098lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18099{
James Smart9a803a72013-09-06 12:17:56 -040018100 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018101 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18102 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018103 "2762 FCF (x%x) reached driver's book "
18104 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018105 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18106 return;
18107 }
18108 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018109 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018110 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18111 list) {
James Smart7d791df2011-07-22 18:37:52 -040018112 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18113 list_del_init(&fcf_pri->list);
18114 break;
18115 }
18116 }
18117 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018118 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018119
18120 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018121 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018122 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018123}
18124
18125/**
James Smartecfd03c2010-02-12 14:41:27 -050018126 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18127 * @phba: pointer to lpfc hba data structure.
18128 *
18129 * This routine is the completion routine for the rediscover FCF table mailbox
18130 * command. If the mailbox command returned failure, it will try to stop the
18131 * FCF rediscover wait timer.
18132 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018133static void
James Smartecfd03c2010-02-12 14:41:27 -050018134lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18135{
18136 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18137 uint32_t shdr_status, shdr_add_status;
18138
18139 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18140
18141 shdr_status = bf_get(lpfc_mbox_hdr_status,
18142 &redisc_fcf->header.cfg_shdr.response);
18143 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18144 &redisc_fcf->header.cfg_shdr.response);
18145 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018146 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018147 "2746 Requesting for FCF rediscovery failed "
18148 "status x%x add_status x%x\n",
18149 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018150 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018151 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018152 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018153 spin_unlock_irq(&phba->hbalock);
18154 /*
18155 * CVL event triggered FCF rediscover request failed,
18156 * last resort to re-try current registered FCF entry.
18157 */
18158 lpfc_retry_pport_discovery(phba);
18159 } else {
18160 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018161 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018162 spin_unlock_irq(&phba->hbalock);
18163 /*
18164 * DEAD FCF event triggered FCF rediscover request
18165 * failed, last resort to fail over as a link down
18166 * to FCF registration.
18167 */
18168 lpfc_sli4_fcf_dead_failthrough(phba);
18169 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018170 } else {
18171 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018172 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018173 /*
18174 * Start FCF rediscovery wait timer for pending FCF
18175 * before rescan FCF record table.
18176 */
18177 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018178 }
James Smartecfd03c2010-02-12 14:41:27 -050018179
18180 mempool_free(mbox, phba->mbox_mem_pool);
18181}
18182
18183/**
James Smart3804dc82010-07-14 15:31:37 -040018184 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018185 * @phba: pointer to lpfc hba data structure.
18186 *
18187 * This routine is invoked to request for rediscovery of the entire FCF table
18188 * by the port.
18189 **/
18190int
18191lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18192{
18193 LPFC_MBOXQ_t *mbox;
18194 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18195 int rc, length;
18196
James Smart0c9ab6f2010-02-26 14:15:57 -050018197 /* Cancel retry delay timers to all vports before FCF rediscover */
18198 lpfc_cancel_all_vport_retry_delay_timer(phba);
18199
James Smartecfd03c2010-02-12 14:41:27 -050018200 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18201 if (!mbox) {
18202 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18203 "2745 Failed to allocate mbox for "
18204 "requesting FCF rediscover.\n");
18205 return -ENOMEM;
18206 }
18207
18208 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18209 sizeof(struct lpfc_sli4_cfg_mhdr));
18210 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18211 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18212 length, LPFC_SLI4_MBX_EMBED);
18213
18214 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18215 /* Set count to 0 for invalidating the entire FCF database */
18216 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18217
18218 /* Issue the mailbox command asynchronously */
18219 mbox->vport = phba->pport;
18220 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18221 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18222
18223 if (rc == MBX_NOT_FINISHED) {
18224 mempool_free(mbox, phba->mbox_mem_pool);
18225 return -EIO;
18226 }
18227 return 0;
18228}
18229
18230/**
James Smartfc2b9892010-02-26 14:15:29 -050018231 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18232 * @phba: pointer to lpfc hba data structure.
18233 *
18234 * This function is the failover routine as a last resort to the FCF DEAD
18235 * event when driver failed to perform fast FCF failover.
18236 **/
18237void
18238lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18239{
18240 uint32_t link_state;
18241
18242 /*
18243 * Last resort as FCF DEAD event failover will treat this as
18244 * a link down, but save the link state because we don't want
18245 * it to be changed to Link Down unless it is already down.
18246 */
18247 link_state = phba->link_state;
18248 lpfc_linkdown(phba);
18249 phba->link_state = link_state;
18250
18251 /* Unregister FCF if no devices connected to it */
18252 lpfc_unregister_unused_fcf(phba);
18253}
18254
18255/**
James Smart026abb82011-12-13 13:20:45 -050018256 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018257 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018258 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018259 *
James Smart026abb82011-12-13 13:20:45 -050018260 * This function gets SLI3 port configure region 23 data through memory dump
18261 * mailbox command. When it successfully retrieves data, the size of the data
18262 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018263 **/
James Smart026abb82011-12-13 13:20:45 -050018264static uint32_t
18265lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018266{
18267 LPFC_MBOXQ_t *pmb = NULL;
18268 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018269 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018270 int rc;
18271
James Smart026abb82011-12-13 13:20:45 -050018272 if (!rgn23_data)
18273 return 0;
18274
James Smarta0c87cb2009-07-19 10:01:10 -040018275 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18276 if (!pmb) {
18277 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018278 "2600 failed to allocate mailbox memory\n");
18279 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018280 }
18281 mb = &pmb->u.mb;
18282
James Smarta0c87cb2009-07-19 10:01:10 -040018283 do {
18284 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18285 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18286
18287 if (rc != MBX_SUCCESS) {
18288 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018289 "2601 failed to read config "
18290 "region 23, rc 0x%x Status 0x%x\n",
18291 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018292 mb->un.varDmp.word_cnt = 0;
18293 }
18294 /*
18295 * dump mem may return a zero when finished or we got a
18296 * mailbox error, either way we are done.
18297 */
18298 if (mb->un.varDmp.word_cnt == 0)
18299 break;
18300 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18301 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18302
18303 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018304 rgn23_data + offset,
18305 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018306 offset += mb->un.varDmp.word_cnt;
18307 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18308
James Smart026abb82011-12-13 13:20:45 -050018309 mempool_free(pmb, phba->mbox_mem_pool);
18310 return offset;
18311}
18312
18313/**
18314 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18315 * @phba: pointer to lpfc hba data structure.
18316 * @rgn23_data: pointer to configure region 23 data.
18317 *
18318 * This function gets SLI4 port configure region 23 data through memory dump
18319 * mailbox command. When it successfully retrieves data, the size of the data
18320 * will be returned, otherwise, 0 will be returned.
18321 **/
18322static uint32_t
18323lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18324{
18325 LPFC_MBOXQ_t *mboxq = NULL;
18326 struct lpfc_dmabuf *mp = NULL;
18327 struct lpfc_mqe *mqe;
18328 uint32_t data_length = 0;
18329 int rc;
18330
18331 if (!rgn23_data)
18332 return 0;
18333
18334 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18335 if (!mboxq) {
18336 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18337 "3105 failed to allocate mailbox memory\n");
18338 return 0;
18339 }
18340
18341 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18342 goto out;
18343 mqe = &mboxq->u.mqe;
18344 mp = (struct lpfc_dmabuf *) mboxq->context1;
18345 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18346 if (rc)
18347 goto out;
18348 data_length = mqe->un.mb_words[5];
18349 if (data_length == 0)
18350 goto out;
18351 if (data_length > DMP_RGN23_SIZE) {
18352 data_length = 0;
18353 goto out;
18354 }
18355 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18356out:
18357 mempool_free(mboxq, phba->mbox_mem_pool);
18358 if (mp) {
18359 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18360 kfree(mp);
18361 }
18362 return data_length;
18363}
18364
18365/**
18366 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18367 * @phba: pointer to lpfc hba data structure.
18368 *
18369 * This function read region 23 and parse TLV for port status to
18370 * decide if the user disaled the port. If the TLV indicates the
18371 * port is disabled, the hba_flag is set accordingly.
18372 **/
18373void
18374lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18375{
18376 uint8_t *rgn23_data = NULL;
18377 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18378 uint32_t offset = 0;
18379
18380 /* Get adapter Region 23 data */
18381 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18382 if (!rgn23_data)
18383 goto out;
18384
18385 if (phba->sli_rev < LPFC_SLI_REV4)
18386 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18387 else {
18388 if_type = bf_get(lpfc_sli_intf_if_type,
18389 &phba->sli4_hba.sli_intf);
18390 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18391 goto out;
18392 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18393 }
James Smarta0c87cb2009-07-19 10:01:10 -040018394
18395 if (!data_size)
18396 goto out;
18397
18398 /* Check the region signature first */
18399 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18401 "2619 Config region 23 has bad signature\n");
18402 goto out;
18403 }
18404 offset += 4;
18405
18406 /* Check the data structure version */
18407 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18408 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18409 "2620 Config region 23 has bad version\n");
18410 goto out;
18411 }
18412 offset += 4;
18413
18414 /* Parse TLV entries in the region */
18415 while (offset < data_size) {
18416 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18417 break;
18418 /*
18419 * If the TLV is not driver specific TLV or driver id is
18420 * not linux driver id, skip the record.
18421 */
18422 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18423 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18424 (rgn23_data[offset + 3] != 0)) {
18425 offset += rgn23_data[offset + 1] * 4 + 4;
18426 continue;
18427 }
18428
18429 /* Driver found a driver specific TLV in the config region */
18430 sub_tlv_len = rgn23_data[offset + 1] * 4;
18431 offset += 4;
18432 tlv_offset = 0;
18433
18434 /*
18435 * Search for configured port state sub-TLV.
18436 */
18437 while ((offset < data_size) &&
18438 (tlv_offset < sub_tlv_len)) {
18439 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18440 offset += 4;
18441 tlv_offset += 4;
18442 break;
18443 }
18444 if (rgn23_data[offset] != PORT_STE_TYPE) {
18445 offset += rgn23_data[offset + 1] * 4 + 4;
18446 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18447 continue;
18448 }
18449
18450 /* This HBA contains PORT_STE configured */
18451 if (!rgn23_data[offset + 2])
18452 phba->hba_flag |= LINK_DISABLED;
18453
18454 goto out;
18455 }
18456 }
James Smart026abb82011-12-13 13:20:45 -050018457
James Smarta0c87cb2009-07-19 10:01:10 -040018458out:
James Smarta0c87cb2009-07-19 10:01:10 -040018459 kfree(rgn23_data);
18460 return;
18461}
James Smart695a8142010-01-26 23:08:03 -050018462
18463/**
James Smart52d52442011-05-24 11:42:45 -040018464 * lpfc_wr_object - write an object to the firmware
18465 * @phba: HBA structure that indicates port to create a queue on.
18466 * @dmabuf_list: list of dmabufs to write to the port.
18467 * @size: the total byte value of the objects to write to the port.
18468 * @offset: the current offset to be used to start the transfer.
18469 *
18470 * This routine will create a wr_object mailbox command to send to the port.
18471 * the mailbox command will be constructed using the dma buffers described in
18472 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18473 * BDEs that the imbedded mailbox can support. The @offset variable will be
18474 * used to indicate the starting offset of the transfer and will also return
18475 * the offset after the write object mailbox has completed. @size is used to
18476 * determine the end of the object and whether the eof bit should be set.
18477 *
18478 * Return 0 is successful and offset will contain the the new offset to use
18479 * for the next write.
18480 * Return negative value for error cases.
18481 **/
18482int
18483lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18484 uint32_t size, uint32_t *offset)
18485{
18486 struct lpfc_mbx_wr_object *wr_object;
18487 LPFC_MBOXQ_t *mbox;
18488 int rc = 0, i = 0;
18489 uint32_t shdr_status, shdr_add_status;
18490 uint32_t mbox_tmo;
18491 union lpfc_sli4_cfg_shdr *shdr;
18492 struct lpfc_dmabuf *dmabuf;
18493 uint32_t written = 0;
18494
18495 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18496 if (!mbox)
18497 return -ENOMEM;
18498
18499 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18500 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18501 sizeof(struct lpfc_mbx_wr_object) -
18502 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18503
18504 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18505 wr_object->u.request.write_offset = *offset;
18506 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18507 wr_object->u.request.object_name[0] =
18508 cpu_to_le32(wr_object->u.request.object_name[0]);
18509 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18510 list_for_each_entry(dmabuf, dmabuf_list, list) {
18511 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18512 break;
18513 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18514 wr_object->u.request.bde[i].addrHigh =
18515 putPaddrHigh(dmabuf->phys);
18516 if (written + SLI4_PAGE_SIZE >= size) {
18517 wr_object->u.request.bde[i].tus.f.bdeSize =
18518 (size - written);
18519 written += (size - written);
18520 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18521 } else {
18522 wr_object->u.request.bde[i].tus.f.bdeSize =
18523 SLI4_PAGE_SIZE;
18524 written += SLI4_PAGE_SIZE;
18525 }
18526 i++;
18527 }
18528 wr_object->u.request.bde_count = i;
18529 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18530 if (!phba->sli4_hba.intr_enable)
18531 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18532 else {
James Smarta183a152011-10-10 21:32:43 -040018533 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018534 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18535 }
18536 /* The IOCTL status is embedded in the mailbox subheader. */
18537 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18538 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18539 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18540 if (rc != MBX_TIMEOUT)
18541 mempool_free(mbox, phba->mbox_mem_pool);
18542 if (shdr_status || shdr_add_status || rc) {
18543 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18544 "3025 Write Object mailbox failed with "
18545 "status x%x add_status x%x, mbx status x%x\n",
18546 shdr_status, shdr_add_status, rc);
18547 rc = -ENXIO;
18548 } else
18549 *offset += wr_object->u.response.actual_write_length;
18550 return rc;
18551}
18552
18553/**
James Smart695a8142010-01-26 23:08:03 -050018554 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18555 * @vport: pointer to vport data structure.
18556 *
18557 * This function iterate through the mailboxq and clean up all REG_LOGIN
18558 * and REG_VPI mailbox commands associated with the vport. This function
18559 * is called when driver want to restart discovery of the vport due to
18560 * a Clear Virtual Link event.
18561 **/
18562void
18563lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18564{
18565 struct lpfc_hba *phba = vport->phba;
18566 LPFC_MBOXQ_t *mb, *nextmb;
18567 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018568 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018569 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018570 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018571 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018572 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018573
James Smartd439d282010-09-29 11:18:45 -040018574 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018575 spin_lock_irq(&phba->hbalock);
18576 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18577 if (mb->vport != vport)
18578 continue;
18579
18580 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18581 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18582 continue;
18583
James Smartd439d282010-09-29 11:18:45 -040018584 list_del(&mb->list);
18585 list_add_tail(&mb->list, &mbox_cmd_list);
18586 }
18587 /* Clean up active mailbox command with the vport */
18588 mb = phba->sli.mbox_active;
18589 if (mb && (mb->vport == vport)) {
18590 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18591 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18592 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18593 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18594 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18595 /* Put reference count for delayed processing */
18596 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18597 /* Unregister the RPI when mailbox complete */
18598 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18599 }
18600 }
James Smart63e801c2010-11-20 23:14:19 -050018601 /* Cleanup any mailbox completions which are not yet processed */
18602 do {
18603 restart_loop = 0;
18604 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18605 /*
18606 * If this mailox is already processed or it is
18607 * for another vport ignore it.
18608 */
18609 if ((mb->vport != vport) ||
18610 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18611 continue;
18612
18613 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18614 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18615 continue;
18616
18617 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18618 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18619 ndlp = (struct lpfc_nodelist *)mb->context2;
18620 /* Unregister the RPI when mailbox complete */
18621 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18622 restart_loop = 1;
18623 spin_unlock_irq(&phba->hbalock);
18624 spin_lock(shost->host_lock);
18625 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18626 spin_unlock(shost->host_lock);
18627 spin_lock_irq(&phba->hbalock);
18628 break;
18629 }
18630 }
18631 } while (restart_loop);
18632
James Smartd439d282010-09-29 11:18:45 -040018633 spin_unlock_irq(&phba->hbalock);
18634
18635 /* Release the cleaned-up mailbox commands */
18636 while (!list_empty(&mbox_cmd_list)) {
18637 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018638 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18639 mp = (struct lpfc_dmabuf *) (mb->context1);
18640 if (mp) {
18641 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18642 kfree(mp);
18643 }
James Smart78730cf2010-04-06 15:06:30 -040018644 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018645 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018646 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018647 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018648 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018649 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018650 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018651 }
James Smart695a8142010-01-26 23:08:03 -050018652 }
James Smart695a8142010-01-26 23:08:03 -050018653 mempool_free(mb, phba->mbox_mem_pool);
18654 }
James Smartd439d282010-09-29 11:18:45 -040018655
18656 /* Release the ndlp with the cleaned-up active mailbox command */
18657 if (act_mbx_ndlp) {
18658 spin_lock(shost->host_lock);
18659 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18660 spin_unlock(shost->host_lock);
18661 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050018662 }
James Smart695a8142010-01-26 23:08:03 -050018663}
18664
James Smart2a9bf3d2010-06-07 15:24:45 -040018665/**
18666 * lpfc_drain_txq - Drain the txq
18667 * @phba: Pointer to HBA context object.
18668 *
18669 * This function attempt to submit IOCBs on the txq
18670 * to the adapter. For SLI4 adapters, the txq contains
18671 * ELS IOCBs that have been deferred because the there
18672 * are no SGLs. This congestion can occur with large
18673 * vport counts during node discovery.
18674 **/
18675
18676uint32_t
18677lpfc_drain_txq(struct lpfc_hba *phba)
18678{
18679 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080018680 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090018681 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040018682 unsigned long iflags = 0;
18683 char *fail_msg = NULL;
18684 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080018685 union lpfc_wqe128 wqe128;
18686 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040018687 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040018688
James Smart895427b2017-02-12 13:52:30 -080018689 pring = lpfc_phba_elsring(phba);
18690
James Smart398d81c2013-05-31 17:04:19 -040018691 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050018692 list_for_each_entry(piocbq, &pring->txq, list) {
18693 txq_cnt++;
18694 }
18695
18696 if (txq_cnt > pring->txq_max)
18697 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018698
James Smart398d81c2013-05-31 17:04:19 -040018699 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018700
James Smart0e9bb8d2013-03-01 16:35:12 -050018701 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040018702 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018703
James Smart19ca7602010-11-20 23:11:55 -050018704 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040018705 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040018706 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040018707 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18708 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050018709 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040018710 break;
18711 }
James Smart895427b2017-02-12 13:52:30 -080018712 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040018713 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050018714 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040018715 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018716 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040018717 }
James Smart0e9bb8d2013-03-01 16:35:12 -050018718 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040018719
18720 /* The xri and iocb resources secured,
18721 * attempt to issue request
18722 */
James Smart6d368e52011-05-24 11:44:12 -040018723 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040018724 piocbq->sli4_xritag = sglq->sli4_xritag;
18725 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18726 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080018727 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018728 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080018729 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018730 fail_msg = " - Wq is full";
18731 else
18732 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18733
18734 if (fail_msg) {
18735 /* Failed means we can't issue and need to cancel */
18736 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18737 "2822 IOCB failed %s iotag 0x%x "
18738 "xri 0x%x\n",
18739 fail_msg,
18740 piocbq->iotag, piocbq->sli4_xritag);
18741 list_add_tail(&piocbq->list, &completions);
18742 }
James Smart398d81c2013-05-31 17:04:19 -040018743 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018744 }
18745
James Smart2a9bf3d2010-06-07 15:24:45 -040018746 /* Cancel all the IOCBs that cannot be issued */
18747 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18748 IOERR_SLI_ABORTED);
18749
James Smart0e9bb8d2013-03-01 16:35:12 -050018750 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018751}
James Smart895427b2017-02-12 13:52:30 -080018752
18753/**
18754 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18755 * @phba: Pointer to HBA context object.
18756 * @pwqe: Pointer to command WQE.
18757 * @sglq: Pointer to the scatter gather queue object.
18758 *
18759 * This routine converts the bpl or bde that is in the WQE
18760 * to a sgl list for the sli4 hardware. The physical address
18761 * of the bpl/bde is converted back to a virtual address.
18762 * If the WQE contains a BPL then the list of BDE's is
18763 * converted to sli4_sge's. If the WQE contains a single
18764 * BDE then it is converted to a single sli_sge.
18765 * The WQE is still in cpu endianness so the contents of
18766 * the bpl can be used without byte swapping.
18767 *
18768 * Returns valid XRI = Success, NO_XRI = Failure.
18769 */
18770static uint16_t
18771lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18772 struct lpfc_sglq *sglq)
18773{
18774 uint16_t xritag = NO_XRI;
18775 struct ulp_bde64 *bpl = NULL;
18776 struct ulp_bde64 bde;
18777 struct sli4_sge *sgl = NULL;
18778 struct lpfc_dmabuf *dmabuf;
18779 union lpfc_wqe *wqe;
18780 int numBdes = 0;
18781 int i = 0;
18782 uint32_t offset = 0; /* accumulated offset in the sg request list */
18783 int inbound = 0; /* number of sg reply entries inbound from firmware */
18784 uint32_t cmd;
18785
18786 if (!pwqeq || !sglq)
18787 return xritag;
18788
18789 sgl = (struct sli4_sge *)sglq->sgl;
18790 wqe = &pwqeq->wqe;
18791 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18792
18793 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18794 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18795 return sglq->sli4_xritag;
18796 numBdes = pwqeq->rsvd2;
18797 if (numBdes) {
18798 /* The addrHigh and addrLow fields within the WQE
18799 * have not been byteswapped yet so there is no
18800 * need to swap them back.
18801 */
18802 if (pwqeq->context3)
18803 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18804 else
18805 return xritag;
18806
18807 bpl = (struct ulp_bde64 *)dmabuf->virt;
18808 if (!bpl)
18809 return xritag;
18810
18811 for (i = 0; i < numBdes; i++) {
18812 /* Should already be byte swapped. */
18813 sgl->addr_hi = bpl->addrHigh;
18814 sgl->addr_lo = bpl->addrLow;
18815
18816 sgl->word2 = le32_to_cpu(sgl->word2);
18817 if ((i+1) == numBdes)
18818 bf_set(lpfc_sli4_sge_last, sgl, 1);
18819 else
18820 bf_set(lpfc_sli4_sge_last, sgl, 0);
18821 /* swap the size field back to the cpu so we
18822 * can assign it to the sgl.
18823 */
18824 bde.tus.w = le32_to_cpu(bpl->tus.w);
18825 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18826 /* The offsets in the sgl need to be accumulated
18827 * separately for the request and reply lists.
18828 * The request is always first, the reply follows.
18829 */
18830 switch (cmd) {
18831 case CMD_GEN_REQUEST64_WQE:
18832 /* add up the reply sg entries */
18833 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18834 inbound++;
18835 /* first inbound? reset the offset */
18836 if (inbound == 1)
18837 offset = 0;
18838 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18839 bf_set(lpfc_sli4_sge_type, sgl,
18840 LPFC_SGE_TYPE_DATA);
18841 offset += bde.tus.f.bdeSize;
18842 break;
18843 case CMD_FCP_TRSP64_WQE:
18844 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18845 bf_set(lpfc_sli4_sge_type, sgl,
18846 LPFC_SGE_TYPE_DATA);
18847 break;
18848 case CMD_FCP_TSEND64_WQE:
18849 case CMD_FCP_TRECEIVE64_WQE:
18850 bf_set(lpfc_sli4_sge_type, sgl,
18851 bpl->tus.f.bdeFlags);
18852 if (i < 3)
18853 offset = 0;
18854 else
18855 offset += bde.tus.f.bdeSize;
18856 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18857 break;
18858 }
18859 sgl->word2 = cpu_to_le32(sgl->word2);
18860 bpl++;
18861 sgl++;
18862 }
18863 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18864 /* The addrHigh and addrLow fields of the BDE have not
18865 * been byteswapped yet so they need to be swapped
18866 * before putting them in the sgl.
18867 */
18868 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18869 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18870 sgl->word2 = le32_to_cpu(sgl->word2);
18871 bf_set(lpfc_sli4_sge_last, sgl, 1);
18872 sgl->word2 = cpu_to_le32(sgl->word2);
18873 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18874 }
18875 return sglq->sli4_xritag;
18876}
18877
18878/**
18879 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18880 * @phba: Pointer to HBA context object.
18881 * @ring_number: Base sli ring number
18882 * @pwqe: Pointer to command WQE.
18883 **/
18884int
18885lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18886 struct lpfc_iocbq *pwqe)
18887{
18888 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080018889 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080018890 struct lpfc_queue *wq;
18891 struct lpfc_sglq *sglq;
18892 struct lpfc_sli_ring *pring;
18893 unsigned long iflags;
18894
18895 /* NVME_LS and NVME_LS ABTS requests. */
18896 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18897 pring = phba->sli4_hba.nvmels_wq->pring;
18898 spin_lock_irqsave(&pring->ring_lock, iflags);
18899 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
18900 if (!sglq) {
18901 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18902 return WQE_BUSY;
18903 }
18904 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18905 pwqe->sli4_xritag = sglq->sli4_xritag;
18906 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
18907 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18908 return WQE_ERROR;
18909 }
18910 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18911 pwqe->sli4_xritag);
18912 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
18913 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18914 return WQE_ERROR;
18915 }
18916 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18917 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18918 return 0;
18919 }
18920
18921 /* NVME_FCREQ and NVME_ABTS requests */
18922 if (pwqe->iocb_flag & LPFC_IO_NVME) {
18923 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18924 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18925
18926 spin_lock_irqsave(&pring->ring_lock, iflags);
18927 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18928 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18929 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18930 if (lpfc_sli4_wq_put(wq, wqe)) {
18931 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18932 return WQE_ERROR;
18933 }
18934 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18935 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18936 return 0;
18937 }
18938
James Smartf358dd02017-02-12 13:52:34 -080018939 /* NVMET requests */
18940 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
18941 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
18942 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
18943
18944 spin_lock_irqsave(&pring->ring_lock, iflags);
18945 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070018946 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080018947 if (pwqe->sli4_xritag == NO_XRI) {
18948 pwqe->sli4_lxritag = sglq->sli4_lxritag;
18949 pwqe->sli4_xritag = sglq->sli4_xritag;
18950 }
18951 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
18952 pwqe->sli4_xritag);
18953 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
18954 bf_set(wqe_cqid, &wqe->generic.wqe_com,
18955 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
18956 if (lpfc_sli4_wq_put(wq, wqe)) {
18957 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18958 return WQE_ERROR;
18959 }
18960 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
18961 spin_unlock_irqrestore(&pring->ring_lock, iflags);
18962 return 0;
18963 }
James Smart895427b2017-02-12 13:52:30 -080018964 return WQE_ERROR;
18965}