blob: 3bff1f9c5df7165b9317125cdaf737d99c8c1d80 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart128bdda2018-01-30 15:59:03 -08004 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
James Smartd080abe2017-02-12 13:52:39 -08005 * “Broadcom” refers to Broadcom Limited and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010029#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050030
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040031#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050032#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040035#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040036#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040037#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050038
James Smart895427b2017-02-12 13:52:30 -080039#include <linux/nvme-fc-driver.h>
40
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_hw.h"
43#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040044#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040045#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080048#include "lpfc_scsi.h"
49#include "lpfc_nvme.h"
James Smartf358dd02017-02-12 13:52:34 -080050#include "lpfc_nvmet.h"
dea31012005-04-17 16:05:31 -050051#include "lpfc_crtn.h"
52#include "lpfc_logmsg.h"
53#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050054#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040055#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070056#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050057
58/* There are only four IOCB completion types. */
59typedef enum _lpfc_iocb_type {
60 LPFC_UNKNOWN_IOCB,
61 LPFC_UNSOL_IOCB,
62 LPFC_SOL_IOCB,
63 LPFC_ABORT_IOCB
64} lpfc_iocb_type;
65
James Smart4f774512009-05-22 14:52:35 -040066
67/* Provide function prototypes local to this module. */
68static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
69 uint32_t);
70static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040071 uint8_t *, uint32_t *);
72static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
73 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040074static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
75 struct hbq_dmabuf *);
James Smartae9e28f2017-05-15 15:20:51 -070076static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
77 struct hbq_dmabuf *dmabuf);
James Smart895427b2017-02-12 13:52:30 -080078static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
James Smart05580562011-05-24 11:40:48 -040079 struct lpfc_cqe *);
James Smart895427b2017-02-12 13:52:30 -080080static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040081 int);
Dick Kennedyf485c182017-09-29 17:34:34 -070082static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
83 struct lpfc_eqe *eqe, uint32_t qidx);
James Smarte8d3c3b2013-10-10 12:21:30 -040084static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
85static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart895427b2017-02-12 13:52:30 -080086static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
87 struct lpfc_sli_ring *pring,
88 struct lpfc_iocbq *cmdiocb);
James Smart05580562011-05-24 11:40:48 -040089
James Smart4f774512009-05-22 14:52:35 -040090static IOCB_t *
91lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
92{
93 return &iocbq->iocb;
94}
95
96/**
97 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
98 * @q: The Work Queue to operate on.
99 * @wqe: The work Queue Entry to put on the Work queue.
100 *
101 * This routine will copy the contents of @wqe to the next available entry on
102 * the @q. This function will then ring the Work Queue Doorbell to signal the
103 * HBA to start processing the Work Queue Entry. This function returns 0 if
104 * successful. If no entries are available on @q then this function will return
105 * -ENOMEM.
106 * The caller is expected to hold the hbalock when calling this routine.
107 **/
Dick Kennedycd22d602017-08-23 16:55:35 -0700108static int
James Smart4f774512009-05-22 14:52:35 -0400109lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
110{
James Smart2e90f4b2011-12-13 13:22:37 -0500111 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400112 struct lpfc_register doorbell;
113 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400114 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400115
James Smart2e90f4b2011-12-13 13:22:37 -0500116 /* sanity check on queue memory */
117 if (unlikely(!q))
118 return -ENOMEM;
119 temp_wqe = q->qe[q->host_index].wqe;
120
James Smart4f774512009-05-22 14:52:35 -0400121 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400122 idx = ((q->host_index + 1) % q->entry_count);
123 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400124 q->WQ_overflow++;
Dick Kennedycd22d602017-08-23 16:55:35 -0700125 return -EBUSY;
James Smartb84daac2012-08-03 12:35:13 -0400126 }
127 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400128 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500129 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400130 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smart04673e32018-01-30 15:58:45 -0800131 else
132 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500133 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
134 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400135 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart6b3b3bd2016-12-19 15:07:30 -0800136 /* ensure WQE bcopy flushed before doorbell write */
137 wmb();
James Smart4f774512009-05-22 14:52:35 -0400138
139 /* Update the host index before invoking device */
140 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400141
142 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400143
144 /* Ring Doorbell */
145 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500146 if (q->db_format == LPFC_DB_LIST_FORMAT) {
147 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
148 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
149 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
150 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
151 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
152 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
153 } else {
154 return -EINVAL;
155 }
156 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400157
158 return 0;
159}
160
161/**
162 * lpfc_sli4_wq_release - Updates internal hba index for WQ
163 * @q: The Work Queue to operate on.
164 * @index: The index to advance the hba index to.
165 *
166 * This routine will update the HBA index of a queue to reflect consumption of
167 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
168 * an entry the host calls this function to update the queue's internal
169 * pointers. This routine returns the number of entries that were consumed by
170 * the HBA.
171 **/
172static uint32_t
173lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
174{
175 uint32_t released = 0;
176
James Smart2e90f4b2011-12-13 13:22:37 -0500177 /* sanity check on queue memory */
178 if (unlikely(!q))
179 return 0;
180
James Smart4f774512009-05-22 14:52:35 -0400181 if (q->hba_index == index)
182 return 0;
183 do {
184 q->hba_index = ((q->hba_index + 1) % q->entry_count);
185 released++;
186 } while (q->hba_index != index);
187 return released;
188}
189
190/**
191 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
192 * @q: The Mailbox Queue to operate on.
193 * @wqe: The Mailbox Queue Entry to put on the Work queue.
194 *
195 * This routine will copy the contents of @mqe to the next available entry on
196 * the @q. This function will then ring the Work Queue Doorbell to signal the
197 * HBA to start processing the Work Queue Entry. This function returns 0 if
198 * successful. If no entries are available on @q then this function will return
199 * -ENOMEM.
200 * The caller is expected to hold the hbalock when calling this routine.
201 **/
202static uint32_t
203lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
204{
James Smart2e90f4b2011-12-13 13:22:37 -0500205 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400206 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400207
James Smart2e90f4b2011-12-13 13:22:37 -0500208 /* sanity check on queue memory */
209 if (unlikely(!q))
210 return -ENOMEM;
211 temp_mqe = q->qe[q->host_index].mqe;
212
James Smart4f774512009-05-22 14:52:35 -0400213 /* If the host has not yet processed the next entry then we are done */
214 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
215 return -ENOMEM;
216 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
217 /* Save off the mailbox pointer for completion */
218 q->phba->mbox = (MAILBOX_t *)temp_mqe;
219
220 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400221 q->host_index = ((q->host_index + 1) % q->entry_count);
222
223 /* Ring Doorbell */
224 doorbell.word0 = 0;
225 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
226 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
227 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400228 return 0;
229}
230
231/**
232 * lpfc_sli4_mq_release - Updates internal hba index for MQ
233 * @q: The Mailbox Queue to operate on.
234 *
235 * This routine will update the HBA index of a queue to reflect consumption of
236 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
237 * an entry the host calls this function to update the queue's internal
238 * pointers. This routine returns the number of entries that were consumed by
239 * the HBA.
240 **/
241static uint32_t
242lpfc_sli4_mq_release(struct lpfc_queue *q)
243{
James Smart2e90f4b2011-12-13 13:22:37 -0500244 /* sanity check on queue memory */
245 if (unlikely(!q))
246 return 0;
247
James Smart4f774512009-05-22 14:52:35 -0400248 /* Clear the mailbox pointer for completion */
249 q->phba->mbox = NULL;
250 q->hba_index = ((q->hba_index + 1) % q->entry_count);
251 return 1;
252}
253
254/**
255 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
256 * @q: The Event Queue to get the first valid EQE from
257 *
258 * This routine will get the first valid Event Queue Entry from @q, update
259 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
260 * the Queue (no more work to do), or the Queue is full of EQEs that have been
261 * processed, but not popped back to the HBA then this routine will return NULL.
262 **/
263static struct lpfc_eqe *
264lpfc_sli4_eq_get(struct lpfc_queue *q)
265{
James Smart2e90f4b2011-12-13 13:22:37 -0500266 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400267 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500268
269 /* sanity check on queue memory */
270 if (unlikely(!q))
271 return NULL;
272 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400273
274 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400275 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400276 return NULL;
277 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400278 idx = ((q->hba_index + 1) % q->entry_count);
279 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400280 return NULL;
281
James Smart027140e2012-08-03 12:35:44 -0400282 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400283
284 /*
285 * insert barrier for instruction interlock : data from the hardware
286 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800287 * upon. Speculative instructions were allowing a bcopy at the start
288 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
289 * after our return, to copy data before the valid bit check above
290 * was done. As such, some of the copied data was stale. The barrier
291 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400292 */
293 mb();
James Smart4f774512009-05-22 14:52:35 -0400294 return eqe;
295}
296
297/**
James Smartba20c852012-08-03 12:36:52 -0400298 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
299 * @q: The Event Queue to disable interrupts
300 *
301 **/
James Smartb71413d2018-02-22 08:18:40 -0800302inline void
James Smartba20c852012-08-03 12:36:52 -0400303lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
304{
305 struct lpfc_register doorbell;
306
307 doorbell.word0 = 0;
308 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
309 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
310 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
311 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
312 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800313 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smartba20c852012-08-03 12:36:52 -0400314}
315
316/**
James Smart27d6ac02018-02-22 08:18:42 -0800317 * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
318 * @q: The Event Queue to disable interrupts
319 *
320 **/
321inline void
322lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
323{
324 struct lpfc_register doorbell;
325
326 doorbell.word0 = 0;
327 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
328 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
329 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
330 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
331 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
332 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
333}
334
335/**
James Smart4f774512009-05-22 14:52:35 -0400336 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
337 * @q: The Event Queue that the host has completed processing for.
338 * @arm: Indicates whether the host wants to arms this CQ.
339 *
340 * This routine will mark all Event Queue Entries on @q, from the last
341 * known completed entry to the last entry that was processed, as completed
342 * by clearing the valid bit for each completion queue entry. Then it will
343 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
344 * The internal host index in the @q will be updated by this routine to indicate
345 * that the host has finished processing the entries. The @arm parameter
346 * indicates that the queue should be rearmed when ringing the doorbell.
347 *
348 * This function will return the number of EQEs that were popped.
349 **/
350uint32_t
351lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
352{
353 uint32_t released = 0;
354 struct lpfc_eqe *temp_eqe;
355 struct lpfc_register doorbell;
356
James Smart2e90f4b2011-12-13 13:22:37 -0500357 /* sanity check on queue memory */
358 if (unlikely(!q))
359 return 0;
360
James Smart4f774512009-05-22 14:52:35 -0400361 /* while there are valid entries */
362 while (q->hba_index != q->host_index) {
363 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400364 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400365 released++;
366 q->host_index = ((q->host_index + 1) % q->entry_count);
367 }
368 if (unlikely(released == 0 && !arm))
369 return 0;
370
371 /* ring doorbell for number popped */
372 doorbell.word0 = 0;
373 if (arm) {
374 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
375 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
376 }
377 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
378 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500379 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
380 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
381 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800382 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500383 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
384 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
James Smart9dd35422018-02-22 08:18:41 -0800385 readl(q->phba->sli4_hba.EQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400386 return released;
387}
388
389/**
James Smart27d6ac02018-02-22 08:18:42 -0800390 * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
391 * @q: The Event Queue that the host has completed processing for.
392 * @arm: Indicates whether the host wants to arms this CQ.
393 *
394 * This routine will mark all Event Queue Entries on @q, from the last
395 * known completed entry to the last entry that was processed, as completed
396 * by clearing the valid bit for each completion queue entry. Then it will
397 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
398 * The internal host index in the @q will be updated by this routine to indicate
399 * that the host has finished processing the entries. The @arm parameter
400 * indicates that the queue should be rearmed when ringing the doorbell.
401 *
402 * This function will return the number of EQEs that were popped.
403 **/
404uint32_t
405lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
406{
407 uint32_t released = 0;
408 struct lpfc_eqe *temp_eqe;
409 struct lpfc_register doorbell;
410
411 /* sanity check on queue memory */
412 if (unlikely(!q))
413 return 0;
414
415 /* while there are valid entries */
416 while (q->hba_index != q->host_index) {
417 temp_eqe = q->qe[q->host_index].eqe;
418 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
419 released++;
420 q->host_index = ((q->host_index + 1) % q->entry_count);
421 }
422 if (unlikely(released == 0 && !arm))
423 return 0;
424
425 /* ring doorbell for number popped */
426 doorbell.word0 = 0;
427 if (arm)
428 bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
429 bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
430 bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
431 writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
432 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
433 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
434 readl(q->phba->sli4_hba.EQDBregaddr);
435 return released;
436}
437
438/**
James Smart4f774512009-05-22 14:52:35 -0400439 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
440 * @q: The Completion Queue to get the first valid CQE from
441 *
442 * This routine will get the first valid Completion Queue Entry from @q, update
443 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
444 * the Queue (no more work to do), or the Queue is full of CQEs that have been
445 * processed, but not popped back to the HBA then this routine will return NULL.
446 **/
447static struct lpfc_cqe *
448lpfc_sli4_cq_get(struct lpfc_queue *q)
449{
450 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400451 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400452
James Smart2e90f4b2011-12-13 13:22:37 -0500453 /* sanity check on queue memory */
454 if (unlikely(!q))
455 return NULL;
456
James Smart4f774512009-05-22 14:52:35 -0400457 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400458 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400459 return NULL;
460 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400461 idx = ((q->hba_index + 1) % q->entry_count);
462 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400463 return NULL;
464
465 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400466 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400467
468 /*
469 * insert barrier for instruction interlock : data from the hardware
470 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800471 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
472 * instructions allowing action on content before valid bit checked,
473 * add barrier here as well. May not be needed as "content" is a
474 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400475 */
476 mb();
James Smart4f774512009-05-22 14:52:35 -0400477 return cqe;
478}
479
480/**
481 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
482 * @q: The Completion Queue that the host has completed processing for.
483 * @arm: Indicates whether the host wants to arms this CQ.
484 *
485 * This routine will mark all Completion queue entries on @q, from the last
486 * known completed entry to the last entry that was processed, as completed
487 * by clearing the valid bit for each completion queue entry. Then it will
488 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
489 * The internal host index in the @q will be updated by this routine to indicate
490 * that the host has finished processing the entries. The @arm parameter
491 * indicates that the queue should be rearmed when ringing the doorbell.
492 *
493 * This function will return the number of CQEs that were released.
494 **/
495uint32_t
496lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
497{
498 uint32_t released = 0;
499 struct lpfc_cqe *temp_qe;
500 struct lpfc_register doorbell;
501
James Smart2e90f4b2011-12-13 13:22:37 -0500502 /* sanity check on queue memory */
503 if (unlikely(!q))
504 return 0;
James Smart4f774512009-05-22 14:52:35 -0400505 /* while there are valid entries */
506 while (q->hba_index != q->host_index) {
507 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400508 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400509 released++;
510 q->host_index = ((q->host_index + 1) % q->entry_count);
511 }
512 if (unlikely(released == 0 && !arm))
513 return 0;
514
515 /* ring doorbell for number popped */
516 doorbell.word0 = 0;
517 if (arm)
518 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
519 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
520 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500521 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
522 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
523 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart9dd35422018-02-22 08:18:41 -0800524 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400525 return released;
526}
527
528/**
James Smart27d6ac02018-02-22 08:18:42 -0800529 * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
530 * @q: The Completion Queue that the host has completed processing for.
531 * @arm: Indicates whether the host wants to arms this CQ.
532 *
533 * This routine will mark all Completion queue entries on @q, from the last
534 * known completed entry to the last entry that was processed, as completed
535 * by clearing the valid bit for each completion queue entry. Then it will
536 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
537 * The internal host index in the @q will be updated by this routine to indicate
538 * that the host has finished processing the entries. The @arm parameter
539 * indicates that the queue should be rearmed when ringing the doorbell.
540 *
541 * This function will return the number of CQEs that were released.
542 **/
543uint32_t
544lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
545{
546 uint32_t released = 0;
547 struct lpfc_cqe *temp_qe;
548 struct lpfc_register doorbell;
549
550 /* sanity check on queue memory */
551 if (unlikely(!q))
552 return 0;
553 /* while there are valid entries */
554 while (q->hba_index != q->host_index) {
555 temp_qe = q->qe[q->host_index].cqe;
556 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
557 released++;
558 q->host_index = ((q->host_index + 1) % q->entry_count);
559 }
560 if (unlikely(released == 0 && !arm))
561 return 0;
562
563 /* ring doorbell for number popped */
564 doorbell.word0 = 0;
565 if (arm)
566 bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
567 bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
568 bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
569 writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
570 return released;
571}
572
573/**
James Smart4f774512009-05-22 14:52:35 -0400574 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
575 * @q: The Header Receive Queue to operate on.
576 * @wqe: The Receive Queue Entry to put on the Receive queue.
577 *
578 * This routine will copy the contents of @wqe to the next available entry on
579 * the @q. This function will then ring the Receive Queue Doorbell to signal the
580 * HBA to start processing the Receive Queue Entry. This function returns the
581 * index that the rqe was copied to if successful. If no entries are available
582 * on @q then this function will return -ENOMEM.
583 * The caller is expected to hold the hbalock when calling this routine.
584 **/
James Smart895427b2017-02-12 13:52:30 -0800585int
James Smart4f774512009-05-22 14:52:35 -0400586lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
587 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
588{
James Smart2e90f4b2011-12-13 13:22:37 -0500589 struct lpfc_rqe *temp_hrqe;
590 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400591 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800592 int hq_put_index;
593 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400594
James Smart2e90f4b2011-12-13 13:22:37 -0500595 /* sanity check on queue memory */
596 if (unlikely(!hq) || unlikely(!dq))
597 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800598 hq_put_index = hq->host_index;
599 dq_put_index = dq->host_index;
600 temp_hrqe = hq->qe[hq_put_index].rqe;
601 temp_drqe = dq->qe[dq_put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500602
James Smart4f774512009-05-22 14:52:35 -0400603 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
604 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800605 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400606 return -EINVAL;
607 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800608 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400609 return -EBUSY;
610 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
611 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
612
613 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800614 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
615 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700616 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400617
618 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400619 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400620 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500621 if (hq->db_format == LPFC_DB_RING_FORMAT) {
622 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
623 hq->entry_repost);
624 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
625 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
626 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
627 hq->entry_repost);
628 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
629 hq->host_index);
630 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
631 } else {
632 return -EINVAL;
633 }
634 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400635 }
James Smartcbc5de12017-12-08 17:18:04 -0800636 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400637}
638
639/**
640 * lpfc_sli4_rq_release - Updates internal hba index for RQ
641 * @q: The Header Receive Queue to operate on.
642 *
643 * This routine will update the HBA index of a queue to reflect consumption of
644 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
645 * consumed an entry the host calls this function to update the queue's
646 * internal pointers. This routine returns the number of entries that were
647 * consumed by the HBA.
648 **/
649static uint32_t
650lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
651{
James Smart2e90f4b2011-12-13 13:22:37 -0500652 /* sanity check on queue memory */
653 if (unlikely(!hq) || unlikely(!dq))
654 return 0;
655
James Smart4f774512009-05-22 14:52:35 -0400656 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
657 return 0;
658 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
659 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
660 return 1;
661}
662
James Smarte59058c2008-08-24 21:49:00 -0400663/**
James Smart3621a712009-04-06 18:47:14 -0400664 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400665 * @phba: Pointer to HBA context object.
666 * @pring: Pointer to driver SLI ring object.
667 *
668 * This function returns pointer to next command iocb entry
669 * in the command ring. The caller must hold hbalock to prevent
670 * other threads consume the next command iocb.
671 * SLI-2/SLI-3 provide different sized iocbs.
672 **/
James Smarted957682007-06-17 19:56:37 -0500673static inline IOCB_t *
674lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
675{
James Smart7e56aa22012-08-03 12:35:34 -0400676 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
677 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500678}
679
James Smarte59058c2008-08-24 21:49:00 -0400680/**
James Smart3621a712009-04-06 18:47:14 -0400681 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400682 * @phba: Pointer to HBA context object.
683 * @pring: Pointer to driver SLI ring object.
684 *
685 * This function returns pointer to next response iocb entry
686 * in the response ring. The caller must hold hbalock to make sure
687 * that no other thread consume the next response iocb.
688 * SLI-2/SLI-3 provide different sized iocbs.
689 **/
James Smarted957682007-06-17 19:56:37 -0500690static inline IOCB_t *
691lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
692{
James Smart7e56aa22012-08-03 12:35:34 -0400693 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
694 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500695}
696
James Smarte59058c2008-08-24 21:49:00 -0400697/**
James Smart3621a712009-04-06 18:47:14 -0400698 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400699 * @phba: Pointer to HBA context object.
700 *
701 * This function is called with hbalock held. This function
702 * allocates a new driver iocb object from the iocb pool. If the
703 * allocation is successful, it returns pointer to the newly
704 * allocated iocb object else it returns NULL.
705 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400706struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500707__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400708{
709 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
710 struct lpfc_iocbq * iocbq = NULL;
711
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100712 lockdep_assert_held(&phba->hbalock);
713
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400714 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400715 if (iocbq)
716 phba->iocb_cnt++;
717 if (phba->iocb_cnt > phba->iocb_max)
718 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400719 return iocbq;
720}
721
James Smarte59058c2008-08-24 21:49:00 -0400722/**
James Smartda0436e2009-05-22 14:51:39 -0400723 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
724 * @phba: Pointer to HBA context object.
725 * @xritag: XRI value.
726 *
727 * This function clears the sglq pointer from the array of acive
728 * sglq's. The xritag that is passed in is used to index into the
729 * array. Before the xritag can be used it needs to be adjusted
730 * by subtracting the xribase.
731 *
732 * Returns sglq ponter = success, NULL = Failure.
733 **/
James Smart895427b2017-02-12 13:52:30 -0800734struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400735__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
736{
James Smartda0436e2009-05-22 14:51:39 -0400737 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400738
739 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
740 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400741 return sglq;
742}
743
744/**
745 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
746 * @phba: Pointer to HBA context object.
747 * @xritag: XRI value.
748 *
749 * This function returns the sglq pointer from the array of acive
750 * sglq's. The xritag that is passed in is used to index into the
751 * array. Before the xritag can be used it needs to be adjusted
752 * by subtracting the xribase.
753 *
754 * Returns sglq ponter = success, NULL = Failure.
755 **/
James Smart0f65ff62010-02-26 14:14:23 -0500756struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400757__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
758{
James Smartda0436e2009-05-22 14:51:39 -0400759 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400760
761 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400762 return sglq;
763}
764
765/**
James Smart1151e3e2011-02-16 12:39:35 -0500766 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500767 * @phba: Pointer to HBA context object.
768 * @xritag: xri used in this exchange.
769 * @rrq: The RRQ to be cleared.
770 *
James Smart19ca7602010-11-20 23:11:55 -0500771 **/
James Smart1151e3e2011-02-16 12:39:35 -0500772void
773lpfc_clr_rrq_active(struct lpfc_hba *phba,
774 uint16_t xritag,
775 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500776{
James Smart1151e3e2011-02-16 12:39:35 -0500777 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500778
James Smart1151e3e2011-02-16 12:39:35 -0500779 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
780 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500781
782 /* The target DID could have been swapped (cable swap)
783 * we should use the ndlp from the findnode if it is
784 * available.
785 */
James Smart1151e3e2011-02-16 12:39:35 -0500786 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500787 ndlp = rrq->ndlp;
788
James Smart1151e3e2011-02-16 12:39:35 -0500789 if (!ndlp)
790 goto out;
791
James Smartcff261f2013-12-17 20:29:47 -0500792 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500793 rrq->send_rrq = 0;
794 rrq->xritag = 0;
795 rrq->rrq_stop_time = 0;
796 }
James Smart1151e3e2011-02-16 12:39:35 -0500797out:
James Smart19ca7602010-11-20 23:11:55 -0500798 mempool_free(rrq, phba->rrq_pool);
799}
800
801/**
802 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
803 * @phba: Pointer to HBA context object.
804 *
805 * This function is called with hbalock held. This function
806 * Checks if stop_time (ratov from setting rrq active) has
807 * been reached, if it has and the send_rrq flag is set then
808 * it will call lpfc_send_rrq. If the send_rrq flag is not set
809 * then it will just call the routine to clear the rrq and
810 * free the rrq resource.
811 * The timer is set to the next rrq that is going to expire before
812 * leaving the routine.
813 *
814 **/
815void
816lpfc_handle_rrq_active(struct lpfc_hba *phba)
817{
818 struct lpfc_node_rrq *rrq;
819 struct lpfc_node_rrq *nextrrq;
820 unsigned long next_time;
821 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500822 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500823
824 spin_lock_irqsave(&phba->hbalock, iflags);
825 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400826 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500827 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500828 &phba->active_rrq_list, list) {
829 if (time_after(jiffies, rrq->rrq_stop_time))
830 list_move(&rrq->list, &send_rrq);
831 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500832 next_time = rrq->rrq_stop_time;
833 }
834 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500835 if ((!list_empty(&phba->active_rrq_list)) &&
836 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500837 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500838 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
839 list_del(&rrq->list);
840 if (!rrq->send_rrq)
841 /* this call will free the rrq */
842 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
843 else if (lpfc_send_rrq(phba, rrq)) {
844 /* if we send the rrq then the completion handler
845 * will clear the bit in the xribitmap.
846 */
847 lpfc_clr_rrq_active(phba, rrq->xritag,
848 rrq);
849 }
850 }
James Smart19ca7602010-11-20 23:11:55 -0500851}
852
853/**
854 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
855 * @vport: Pointer to vport context object.
856 * @xri: The xri used in the exchange.
857 * @did: The targets DID for this exchange.
858 *
859 * returns NULL = rrq not found in the phba->active_rrq_list.
860 * rrq = rrq for this xri and target.
861 **/
862struct lpfc_node_rrq *
863lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
864{
865 struct lpfc_hba *phba = vport->phba;
866 struct lpfc_node_rrq *rrq;
867 struct lpfc_node_rrq *nextrrq;
868 unsigned long iflags;
869
870 if (phba->sli_rev != LPFC_SLI_REV4)
871 return NULL;
872 spin_lock_irqsave(&phba->hbalock, iflags);
873 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
874 if (rrq->vport == vport && rrq->xritag == xri &&
875 rrq->nlp_DID == did){
876 list_del(&rrq->list);
877 spin_unlock_irqrestore(&phba->hbalock, iflags);
878 return rrq;
879 }
880 }
881 spin_unlock_irqrestore(&phba->hbalock, iflags);
882 return NULL;
883}
884
885/**
886 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
887 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500888 * @ndlp: Pointer to the lpfc_node_list structure.
889 * If ndlp is NULL Remove all active RRQs for this vport from the
890 * phba->active_rrq_list and clear the rrq.
891 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500892 **/
893void
James Smart1151e3e2011-02-16 12:39:35 -0500894lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500895
896{
897 struct lpfc_hba *phba = vport->phba;
898 struct lpfc_node_rrq *rrq;
899 struct lpfc_node_rrq *nextrrq;
900 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500901 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500902
903 if (phba->sli_rev != LPFC_SLI_REV4)
904 return;
James Smart1151e3e2011-02-16 12:39:35 -0500905 if (!ndlp) {
906 lpfc_sli4_vport_delete_els_xri_aborted(vport);
907 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500908 }
James Smart1151e3e2011-02-16 12:39:35 -0500909 spin_lock_irqsave(&phba->hbalock, iflags);
910 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
911 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
912 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500913 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500914
915 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
916 list_del(&rrq->list);
917 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
918 }
James Smart19ca7602010-11-20 23:11:55 -0500919}
920
921/**
James Smart1151e3e2011-02-16 12:39:35 -0500922 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500923 * @phba: Pointer to HBA context object.
924 * @ndlp: Targets nodelist pointer for this exchange.
925 * @xritag the xri in the bitmap to test.
926 *
927 * This function is called with hbalock held. This function
928 * returns 0 = rrq not active for this xri
929 * 1 = rrq is valid for this xri.
930 **/
James Smart1151e3e2011-02-16 12:39:35 -0500931int
932lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500933 uint16_t xritag)
934{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100935 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500936 if (!ndlp)
937 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500938 if (!ndlp->active_rrqs_xri_bitmap)
939 return 0;
940 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500941 return 1;
942 else
943 return 0;
944}
945
946/**
947 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
948 * @phba: Pointer to HBA context object.
949 * @ndlp: nodelist pointer for this target.
950 * @xritag: xri used in this exchange.
951 * @rxid: Remote Exchange ID.
952 * @send_rrq: Flag used to determine if we should send rrq els cmd.
953 *
954 * This function takes the hbalock.
955 * The active bit is always set in the active rrq xri_bitmap even
956 * if there is no slot avaiable for the other rrq information.
957 *
958 * returns 0 rrq actived for this xri
959 * < 0 No memory or invalid ndlp.
960 **/
961int
962lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500963 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500964{
James Smart19ca7602010-11-20 23:11:55 -0500965 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500966 struct lpfc_node_rrq *rrq;
967 int empty;
968
969 if (!ndlp)
970 return -EINVAL;
971
972 if (!phba->cfg_enable_rrq)
973 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500974
975 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500976 if (phba->pport->load_flag & FC_UNLOADING) {
977 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
978 goto out;
979 }
980
981 /*
982 * set the active bit even if there is no mem available.
983 */
984 if (NLP_CHK_FREE_REQ(ndlp))
985 goto out;
986
987 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
988 goto out;
989
James Smartcff261f2013-12-17 20:29:47 -0500990 if (!ndlp->active_rrqs_xri_bitmap)
991 goto out;
992
993 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500994 goto out;
995
James Smart19ca7602010-11-20 23:11:55 -0500996 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500997 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
998 if (!rrq) {
999 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1000 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
1001 " DID:0x%x Send:%d\n",
1002 xritag, rxid, ndlp->nlp_DID, send_rrq);
1003 return -EINVAL;
1004 }
James Smarte5771b42013-03-01 16:37:14 -05001005 if (phba->cfg_enable_rrq == 1)
1006 rrq->send_rrq = send_rrq;
1007 else
1008 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -05001009 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -04001010 rrq->rrq_stop_time = jiffies +
1011 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -05001012 rrq->ndlp = ndlp;
1013 rrq->nlp_DID = ndlp->nlp_DID;
1014 rrq->vport = ndlp->vport;
1015 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -05001016 spin_lock_irqsave(&phba->hbalock, iflags);
1017 empty = list_empty(&phba->active_rrq_list);
1018 list_add_tail(&rrq->list, &phba->active_rrq_list);
1019 phba->hba_flag |= HBA_RRQ_ACTIVE;
1020 if (empty)
1021 lpfc_worker_wake_up(phba);
1022 spin_unlock_irqrestore(&phba->hbalock, iflags);
1023 return 0;
1024out:
1025 spin_unlock_irqrestore(&phba->hbalock, iflags);
1026 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1027 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
1028 " DID:0x%x Send:%d\n",
1029 xritag, rxid, ndlp->nlp_DID, send_rrq);
1030 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -05001031}
1032
1033/**
James Smart895427b2017-02-12 13:52:30 -08001034 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -04001035 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -05001036 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -04001037 *
James Smartdafe8ce2014-09-03 12:56:40 -04001038 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -04001039 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -04001040 * list is not empty then it is successful, it returns pointer to the newly
1041 * allocated sglq object else it returns NULL.
1042 **/
1043static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -08001044__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -04001045{
James Smart895427b2017-02-12 13:52:30 -08001046 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -04001047 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001048 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -05001049 struct lpfc_scsi_buf *lpfc_cmd;
1050 struct lpfc_nodelist *ndlp;
1051 int found = 0;
1052
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001053 lockdep_assert_held(&phba->hbalock);
1054
James Smart19ca7602010-11-20 23:11:55 -05001055 if (piocbq->iocb_flag & LPFC_IO_FCP) {
1056 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
1057 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -05001058 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -04001059 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -05001060 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -04001061 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
1062 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
1063 ndlp = NULL;
1064 else
1065 ndlp = piocbq->context_un.ndlp;
1066 } else {
James Smart19ca7602010-11-20 23:11:55 -05001067 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -04001068 }
James Smart19ca7602010-11-20 23:11:55 -05001069
James Smart895427b2017-02-12 13:52:30 -08001070 spin_lock(&phba->sli4_hba.sgl_list_lock);
1071 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -05001072 start_sglq = sglq;
1073 while (!found) {
1074 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -08001075 break;
James Smart895427b2017-02-12 13:52:30 -08001076 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
1077 test_bit(sglq->sli4_lxritag,
1078 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -05001079 /* This xri has an rrq outstanding for this DID.
1080 * put it back in the list and get another xri.
1081 */
James Smart895427b2017-02-12 13:52:30 -08001082 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001083 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -08001084 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -05001085 struct lpfc_sglq, list);
1086 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -07001087 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -05001088 sglq = NULL;
1089 break;
1090 } else
1091 continue;
1092 }
1093 sglq->ndlp = ndlp;
1094 found = 1;
James Smart6d368e52011-05-24 11:44:12 -04001095 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -05001096 sglq->state = SGL_ALLOCATED;
1097 }
James Smart895427b2017-02-12 13:52:30 -08001098 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -04001099 return sglq;
1100}
1101
1102/**
James Smartf358dd02017-02-12 13:52:34 -08001103 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
1104 * @phba: Pointer to HBA context object.
1105 * @piocb: Pointer to the iocbq.
1106 *
1107 * This function is called with the sgl_list lock held. This function
1108 * gets a new driver sglq object from the sglq list. If the
1109 * list is not empty then it is successful, it returns pointer to the newly
1110 * allocated sglq object else it returns NULL.
1111 **/
1112struct lpfc_sglq *
1113__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1114{
1115 struct list_head *lpfc_nvmet_sgl_list;
1116 struct lpfc_sglq *sglq = NULL;
1117
1118 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1119
1120 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1121
1122 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1123 if (!sglq)
1124 return NULL;
1125 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1126 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001127 return sglq;
1128}
1129
James Smarte59058c2008-08-24 21:49:00 -04001130/**
James Smart3621a712009-04-06 18:47:14 -04001131 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001132 * @phba: Pointer to HBA context object.
1133 *
1134 * This function is called with no lock held. This function
1135 * allocates a new driver iocb object from the iocb pool. If the
1136 * allocation is successful, it returns pointer to the newly
1137 * allocated iocb object else it returns NULL.
1138 **/
James Smart2e0fef82007-06-17 19:56:36 -05001139struct lpfc_iocbq *
1140lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001141{
James Smart2e0fef82007-06-17 19:56:36 -05001142 struct lpfc_iocbq * iocbq = NULL;
1143 unsigned long iflags;
1144
1145 spin_lock_irqsave(&phba->hbalock, iflags);
1146 iocbq = __lpfc_sli_get_iocbq(phba);
1147 spin_unlock_irqrestore(&phba->hbalock, iflags);
1148 return iocbq;
1149}
1150
James Smarte59058c2008-08-24 21:49:00 -04001151/**
James Smart4f774512009-05-22 14:52:35 -04001152 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1153 * @phba: Pointer to HBA context object.
1154 * @iocbq: Pointer to driver iocb object.
1155 *
1156 * This function is called with hbalock held to release driver
1157 * iocb object to the iocb pool. The iotag in the iocb object
1158 * does not change for each use of the iocb object. This function
1159 * clears all other fields of the iocb object when it is freed.
1160 * The sqlq structure that holds the xritag and phys and virtual
1161 * mappings for the scatter gather list is retrieved from the
1162 * active array of sglq. The get of the sglq pointer also clears
1163 * the entry in the array. If the status of the IO indiactes that
1164 * this IO was aborted then the sglq entry it put on the
1165 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1166 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001167 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001168 **/
1169static void
1170__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1171{
1172 struct lpfc_sglq *sglq;
1173 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001174 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001175 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001176
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001177 lockdep_assert_held(&phba->hbalock);
1178
James Smart4f774512009-05-22 14:52:35 -04001179 if (iocbq->sli4_xritag == NO_XRI)
1180 sglq = NULL;
1181 else
James Smart6d368e52011-05-24 11:44:12 -04001182 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1183
James Smart0e9bb8d2013-03-01 16:35:12 -05001184
James Smart4f774512009-05-22 14:52:35 -04001185 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001186 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1187 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1188 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001189 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001190 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001191 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001192 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1193 spin_unlock_irqrestore(
1194 &phba->sli4_hba.sgl_list_lock, iflag);
1195 goto out;
1196 }
1197
James Smart895427b2017-02-12 13:52:30 -08001198 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001199 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1200 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001201 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1202 iflag);
James Smart341af102010-01-26 23:07:37 -05001203 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001204 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001205 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001206 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001207 } else {
James Smart895427b2017-02-12 13:52:30 -08001208 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1209 iflag);
James Smart4f774512009-05-22 14:52:35 -04001210 sglq->state = SGL_FREED;
1211 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001212 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001213 &phba->sli4_hba.lpfc_els_sgl_list);
1214 spin_unlock_irqrestore(
1215 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001216
1217 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001218 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001219 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001220 }
James Smart4f774512009-05-22 14:52:35 -04001221 }
1222
James Smartf358dd02017-02-12 13:52:34 -08001223out:
James Smart4f774512009-05-22 14:52:35 -04001224 /*
1225 * Clean all volatile data fields, preserve iotag and node struct.
1226 */
1227 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001228 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001229 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001230 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1231 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001232 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1233}
1234
James Smart2a9bf3d2010-06-07 15:24:45 -04001235
James Smart4f774512009-05-22 14:52:35 -04001236/**
James Smart3772a992009-05-22 14:50:54 -04001237 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1238 * @phba: Pointer to HBA context object.
1239 * @iocbq: Pointer to driver iocb object.
1240 *
1241 * This function is called with hbalock held to release driver
1242 * iocb object to the iocb pool. The iotag in the iocb object
1243 * does not change for each use of the iocb object. This function
1244 * clears all other fields of the iocb object when it is freed.
1245 **/
1246static void
1247__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1248{
1249 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1250
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001251 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001252
1253 /*
James Smart3772a992009-05-22 14:50:54 -04001254 * Clean all volatile data fields, preserve iotag and node struct.
1255 */
1256 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1257 iocbq->sli4_xritag = NO_XRI;
1258 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1259}
1260
1261/**
James Smart3621a712009-04-06 18:47:14 -04001262 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001263 * @phba: Pointer to HBA context object.
1264 * @iocbq: Pointer to driver iocb object.
1265 *
1266 * This function is called with hbalock held to release driver
1267 * iocb object to the iocb pool. The iotag in the iocb object
1268 * does not change for each use of the iocb object. This function
1269 * clears all other fields of the iocb object when it is freed.
1270 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001271static void
James Smart2e0fef82007-06-17 19:56:36 -05001272__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1273{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001274 lockdep_assert_held(&phba->hbalock);
1275
James Smart3772a992009-05-22 14:50:54 -04001276 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001277 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001278}
1279
James Smarte59058c2008-08-24 21:49:00 -04001280/**
James Smart3621a712009-04-06 18:47:14 -04001281 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001282 * @phba: Pointer to HBA context object.
1283 * @iocbq: Pointer to driver iocb object.
1284 *
1285 * This function is called with no lock held to release the iocb to
1286 * iocb pool.
1287 **/
James Smart2e0fef82007-06-17 19:56:36 -05001288void
1289lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1290{
1291 unsigned long iflags;
1292
1293 /*
1294 * Clean all volatile data fields, preserve iotag and node struct.
1295 */
1296 spin_lock_irqsave(&phba->hbalock, iflags);
1297 __lpfc_sli_release_iocbq(phba, iocbq);
1298 spin_unlock_irqrestore(&phba->hbalock, iflags);
1299}
1300
James Smarte59058c2008-08-24 21:49:00 -04001301/**
James Smarta257bf92009-04-06 18:48:10 -04001302 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1303 * @phba: Pointer to HBA context object.
1304 * @iocblist: List of IOCBs.
1305 * @ulpstatus: ULP status in IOCB command field.
1306 * @ulpWord4: ULP word-4 in IOCB command field.
1307 *
1308 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1309 * on the list by invoking the complete callback function associated with the
1310 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1311 * fields.
1312 **/
1313void
1314lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1315 uint32_t ulpstatus, uint32_t ulpWord4)
1316{
1317 struct lpfc_iocbq *piocb;
1318
1319 while (!list_empty(iocblist)) {
1320 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001321 if (!piocb->iocb_cmpl)
1322 lpfc_sli_release_iocbq(phba, piocb);
1323 else {
1324 piocb->iocb.ulpStatus = ulpstatus;
1325 piocb->iocb.un.ulpWord[4] = ulpWord4;
1326 (piocb->iocb_cmpl) (phba, piocb, piocb);
1327 }
1328 }
1329 return;
1330}
1331
1332/**
James Smart3621a712009-04-06 18:47:14 -04001333 * lpfc_sli_iocb_cmd_type - Get the iocb type
1334 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001335 *
1336 * This function is called by ring event handler function to get the iocb type.
1337 * This function translates the iocb command to an iocb command type used to
1338 * decide the final disposition of each completed IOCB.
1339 * The function returns
1340 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1341 * LPFC_SOL_IOCB if it is a solicited iocb completion
1342 * LPFC_ABORT_IOCB if it is an abort iocb
1343 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1344 *
1345 * The caller is not required to hold any lock.
1346 **/
dea31012005-04-17 16:05:31 -05001347static lpfc_iocb_type
1348lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1349{
1350 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1351
1352 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1353 return 0;
1354
1355 switch (iocb_cmnd) {
1356 case CMD_XMIT_SEQUENCE_CR:
1357 case CMD_XMIT_SEQUENCE_CX:
1358 case CMD_XMIT_BCAST_CN:
1359 case CMD_XMIT_BCAST_CX:
1360 case CMD_ELS_REQUEST_CR:
1361 case CMD_ELS_REQUEST_CX:
1362 case CMD_CREATE_XRI_CR:
1363 case CMD_CREATE_XRI_CX:
1364 case CMD_GET_RPI_CN:
1365 case CMD_XMIT_ELS_RSP_CX:
1366 case CMD_GET_RPI_CR:
1367 case CMD_FCP_IWRITE_CR:
1368 case CMD_FCP_IWRITE_CX:
1369 case CMD_FCP_IREAD_CR:
1370 case CMD_FCP_IREAD_CX:
1371 case CMD_FCP_ICMND_CR:
1372 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001373 case CMD_FCP_TSEND_CX:
1374 case CMD_FCP_TRSP_CX:
1375 case CMD_FCP_TRECEIVE_CX:
1376 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001377 case CMD_ADAPTER_MSG:
1378 case CMD_ADAPTER_DUMP:
1379 case CMD_XMIT_SEQUENCE64_CR:
1380 case CMD_XMIT_SEQUENCE64_CX:
1381 case CMD_XMIT_BCAST64_CN:
1382 case CMD_XMIT_BCAST64_CX:
1383 case CMD_ELS_REQUEST64_CR:
1384 case CMD_ELS_REQUEST64_CX:
1385 case CMD_FCP_IWRITE64_CR:
1386 case CMD_FCP_IWRITE64_CX:
1387 case CMD_FCP_IREAD64_CR:
1388 case CMD_FCP_IREAD64_CX:
1389 case CMD_FCP_ICMND64_CR:
1390 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001391 case CMD_FCP_TSEND64_CX:
1392 case CMD_FCP_TRSP64_CX:
1393 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001394 case CMD_GEN_REQUEST64_CR:
1395 case CMD_GEN_REQUEST64_CX:
1396 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001397 case DSSCMD_IWRITE64_CR:
1398 case DSSCMD_IWRITE64_CX:
1399 case DSSCMD_IREAD64_CR:
1400 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001401 type = LPFC_SOL_IOCB;
1402 break;
1403 case CMD_ABORT_XRI_CN:
1404 case CMD_ABORT_XRI_CX:
1405 case CMD_CLOSE_XRI_CN:
1406 case CMD_CLOSE_XRI_CX:
1407 case CMD_XRI_ABORTED_CX:
1408 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001409 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001410 type = LPFC_ABORT_IOCB;
1411 break;
1412 case CMD_RCV_SEQUENCE_CX:
1413 case CMD_RCV_ELS_REQ_CX:
1414 case CMD_RCV_SEQUENCE64_CX:
1415 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001416 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001417 case CMD_IOCB_RCV_SEQ64_CX:
1418 case CMD_IOCB_RCV_ELS64_CX:
1419 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001420 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001421 type = LPFC_UNSOL_IOCB;
1422 break;
James Smart3163f722008-02-08 18:50:25 -05001423 case CMD_IOCB_XMIT_MSEQ64_CR:
1424 case CMD_IOCB_XMIT_MSEQ64_CX:
1425 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1426 case CMD_IOCB_RCV_ELS_LIST64_CX:
1427 case CMD_IOCB_CLOSE_EXTENDED_CN:
1428 case CMD_IOCB_ABORT_EXTENDED_CN:
1429 case CMD_IOCB_RET_HBQE64_CN:
1430 case CMD_IOCB_FCP_IBIDIR64_CR:
1431 case CMD_IOCB_FCP_IBIDIR64_CX:
1432 case CMD_IOCB_FCP_ITASKMGT64_CX:
1433 case CMD_IOCB_LOGENTRY_CN:
1434 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1435 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001436 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001437 type = LPFC_UNKNOWN_IOCB;
1438 break;
dea31012005-04-17 16:05:31 -05001439 default:
1440 type = LPFC_UNKNOWN_IOCB;
1441 break;
1442 }
1443
1444 return type;
1445}
1446
James Smarte59058c2008-08-24 21:49:00 -04001447/**
James Smart3621a712009-04-06 18:47:14 -04001448 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001449 * @phba: Pointer to HBA context object.
1450 *
1451 * This function is called from SLI initialization code
1452 * to configure every ring of the HBA's SLI interface. The
1453 * caller is not required to hold any lock. This function issues
1454 * a config_ring mailbox command for each ring.
1455 * This function returns zero if successful else returns a negative
1456 * error code.
1457 **/
dea31012005-04-17 16:05:31 -05001458static int
James Smarted957682007-06-17 19:56:37 -05001459lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001460{
1461 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001462 LPFC_MBOXQ_t *pmb;
1463 MAILBOX_t *pmbox;
1464 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001465
James Smarted957682007-06-17 19:56:37 -05001466 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1467 if (!pmb)
1468 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001469 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001470 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001471 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001472 lpfc_config_ring(phba, i, pmb);
1473 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1474 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001476 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001477 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1478 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001479 rc, pmbox->mbxCommand,
1480 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001481 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001482 ret = -ENXIO;
1483 break;
dea31012005-04-17 16:05:31 -05001484 }
1485 }
James Smarted957682007-06-17 19:56:37 -05001486 mempool_free(pmb, phba->mbox_mem_pool);
1487 return ret;
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_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001492 * @phba: Pointer to HBA context object.
1493 * @pring: Pointer to driver SLI ring object.
1494 * @piocb: Pointer to the driver iocb object.
1495 *
1496 * This function is called with hbalock held. The function adds the
1497 * new iocb to txcmplq of the given ring. This function always returns
1498 * 0. If this function is called for ELS ring, this function checks if
1499 * there is a vport associated with the ELS command. This function also
1500 * starts els_tmofunc timer if this is an ELS command.
1501 **/
dea31012005-04-17 16:05:31 -05001502static int
James Smart2e0fef82007-06-17 19:56:36 -05001503lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1504 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001505{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001506 lockdep_assert_held(&phba->hbalock);
1507
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001508 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001509
dea31012005-04-17 16:05:31 -05001510 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001511 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001512
James Smart92d7f7b2007-06-17 19:56:38 -05001513 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1514 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001515 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1516 BUG_ON(!piocb->vport);
1517 if (!(piocb->vport->load_flag & FC_UNLOADING))
1518 mod_timer(&piocb->vport->els_tmofunc,
1519 jiffies +
1520 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1521 }
dea31012005-04-17 16:05:31 -05001522
James Smart2e0fef82007-06-17 19:56:36 -05001523 return 0;
dea31012005-04-17 16:05:31 -05001524}
1525
James Smarte59058c2008-08-24 21:49:00 -04001526/**
James Smart3621a712009-04-06 18:47:14 -04001527 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001528 * @phba: Pointer to HBA context object.
1529 * @pring: Pointer to driver SLI ring object.
1530 *
1531 * This function is called with hbalock held to get next
1532 * iocb in txq of the given ring. If there is any iocb in
1533 * the txq, the function returns first iocb in the list after
1534 * removing the iocb from the list, else it returns NULL.
1535 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001536struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001537lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001538{
dea31012005-04-17 16:05:31 -05001539 struct lpfc_iocbq *cmd_iocb;
1540
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001541 lockdep_assert_held(&phba->hbalock);
1542
James Smart858c9f62007-06-17 19:56:39 -05001543 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001544 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001545}
1546
James Smarte59058c2008-08-24 21:49:00 -04001547/**
James Smart3621a712009-04-06 18:47:14 -04001548 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001549 * @phba: Pointer to HBA context object.
1550 * @pring: Pointer to driver SLI ring object.
1551 *
1552 * This function is called with hbalock held and the caller must post the
1553 * iocb without releasing the lock. If the caller releases the lock,
1554 * iocb slot returned by the function is not guaranteed to be available.
1555 * The function returns pointer to the next available iocb slot if there
1556 * is available slot in the ring, else it returns NULL.
1557 * If the get index of the ring is ahead of the put index, the function
1558 * will post an error attention event to the worker thread to take the
1559 * HBA to offline state.
1560 **/
dea31012005-04-17 16:05:31 -05001561static IOCB_t *
1562lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1563{
James Smart34b02dc2008-08-24 21:49:55 -04001564 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001565 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001566
1567 lockdep_assert_held(&phba->hbalock);
1568
James Smart7e56aa22012-08-03 12:35:34 -04001569 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1570 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1571 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001572
James Smart7e56aa22012-08-03 12:35:34 -04001573 if (unlikely(pring->sli.sli3.local_getidx ==
1574 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001575
James Smart7e56aa22012-08-03 12:35:34 -04001576 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001577
James Smart7e56aa22012-08-03 12:35:34 -04001578 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001579 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001580 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001581 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001582 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001583 pring->sli.sli3.local_getidx,
1584 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001585
James Smart2e0fef82007-06-17 19:56:36 -05001586 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001587 /*
1588 * All error attention handlers are posted to
1589 * worker thread
1590 */
1591 phba->work_ha |= HA_ERATT;
1592 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001593
James Smart5e9d9b82008-06-14 22:52:53 -04001594 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001595
1596 return NULL;
1597 }
1598
James Smart7e56aa22012-08-03 12:35:34 -04001599 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001600 return NULL;
1601 }
1602
James Smarted957682007-06-17 19:56:37 -05001603 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001604}
1605
James Smarte59058c2008-08-24 21:49:00 -04001606/**
James Smart3621a712009-04-06 18:47:14 -04001607 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001608 * @phba: Pointer to HBA context object.
1609 * @iocbq: Pointer to driver iocb object.
1610 *
1611 * This function gets an iotag for the iocb. If there is no unused iotag and
1612 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1613 * array and assigns a new iotag.
1614 * The function returns the allocated iotag if successful, else returns zero.
1615 * Zero is not a valid iotag.
1616 * The caller is not required to hold any lock.
1617 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001618uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001619lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001620{
James Smart2e0fef82007-06-17 19:56:36 -05001621 struct lpfc_iocbq **new_arr;
1622 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001623 size_t new_len;
1624 struct lpfc_sli *psli = &phba->sli;
1625 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001626
James Smart2e0fef82007-06-17 19:56:36 -05001627 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001628 iotag = psli->last_iotag;
1629 if(++iotag < psli->iocbq_lookup_len) {
1630 psli->last_iotag = iotag;
1631 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001632 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001633 iocbq->iotag = iotag;
1634 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001635 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001636 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1637 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001638 spin_unlock_irq(&phba->hbalock);
1639 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001640 GFP_KERNEL);
1641 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001642 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001643 old_arr = psli->iocbq_lookup;
1644 if (new_len <= psli->iocbq_lookup_len) {
1645 /* highly unprobable case */
1646 kfree(new_arr);
1647 iotag = psli->last_iotag;
1648 if(++iotag < psli->iocbq_lookup_len) {
1649 psli->last_iotag = iotag;
1650 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001651 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001652 iocbq->iotag = iotag;
1653 return iotag;
1654 }
James Smart2e0fef82007-06-17 19:56:36 -05001655 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001656 return 0;
1657 }
1658 if (psli->iocbq_lookup)
1659 memcpy(new_arr, old_arr,
1660 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001661 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001662 psli->iocbq_lookup = new_arr;
1663 psli->iocbq_lookup_len = new_len;
1664 psli->last_iotag = iotag;
1665 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001666 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001667 iocbq->iotag = iotag;
1668 kfree(old_arr);
1669 return iotag;
1670 }
James Smart8f6d98d2006-08-01 07:34:00 -04001671 } else
James Smart2e0fef82007-06-17 19:56:36 -05001672 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001673
James Smartbc739052010-08-04 16:11:18 -04001674 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001675 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1676 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001677
James Bottomley604a3e32005-10-29 10:28:33 -05001678 return 0;
dea31012005-04-17 16:05:31 -05001679}
1680
James Smarte59058c2008-08-24 21:49:00 -04001681/**
James Smart3621a712009-04-06 18:47:14 -04001682 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001683 * @phba: Pointer to HBA context object.
1684 * @pring: Pointer to driver SLI ring object.
1685 * @iocb: Pointer to iocb slot in the ring.
1686 * @nextiocb: Pointer to driver iocb object which need to be
1687 * posted to firmware.
1688 *
1689 * This function is called with hbalock held to post a new iocb to
1690 * the firmware. This function copies the new iocb to ring iocb slot and
1691 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1692 * a completion call back for this iocb else the function will free the
1693 * iocb object.
1694 **/
dea31012005-04-17 16:05:31 -05001695static void
1696lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1697 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1698{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001699 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001700 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001701 * Set up an iotag
dea31012005-04-17 16:05:31 -05001702 */
James Bottomley604a3e32005-10-29 10:28:33 -05001703 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001704
James Smarte2a0a9d2008-12-04 22:40:02 -05001705
James Smarta58cbd52007-08-02 11:09:43 -04001706 if (pring->ringno == LPFC_ELS_RING) {
1707 lpfc_debugfs_slow_ring_trc(phba,
1708 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1709 *(((uint32_t *) &nextiocb->iocb) + 4),
1710 *(((uint32_t *) &nextiocb->iocb) + 6),
1711 *(((uint32_t *) &nextiocb->iocb) + 7));
1712 }
1713
dea31012005-04-17 16:05:31 -05001714 /*
1715 * Issue iocb command to adapter
1716 */
James Smart92d7f7b2007-06-17 19:56:38 -05001717 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001718 wmb();
1719 pring->stats.iocb_cmd++;
1720
1721 /*
1722 * If there is no completion routine to call, we can release the
1723 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1724 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1725 */
1726 if (nextiocb->iocb_cmpl)
1727 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001728 else
James Smart2e0fef82007-06-17 19:56:36 -05001729 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001730
1731 /*
1732 * Let the HBA know what IOCB slot will be the next one the
1733 * driver will put a command into.
1734 */
James Smart7e56aa22012-08-03 12:35:34 -04001735 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1736 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001737}
1738
James Smarte59058c2008-08-24 21:49:00 -04001739/**
James Smart3621a712009-04-06 18:47:14 -04001740 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001741 * @phba: Pointer to HBA context object.
1742 * @pring: Pointer to driver SLI ring object.
1743 *
1744 * The caller is not required to hold any lock for calling this function.
1745 * This function updates the chip attention bits for the ring to inform firmware
1746 * that there are pending work to be done for this ring and requests an
1747 * interrupt when there is space available in the ring. This function is
1748 * called when the driver is unable to post more iocbs to the ring due
1749 * to unavailability of space in the ring.
1750 **/
dea31012005-04-17 16:05:31 -05001751static void
James Smart2e0fef82007-06-17 19:56:36 -05001752lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001753{
1754 int ringno = pring->ringno;
1755
1756 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1757
1758 wmb();
1759
1760 /*
1761 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1762 * The HBA will tell us when an IOCB entry is available.
1763 */
1764 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1765 readl(phba->CAregaddr); /* flush */
1766
1767 pring->stats.iocb_cmd_full++;
1768}
1769
James Smarte59058c2008-08-24 21:49:00 -04001770/**
James Smart3621a712009-04-06 18:47:14 -04001771 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001772 * @phba: Pointer to HBA context object.
1773 * @pring: Pointer to driver SLI ring object.
1774 *
1775 * This function updates the chip attention register bit for the
1776 * given ring to inform HBA that there is more work to be done
1777 * in this ring. The caller is not required to hold any lock.
1778 **/
dea31012005-04-17 16:05:31 -05001779static void
James Smart2e0fef82007-06-17 19:56:36 -05001780lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001781{
1782 int ringno = pring->ringno;
1783
1784 /*
1785 * Tell the HBA that there is work to do in this ring.
1786 */
James Smart34b02dc2008-08-24 21:49:55 -04001787 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1788 wmb();
1789 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1790 readl(phba->CAregaddr); /* flush */
1791 }
dea31012005-04-17 16:05:31 -05001792}
1793
James Smarte59058c2008-08-24 21:49:00 -04001794/**
James Smart3621a712009-04-06 18:47:14 -04001795 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001796 * @phba: Pointer to HBA context object.
1797 * @pring: Pointer to driver SLI ring object.
1798 *
1799 * This function is called with hbalock held to post pending iocbs
1800 * in the txq to the firmware. This function is called when driver
1801 * detects space available in the ring.
1802 **/
dea31012005-04-17 16:05:31 -05001803static void
James Smart2e0fef82007-06-17 19:56:36 -05001804lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001805{
1806 IOCB_t *iocb;
1807 struct lpfc_iocbq *nextiocb;
1808
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001809 lockdep_assert_held(&phba->hbalock);
1810
dea31012005-04-17 16:05:31 -05001811 /*
1812 * Check to see if:
1813 * (a) there is anything on the txq to send
1814 * (b) link is up
1815 * (c) link attention events can be processed (fcp ring only)
1816 * (d) IOCB processing is not blocked by the outstanding mbox command.
1817 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001818
1819 if (lpfc_is_link_up(phba) &&
1820 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001821 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001822 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001823
1824 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1825 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1826 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1827
1828 if (iocb)
1829 lpfc_sli_update_ring(phba, pring);
1830 else
1831 lpfc_sli_update_full_ring(phba, pring);
1832 }
1833
1834 return;
1835}
1836
James Smarte59058c2008-08-24 21:49:00 -04001837/**
James Smart3621a712009-04-06 18:47:14 -04001838 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001839 * @phba: Pointer to HBA context object.
1840 * @hbqno: HBQ number.
1841 *
1842 * This function is called with hbalock held to get the next
1843 * available slot for the given HBQ. If there is free slot
1844 * available for the HBQ it will return pointer to the next available
1845 * HBQ entry else it will return NULL.
1846 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001847static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001848lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1849{
1850 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1851
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001852 lockdep_assert_held(&phba->hbalock);
1853
James Smarted957682007-06-17 19:56:37 -05001854 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1855 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1856 hbqp->next_hbqPutIdx = 0;
1857
1858 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001859 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001860 uint32_t getidx = le32_to_cpu(raw_index);
1861
1862 hbqp->local_hbqGetIdx = getidx;
1863
1864 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1865 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001866 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001867 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001868 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001869 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001870 hbqp->entry_count);
1871
1872 phba->link_state = LPFC_HBA_ERROR;
1873 return NULL;
1874 }
1875
1876 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1877 return NULL;
1878 }
1879
James Smart51ef4c22007-08-02 11:10:31 -04001880 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1881 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001882}
1883
James Smarte59058c2008-08-24 21:49:00 -04001884/**
James Smart3621a712009-04-06 18:47:14 -04001885 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001886 * @phba: Pointer to HBA context object.
1887 *
1888 * This function is called with no lock held to free all the
1889 * hbq buffers while uninitializing the SLI interface. It also
1890 * frees the HBQ buffers returned by the firmware but not yet
1891 * processed by the upper layers.
1892 **/
James Smarted957682007-06-17 19:56:37 -05001893void
1894lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1895{
James Smart92d7f7b2007-06-17 19:56:38 -05001896 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1897 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001898 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001899 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001900
James Smart51ef4c22007-08-02 11:10:31 -04001901 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001902 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001903 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001904 for (i = 0; i < hbq_count; ++i) {
1905 list_for_each_entry_safe(dmabuf, next_dmabuf,
1906 &phba->hbqs[i].hbq_buffer_list, list) {
1907 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1908 list_del(&hbq_buf->dbuf.list);
1909 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1910 }
James Smarta8adb832007-10-27 13:37:53 -04001911 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001912 }
James Smart3163f722008-02-08 18:50:25 -05001913
1914 /* Mark the HBQs not in use */
1915 phba->hbq_in_use = 0;
1916 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001917}
1918
James Smarte59058c2008-08-24 21:49:00 -04001919/**
James Smart3621a712009-04-06 18:47:14 -04001920 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001921 * @phba: Pointer to HBA context object.
1922 * @hbqno: HBQ number.
1923 * @hbq_buf: Pointer to HBQ buffer.
1924 *
1925 * This function is called with the hbalock held to post a
1926 * hbq buffer to the firmware. If the function finds an empty
1927 * slot in the HBQ, it will post the buffer. The function will return
1928 * pointer to the hbq entry if it successfully post the buffer
1929 * else it will return NULL.
1930 **/
James Smart3772a992009-05-22 14:50:54 -04001931static int
James Smarted957682007-06-17 19:56:37 -05001932lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001933 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001934{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001935 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001936 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1937}
1938
1939/**
1940 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1941 * @phba: Pointer to HBA context object.
1942 * @hbqno: HBQ number.
1943 * @hbq_buf: Pointer to HBQ buffer.
1944 *
1945 * This function is called with the hbalock held to post a hbq buffer to the
1946 * firmware. If the function finds an empty slot in the HBQ, it will post the
1947 * buffer and place it on the hbq_buffer_list. The function will return zero if
1948 * it successfully post the buffer else it will return an error.
1949 **/
1950static int
1951lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1952 struct hbq_dmabuf *hbq_buf)
1953{
James Smarted957682007-06-17 19:56:37 -05001954 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001955 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001956
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001957 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001958 /* Get next HBQ entry slot to use */
1959 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1960 if (hbqe) {
1961 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1962
James Smart92d7f7b2007-06-17 19:56:38 -05001963 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1964 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08001965 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05001966 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001967 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1968 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1969 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001970 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1971 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001972 /* flush */
James Smarted957682007-06-17 19:56:37 -05001973 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001974 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001975 return 0;
1976 } else
1977 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001978}
1979
James Smart4f774512009-05-22 14:52:35 -04001980/**
1981 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1982 * @phba: Pointer to HBA context object.
1983 * @hbqno: HBQ number.
1984 * @hbq_buf: Pointer to HBQ buffer.
1985 *
1986 * This function is called with the hbalock held to post an RQE to the SLI4
1987 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1988 * the hbq_buffer_list and return zero, otherwise it will return an error.
1989 **/
1990static int
1991lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1992 struct hbq_dmabuf *hbq_buf)
1993{
1994 int rc;
1995 struct lpfc_rqe hrqe;
1996 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08001997 struct lpfc_queue *hrq;
1998 struct lpfc_queue *drq;
1999
2000 if (hbqno != LPFC_ELS_HBQ)
2001 return 1;
2002 hrq = phba->sli4_hba.hdr_rq;
2003 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04002004
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002005 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04002006 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
2007 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
2008 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
2009 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08002010 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04002011 if (rc < 0)
2012 return rc;
James Smart895427b2017-02-12 13:52:30 -08002013 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04002014 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
2015 return 0;
2016}
2017
James Smarte59058c2008-08-24 21:49:00 -04002018/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05002019static struct lpfc_hbq_init lpfc_els_hbq = {
2020 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05002021 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05002022 .mask_count = 0,
2023 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04002024 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05002025 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04002026 .init_count = 40,
2027 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05002028};
James Smarted957682007-06-17 19:56:37 -05002029
James Smarte59058c2008-08-24 21:49:00 -04002030/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04002031struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05002032 &lpfc_els_hbq,
2033};
2034
James Smarte59058c2008-08-24 21:49:00 -04002035/**
James Smart3621a712009-04-06 18:47:14 -04002036 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04002037 * @phba: Pointer to HBA context object.
2038 * @hbqno: HBQ number.
2039 * @count: Number of HBQ buffers to be posted.
2040 *
James Smartd7c255b2008-08-24 21:50:00 -04002041 * This function is called with no lock held to post more hbq buffers to the
2042 * given HBQ. The function returns the number of HBQ buffers successfully
2043 * posted.
James Smarte59058c2008-08-24 21:49:00 -04002044 **/
James Smart311464e2007-08-02 11:10:37 -04002045static int
James Smart92d7f7b2007-06-17 19:56:38 -05002046lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
2047{
James Smartd7c255b2008-08-24 21:50:00 -04002048 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05002049 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05002050 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04002051 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002052 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04002053 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04002054
James Smartd7c255b2008-08-24 21:50:00 -04002055 if ((phba->hbqs[hbqno].buffer_count + count) >
2056 lpfc_hbq_defs[hbqno]->entry_count)
2057 count = lpfc_hbq_defs[hbqno]->entry_count -
2058 phba->hbqs[hbqno].buffer_count;
2059 if (!count)
2060 return 0;
2061 /* Allocate HBQ entries */
2062 for (i = 0; i < count; i++) {
2063 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
2064 if (!hbq_buffer)
2065 break;
2066 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
2067 }
James Smart3163f722008-02-08 18:50:25 -05002068 /* Check whether HBQ is still in use */
2069 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07002070 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04002071 goto err;
2072 while (!list_empty(&hbq_buf_list)) {
2073 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2074 dbuf.list);
2075 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
2076 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04002077 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04002078 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04002079 posted++;
2080 } else
James Smart51ef4c22007-08-02 11:10:31 -04002081 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05002082 }
James Smart3163f722008-02-08 18:50:25 -05002083 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04002084 return posted;
2085err:
2086 spin_unlock_irqrestore(&phba->hbalock, flags);
2087 while (!list_empty(&hbq_buf_list)) {
2088 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
2089 dbuf.list);
2090 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2091 }
James Smart92d7f7b2007-06-17 19:56:38 -05002092 return 0;
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_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04002097 * @phba: Pointer to HBA context object.
2098 * @qno: HBQ number.
2099 *
2100 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04002101 * is called with no lock held. The function returns the number of HBQ entries
2102 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002103 **/
James Smarted957682007-06-17 19:56:37 -05002104int
James Smart92d7f7b2007-06-17 19:56:38 -05002105lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002106{
James Smartdef9c7a2009-12-21 17:02:28 -05002107 if (phba->sli_rev == LPFC_SLI_REV4)
2108 return 0;
2109 else
2110 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2111 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05002112}
2113
James Smarte59058c2008-08-24 21:49:00 -04002114/**
James Smart3621a712009-04-06 18:47:14 -04002115 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002116 * @phba: Pointer to HBA context object.
2117 * @qno: HBQ queue number.
2118 *
2119 * This function is called from SLI initialization code path with
2120 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002121 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002122 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002123static int
James Smart92d7f7b2007-06-17 19:56:38 -05002124lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002125{
James Smartdef9c7a2009-12-21 17:02:28 -05002126 if (phba->sli_rev == LPFC_SLI_REV4)
2127 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002128 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002129 else
2130 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2131 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002132}
2133
James Smarte59058c2008-08-24 21:49:00 -04002134/**
James Smart3772a992009-05-22 14:50:54 -04002135 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2136 * @phba: Pointer to HBA context object.
2137 * @hbqno: HBQ number.
2138 *
2139 * This function removes the first hbq buffer on an hbq list and returns a
2140 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2141 **/
2142static struct hbq_dmabuf *
2143lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2144{
2145 struct lpfc_dmabuf *d_buf;
2146
2147 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2148 if (!d_buf)
2149 return NULL;
2150 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2151}
2152
2153/**
James Smart2d7dbc42017-02-12 13:52:35 -08002154 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2155 * @phba: Pointer to HBA context object.
2156 * @hbqno: HBQ number.
2157 *
2158 * This function removes the first RQ buffer on an RQ buffer list and returns a
2159 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2160 **/
2161static struct rqb_dmabuf *
2162lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2163{
2164 struct lpfc_dmabuf *h_buf;
2165 struct lpfc_rqb *rqbp;
2166
2167 rqbp = hrq->rqbp;
2168 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2169 struct lpfc_dmabuf, list);
2170 if (!h_buf)
2171 return NULL;
2172 rqbp->buffer_count--;
2173 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2174}
2175
2176/**
James Smart3621a712009-04-06 18:47:14 -04002177 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002178 * @phba: Pointer to HBA context object.
2179 * @tag: Tag of the hbq buffer.
2180 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002181 * This function searches for the hbq buffer associated with the given tag in
2182 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2183 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002184 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002185static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002186lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2187{
James Smart92d7f7b2007-06-17 19:56:38 -05002188 struct lpfc_dmabuf *d_buf;
2189 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002190 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002191
James Smart51ef4c22007-08-02 11:10:31 -04002192 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002193 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002194 return NULL;
2195
James Smart3772a992009-05-22 14:50:54 -04002196 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002197 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002198 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002199 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002200 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002201 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002202 }
2203 }
James Smart3772a992009-05-22 14:50:54 -04002204 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002205 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002206 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002207 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002208 return NULL;
James Smarted957682007-06-17 19:56:37 -05002209}
2210
James Smarte59058c2008-08-24 21:49:00 -04002211/**
James Smart3621a712009-04-06 18:47:14 -04002212 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002213 * @phba: Pointer to HBA context object.
2214 * @hbq_buffer: Pointer to HBQ buffer.
2215 *
2216 * This function is called with hbalock. This function gives back
2217 * the hbq buffer to firmware. If the HBQ does not have space to
2218 * post the buffer, it will free the buffer.
2219 **/
James Smarted957682007-06-17 19:56:37 -05002220void
James Smart51ef4c22007-08-02 11:10:31 -04002221lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002222{
2223 uint32_t hbqno;
2224
James Smart51ef4c22007-08-02 11:10:31 -04002225 if (hbq_buffer) {
2226 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002227 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002228 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002229 }
2230}
2231
James Smarte59058c2008-08-24 21:49:00 -04002232/**
James Smart3621a712009-04-06 18:47:14 -04002233 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002234 * @mbxCommand: mailbox command code.
2235 *
2236 * This function is called by the mailbox event handler function to verify
2237 * that the completed mailbox command is a legitimate mailbox command. If the
2238 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2239 * and the mailbox event handler will take the HBA offline.
2240 **/
dea31012005-04-17 16:05:31 -05002241static int
2242lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2243{
2244 uint8_t ret;
2245
2246 switch (mbxCommand) {
2247 case MBX_LOAD_SM:
2248 case MBX_READ_NV:
2249 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002250 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002251 case MBX_RUN_BIU_DIAG:
2252 case MBX_INIT_LINK:
2253 case MBX_DOWN_LINK:
2254 case MBX_CONFIG_LINK:
2255 case MBX_CONFIG_RING:
2256 case MBX_RESET_RING:
2257 case MBX_READ_CONFIG:
2258 case MBX_READ_RCONFIG:
2259 case MBX_READ_SPARM:
2260 case MBX_READ_STATUS:
2261 case MBX_READ_RPI:
2262 case MBX_READ_XRI:
2263 case MBX_READ_REV:
2264 case MBX_READ_LNK_STAT:
2265 case MBX_REG_LOGIN:
2266 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002267 case MBX_CLEAR_LA:
2268 case MBX_DUMP_MEMORY:
2269 case MBX_DUMP_CONTEXT:
2270 case MBX_RUN_DIAGS:
2271 case MBX_RESTART:
2272 case MBX_UPDATE_CFG:
2273 case MBX_DOWN_LOAD:
2274 case MBX_DEL_LD_ENTRY:
2275 case MBX_RUN_PROGRAM:
2276 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002277 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002278 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002279 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002280 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002281 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002282 case MBX_LOAD_AREA:
2283 case MBX_RUN_BIU_DIAG64:
2284 case MBX_CONFIG_PORT:
2285 case MBX_READ_SPARM64:
2286 case MBX_READ_RPI64:
2287 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002288 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002289 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002290 case MBX_SET_DEBUG:
2291 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002292 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002293 case MBX_REG_VPI:
2294 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002295 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002296 case MBX_PORT_CAPABILITIES:
2297 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002298 case MBX_SLI4_CONFIG:
2299 case MBX_SLI4_REQ_FTRS:
2300 case MBX_REG_FCFI:
2301 case MBX_UNREG_FCFI:
2302 case MBX_REG_VFI:
2303 case MBX_UNREG_VFI:
2304 case MBX_INIT_VPI:
2305 case MBX_INIT_VFI:
2306 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002307 case MBX_READ_EVENT_LOG_STATUS:
2308 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002309 case MBX_SECURITY_MGMT:
2310 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002311 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002312 ret = mbxCommand;
2313 break;
2314 default:
2315 ret = MBX_SHUTDOWN;
2316 break;
2317 }
James Smart2e0fef82007-06-17 19:56:36 -05002318 return ret;
dea31012005-04-17 16:05:31 -05002319}
James Smarte59058c2008-08-24 21:49:00 -04002320
2321/**
James Smart3621a712009-04-06 18:47:14 -04002322 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002323 * @phba: Pointer to HBA context object.
2324 * @pmboxq: Pointer to mailbox command.
2325 *
2326 * This is completion handler function for mailbox commands issued from
2327 * lpfc_sli_issue_mbox_wait function. This function is called by the
2328 * mailbox event handler function with no lock held. This function
2329 * will wake up thread waiting on the wait queue pointed by context1
2330 * of the mailbox.
2331 **/
James Smart04c68492009-05-22 14:52:52 -04002332void
James Smart2e0fef82007-06-17 19:56:36 -05002333lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002334{
2335 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002336 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002337
2338 /*
2339 * If pdone_q is empty, the driver thread gave up waiting and
2340 * continued running.
2341 */
James Smart7054a602007-04-25 09:52:34 -04002342 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002343 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002344 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2345 if (pdone_q)
2346 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002347 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002348 return;
2349}
2350
James Smarte59058c2008-08-24 21:49:00 -04002351
2352/**
James Smart3621a712009-04-06 18:47:14 -04002353 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002354 * @phba: Pointer to HBA context object.
2355 * @pmb: Pointer to mailbox object.
2356 *
2357 * This function is the default mailbox completion handler. It
2358 * frees the memory resources associated with the completed mailbox
2359 * command. If the completed command is a REG_LOGIN mailbox command,
2360 * this function will issue a UREG_LOGIN to re-claim the RPI.
2361 **/
dea31012005-04-17 16:05:31 -05002362void
James Smart2e0fef82007-06-17 19:56:36 -05002363lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002364{
James Smartd439d282010-09-29 11:18:45 -04002365 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002366 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002367 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002368 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002369 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002370 int rc;
2371
dea31012005-04-17 16:05:31 -05002372 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002373
dea31012005-04-17 16:05:31 -05002374 if (mp) {
2375 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2376 kfree(mp);
2377 }
James Smart7054a602007-04-25 09:52:34 -04002378
2379 /*
2380 * If a REG_LOGIN succeeded after node is destroyed or node
2381 * is in re-discovery driver need to cleanup the RPI.
2382 */
James Smart2e0fef82007-06-17 19:56:36 -05002383 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002384 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2385 !pmb->u.mb.mbxStatus) {
2386 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002387 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002388 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002389 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002390 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002391 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2392 if (rc != MBX_NOT_FINISHED)
2393 return;
2394 }
2395
James Smart695a8142010-01-26 23:08:03 -05002396 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2397 !(phba->pport->load_flag & FC_UNLOADING) &&
2398 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002399 shost = lpfc_shost_from_vport(vport);
2400 spin_lock_irq(shost->host_lock);
2401 vport->vpi_state |= LPFC_VPI_REGISTERED;
2402 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2403 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002404 }
2405
James Smartd439d282010-09-29 11:18:45 -04002406 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2407 ndlp = (struct lpfc_nodelist *)pmb->context2;
2408 lpfc_nlp_put(ndlp);
2409 pmb->context2 = NULL;
2410 }
2411
James Smartdcf2a4e2010-09-29 11:18:53 -04002412 /* Check security permission status on INIT_LINK mailbox command */
2413 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2414 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2415 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2416 "2860 SLI authentication is required "
2417 "for INIT_LINK but has not done yet\n");
2418
James Smart04c68492009-05-22 14:52:52 -04002419 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2420 lpfc_sli4_mbox_cmd_free(phba, pmb);
2421 else
2422 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002423}
James Smartbe6bb942015-04-07 15:07:22 -04002424 /**
2425 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2426 * @phba: Pointer to HBA context object.
2427 * @pmb: Pointer to mailbox object.
2428 *
2429 * This function is the unreg rpi mailbox completion handler. It
2430 * frees the memory resources associated with the completed mailbox
2431 * command. An additional refrenece is put on the ndlp to prevent
2432 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2433 * the unreg mailbox command completes, this routine puts the
2434 * reference back.
2435 *
2436 **/
2437void
2438lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2439{
2440 struct lpfc_vport *vport = pmb->vport;
2441 struct lpfc_nodelist *ndlp;
2442
2443 ndlp = pmb->context1;
2444 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2445 if (phba->sli_rev == LPFC_SLI_REV4 &&
2446 (bf_get(lpfc_sli_intf_if_type,
James Smart27d6ac02018-02-22 08:18:42 -08002447 &phba->sli4_hba.sli_intf) >=
James Smartbe6bb942015-04-07 15:07:22 -04002448 LPFC_SLI_INTF_IF_TYPE_2)) {
2449 if (ndlp) {
2450 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2451 "0010 UNREG_LOGIN vpi:%x "
2452 "rpi:%x DID:%x map:%x %p\n",
2453 vport->vpi, ndlp->nlp_rpi,
2454 ndlp->nlp_DID,
2455 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002456 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002457 lpfc_nlp_put(ndlp);
2458 }
2459 }
2460 }
2461
2462 mempool_free(pmb, phba->mbox_mem_pool);
2463}
dea31012005-04-17 16:05:31 -05002464
James Smarte59058c2008-08-24 21:49:00 -04002465/**
James Smart3621a712009-04-06 18:47:14 -04002466 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002467 * @phba: Pointer to HBA context object.
2468 *
2469 * This function is called with no lock held. This function processes all
2470 * the completed mailbox commands and gives it to upper layers. The interrupt
2471 * service routine processes mailbox completion interrupt and adds completed
2472 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2473 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2474 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2475 * function returns the mailbox commands to the upper layer by calling the
2476 * completion handler function of each mailbox.
2477 **/
dea31012005-04-17 16:05:31 -05002478int
James Smart2e0fef82007-06-17 19:56:36 -05002479lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002480{
James Smart92d7f7b2007-06-17 19:56:38 -05002481 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002482 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002483 int rc;
2484 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002485
2486 phba->sli.slistat.mbox_event++;
2487
James Smart92d7f7b2007-06-17 19:56:38 -05002488 /* Get all completed mailboxe buffers into the cmplq */
2489 spin_lock_irq(&phba->hbalock);
2490 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2491 spin_unlock_irq(&phba->hbalock);
2492
dea31012005-04-17 16:05:31 -05002493 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002494 do {
2495 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2496 if (pmb == NULL)
2497 break;
2498
James Smart04c68492009-05-22 14:52:52 -04002499 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002500
James Smart858c9f62007-06-17 19:56:39 -05002501 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2502 if (pmb->vport) {
2503 lpfc_debugfs_disc_trc(pmb->vport,
2504 LPFC_DISC_TRC_MBOX_VPORT,
2505 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2506 (uint32_t)pmbox->mbxCommand,
2507 pmbox->un.varWords[0],
2508 pmbox->un.varWords[1]);
2509 }
2510 else {
2511 lpfc_debugfs_disc_trc(phba->pport,
2512 LPFC_DISC_TRC_MBOX,
2513 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2514 (uint32_t)pmbox->mbxCommand,
2515 pmbox->un.varWords[0],
2516 pmbox->un.varWords[1]);
2517 }
2518 }
2519
dea31012005-04-17 16:05:31 -05002520 /*
2521 * It is a fatal error if unknown mbox command completion.
2522 */
2523 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2524 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002525 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002526 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002527 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002528 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002529 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002530 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002531 lpfc_sli_config_mbox_subsys_get(phba,
2532 pmb),
2533 lpfc_sli_config_mbox_opcode_get(phba,
2534 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002535 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002536 phba->work_hs = HS_FFER3;
2537 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002538 continue;
dea31012005-04-17 16:05:31 -05002539 }
2540
dea31012005-04-17 16:05:31 -05002541 if (pmbox->mbxStatus) {
2542 phba->sli.slistat.mbox_stat_err++;
2543 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2544 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002545 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002546 LOG_MBOX | LOG_SLI,
2547 "(%d):0305 Mbox cmd cmpl "
2548 "error - RETRYing Data: x%x "
2549 "(x%x/x%x) x%x x%x x%x\n",
2550 pmb->vport ? pmb->vport->vpi : 0,
2551 pmbox->mbxCommand,
2552 lpfc_sli_config_mbox_subsys_get(phba,
2553 pmb),
2554 lpfc_sli_config_mbox_opcode_get(phba,
2555 pmb),
2556 pmbox->mbxStatus,
2557 pmbox->un.varWords[0],
2558 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002559 pmbox->mbxStatus = 0;
2560 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002561 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002562 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002563 continue;
dea31012005-04-17 16:05:31 -05002564 }
2565 }
2566
2567 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002568 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002569 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002570 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2571 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002572 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002573 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002574 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2575 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002576 pmb->mbox_cmpl,
2577 *((uint32_t *) pmbox),
2578 pmbox->un.varWords[0],
2579 pmbox->un.varWords[1],
2580 pmbox->un.varWords[2],
2581 pmbox->un.varWords[3],
2582 pmbox->un.varWords[4],
2583 pmbox->un.varWords[5],
2584 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002585 pmbox->un.varWords[7],
2586 pmbox->un.varWords[8],
2587 pmbox->un.varWords[9],
2588 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002589
James Smart92d7f7b2007-06-17 19:56:38 -05002590 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002591 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002592 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002593 return 0;
dea31012005-04-17 16:05:31 -05002594}
James Smart92d7f7b2007-06-17 19:56:38 -05002595
James Smarte59058c2008-08-24 21:49:00 -04002596/**
James Smart3621a712009-04-06 18:47:14 -04002597 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002598 * @phba: Pointer to HBA context object.
2599 * @pring: Pointer to driver SLI ring object.
2600 * @tag: buffer tag.
2601 *
2602 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2603 * is set in the tag the buffer is posted for a particular exchange,
2604 * the function will return the buffer without replacing the buffer.
2605 * If the buffer is for unsolicited ELS or CT traffic, this function
2606 * returns the buffer and also posts another buffer to the firmware.
2607 **/
James Smart76bb24e2007-10-27 13:38:00 -04002608static struct lpfc_dmabuf *
2609lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002610 struct lpfc_sli_ring *pring,
2611 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002612{
James Smart9f1e1b52008-12-04 22:39:40 -05002613 struct hbq_dmabuf *hbq_entry;
2614
James Smart76bb24e2007-10-27 13:38:00 -04002615 if (tag & QUE_BUFTAG_BIT)
2616 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002617 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2618 if (!hbq_entry)
2619 return NULL;
2620 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002621}
James Smart57127f12007-10-27 13:37:05 -04002622
James Smart3772a992009-05-22 14:50:54 -04002623/**
2624 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2625 * @phba: Pointer to HBA context object.
2626 * @pring: Pointer to driver SLI ring object.
2627 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2628 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2629 * @fch_type: the type for the first frame of the sequence.
2630 *
2631 * This function is called with no lock held. This function uses the r_ctl and
2632 * type of the received sequence to find the correct callback function to call
2633 * to process the sequence.
2634 **/
2635static int
2636lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2637 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2638 uint32_t fch_type)
2639{
2640 int i;
2641
James Smartf358dd02017-02-12 13:52:34 -08002642 switch (fch_type) {
2643 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002644 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002645 return 1;
2646 default:
2647 break;
2648 }
2649
James Smart3772a992009-05-22 14:50:54 -04002650 /* unSolicited Responses */
2651 if (pring->prt[0].profile) {
2652 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2653 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2654 saveq);
2655 return 1;
2656 }
2657 /* We must search, based on rctl / type
2658 for the right routine */
2659 for (i = 0; i < pring->num_mask; i++) {
2660 if ((pring->prt[i].rctl == fch_r_ctl) &&
2661 (pring->prt[i].type == fch_type)) {
2662 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2663 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2664 (phba, pring, saveq);
2665 return 1;
2666 }
2667 }
2668 return 0;
2669}
James Smarte59058c2008-08-24 21:49:00 -04002670
2671/**
James Smart3621a712009-04-06 18:47:14 -04002672 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002673 * @phba: Pointer to HBA context object.
2674 * @pring: Pointer to driver SLI ring object.
2675 * @saveq: Pointer to the unsolicited iocb.
2676 *
2677 * This function is called with no lock held by the ring event handler
2678 * when there is an unsolicited iocb posted to the response ring by the
2679 * firmware. This function gets the buffer associated with the iocbs
2680 * and calls the event handler for the ring. This function handles both
2681 * qring buffers and hbq buffers.
2682 * When the function returns 1 the caller can free the iocb object otherwise
2683 * upper layer functions will free the iocb objects.
2684 **/
dea31012005-04-17 16:05:31 -05002685static int
2686lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2687 struct lpfc_iocbq *saveq)
2688{
2689 IOCB_t * irsp;
2690 WORD5 * w5p;
2691 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002692 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002693 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002694
dea31012005-04-17 16:05:31 -05002695 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002696
2697 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2698 if (pring->lpfc_sli_rcv_async_status)
2699 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2700 else
2701 lpfc_printf_log(phba,
2702 KERN_WARNING,
2703 LOG_SLI,
2704 "0316 Ring %d handler: unexpected "
2705 "ASYNC_STATUS iocb received evt_code "
2706 "0x%x\n",
2707 pring->ringno,
2708 irsp->un.asyncstat.evt_code);
2709 return 1;
2710 }
2711
James Smart3163f722008-02-08 18:50:25 -05002712 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2713 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2714 if (irsp->ulpBdeCount > 0) {
2715 dmzbuf = lpfc_sli_get_buff(phba, pring,
2716 irsp->un.ulpWord[3]);
2717 lpfc_in_buf_free(phba, dmzbuf);
2718 }
2719
2720 if (irsp->ulpBdeCount > 1) {
2721 dmzbuf = lpfc_sli_get_buff(phba, pring,
2722 irsp->unsli3.sli3Words[3]);
2723 lpfc_in_buf_free(phba, dmzbuf);
2724 }
2725
2726 if (irsp->ulpBdeCount > 2) {
2727 dmzbuf = lpfc_sli_get_buff(phba, pring,
2728 irsp->unsli3.sli3Words[7]);
2729 lpfc_in_buf_free(phba, dmzbuf);
2730 }
2731
2732 return 1;
2733 }
2734
James Smart92d7f7b2007-06-17 19:56:38 -05002735 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002736 if (irsp->ulpBdeCount != 0) {
2737 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002738 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002739 if (!saveq->context2)
2740 lpfc_printf_log(phba,
2741 KERN_ERR,
2742 LOG_SLI,
2743 "0341 Ring %d Cannot find buffer for "
2744 "an unsolicited iocb. tag 0x%x\n",
2745 pring->ringno,
2746 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002747 }
2748 if (irsp->ulpBdeCount == 2) {
2749 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002750 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002751 if (!saveq->context3)
2752 lpfc_printf_log(phba,
2753 KERN_ERR,
2754 LOG_SLI,
2755 "0342 Ring %d Cannot find buffer for an"
2756 " unsolicited iocb. tag 0x%x\n",
2757 pring->ringno,
2758 irsp->unsli3.sli3Words[7]);
2759 }
2760 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002761 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002762 if (irsp->ulpBdeCount != 0) {
2763 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2764 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002765 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002766 lpfc_printf_log(phba,
2767 KERN_ERR,
2768 LOG_SLI,
2769 "0343 Ring %d Cannot find "
2770 "buffer for an unsolicited iocb"
2771 ". tag 0x%x\n", pring->ringno,
2772 irsp->un.ulpWord[3]);
2773 }
2774 if (irsp->ulpBdeCount == 2) {
2775 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2776 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002777 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002778 lpfc_printf_log(phba,
2779 KERN_ERR,
2780 LOG_SLI,
2781 "0344 Ring %d Cannot find "
2782 "buffer for an unsolicited "
2783 "iocb. tag 0x%x\n",
2784 pring->ringno,
2785 irsp->unsli3.sli3Words[7]);
2786 }
2787 }
James Smart92d7f7b2007-06-17 19:56:38 -05002788 }
James Smart9c2face2008-01-11 01:53:18 -05002789 if (irsp->ulpBdeCount != 0 &&
2790 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2791 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2792 int found = 0;
2793
2794 /* search continue save q for same XRI */
2795 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002796 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2797 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002798 list_add_tail(&saveq->list, &iocbq->list);
2799 found = 1;
2800 break;
2801 }
2802 }
2803 if (!found)
2804 list_add_tail(&saveq->clist,
2805 &pring->iocb_continue_saveq);
2806 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2807 list_del_init(&iocbq->clist);
2808 saveq = iocbq;
2809 irsp = &(saveq->iocb);
2810 } else
2811 return 0;
2812 }
2813 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2814 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2815 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002816 Rctl = FC_RCTL_ELS_REQ;
2817 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002818 } else {
2819 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2820 Rctl = w5p->hcsw.Rctl;
2821 Type = w5p->hcsw.Type;
2822
2823 /* Firmware Workaround */
2824 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2825 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2826 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002827 Rctl = FC_RCTL_ELS_REQ;
2828 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002829 w5p->hcsw.Rctl = Rctl;
2830 w5p->hcsw.Type = Type;
2831 }
2832 }
James Smart92d7f7b2007-06-17 19:56:38 -05002833
James Smart3772a992009-05-22 14:50:54 -04002834 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002835 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002836 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002837 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002838 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002839
James Smart92d7f7b2007-06-17 19:56:38 -05002840 return 1;
dea31012005-04-17 16:05:31 -05002841}
2842
James Smarte59058c2008-08-24 21:49:00 -04002843/**
James Smart3621a712009-04-06 18:47:14 -04002844 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002845 * @phba: Pointer to HBA context object.
2846 * @pring: Pointer to driver SLI ring object.
2847 * @prspiocb: Pointer to response iocb object.
2848 *
2849 * This function looks up the iocb_lookup table to get the command iocb
2850 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002851 * response iocb. This function is called with the hbalock held
2852 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002853 * This function returns the command iocb object if it finds the command
2854 * iocb else returns NULL.
2855 **/
dea31012005-04-17 16:05:31 -05002856static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002857lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2858 struct lpfc_sli_ring *pring,
2859 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002860{
dea31012005-04-17 16:05:31 -05002861 struct lpfc_iocbq *cmd_iocb = NULL;
2862 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002863 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002864
James Bottomley604a3e32005-10-29 10:28:33 -05002865 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002866
James Bottomley604a3e32005-10-29 10:28:33 -05002867 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2868 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002869 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002870 /* remove from txcmpl queue list */
2871 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002872 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002873 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002874 }
dea31012005-04-17 16:05:31 -05002875 }
2876
dea31012005-04-17 16:05:31 -05002877 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002878 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002879 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002880 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002881 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002882 return NULL;
2883}
2884
James Smarte59058c2008-08-24 21:49:00 -04002885/**
James Smart3772a992009-05-22 14:50:54 -04002886 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2887 * @phba: Pointer to HBA context object.
2888 * @pring: Pointer to driver SLI ring object.
2889 * @iotag: IOCB tag.
2890 *
2891 * This function looks up the iocb_lookup table to get the command iocb
2892 * corresponding to the given iotag. This function is called with the
2893 * hbalock held.
2894 * This function returns the command iocb object if it finds the command
2895 * iocb else returns NULL.
2896 **/
2897static struct lpfc_iocbq *
2898lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2899 struct lpfc_sli_ring *pring, uint16_t iotag)
2900{
James Smart895427b2017-02-12 13:52:30 -08002901 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002902
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002903 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002904 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2905 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002906 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2907 /* remove from txcmpl queue list */
2908 list_del_init(&cmd_iocb->list);
2909 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002910 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002911 }
James Smart3772a992009-05-22 14:50:54 -04002912 }
James Smart89533e92016-10-13 15:06:15 -07002913
James Smart3772a992009-05-22 14:50:54 -04002914 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002915 "0372 iotag x%x lookup error: max iotag (x%x) "
2916 "iocb_flag x%x\n",
2917 iotag, phba->sli.last_iotag,
2918 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002919 return NULL;
2920}
2921
2922/**
James Smart3621a712009-04-06 18:47:14 -04002923 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002924 * @phba: Pointer to HBA context object.
2925 * @pring: Pointer to driver SLI ring object.
2926 * @saveq: Pointer to the response iocb to be processed.
2927 *
2928 * This function is called by the ring event handler for non-fcp
2929 * rings when there is a new response iocb in the response ring.
2930 * The caller is not required to hold any locks. This function
2931 * gets the command iocb associated with the response iocb and
2932 * calls the completion handler for the command iocb. If there
2933 * is no completion handler, the function will free the resources
2934 * associated with command iocb. If the response iocb is for
2935 * an already aborted command iocb, the status of the completion
2936 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2937 * This function always returns 1.
2938 **/
dea31012005-04-17 16:05:31 -05002939static int
James Smart2e0fef82007-06-17 19:56:36 -05002940lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002941 struct lpfc_iocbq *saveq)
2942{
James Smart2e0fef82007-06-17 19:56:36 -05002943 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002944 int rc = 1;
2945 unsigned long iflag;
2946
2947 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002948 if (phba->sli_rev == LPFC_SLI_REV4)
2949 spin_lock_irqsave(&pring->ring_lock, iflag);
2950 else
2951 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002952 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002953 if (phba->sli_rev == LPFC_SLI_REV4)
2954 spin_unlock_irqrestore(&pring->ring_lock, iflag);
2955 else
2956 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05002957
dea31012005-04-17 16:05:31 -05002958 if (cmdiocbp) {
2959 if (cmdiocbp->iocb_cmpl) {
2960 /*
James Smartea2151b2008-09-07 11:52:10 -04002961 * If an ELS command failed send an event to mgmt
2962 * application.
2963 */
2964 if (saveq->iocb.ulpStatus &&
2965 (pring->ringno == LPFC_ELS_RING) &&
2966 (cmdiocbp->iocb.ulpCommand ==
2967 CMD_ELS_REQUEST64_CR))
2968 lpfc_send_els_failure_event(phba,
2969 cmdiocbp, saveq);
2970
2971 /*
dea31012005-04-17 16:05:31 -05002972 * Post all ELS completions to the worker thread.
2973 * All other are passed to the completion callback.
2974 */
2975 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002976 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2977 (cmdiocbp->iocb_flag &
2978 LPFC_DRIVER_ABORTED)) {
2979 spin_lock_irqsave(&phba->hbalock,
2980 iflag);
James Smart07951072007-04-25 09:51:38 -04002981 cmdiocbp->iocb_flag &=
2982 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002983 spin_unlock_irqrestore(&phba->hbalock,
2984 iflag);
James Smart07951072007-04-25 09:51:38 -04002985 saveq->iocb.ulpStatus =
2986 IOSTAT_LOCAL_REJECT;
2987 saveq->iocb.un.ulpWord[4] =
2988 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002989
2990 /* Firmware could still be in progress
2991 * of DMAing payload, so don't free data
2992 * buffer till after a hbeat.
2993 */
James Smart341af102010-01-26 23:07:37 -05002994 spin_lock_irqsave(&phba->hbalock,
2995 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002996 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002997 spin_unlock_irqrestore(&phba->hbalock,
2998 iflag);
2999 }
James Smart0f65ff62010-02-26 14:14:23 -05003000 if (phba->sli_rev == LPFC_SLI_REV4) {
3001 if (saveq->iocb_flag &
3002 LPFC_EXCHANGE_BUSY) {
3003 /* Set cmdiocb flag for the
3004 * exchange busy so sgl (xri)
3005 * will not be released until
3006 * the abort xri is received
3007 * from hba.
3008 */
3009 spin_lock_irqsave(
3010 &phba->hbalock, iflag);
3011 cmdiocbp->iocb_flag |=
3012 LPFC_EXCHANGE_BUSY;
3013 spin_unlock_irqrestore(
3014 &phba->hbalock, iflag);
3015 }
3016 if (cmdiocbp->iocb_flag &
3017 LPFC_DRIVER_ABORTED) {
3018 /*
3019 * Clear LPFC_DRIVER_ABORTED
3020 * bit in case it was driver
3021 * initiated abort.
3022 */
3023 spin_lock_irqsave(
3024 &phba->hbalock, iflag);
3025 cmdiocbp->iocb_flag &=
3026 ~LPFC_DRIVER_ABORTED;
3027 spin_unlock_irqrestore(
3028 &phba->hbalock, iflag);
3029 cmdiocbp->iocb.ulpStatus =
3030 IOSTAT_LOCAL_REJECT;
3031 cmdiocbp->iocb.un.ulpWord[4] =
3032 IOERR_ABORT_REQUESTED;
3033 /*
3034 * For SLI4, irsiocb contains
3035 * NO_XRI in sli_xritag, it
3036 * shall not affect releasing
3037 * sgl (xri) process.
3038 */
3039 saveq->iocb.ulpStatus =
3040 IOSTAT_LOCAL_REJECT;
3041 saveq->iocb.un.ulpWord[4] =
3042 IOERR_SLI_ABORTED;
3043 spin_lock_irqsave(
3044 &phba->hbalock, iflag);
3045 saveq->iocb_flag |=
3046 LPFC_DELAY_MEM_FREE;
3047 spin_unlock_irqrestore(
3048 &phba->hbalock, iflag);
3049 }
James Smart07951072007-04-25 09:51:38 -04003050 }
dea31012005-04-17 16:05:31 -05003051 }
James Smart2e0fef82007-06-17 19:56:36 -05003052 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05003053 } else
3054 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05003055 } else {
3056 /*
3057 * Unknown initiating command based on the response iotag.
3058 * This could be the case on the ELS ring because of
3059 * lpfc_els_abort().
3060 */
3061 if (pring->ringno != LPFC_ELS_RING) {
3062 /*
3063 * Ring <ringno> handler: unexpected completion IoTag
3064 * <IoTag>
3065 */
James Smarta257bf92009-04-06 18:48:10 -04003066 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003067 "0322 Ring %d handler: "
3068 "unexpected completion IoTag x%x "
3069 "Data: x%x x%x x%x x%x\n",
3070 pring->ringno,
3071 saveq->iocb.ulpIoTag,
3072 saveq->iocb.ulpStatus,
3073 saveq->iocb.un.ulpWord[4],
3074 saveq->iocb.ulpCommand,
3075 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05003076 }
3077 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04003078
dea31012005-04-17 16:05:31 -05003079 return rc;
3080}
3081
James Smarte59058c2008-08-24 21:49:00 -04003082/**
James Smart3621a712009-04-06 18:47:14 -04003083 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04003084 * @phba: Pointer to HBA context object.
3085 * @pring: Pointer to driver SLI ring object.
3086 *
3087 * This function is called from the iocb ring event handlers when
3088 * put pointer is ahead of the get pointer for a ring. This function signal
3089 * an error attention condition to the worker thread and the worker
3090 * thread will transition the HBA to offline state.
3091 **/
James Smart2e0fef82007-06-17 19:56:36 -05003092static void
3093lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003094{
James Smart34b02dc2008-08-24 21:49:55 -04003095 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003096 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003097 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003098 * rsp ring <portRspMax>
3099 */
3100 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003101 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003102 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003103 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04003104 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003105
James Smart2e0fef82007-06-17 19:56:36 -05003106 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003107
3108 /*
3109 * All error attention handlers are posted to
3110 * worker thread
3111 */
3112 phba->work_ha |= HA_ERATT;
3113 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003114
James Smart5e9d9b82008-06-14 22:52:53 -04003115 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003116
3117 return;
3118}
3119
James Smarte59058c2008-08-24 21:49:00 -04003120/**
James Smart3621a712009-04-06 18:47:14 -04003121 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003122 * @ptr: Pointer to address of HBA context object.
3123 *
3124 * This function is invoked by the Error Attention polling timer when the
3125 * timer times out. It will check the SLI Error Attention register for
3126 * possible attention events. If so, it will post an Error Attention event
3127 * and wake up worker thread to process it. Otherwise, it will set up the
3128 * Error Attention polling timer for the next poll.
3129 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003130void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003131{
3132 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003133 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003134 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003135
Kees Cookf22eb4d2017-09-06 20:24:26 -07003136 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003137
James Smartaa6fbb72012-08-03 12:36:03 -04003138 /* Here we will also keep track of interrupts per sec of the hba */
3139 sli_intr = phba->sli.slistat.sli_intr;
3140
3141 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3142 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3143 sli_intr);
3144 else
3145 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3146
James Smart65791f12016-07-06 12:35:56 -07003147 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3148 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003149 phba->sli.slistat.sli_ips = cnt;
3150
3151 phba->sli.slistat.sli_prev_intr = sli_intr;
3152
James Smart93996272008-08-24 21:50:30 -04003153 /* Check chip HA register for error event */
3154 eratt = lpfc_sli_check_eratt(phba);
3155
3156 if (eratt)
3157 /* Tell the worker thread there is work to do */
3158 lpfc_worker_wake_up(phba);
3159 else
3160 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003161 mod_timer(&phba->eratt_poll,
3162 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003163 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003164 return;
3165}
3166
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003167
James Smarte59058c2008-08-24 21:49:00 -04003168/**
James Smart3621a712009-04-06 18:47:14 -04003169 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003170 * @phba: Pointer to HBA context object.
3171 * @pring: Pointer to driver SLI ring object.
3172 * @mask: Host attention register mask for this ring.
3173 *
3174 * This function is called from the interrupt context when there is a ring
3175 * event for the fcp ring. The caller does not hold any lock.
3176 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003177 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003178 * LE bit set. The function will call the completion handler of the command iocb
3179 * if the response iocb indicates a completion for a command iocb or it is
3180 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3181 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003182 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003183 * to check it explicitly.
3184 */
3185int
James Smart2e0fef82007-06-17 19:56:36 -05003186lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3187 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003188{
James Smart34b02dc2008-08-24 21:49:55 -04003189 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003190 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003191 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003192 struct lpfc_iocbq *cmdiocbq = NULL;
3193 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003194 uint32_t status;
3195 uint32_t portRspPut, portRspMax;
3196 int rc = 1;
3197 lpfc_iocb_type type;
3198 unsigned long iflag;
3199 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003200
James Smart2e0fef82007-06-17 19:56:36 -05003201 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003202 pring->stats.iocb_event++;
3203
dea31012005-04-17 16:05:31 -05003204 /*
3205 * The next available response entry should never exceed the maximum
3206 * entries. If it does, treat it as an adapter hardware error.
3207 */
James Smart7e56aa22012-08-03 12:35:34 -04003208 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003209 portRspPut = le32_to_cpu(pgp->rspPutInx);
3210 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003211 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003212 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003213 return 1;
3214 }
James Smart45ed1192009-10-02 15:17:02 -04003215 if (phba->fcp_ring_in_use) {
3216 spin_unlock_irqrestore(&phba->hbalock, iflag);
3217 return 1;
3218 } else
3219 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003220
3221 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003222 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003223 /*
3224 * Fetch an entry off the ring and copy it into a local data
3225 * structure. The copy involves a byte-swap since the
3226 * network byte order and pci byte orders are different.
3227 */
James Smarted957682007-06-17 19:56:37 -05003228 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003229 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003230
James Smart7e56aa22012-08-03 12:35:34 -04003231 if (++pring->sli.sli3.rspidx >= portRspMax)
3232 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003233
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003234 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3235 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003236 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003237 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003238 irsp = &rspiocbq.iocb;
3239
dea31012005-04-17 16:05:31 -05003240 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3241 pring->stats.iocb_rsp++;
3242 rsp_cmpl++;
3243
3244 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003245 /*
3246 * If resource errors reported from HBA, reduce
3247 * queuedepths of the SCSI device.
3248 */
3249 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003250 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3251 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003252 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003253 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003254 spin_lock_irqsave(&phba->hbalock, iflag);
3255 }
3256
dea31012005-04-17 16:05:31 -05003257 /* Rsp ring <ringno> error: IOCB */
3258 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003259 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003260 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003261 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003262 irsp->un.ulpWord[0],
3263 irsp->un.ulpWord[1],
3264 irsp->un.ulpWord[2],
3265 irsp->un.ulpWord[3],
3266 irsp->un.ulpWord[4],
3267 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003268 *(uint32_t *)&irsp->un1,
3269 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003270 }
3271
3272 switch (type) {
3273 case LPFC_ABORT_IOCB:
3274 case LPFC_SOL_IOCB:
3275 /*
3276 * Idle exchange closed via ABTS from port. No iocb
3277 * resources need to be recovered.
3278 */
3279 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003280 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003281 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003282 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003283 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003284 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003285 break;
3286 }
3287
James Bottomley604a3e32005-10-29 10:28:33 -05003288 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3289 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003290 if (unlikely(!cmdiocbq))
3291 break;
3292 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3293 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3294 if (cmdiocbq->iocb_cmpl) {
3295 spin_unlock_irqrestore(&phba->hbalock, iflag);
3296 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3297 &rspiocbq);
3298 spin_lock_irqsave(&phba->hbalock, iflag);
3299 }
dea31012005-04-17 16:05:31 -05003300 break;
James Smarta4bc3372006-12-02 13:34:16 -05003301 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003302 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003303 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003304 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003305 break;
dea31012005-04-17 16:05:31 -05003306 default:
3307 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3308 char adaptermsg[LPFC_MAX_ADPTMSG];
3309 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3310 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3311 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003312 dev_warn(&((phba->pcidev)->dev),
3313 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003314 phba->brd_no, adaptermsg);
3315 } else {
3316 /* Unknown IOCB command */
3317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003318 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003319 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003320 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003321 irsp->ulpStatus,
3322 irsp->ulpIoTag,
3323 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003324 }
3325 break;
3326 }
3327
3328 /*
3329 * The response IOCB has been processed. Update the ring
3330 * pointer in SLIM. If the port response put pointer has not
3331 * been updated, sync the pgp->rspPutInx and fetch the new port
3332 * response put pointer.
3333 */
James Smart7e56aa22012-08-03 12:35:34 -04003334 writel(pring->sli.sli3.rspidx,
3335 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003336
James Smart7e56aa22012-08-03 12:35:34 -04003337 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003338 portRspPut = le32_to_cpu(pgp->rspPutInx);
3339 }
3340
3341 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3342 pring->stats.iocb_rsp_full++;
3343 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3344 writel(status, phba->CAregaddr);
3345 readl(phba->CAregaddr);
3346 }
3347 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3348 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3349 pring->stats.iocb_cmd_empty++;
3350
3351 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003352 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003353 lpfc_sli_resume_iocb(phba, pring);
3354
3355 if ((pring->lpfc_sli_cmd_available))
3356 (pring->lpfc_sli_cmd_available) (phba, pring);
3357
3358 }
3359
James Smart45ed1192009-10-02 15:17:02 -04003360 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003361 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003362 return rc;
3363}
3364
James Smarte59058c2008-08-24 21:49:00 -04003365/**
James Smart3772a992009-05-22 14:50:54 -04003366 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3367 * @phba: Pointer to HBA context object.
3368 * @pring: Pointer to driver SLI ring object.
3369 * @rspiocbp: Pointer to driver response IOCB object.
3370 *
3371 * This function is called from the worker thread when there is a slow-path
3372 * response IOCB to process. This function chains all the response iocbs until
3373 * seeing the iocb with the LE bit set. The function will call
3374 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3375 * completion of a command iocb. The function will call the
3376 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3377 * The function frees the resources or calls the completion handler if this
3378 * iocb is an abort completion. The function returns NULL when the response
3379 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3380 * this function shall chain the iocb on to the iocb_continueq and return the
3381 * response iocb passed in.
3382 **/
3383static struct lpfc_iocbq *
3384lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3385 struct lpfc_iocbq *rspiocbp)
3386{
3387 struct lpfc_iocbq *saveq;
3388 struct lpfc_iocbq *cmdiocbp;
3389 struct lpfc_iocbq *next_iocb;
3390 IOCB_t *irsp = NULL;
3391 uint32_t free_saveq;
3392 uint8_t iocb_cmd_type;
3393 lpfc_iocb_type type;
3394 unsigned long iflag;
3395 int rc;
3396
3397 spin_lock_irqsave(&phba->hbalock, iflag);
3398 /* First add the response iocb to the countinueq list */
3399 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3400 pring->iocb_continueq_cnt++;
3401
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003402 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003403 irsp = &rspiocbp->iocb;
3404 if (irsp->ulpLe) {
3405 /*
3406 * By default, the driver expects to free all resources
3407 * associated with this iocb completion.
3408 */
3409 free_saveq = 1;
3410 saveq = list_get_first(&pring->iocb_continueq,
3411 struct lpfc_iocbq, list);
3412 irsp = &(saveq->iocb);
3413 list_del_init(&pring->iocb_continueq);
3414 pring->iocb_continueq_cnt = 0;
3415
3416 pring->stats.iocb_rsp++;
3417
3418 /*
3419 * If resource errors reported from HBA, reduce
3420 * queuedepths of the SCSI device.
3421 */
3422 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003423 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3424 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003425 spin_unlock_irqrestore(&phba->hbalock, iflag);
3426 phba->lpfc_rampdown_queue_depth(phba);
3427 spin_lock_irqsave(&phba->hbalock, iflag);
3428 }
3429
3430 if (irsp->ulpStatus) {
3431 /* Rsp ring <ringno> error: IOCB */
3432 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3433 "0328 Rsp Ring %d error: "
3434 "IOCB Data: "
3435 "x%x x%x x%x x%x "
3436 "x%x x%x x%x x%x "
3437 "x%x x%x x%x x%x "
3438 "x%x x%x x%x x%x\n",
3439 pring->ringno,
3440 irsp->un.ulpWord[0],
3441 irsp->un.ulpWord[1],
3442 irsp->un.ulpWord[2],
3443 irsp->un.ulpWord[3],
3444 irsp->un.ulpWord[4],
3445 irsp->un.ulpWord[5],
3446 *(((uint32_t *) irsp) + 6),
3447 *(((uint32_t *) irsp) + 7),
3448 *(((uint32_t *) irsp) + 8),
3449 *(((uint32_t *) irsp) + 9),
3450 *(((uint32_t *) irsp) + 10),
3451 *(((uint32_t *) irsp) + 11),
3452 *(((uint32_t *) irsp) + 12),
3453 *(((uint32_t *) irsp) + 13),
3454 *(((uint32_t *) irsp) + 14),
3455 *(((uint32_t *) irsp) + 15));
3456 }
3457
3458 /*
3459 * Fetch the IOCB command type and call the correct completion
3460 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3461 * get freed back to the lpfc_iocb_list by the discovery
3462 * kernel thread.
3463 */
3464 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3465 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3466 switch (type) {
3467 case LPFC_SOL_IOCB:
3468 spin_unlock_irqrestore(&phba->hbalock, iflag);
3469 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3470 spin_lock_irqsave(&phba->hbalock, iflag);
3471 break;
3472
3473 case LPFC_UNSOL_IOCB:
3474 spin_unlock_irqrestore(&phba->hbalock, iflag);
3475 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3476 spin_lock_irqsave(&phba->hbalock, iflag);
3477 if (!rc)
3478 free_saveq = 0;
3479 break;
3480
3481 case LPFC_ABORT_IOCB:
3482 cmdiocbp = NULL;
3483 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3484 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3485 saveq);
3486 if (cmdiocbp) {
3487 /* Call the specified completion routine */
3488 if (cmdiocbp->iocb_cmpl) {
3489 spin_unlock_irqrestore(&phba->hbalock,
3490 iflag);
3491 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3492 saveq);
3493 spin_lock_irqsave(&phba->hbalock,
3494 iflag);
3495 } else
3496 __lpfc_sli_release_iocbq(phba,
3497 cmdiocbp);
3498 }
3499 break;
3500
3501 case LPFC_UNKNOWN_IOCB:
3502 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3503 char adaptermsg[LPFC_MAX_ADPTMSG];
3504 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3505 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3506 MAX_MSG_DATA);
3507 dev_warn(&((phba->pcidev)->dev),
3508 "lpfc%d: %s\n",
3509 phba->brd_no, adaptermsg);
3510 } else {
3511 /* Unknown IOCB command */
3512 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3513 "0335 Unknown IOCB "
3514 "command Data: x%x "
3515 "x%x x%x x%x\n",
3516 irsp->ulpCommand,
3517 irsp->ulpStatus,
3518 irsp->ulpIoTag,
3519 irsp->ulpContext);
3520 }
3521 break;
3522 }
3523
3524 if (free_saveq) {
3525 list_for_each_entry_safe(rspiocbp, next_iocb,
3526 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003527 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003528 __lpfc_sli_release_iocbq(phba, rspiocbp);
3529 }
3530 __lpfc_sli_release_iocbq(phba, saveq);
3531 }
3532 rspiocbp = NULL;
3533 }
3534 spin_unlock_irqrestore(&phba->hbalock, iflag);
3535 return rspiocbp;
3536}
3537
3538/**
3539 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003540 * @phba: Pointer to HBA context object.
3541 * @pring: Pointer to driver SLI ring object.
3542 * @mask: Host attention register mask for this ring.
3543 *
James Smart3772a992009-05-22 14:50:54 -04003544 * This routine wraps the actual slow_ring event process routine from the
3545 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003546 **/
James Smart3772a992009-05-22 14:50:54 -04003547void
James Smart2e0fef82007-06-17 19:56:36 -05003548lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3549 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003550{
James Smart3772a992009-05-22 14:50:54 -04003551 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3552}
3553
3554/**
3555 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3556 * @phba: Pointer to HBA context object.
3557 * @pring: Pointer to driver SLI ring object.
3558 * @mask: Host attention register mask for this ring.
3559 *
3560 * This function is called from the worker thread when there is a ring event
3561 * for non-fcp rings. The caller does not hold any lock. The function will
3562 * remove each response iocb in the response ring and calls the handle
3563 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3564 **/
3565static void
3566lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3567 struct lpfc_sli_ring *pring, uint32_t mask)
3568{
James Smart34b02dc2008-08-24 21:49:55 -04003569 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003570 IOCB_t *entry;
3571 IOCB_t *irsp = NULL;
3572 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003573 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003574 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003575 uint32_t status;
dea31012005-04-17 16:05:31 -05003576
James Smart34b02dc2008-08-24 21:49:55 -04003577 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003578 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003579 pring->stats.iocb_event++;
3580
dea31012005-04-17 16:05:31 -05003581 /*
3582 * The next available response entry should never exceed the maximum
3583 * entries. If it does, treat it as an adapter hardware error.
3584 */
James Smart7e56aa22012-08-03 12:35:34 -04003585 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003586 portRspPut = le32_to_cpu(pgp->rspPutInx);
3587 if (portRspPut >= portRspMax) {
3588 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003589 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003590 * rsp ring <portRspMax>
3591 */
James Smarted957682007-06-17 19:56:37 -05003592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003593 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003594 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003595 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003596
James Smart2e0fef82007-06-17 19:56:36 -05003597 phba->link_state = LPFC_HBA_ERROR;
3598 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003599
3600 phba->work_hs = HS_FFER3;
3601 lpfc_handle_eratt(phba);
3602
James Smart3772a992009-05-22 14:50:54 -04003603 return;
dea31012005-04-17 16:05:31 -05003604 }
3605
3606 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003607 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003608 /*
3609 * Build a completion list and call the appropriate handler.
3610 * The process is to get the next available response iocb, get
3611 * a free iocb from the list, copy the response data into the
3612 * free iocb, insert to the continuation list, and update the
3613 * next response index to slim. This process makes response
3614 * iocb's in the ring available to DMA as fast as possible but
3615 * pays a penalty for a copy operation. Since the iocb is
3616 * only 32 bytes, this penalty is considered small relative to
3617 * the PCI reads for register values and a slim write. When
3618 * the ulpLe field is set, the entire Command has been
3619 * received.
3620 */
James Smarted957682007-06-17 19:56:37 -05003621 entry = lpfc_resp_iocb(phba, pring);
3622
James Smart858c9f62007-06-17 19:56:39 -05003623 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003624 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003625 if (rspiocbp == NULL) {
3626 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003627 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003628 break;
3629 }
3630
James Smarted957682007-06-17 19:56:37 -05003631 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3632 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003633 irsp = &rspiocbp->iocb;
3634
James Smart7e56aa22012-08-03 12:35:34 -04003635 if (++pring->sli.sli3.rspidx >= portRspMax)
3636 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003637
James Smarta58cbd52007-08-02 11:09:43 -04003638 if (pring->ringno == LPFC_ELS_RING) {
3639 lpfc_debugfs_slow_ring_trc(phba,
3640 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3641 *(((uint32_t *) irsp) + 4),
3642 *(((uint32_t *) irsp) + 6),
3643 *(((uint32_t *) irsp) + 7));
3644 }
3645
James Smart7e56aa22012-08-03 12:35:34 -04003646 writel(pring->sli.sli3.rspidx,
3647 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003648
James Smart3772a992009-05-22 14:50:54 -04003649 spin_unlock_irqrestore(&phba->hbalock, iflag);
3650 /* Handle the response IOCB */
3651 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3652 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003653
3654 /*
3655 * If the port response put pointer has not been updated, sync
3656 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3657 * response put pointer.
3658 */
James Smart7e56aa22012-08-03 12:35:34 -04003659 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003660 portRspPut = le32_to_cpu(pgp->rspPutInx);
3661 }
James Smart7e56aa22012-08-03 12:35:34 -04003662 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003663
James Smart92d7f7b2007-06-17 19:56:38 -05003664 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003665 /* At least one response entry has been freed */
3666 pring->stats.iocb_rsp_full++;
3667 /* SET RxRE_RSP in Chip Att register */
3668 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3669 writel(status, phba->CAregaddr);
3670 readl(phba->CAregaddr); /* flush */
3671 }
3672 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3673 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3674 pring->stats.iocb_cmd_empty++;
3675
3676 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003677 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003678 lpfc_sli_resume_iocb(phba, pring);
3679
3680 if ((pring->lpfc_sli_cmd_available))
3681 (pring->lpfc_sli_cmd_available) (phba, pring);
3682
3683 }
3684
James Smart2e0fef82007-06-17 19:56:36 -05003685 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003686 return;
dea31012005-04-17 16:05:31 -05003687}
3688
James Smarte59058c2008-08-24 21:49:00 -04003689/**
James Smart4f774512009-05-22 14:52:35 -04003690 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3691 * @phba: Pointer to HBA context object.
3692 * @pring: Pointer to driver SLI ring object.
3693 * @mask: Host attention register mask for this ring.
3694 *
3695 * This function is called from the worker thread when there is a pending
3696 * ELS response iocb on the driver internal slow-path response iocb worker
3697 * queue. The caller does not hold any lock. The function will remove each
3698 * response iocb from the response worker queue and calls the handle
3699 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3700 **/
3701static void
3702lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3703 struct lpfc_sli_ring *pring, uint32_t mask)
3704{
3705 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003706 struct hbq_dmabuf *dmabuf;
3707 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003708 unsigned long iflag;
3709
James Smart45ed1192009-10-02 15:17:02 -04003710 spin_lock_irqsave(&phba->hbalock, iflag);
3711 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3712 spin_unlock_irqrestore(&phba->hbalock, iflag);
3713 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003714 /* Get the response iocb from the head of work queue */
3715 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003716 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003717 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003718 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003719
3720 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3721 case CQE_CODE_COMPL_WQE:
3722 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3723 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003724 /* Translate ELS WCQE to response IOCBQ */
3725 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3726 irspiocbq);
3727 if (irspiocbq)
3728 lpfc_sli_sp_handle_rspiocb(phba, pring,
3729 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003730 break;
3731 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003732 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003733 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3734 cq_event);
3735 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3736 break;
3737 default:
3738 break;
3739 }
James Smart4f774512009-05-22 14:52:35 -04003740 }
3741}
3742
3743/**
James Smart3621a712009-04-06 18:47:14 -04003744 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003745 * @phba: Pointer to HBA context object.
3746 * @pring: Pointer to driver SLI ring object.
3747 *
3748 * This function aborts all iocbs in the given ring and frees all the iocb
3749 * objects in txq. This function issues an abort iocb for all the iocb commands
3750 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3751 * the return of this function. The caller is not required to hold any locks.
3752 **/
James Smart2e0fef82007-06-17 19:56:36 -05003753void
dea31012005-04-17 16:05:31 -05003754lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3755{
James Smart2534ba72007-04-25 09:52:20 -04003756 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003757 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003758
James Smart92d7f7b2007-06-17 19:56:38 -05003759 if (pring->ringno == LPFC_ELS_RING) {
3760 lpfc_fabric_abort_hba(phba);
3761 }
3762
dea31012005-04-17 16:05:31 -05003763 /* Error everything on txq and txcmplq
3764 * First do the txq.
3765 */
James Smartdb55fba2014-04-04 13:52:02 -04003766 if (phba->sli_rev >= LPFC_SLI_REV4) {
3767 spin_lock_irq(&pring->ring_lock);
3768 list_splice_init(&pring->txq, &completions);
3769 pring->txq_cnt = 0;
3770 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003771
James Smartdb55fba2014-04-04 13:52:02 -04003772 spin_lock_irq(&phba->hbalock);
3773 /* Next issue ABTS for everything on the txcmplq */
3774 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3775 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3776 spin_unlock_irq(&phba->hbalock);
3777 } else {
3778 spin_lock_irq(&phba->hbalock);
3779 list_splice_init(&pring->txq, &completions);
3780 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003781
James Smartdb55fba2014-04-04 13:52:02 -04003782 /* Next issue ABTS for everything on the txcmplq */
3783 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3784 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3785 spin_unlock_irq(&phba->hbalock);
3786 }
James Smart2534ba72007-04-25 09:52:20 -04003787
James Smarta257bf92009-04-06 18:48:10 -04003788 /* Cancel all the IOCBs from the completions list */
3789 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3790 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003791}
3792
James Smarte59058c2008-08-24 21:49:00 -04003793/**
James Smart895427b2017-02-12 13:52:30 -08003794 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3795 * @phba: Pointer to HBA context object.
3796 * @pring: Pointer to driver SLI ring object.
3797 *
3798 * This function aborts all iocbs in the given ring and frees all the iocb
3799 * objects in txq. This function issues an abort iocb for all the iocb commands
3800 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3801 * the return of this function. The caller is not required to hold any locks.
3802 **/
3803void
3804lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3805{
3806 LIST_HEAD(completions);
3807 struct lpfc_iocbq *iocb, *next_iocb;
3808
3809 if (pring->ringno == LPFC_ELS_RING)
3810 lpfc_fabric_abort_hba(phba);
3811
3812 spin_lock_irq(&phba->hbalock);
3813 /* Next issue ABTS for everything on the txcmplq */
3814 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3815 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3816 spin_unlock_irq(&phba->hbalock);
3817}
3818
3819
3820/**
James Smartdb55fba2014-04-04 13:52:02 -04003821 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3822 * @phba: Pointer to HBA context object.
3823 * @pring: Pointer to driver SLI ring object.
3824 *
3825 * This function aborts all iocbs in FCP rings and frees all the iocb
3826 * objects in txq. This function issues an abort iocb for all the iocb commands
3827 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3828 * the return of this function. The caller is not required to hold any locks.
3829 **/
3830void
3831lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3832{
3833 struct lpfc_sli *psli = &phba->sli;
3834 struct lpfc_sli_ring *pring;
3835 uint32_t i;
3836
3837 /* Look on all the FCP Rings for the iotag */
3838 if (phba->sli_rev >= LPFC_SLI_REV4) {
3839 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003840 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003841 lpfc_sli_abort_iocb_ring(phba, pring);
3842 }
3843 } else {
James Smart895427b2017-02-12 13:52:30 -08003844 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003845 lpfc_sli_abort_iocb_ring(phba, pring);
3846 }
3847}
3848
James Smart895427b2017-02-12 13:52:30 -08003849/**
3850 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3851 * @phba: Pointer to HBA context object.
3852 *
3853 * This function aborts all wqes in NVME rings. This function issues an
3854 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3855 * the txcmplq is not guaranteed to complete before the return of this
3856 * function. The caller is not required to hold any locks.
3857 **/
3858void
3859lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3860{
3861 struct lpfc_sli_ring *pring;
3862 uint32_t i;
3863
3864 if (phba->sli_rev < LPFC_SLI_REV4)
3865 return;
3866
3867 /* Abort all IO on each NVME ring. */
3868 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3869 pring = phba->sli4_hba.nvme_wq[i]->pring;
3870 lpfc_sli_abort_wqe_ring(phba, pring);
3871 }
3872}
3873
James Smartdb55fba2014-04-04 13:52:02 -04003874
3875/**
James Smart3621a712009-04-06 18:47:14 -04003876 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003877 * @phba: Pointer to HBA context object.
3878 *
3879 * This function flushes all iocbs in the fcp ring and frees all the iocb
3880 * objects in txq and txcmplq. This function will not issue abort iocbs
3881 * for all the iocb commands in txcmplq, they will just be returned with
3882 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3883 * slot has been permanently disabled.
3884 **/
3885void
3886lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3887{
3888 LIST_HEAD(txq);
3889 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003890 struct lpfc_sli *psli = &phba->sli;
3891 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003892 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003893 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04003894
3895 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003896 /* Indicate the I/O queues are flushed */
3897 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003898 spin_unlock_irq(&phba->hbalock);
3899
James Smartdb55fba2014-04-04 13:52:02 -04003900 /* Look on all the FCP Rings for the iotag */
3901 if (phba->sli_rev >= LPFC_SLI_REV4) {
3902 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003903 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003904
James Smartdb55fba2014-04-04 13:52:02 -04003905 spin_lock_irq(&pring->ring_lock);
3906 /* Retrieve everything on txq */
3907 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003908 list_for_each_entry_safe(piocb, next_iocb,
3909 &pring->txcmplq, list)
3910 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003911 /* Retrieve everything on the txcmplq */
3912 list_splice_init(&pring->txcmplq, &txcmplq);
3913 pring->txq_cnt = 0;
3914 pring->txcmplq_cnt = 0;
3915 spin_unlock_irq(&pring->ring_lock);
3916
3917 /* Flush the txq */
3918 lpfc_sli_cancel_iocbs(phba, &txq,
3919 IOSTAT_LOCAL_REJECT,
3920 IOERR_SLI_DOWN);
3921 /* Flush the txcmpq */
3922 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3923 IOSTAT_LOCAL_REJECT,
3924 IOERR_SLI_DOWN);
3925 }
3926 } else {
James Smart895427b2017-02-12 13:52:30 -08003927 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003928
3929 spin_lock_irq(&phba->hbalock);
3930 /* Retrieve everything on txq */
3931 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003932 list_for_each_entry_safe(piocb, next_iocb,
3933 &pring->txcmplq, list)
3934 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003935 /* Retrieve everything on the txcmplq */
3936 list_splice_init(&pring->txcmplq, &txcmplq);
3937 pring->txq_cnt = 0;
3938 pring->txcmplq_cnt = 0;
3939 spin_unlock_irq(&phba->hbalock);
3940
3941 /* Flush the txq */
3942 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3943 IOERR_SLI_DOWN);
3944 /* Flush the txcmpq */
3945 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3946 IOERR_SLI_DOWN);
3947 }
James Smarta8e497d2008-08-24 21:50:11 -04003948}
3949
3950/**
James Smart895427b2017-02-12 13:52:30 -08003951 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3952 * @phba: Pointer to HBA context object.
3953 *
3954 * This function flushes all wqes in the nvme rings and frees all resources
3955 * in the txcmplq. This function does not issue abort wqes for the IO
3956 * commands in txcmplq, they will just be returned with
3957 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3958 * slot has been permanently disabled.
3959 **/
3960void
3961lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3962{
3963 LIST_HEAD(txcmplq);
3964 struct lpfc_sli_ring *pring;
3965 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003966 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08003967
3968 if (phba->sli_rev < LPFC_SLI_REV4)
3969 return;
3970
3971 /* Hint to other driver operations that a flush is in progress. */
3972 spin_lock_irq(&phba->hbalock);
3973 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3974 spin_unlock_irq(&phba->hbalock);
3975
3976 /* Cycle through all NVME rings and complete each IO with
3977 * a local driver reason code. This is a flush so no
3978 * abort exchange to FW.
3979 */
3980 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3981 pring = phba->sli4_hba.nvme_wq[i]->pring;
3982
James Smart895427b2017-02-12 13:52:30 -08003983 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08003984 list_for_each_entry_safe(piocb, next_iocb,
3985 &pring->txcmplq, list)
3986 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3987 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08003988 list_splice_init(&pring->txcmplq, &txcmplq);
3989 pring->txcmplq_cnt = 0;
3990 spin_unlock_irq(&pring->ring_lock);
3991
3992 /* Flush the txcmpq &&&PAE */
3993 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3994 IOSTAT_LOCAL_REJECT,
3995 IOERR_SLI_DOWN);
3996 }
3997}
3998
3999/**
James Smart3772a992009-05-22 14:50:54 -04004000 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04004001 * @phba: Pointer to HBA context object.
4002 * @mask: Bit mask to be checked.
4003 *
4004 * This function reads the host status register and compares
4005 * with the provided bit mask to check if HBA completed
4006 * the restart. This function will wait in a loop for the
4007 * HBA to complete restart. If the HBA does not restart within
4008 * 15 iterations, the function will reset the HBA again. The
4009 * function returns 1 when HBA fail to restart otherwise returns
4010 * zero.
4011 **/
James Smart3772a992009-05-22 14:50:54 -04004012static int
4013lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05004014{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004015 uint32_t status;
4016 int i = 0;
4017 int retval = 0;
dea31012005-04-17 16:05:31 -05004018
Jamie Wellnitz41415862006-02-28 19:25:27 -05004019 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004020 if (lpfc_readl(phba->HSregaddr, &status))
4021 return 1;
dea31012005-04-17 16:05:31 -05004022
Jamie Wellnitz41415862006-02-28 19:25:27 -05004023 /*
4024 * Check status register every 100ms for 5 retries, then every
4025 * 500ms for 5, then every 2.5 sec for 5, then reset board and
4026 * every 2.5 sec for 4.
4027 * Break our of the loop if errors occurred during init.
4028 */
4029 while (((status & mask) != mask) &&
4030 !(status & HS_FFERM) &&
4031 i++ < 20) {
dea31012005-04-17 16:05:31 -05004032
Jamie Wellnitz41415862006-02-28 19:25:27 -05004033 if (i <= 5)
4034 msleep(10);
4035 else if (i <= 10)
4036 msleep(500);
4037 else
4038 msleep(2500);
dea31012005-04-17 16:05:31 -05004039
Jamie Wellnitz41415862006-02-28 19:25:27 -05004040 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05004041 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004042 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004043 lpfc_sli_brdrestart(phba);
4044 }
4045 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004046 if (lpfc_readl(phba->HSregaddr, &status)) {
4047 retval = 1;
4048 break;
4049 }
dea31012005-04-17 16:05:31 -05004050 }
dea31012005-04-17 16:05:31 -05004051
Jamie Wellnitz41415862006-02-28 19:25:27 -05004052 /* Check to see if any errors occurred during init */
4053 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05004054 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4055 "2751 Adapter failed to restart, "
4056 "status reg x%x, FW Data: A8 x%x AC x%x\n",
4057 status,
4058 readl(phba->MBslimaddr + 0xa8),
4059 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004060 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004061 retval = 1;
4062 }
dea31012005-04-17 16:05:31 -05004063
Jamie Wellnitz41415862006-02-28 19:25:27 -05004064 return retval;
dea31012005-04-17 16:05:31 -05004065}
4066
James Smartda0436e2009-05-22 14:51:39 -04004067/**
4068 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
4069 * @phba: Pointer to HBA context object.
4070 * @mask: Bit mask to be checked.
4071 *
4072 * This function checks the host status register to check if HBA is
4073 * ready. This function will wait in a loop for the HBA to be ready
4074 * If the HBA is not ready , the function will will reset the HBA PCI
4075 * function again. The function returns 1 when HBA fail to be ready
4076 * otherwise returns zero.
4077 **/
4078static int
4079lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
4080{
4081 uint32_t status;
4082 int retval = 0;
4083
4084 /* Read the HBA Host Status Register */
4085 status = lpfc_sli4_post_status_check(phba);
4086
4087 if (status) {
4088 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4089 lpfc_sli_brdrestart(phba);
4090 status = lpfc_sli4_post_status_check(phba);
4091 }
4092
4093 /* Check to see if any errors occurred during init */
4094 if (status) {
4095 phba->link_state = LPFC_HBA_ERROR;
4096 retval = 1;
4097 } else
4098 phba->sli4_hba.intr_enable = 0;
4099
4100 return retval;
4101}
4102
4103/**
4104 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
4105 * @phba: Pointer to HBA context object.
4106 * @mask: Bit mask to be checked.
4107 *
4108 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
4109 * from the API jump table function pointer from the lpfc_hba struct.
4110 **/
4111int
4112lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4113{
4114 return phba->lpfc_sli_brdready(phba, mask);
4115}
4116
James Smart92908312006-03-07 15:04:13 -05004117#define BARRIER_TEST_PATTERN (0xdeadbeef)
4118
James Smarte59058c2008-08-24 21:49:00 -04004119/**
James Smart3621a712009-04-06 18:47:14 -04004120 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004121 * @phba: Pointer to HBA context object.
4122 *
James Smart1b511972011-12-13 13:23:09 -05004123 * This function is called before resetting an HBA. This function is called
4124 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004125 **/
James Smart2e0fef82007-06-17 19:56:36 -05004126void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004127{
James Smart65a29c12006-07-06 15:50:50 -04004128 uint32_t __iomem *resp_buf;
4129 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004130 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004131 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004132 int i;
4133 uint8_t hdrtype;
4134
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004135 lockdep_assert_held(&phba->hbalock);
4136
James Smart92908312006-03-07 15:04:13 -05004137 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4138 if (hdrtype != 0x80 ||
4139 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4140 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4141 return;
4142
4143 /*
4144 * Tell the other part of the chip to suspend temporarily all
4145 * its DMA activity.
4146 */
James Smart65a29c12006-07-06 15:50:50 -04004147 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004148
4149 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004150 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4151 return;
James Smart92908312006-03-07 15:04:13 -05004152 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4153 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004154 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004155
James Smart9940b972011-03-11 16:06:12 -05004156 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4157 return;
4158 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004159 /* Clear Chip error bit */
4160 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004161 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004162 }
4163
4164 mbox = 0;
4165 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4166 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4167
4168 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004169 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004170 writel(mbox, mbox_buf);
4171
James Smart9940b972011-03-11 16:06:12 -05004172 for (i = 0; i < 50; i++) {
4173 if (lpfc_readl((resp_buf + 1), &resp_data))
4174 return;
4175 if (resp_data != ~(BARRIER_TEST_PATTERN))
4176 mdelay(1);
4177 else
4178 break;
4179 }
4180 resp_data = 0;
4181 if (lpfc_readl((resp_buf + 1), &resp_data))
4182 return;
4183 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004184 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004185 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004186 goto restore_hc;
4187 else
4188 goto clear_errat;
4189 }
4190
4191 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004192 resp_data = 0;
4193 for (i = 0; i < 500; i++) {
4194 if (lpfc_readl(resp_buf, &resp_data))
4195 return;
4196 if (resp_data != mbox)
4197 mdelay(1);
4198 else
4199 break;
4200 }
James Smart92908312006-03-07 15:04:13 -05004201
4202clear_errat:
4203
James Smart9940b972011-03-11 16:06:12 -05004204 while (++i < 500) {
4205 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4206 return;
4207 if (!(ha_copy & HA_ERATT))
4208 mdelay(1);
4209 else
4210 break;
4211 }
James Smart92908312006-03-07 15:04:13 -05004212
4213 if (readl(phba->HAregaddr) & HA_ERATT) {
4214 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004215 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004216 }
4217
4218restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004219 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004220 writel(hc_copy, phba->HCregaddr);
4221 readl(phba->HCregaddr); /* flush */
4222}
4223
James Smarte59058c2008-08-24 21:49:00 -04004224/**
James Smart3621a712009-04-06 18:47:14 -04004225 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004226 * @phba: Pointer to HBA context object.
4227 *
4228 * This function issues a kill_board mailbox command and waits for
4229 * the error attention interrupt. This function is called for stopping
4230 * the firmware processing. The caller is not required to hold any
4231 * locks. This function calls lpfc_hba_down_post function to free
4232 * any pending commands after the kill. The function will return 1 when it
4233 * fails to kill the board else will return 0.
4234 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004235int
James Smart2e0fef82007-06-17 19:56:36 -05004236lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004237{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004238 struct lpfc_sli *psli;
4239 LPFC_MBOXQ_t *pmb;
4240 uint32_t status;
4241 uint32_t ha_copy;
4242 int retval;
4243 int i = 0;
4244
4245 psli = &phba->sli;
4246
4247 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004248 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004249 "0329 Kill HBA Data: x%x x%x\n",
4250 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004251
James Smart98c9ea52007-10-27 13:37:33 -04004252 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4253 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004254 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004255
4256 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004257 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004258 if (lpfc_readl(phba->HCregaddr, &status)) {
4259 spin_unlock_irq(&phba->hbalock);
4260 mempool_free(pmb, phba->mbox_mem_pool);
4261 return 1;
4262 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004263 status &= ~HC_ERINT_ENA;
4264 writel(status, phba->HCregaddr);
4265 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004266 phba->link_flag |= LS_IGNORE_ERATT;
4267 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004268
4269 lpfc_kill_board(phba, pmb);
4270 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4271 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4272
4273 if (retval != MBX_SUCCESS) {
4274 if (retval != MBX_BUSY)
4275 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004276 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4277 "2752 KILL_BOARD command failed retval %d\n",
4278 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004279 spin_lock_irq(&phba->hbalock);
4280 phba->link_flag &= ~LS_IGNORE_ERATT;
4281 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004282 return 1;
4283 }
4284
James Smartf4b4c682009-05-22 14:53:12 -04004285 spin_lock_irq(&phba->hbalock);
4286 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4287 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004288
Jamie Wellnitz41415862006-02-28 19:25:27 -05004289 mempool_free(pmb, phba->mbox_mem_pool);
4290
4291 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4292 * attention every 100ms for 3 seconds. If we don't get ERATT after
4293 * 3 seconds we still set HBA_ERROR state because the status of the
4294 * board is now undefined.
4295 */
James Smart9940b972011-03-11 16:06:12 -05004296 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4297 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004298 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4299 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004300 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4301 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004302 }
4303
4304 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004305 if (ha_copy & HA_ERATT) {
4306 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004307 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004308 }
James Smart2e0fef82007-06-17 19:56:36 -05004309 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004310 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004311 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004312 phba->link_flag &= ~LS_IGNORE_ERATT;
4313 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004314
Jamie Wellnitz41415862006-02-28 19:25:27 -05004315 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004316 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004317
James Smart2e0fef82007-06-17 19:56:36 -05004318 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004319}
4320
James Smarte59058c2008-08-24 21:49:00 -04004321/**
James Smart3772a992009-05-22 14:50:54 -04004322 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004323 * @phba: Pointer to HBA context object.
4324 *
4325 * This function resets the HBA by writing HC_INITFF to the control
4326 * register. After the HBA resets, this function resets all the iocb ring
4327 * indices. This function disables PCI layer parity checking during
4328 * the reset.
4329 * This function returns 0 always.
4330 * The caller is not required to hold any locks.
4331 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004332int
James Smart2e0fef82007-06-17 19:56:36 -05004333lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004334{
4335 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004336 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004337 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004338 int i;
dea31012005-04-17 16:05:31 -05004339
Jamie Wellnitz41415862006-02-28 19:25:27 -05004340 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004341
Jamie Wellnitz41415862006-02-28 19:25:27 -05004342 /* Reset HBA */
4343 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004344 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004345 (phba->pport) ? phba->pport->port_state : 0,
4346 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004347
4348 /* perform board reset */
4349 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004350 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004351 if (phba->pport) {
4352 phba->pport->fc_myDID = 0;
4353 phba->pport->fc_prevDID = 0;
4354 }
dea31012005-04-17 16:05:31 -05004355
Jamie Wellnitz41415862006-02-28 19:25:27 -05004356 /* Turn off parity checking and serr during the physical reset */
4357 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4358 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4359 (cfg_value &
4360 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4361
James Smart3772a992009-05-22 14:50:54 -04004362 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4363
Jamie Wellnitz41415862006-02-28 19:25:27 -05004364 /* Now toggle INITFF bit in the Host Control Register */
4365 writel(HC_INITFF, phba->HCregaddr);
4366 mdelay(1);
4367 readl(phba->HCregaddr); /* flush */
4368 writel(0, phba->HCregaddr);
4369 readl(phba->HCregaddr); /* flush */
4370
4371 /* Restore PCI cmd register */
4372 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004373
4374 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004375 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004376 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004377 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004378 pring->sli.sli3.rspidx = 0;
4379 pring->sli.sli3.next_cmdidx = 0;
4380 pring->sli.sli3.local_getidx = 0;
4381 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004382 pring->missbufcnt = 0;
4383 }
dea31012005-04-17 16:05:31 -05004384
James Smart2e0fef82007-06-17 19:56:36 -05004385 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004386 return 0;
4387}
4388
James Smarte59058c2008-08-24 21:49:00 -04004389/**
James Smartda0436e2009-05-22 14:51:39 -04004390 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4391 * @phba: Pointer to HBA context object.
4392 *
4393 * This function resets a SLI4 HBA. This function disables PCI layer parity
4394 * checking during resets the device. The caller is not required to hold
4395 * any locks.
4396 *
4397 * This function returns 0 always.
4398 **/
4399int
4400lpfc_sli4_brdreset(struct lpfc_hba *phba)
4401{
4402 struct lpfc_sli *psli = &phba->sli;
4403 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004404 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004405
4406 /* Reset HBA */
4407 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004408 "0295 Reset HBA Data: x%x x%x x%x\n",
4409 phba->pport->port_state, psli->sli_flag,
4410 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004411
4412 /* perform board reset */
4413 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004414 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004415 phba->pport->fc_myDID = 0;
4416 phba->pport->fc_prevDID = 0;
4417
James Smartda0436e2009-05-22 14:51:39 -04004418 spin_lock_irq(&phba->hbalock);
4419 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4420 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004421 spin_unlock_irq(&phba->hbalock);
4422
James Smart02936352014-04-04 13:52:12 -04004423 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4424 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4425 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4426 return rc;
4427 }
4428
James Smartda0436e2009-05-22 14:51:39 -04004429 /* Now physically reset the device */
4430 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4431 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004432
4433 /* Turn off parity checking and serr during the physical reset */
4434 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4435 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4436 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4437
James Smart88318812012-09-29 11:29:29 -04004438 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004439 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004440
James Smartbe858b62010-12-15 17:57:20 -05004441 /* Restore PCI cmd register */
4442 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4443
James Smart27b01b82012-05-09 21:19:44 -04004444 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004445}
4446
4447/**
4448 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004449 * @phba: Pointer to HBA context object.
4450 *
4451 * This function is called in the SLI initialization code path to
4452 * restart the HBA. The caller is not required to hold any lock.
4453 * This function writes MBX_RESTART mailbox command to the SLIM and
4454 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4455 * function to free any pending commands. The function enables
4456 * POST only during the first initialization. The function returns zero.
4457 * The function does not guarantee completion of MBX_RESTART mailbox
4458 * command before the return of this function.
4459 **/
James Smartda0436e2009-05-22 14:51:39 -04004460static int
4461lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004462{
4463 MAILBOX_t *mb;
4464 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004465 volatile uint32_t word0;
4466 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004467 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004468
James Smart2e0fef82007-06-17 19:56:36 -05004469 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004470
James Smart0d878412009-10-02 15:16:56 -04004471 /* Take PCIe device Advanced Error Reporting (AER) state */
4472 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4473
Jamie Wellnitz41415862006-02-28 19:25:27 -05004474 psli = &phba->sli;
4475
4476 /* Restart HBA */
4477 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004478 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004479 (phba->pport) ? phba->pport->port_state : 0,
4480 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004481
4482 word0 = 0;
4483 mb = (MAILBOX_t *) &word0;
4484 mb->mbxCommand = MBX_RESTART;
4485 mb->mbxHc = 1;
4486
James Smart92908312006-03-07 15:04:13 -05004487 lpfc_reset_barrier(phba);
4488
Jamie Wellnitz41415862006-02-28 19:25:27 -05004489 to_slim = phba->MBslimaddr;
4490 writel(*(uint32_t *) mb, to_slim);
4491 readl(to_slim); /* flush */
4492
4493 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004494 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004495 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004496 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004497 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004498 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004499 writel(*(uint32_t *) mb, to_slim);
4500 readl(to_slim); /* flush */
4501
4502 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004503 if (phba->pport)
4504 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004505 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004506 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004507 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004508
James Smart64ba8812006-08-02 15:24:34 -04004509 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4510 psli->stats_start = get_seconds();
4511
James Smarteaf15d52008-12-04 22:39:29 -05004512 /* Give the INITFF and Post time to settle. */
4513 mdelay(100);
dea31012005-04-17 16:05:31 -05004514
James Smart0d878412009-10-02 15:16:56 -04004515 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4516 if (hba_aer_enabled)
4517 pci_disable_pcie_error_reporting(phba->pcidev);
4518
Jamie Wellnitz41415862006-02-28 19:25:27 -05004519 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004520
4521 return 0;
4522}
4523
James Smarte59058c2008-08-24 21:49:00 -04004524/**
James Smartda0436e2009-05-22 14:51:39 -04004525 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4526 * @phba: Pointer to HBA context object.
4527 *
4528 * This function is called in the SLI initialization code path to restart
4529 * a SLI4 HBA. The caller is not required to hold any lock.
4530 * At the end of the function, it calls lpfc_hba_down_post function to
4531 * free any pending commands.
4532 **/
4533static int
4534lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4535{
4536 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004537 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004538 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004539
4540 /* Restart HBA */
4541 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4542 "0296 Restart HBA Data: x%x x%x\n",
4543 phba->pport->port_state, psli->sli_flag);
4544
James Smart75baf692010-06-08 18:31:21 -04004545 /* Take PCIe device Advanced Error Reporting (AER) state */
4546 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4547
James Smart27b01b82012-05-09 21:19:44 -04004548 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004549
4550 spin_lock_irq(&phba->hbalock);
4551 phba->pport->stopped = 0;
4552 phba->link_state = LPFC_INIT_START;
4553 phba->hba_flag = 0;
4554 spin_unlock_irq(&phba->hbalock);
4555
4556 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4557 psli->stats_start = get_seconds();
4558
James Smart75baf692010-06-08 18:31:21 -04004559 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4560 if (hba_aer_enabled)
4561 pci_disable_pcie_error_reporting(phba->pcidev);
4562
James Smartda0436e2009-05-22 14:51:39 -04004563 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004564 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004565
James Smart27b01b82012-05-09 21:19:44 -04004566 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004567}
4568
4569/**
4570 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4571 * @phba: Pointer to HBA context object.
4572 *
4573 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4574 * API jump table function pointer from the lpfc_hba struct.
4575**/
4576int
4577lpfc_sli_brdrestart(struct lpfc_hba *phba)
4578{
4579 return phba->lpfc_sli_brdrestart(phba);
4580}
4581
4582/**
James Smart3621a712009-04-06 18:47:14 -04004583 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004584 * @phba: Pointer to HBA context object.
4585 *
4586 * This function is called after a HBA restart to wait for successful
4587 * restart of the HBA. Successful restart of the HBA is indicated by
4588 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4589 * iteration, the function will restart the HBA again. The function returns
4590 * zero if HBA successfully restarted else returns negative error code.
4591 **/
James Smart4492b732017-04-27 15:08:26 -07004592int
dea31012005-04-17 16:05:31 -05004593lpfc_sli_chipset_init(struct lpfc_hba *phba)
4594{
4595 uint32_t status, i = 0;
4596
4597 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004598 if (lpfc_readl(phba->HSregaddr, &status))
4599 return -EIO;
dea31012005-04-17 16:05:31 -05004600
4601 /* Check status register to see what current state is */
4602 i = 0;
4603 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4604
James Smartdcf2a4e2010-09-29 11:18:53 -04004605 /* Check every 10ms for 10 retries, then every 100ms for 90
4606 * retries, then every 1 sec for 50 retires for a total of
4607 * ~60 seconds before reset the board again and check every
4608 * 1 sec for 50 retries. The up to 60 seconds before the
4609 * board ready is required by the Falcon FIPS zeroization
4610 * complete, and any reset the board in between shall cause
4611 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004612 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004613 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004614 /* Adapter failed to init, timeout, status reg
4615 <status> */
James Smarted957682007-06-17 19:56:37 -05004616 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004617 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004618 "timeout, status reg x%x, "
4619 "FW Data: A8 x%x AC x%x\n", status,
4620 readl(phba->MBslimaddr + 0xa8),
4621 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004622 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004623 return -ETIMEDOUT;
4624 }
4625
4626 /* Check to see if any errors occurred during init */
4627 if (status & HS_FFERM) {
4628 /* ERROR: During chipset initialization */
4629 /* Adapter failed to init, chipset, status reg
4630 <status> */
James Smarted957682007-06-17 19:56:37 -05004631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004632 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004633 "chipset, status reg x%x, "
4634 "FW Data: A8 x%x AC x%x\n", status,
4635 readl(phba->MBslimaddr + 0xa8),
4636 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004637 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004638 return -EIO;
4639 }
4640
James Smartdcf2a4e2010-09-29 11:18:53 -04004641 if (i <= 10)
dea31012005-04-17 16:05:31 -05004642 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004643 else if (i <= 100)
4644 msleep(100);
4645 else
4646 msleep(1000);
dea31012005-04-17 16:05:31 -05004647
James Smartdcf2a4e2010-09-29 11:18:53 -04004648 if (i == 150) {
4649 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004650 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004651 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004652 }
4653 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004654 if (lpfc_readl(phba->HSregaddr, &status))
4655 return -EIO;
dea31012005-04-17 16:05:31 -05004656 }
4657
4658 /* Check to see if any errors occurred during init */
4659 if (status & HS_FFERM) {
4660 /* ERROR: During chipset initialization */
4661 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004663 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004664 "status reg x%x, "
4665 "FW Data: A8 x%x AC x%x\n", status,
4666 readl(phba->MBslimaddr + 0xa8),
4667 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004668 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004669 return -EIO;
4670 }
4671
4672 /* Clear all interrupt enable conditions */
4673 writel(0, phba->HCregaddr);
4674 readl(phba->HCregaddr); /* flush */
4675
4676 /* setup host attn register */
4677 writel(0xffffffff, phba->HAregaddr);
4678 readl(phba->HAregaddr); /* flush */
4679 return 0;
4680}
4681
James Smarte59058c2008-08-24 21:49:00 -04004682/**
James Smart3621a712009-04-06 18:47:14 -04004683 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004684 *
4685 * This function calculates and returns the number of HBQs required to be
4686 * configured.
4687 **/
James Smart78b2d852007-08-02 11:10:21 -04004688int
James Smarted957682007-06-17 19:56:37 -05004689lpfc_sli_hbq_count(void)
4690{
James Smart92d7f7b2007-06-17 19:56:38 -05004691 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004692}
4693
James Smarte59058c2008-08-24 21:49:00 -04004694/**
James Smart3621a712009-04-06 18:47:14 -04004695 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004696 *
4697 * This function adds the number of hbq entries in every HBQ to get
4698 * the total number of hbq entries required for the HBA and returns
4699 * the total count.
4700 **/
James Smarted957682007-06-17 19:56:37 -05004701static int
4702lpfc_sli_hbq_entry_count(void)
4703{
4704 int hbq_count = lpfc_sli_hbq_count();
4705 int count = 0;
4706 int i;
4707
4708 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004709 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004710 return count;
4711}
4712
James Smarte59058c2008-08-24 21:49:00 -04004713/**
James Smart3621a712009-04-06 18:47:14 -04004714 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004715 *
4716 * This function calculates amount of memory required for all hbq entries
4717 * to be configured and returns the total memory required.
4718 **/
dea31012005-04-17 16:05:31 -05004719int
James Smarted957682007-06-17 19:56:37 -05004720lpfc_sli_hbq_size(void)
4721{
4722 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4723}
4724
James Smarte59058c2008-08-24 21:49:00 -04004725/**
James Smart3621a712009-04-06 18:47:14 -04004726 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004727 * @phba: Pointer to HBA context object.
4728 *
4729 * This function is called during the SLI initialization to configure
4730 * all the HBQs and post buffers to the HBQ. The caller is not
4731 * required to hold any locks. This function will return zero if successful
4732 * else it will return negative error code.
4733 **/
James Smarted957682007-06-17 19:56:37 -05004734static int
4735lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4736{
4737 int hbq_count = lpfc_sli_hbq_count();
4738 LPFC_MBOXQ_t *pmb;
4739 MAILBOX_t *pmbox;
4740 uint32_t hbqno;
4741 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004742
James Smart92d7f7b2007-06-17 19:56:38 -05004743 /* Get a Mailbox buffer to setup mailbox
4744 * commands for HBA initialization
4745 */
James Smarted957682007-06-17 19:56:37 -05004746 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4747
4748 if (!pmb)
4749 return -ENOMEM;
4750
James Smart04c68492009-05-22 14:52:52 -04004751 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004752
4753 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4754 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004755 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004756
4757 hbq_entry_index = 0;
4758 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4759 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4760 phba->hbqs[hbqno].hbqPutIdx = 0;
4761 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4762 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004763 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004764 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4765 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004766 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4767
4768 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4769 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4770 mbxStatus <status>, ring <num> */
4771
4772 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004773 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004774 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004775 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004776 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004777 pmbox->mbxStatus, hbqno);
4778
4779 phba->link_state = LPFC_HBA_ERROR;
4780 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004781 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004782 }
4783 }
4784 phba->hbq_count = hbq_count;
4785
James Smarted957682007-06-17 19:56:37 -05004786 mempool_free(pmb, phba->mbox_mem_pool);
4787
James Smart92d7f7b2007-06-17 19:56:38 -05004788 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004789 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4790 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004791 return 0;
4792}
4793
James Smarte59058c2008-08-24 21:49:00 -04004794/**
James Smart4f774512009-05-22 14:52:35 -04004795 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4796 * @phba: Pointer to HBA context object.
4797 *
4798 * This function is called during the SLI initialization to configure
4799 * all the HBQs and post buffers to the HBQ. The caller is not
4800 * required to hold any locks. This function will return zero if successful
4801 * else it will return negative error code.
4802 **/
4803static int
4804lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4805{
4806 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004807 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4808 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004809 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004810 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004811 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004812 return 0;
4813}
4814
4815/**
James Smart3621a712009-04-06 18:47:14 -04004816 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004817 * @phba: Pointer to HBA context object.
4818 * @sli_mode: sli mode - 2/3
4819 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004820 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004821 * to issue config_port mailbox command. This function restarts the
4822 * HBA firmware and issues a config_port mailbox command to configure
4823 * the SLI interface in the sli mode specified by sli_mode
4824 * variable. The caller is not required to hold any locks.
4825 * The function returns 0 if successful, else returns negative error
4826 * code.
4827 **/
James Smart93996272008-08-24 21:50:30 -04004828int
4829lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004830{
4831 LPFC_MBOXQ_t *pmb;
4832 uint32_t resetcount = 0, rc = 0, done = 0;
4833
4834 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4835 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004836 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004837 return -ENOMEM;
4838 }
4839
James Smarted957682007-06-17 19:56:37 -05004840 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004841 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004842 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004843 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004844 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004845 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004846 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004847 rc = lpfc_sli_chipset_init(phba);
4848 if (rc)
4849 break;
4850
James Smart2e0fef82007-06-17 19:56:36 -05004851 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004852 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004853 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004854 resetcount++;
4855
James Smarted957682007-06-17 19:56:37 -05004856 /* Call pre CONFIG_PORT mailbox command initialization. A
4857 * value of 0 means the call was successful. Any other
4858 * nonzero value is a failure, but if ERESTART is returned,
4859 * the driver may reset the HBA and try again.
4860 */
dea31012005-04-17 16:05:31 -05004861 rc = lpfc_config_port_prep(phba);
4862 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004863 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004864 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004865 } else if (rc)
dea31012005-04-17 16:05:31 -05004866 break;
James Smart6d368e52011-05-24 11:44:12 -04004867
James Smart2e0fef82007-06-17 19:56:36 -05004868 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004869 lpfc_config_port(phba, pmb);
4870 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004871 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4872 LPFC_SLI3_HBQ_ENABLED |
4873 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004874 LPFC_SLI3_BG_ENABLED |
4875 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004876 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004878 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004879 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004880 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004881 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004882 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004883 spin_unlock_irq(&phba->hbalock);
4884 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004885 } else {
4886 /* Allow asynchronous mailbox command to go through */
4887 spin_lock_irq(&phba->hbalock);
4888 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4889 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004890 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004891
4892 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4893 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4894 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4895 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004896 }
dea31012005-04-17 16:05:31 -05004897 }
James Smarted957682007-06-17 19:56:37 -05004898 if (!done) {
4899 rc = -EINVAL;
4900 goto do_prep_failed;
4901 }
James Smart04c68492009-05-22 14:52:52 -04004902 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4903 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004904 rc = -ENXIO;
4905 goto do_prep_failed;
4906 }
James Smart04c68492009-05-22 14:52:52 -04004907 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004908 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004909 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4910 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4911 phba->max_vpi : phba->max_vports;
4912
James Smart34b02dc2008-08-24 21:49:55 -04004913 } else
4914 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004915 phba->fips_level = 0;
4916 phba->fips_spec_rev = 0;
4917 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004918 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004919 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4920 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4921 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4922 "2850 Security Crypto Active. FIPS x%d "
4923 "(Spec Rev: x%d)",
4924 phba->fips_level, phba->fips_spec_rev);
4925 }
4926 if (pmb->u.mb.un.varCfgPort.sec_err) {
4927 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4928 "2856 Config Port Security Crypto "
4929 "Error: x%x ",
4930 pmb->u.mb.un.varCfgPort.sec_err);
4931 }
James Smart04c68492009-05-22 14:52:52 -04004932 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004933 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004934 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004935 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004936
4937 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4938 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004939
4940 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004941 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004942 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4943 else
4944 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4945 "0443 Adapter did not grant "
4946 "BlockGuard\n");
4947 }
James Smart34b02dc2008-08-24 21:49:55 -04004948 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004949 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004950 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004951 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004952 }
James Smart92d7f7b2007-06-17 19:56:38 -05004953do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004954 mempool_free(pmb, phba->mbox_mem_pool);
4955 return rc;
4956}
4957
James Smarte59058c2008-08-24 21:49:00 -04004958
4959/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08004960 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04004961 * @phba: Pointer to HBA context object.
4962 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004963 * This function is the main SLI initialization function. This function
4964 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04004965 * error attention handler code. Caller is not required to hold any
4966 * locks. This function issues config_port mailbox command to configure
4967 * the SLI, setup iocb rings and HBQ rings. In the end the function
4968 * calls the config_port_post function to issue init_link mailbox
4969 * command and to start the discovery. The function will return zero
4970 * if successful, else it will return negative error code.
4971 **/
James Smarted957682007-06-17 19:56:37 -05004972int
4973lpfc_sli_hba_setup(struct lpfc_hba *phba)
4974{
4975 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004976 int mode = 3, i;
4977 int longs;
James Smarted957682007-06-17 19:56:37 -05004978
James Smart12247e82016-07-06 12:36:09 -07004979 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004980 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004981 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004982 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004983 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004984 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004985 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004986 break;
4987 }
James Smarted957682007-06-17 19:56:37 -05004988 mode = 2;
4989 break;
4990 case 0:
4991 case 3:
4992 break;
4993 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004995 "1819 Unrecognized sli_mode parameter: %d.\n",
4996 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004997
4998 break;
4999 }
James Smartb5c53952016-03-31 14:12:30 -07005000 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05005001
James Smart93996272008-08-24 21:50:30 -04005002 rc = lpfc_sli_config_port(phba, mode);
5003
James Smart12247e82016-07-06 12:36:09 -07005004 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05005005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005006 "1820 Unable to select SLI-3. "
5007 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05005008 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04005009 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07005010 else if (rc && mode == 2)
5011 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05005012 if (rc)
dea31012005-04-17 16:05:31 -05005013 goto lpfc_sli_hba_setup_error;
5014
James Smart0d878412009-10-02 15:16:56 -04005015 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
5016 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
5017 rc = pci_enable_pcie_error_reporting(phba->pcidev);
5018 if (!rc) {
5019 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5020 "2709 This device supports "
5021 "Advanced Error Reporting (AER)\n");
5022 spin_lock_irq(&phba->hbalock);
5023 phba->hba_flag |= HBA_AER_ENABLED;
5024 spin_unlock_irq(&phba->hbalock);
5025 } else {
5026 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5027 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04005028 "Advanced Error Reporting (AER): %d\n",
5029 rc);
James Smart0d878412009-10-02 15:16:56 -04005030 phba->cfg_aer_support = 0;
5031 }
5032 }
5033
James Smarted957682007-06-17 19:56:37 -05005034 if (phba->sli_rev == 3) {
5035 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
5036 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05005037 } else {
5038 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
5039 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05005040 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05005041 }
5042
5043 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005044 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
5045 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05005046 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05005047
5048 if (rc)
5049 goto lpfc_sli_hba_setup_error;
5050
James Smart6d368e52011-05-24 11:44:12 -04005051 /* Initialize VPIs. */
5052 if (phba->sli_rev == LPFC_SLI_REV3) {
5053 /*
5054 * The VPI bitmask and physical ID array are allocated
5055 * and initialized once only - at driver load. A port
5056 * reset doesn't need to reinitialize this memory.
5057 */
5058 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
5059 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
5060 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
5061 GFP_KERNEL);
5062 if (!phba->vpi_bmask) {
5063 rc = -ENOMEM;
5064 goto lpfc_sli_hba_setup_error;
5065 }
5066
5067 phba->vpi_ids = kzalloc(
5068 (phba->max_vpi+1) * sizeof(uint16_t),
5069 GFP_KERNEL);
5070 if (!phba->vpi_ids) {
5071 kfree(phba->vpi_bmask);
5072 rc = -ENOMEM;
5073 goto lpfc_sli_hba_setup_error;
5074 }
5075 for (i = 0; i < phba->max_vpi; i++)
5076 phba->vpi_ids[i] = i;
5077 }
5078 }
5079
James Smart93996272008-08-24 21:50:30 -04005080 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05005081 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5082 rc = lpfc_sli_hbq_setup(phba);
5083 if (rc)
5084 goto lpfc_sli_hba_setup_error;
5085 }
James Smart04c68492009-05-22 14:52:52 -04005086 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005087 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04005088 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005089
5090 rc = lpfc_config_port_post(phba);
5091 if (rc)
5092 goto lpfc_sli_hba_setup_error;
5093
James Smarted957682007-06-17 19:56:37 -05005094 return rc;
5095
James Smart92d7f7b2007-06-17 19:56:38 -05005096lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05005097 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05005098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04005099 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05005100 return rc;
5101}
5102
James Smartda0436e2009-05-22 14:51:39 -04005103/**
5104 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
5105 * @phba: Pointer to HBA context object.
5106 * @mboxq: mailbox pointer.
5107 * This function issue a dump mailbox command to read config region
5108 * 23 and parse the records in the region and populate driver
5109 * data structure.
5110 **/
5111static int
James Smartff78d8f2011-12-13 13:21:35 -05005112lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005113{
James Smartff78d8f2011-12-13 13:21:35 -05005114 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005115 struct lpfc_dmabuf *mp;
5116 struct lpfc_mqe *mqe;
5117 uint32_t data_length;
5118 int rc;
5119
5120 /* Program the default value of vlan_id and fc_map */
5121 phba->valid_vlan = 0;
5122 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5123 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5124 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5125
James Smartff78d8f2011-12-13 13:21:35 -05005126 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5127 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005128 return -ENOMEM;
5129
James Smartff78d8f2011-12-13 13:21:35 -05005130 mqe = &mboxq->u.mqe;
5131 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5132 rc = -ENOMEM;
5133 goto out_free_mboxq;
5134 }
5135
James Smartda0436e2009-05-22 14:51:39 -04005136 mp = (struct lpfc_dmabuf *) mboxq->context1;
5137 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5138
5139 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5140 "(%d):2571 Mailbox cmd x%x Status x%x "
5141 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5142 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5143 "CQ: x%x x%x x%x x%x\n",
5144 mboxq->vport ? mboxq->vport->vpi : 0,
5145 bf_get(lpfc_mqe_command, mqe),
5146 bf_get(lpfc_mqe_status, mqe),
5147 mqe->un.mb_words[0], mqe->un.mb_words[1],
5148 mqe->un.mb_words[2], mqe->un.mb_words[3],
5149 mqe->un.mb_words[4], mqe->un.mb_words[5],
5150 mqe->un.mb_words[6], mqe->un.mb_words[7],
5151 mqe->un.mb_words[8], mqe->un.mb_words[9],
5152 mqe->un.mb_words[10], mqe->un.mb_words[11],
5153 mqe->un.mb_words[12], mqe->un.mb_words[13],
5154 mqe->un.mb_words[14], mqe->un.mb_words[15],
5155 mqe->un.mb_words[16], mqe->un.mb_words[50],
5156 mboxq->mcqe.word0,
5157 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5158 mboxq->mcqe.trailer);
5159
5160 if (rc) {
5161 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5162 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005163 rc = -EIO;
5164 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005165 }
5166 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005167 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005168 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5169 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005170 rc = -EIO;
5171 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005172 }
James Smartda0436e2009-05-22 14:51:39 -04005173
5174 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5175 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5176 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005177 rc = 0;
5178
5179out_free_mboxq:
5180 mempool_free(mboxq, phba->mbox_mem_pool);
5181 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005182}
5183
5184/**
5185 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5186 * @phba: pointer to lpfc hba data structure.
5187 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5188 * @vpd: pointer to the memory to hold resulting port vpd data.
5189 * @vpd_size: On input, the number of bytes allocated to @vpd.
5190 * On output, the number of data bytes in @vpd.
5191 *
5192 * This routine executes a READ_REV SLI4 mailbox command. In
5193 * addition, this routine gets the port vpd data.
5194 *
5195 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005196 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005197 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005198 **/
5199static int
5200lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5201 uint8_t *vpd, uint32_t *vpd_size)
5202{
5203 int rc = 0;
5204 uint32_t dma_size;
5205 struct lpfc_dmabuf *dmabuf;
5206 struct lpfc_mqe *mqe;
5207
5208 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5209 if (!dmabuf)
5210 return -ENOMEM;
5211
5212 /*
5213 * Get a DMA buffer for the vpd data resulting from the READ_REV
5214 * mailbox command.
5215 */
5216 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005217 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5218 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005219 if (!dmabuf->virt) {
5220 kfree(dmabuf);
5221 return -ENOMEM;
5222 }
James Smartda0436e2009-05-22 14:51:39 -04005223
5224 /*
5225 * The SLI4 implementation of READ_REV conflicts at word1,
5226 * bits 31:16 and SLI4 adds vpd functionality not present
5227 * in SLI3. This code corrects the conflicts.
5228 */
5229 lpfc_read_rev(phba, mboxq);
5230 mqe = &mboxq->u.mqe;
5231 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5232 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5233 mqe->un.read_rev.word1 &= 0x0000FFFF;
5234 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5235 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5236
5237 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5238 if (rc) {
5239 dma_free_coherent(&phba->pcidev->dev, dma_size,
5240 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005241 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005242 return -EIO;
5243 }
5244
James Smartda0436e2009-05-22 14:51:39 -04005245 /*
5246 * The available vpd length cannot be bigger than the
5247 * DMA buffer passed to the port. Catch the less than
5248 * case and update the caller's size.
5249 */
5250 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5251 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5252
James Smartd7c47992010-06-08 18:31:54 -04005253 memcpy(vpd, dmabuf->virt, *vpd_size);
5254
James Smartda0436e2009-05-22 14:51:39 -04005255 dma_free_coherent(&phba->pcidev->dev, dma_size,
5256 dmabuf->virt, dmabuf->phys);
5257 kfree(dmabuf);
5258 return 0;
5259}
5260
5261/**
James Smartcd1c8302011-10-10 21:33:25 -04005262 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5263 * @phba: pointer to lpfc hba data structure.
5264 *
5265 * This routine retrieves SLI4 device physical port name this PCI function
5266 * is attached to.
5267 *
5268 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005269 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005270 * otherwise - failed to retrieve physical port name
5271 **/
5272static int
5273lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5274{
5275 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005276 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5277 struct lpfc_controller_attribute *cntl_attr;
5278 struct lpfc_mbx_get_port_name *get_port_name;
5279 void *virtaddr = NULL;
5280 uint32_t alloclen, reqlen;
5281 uint32_t shdr_status, shdr_add_status;
5282 union lpfc_sli4_cfg_shdr *shdr;
5283 char cport_name = 0;
5284 int rc;
5285
5286 /* We assume nothing at this point */
5287 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5288 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5289
5290 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5291 if (!mboxq)
5292 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005293 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005294 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5295 lpfc_sli4_read_config(phba);
5296 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5297 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005298
5299 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5300 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5301 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5302 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5303 LPFC_SLI4_MBX_NEMBED);
5304 if (alloclen < reqlen) {
5305 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5306 "3084 Allocated DMA memory size (%d) is "
5307 "less than the requested DMA memory size "
5308 "(%d)\n", alloclen, reqlen);
5309 rc = -ENOMEM;
5310 goto out_free_mboxq;
5311 }
5312 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5313 virtaddr = mboxq->sge_array->addr[0];
5314 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5315 shdr = &mbx_cntl_attr->cfg_shdr;
5316 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5317 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5318 if (shdr_status || shdr_add_status || rc) {
5319 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5320 "3085 Mailbox x%x (x%x/x%x) failed, "
5321 "rc:x%x, status:x%x, add_status:x%x\n",
5322 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5323 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5324 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5325 rc, shdr_status, shdr_add_status);
5326 rc = -ENXIO;
5327 goto out_free_mboxq;
5328 }
5329 cntl_attr = &mbx_cntl_attr->cntl_attr;
5330 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5331 phba->sli4_hba.lnk_info.lnk_tp =
5332 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5333 phba->sli4_hba.lnk_info.lnk_no =
5334 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5335 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5336 "3086 lnk_type:%d, lnk_numb:%d\n",
5337 phba->sli4_hba.lnk_info.lnk_tp,
5338 phba->sli4_hba.lnk_info.lnk_no);
5339
5340retrieve_ppname:
5341 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5342 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5343 sizeof(struct lpfc_mbx_get_port_name) -
5344 sizeof(struct lpfc_sli4_cfg_mhdr),
5345 LPFC_SLI4_MBX_EMBED);
5346 get_port_name = &mboxq->u.mqe.un.get_port_name;
5347 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5348 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5349 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5350 phba->sli4_hba.lnk_info.lnk_tp);
5351 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5352 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5353 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5354 if (shdr_status || shdr_add_status || rc) {
5355 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5356 "3087 Mailbox x%x (x%x/x%x) failed: "
5357 "rc:x%x, status:x%x, add_status:x%x\n",
5358 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5359 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5360 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5361 rc, shdr_status, shdr_add_status);
5362 rc = -ENXIO;
5363 goto out_free_mboxq;
5364 }
5365 switch (phba->sli4_hba.lnk_info.lnk_no) {
5366 case LPFC_LINK_NUMBER_0:
5367 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5368 &get_port_name->u.response);
5369 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5370 break;
5371 case LPFC_LINK_NUMBER_1:
5372 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5373 &get_port_name->u.response);
5374 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5375 break;
5376 case LPFC_LINK_NUMBER_2:
5377 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5378 &get_port_name->u.response);
5379 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5380 break;
5381 case LPFC_LINK_NUMBER_3:
5382 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5383 &get_port_name->u.response);
5384 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5385 break;
5386 default:
5387 break;
5388 }
5389
5390 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5391 phba->Port[0] = cport_name;
5392 phba->Port[1] = '\0';
5393 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5394 "3091 SLI get port name: %s\n", phba->Port);
5395 }
5396
5397out_free_mboxq:
5398 if (rc != MBX_TIMEOUT) {
5399 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5400 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5401 else
5402 mempool_free(mboxq, phba->mbox_mem_pool);
5403 }
5404 return rc;
5405}
5406
5407/**
James Smartda0436e2009-05-22 14:51:39 -04005408 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5409 * @phba: pointer to lpfc hba data structure.
5410 *
5411 * This routine is called to explicitly arm the SLI4 device's completion and
5412 * event queues
5413 **/
5414static void
5415lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5416{
James Smart895427b2017-02-12 13:52:30 -08005417 int qidx;
James Smartb71413d2018-02-22 08:18:40 -08005418 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smartda0436e2009-05-22 14:51:39 -04005419
James Smartb71413d2018-02-22 08:18:40 -08005420 sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
5421 sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
5422 if (sli4_hba->nvmels_cq)
5423 sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
James Smart895427b2017-02-12 13:52:30 -08005424 LPFC_QUEUE_REARM);
5425
James Smartb71413d2018-02-22 08:18:40 -08005426 if (sli4_hba->fcp_cq)
James Smart895427b2017-02-12 13:52:30 -08005427 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005428 sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005429 LPFC_QUEUE_REARM);
5430
James Smartb71413d2018-02-22 08:18:40 -08005431 if (sli4_hba->nvme_cq)
James Smart895427b2017-02-12 13:52:30 -08005432 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005433 sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
James Smart895427b2017-02-12 13:52:30 -08005434 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005435
James Smartf38fa0b2014-04-04 13:52:21 -04005436 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005437 sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005438
James Smartb71413d2018-02-22 08:18:40 -08005439 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08005440 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
James Smartb71413d2018-02-22 08:18:40 -08005441 sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
5442 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005443
James Smart2d7dbc42017-02-12 13:52:35 -08005444 if (phba->nvmet_support) {
5445 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
James Smartb71413d2018-02-22 08:18:40 -08005446 sli4_hba->sli4_cq_release(
5447 sli4_hba->nvmet_cqset[qidx],
James Smart2d7dbc42017-02-12 13:52:35 -08005448 LPFC_QUEUE_REARM);
5449 }
James Smart2e90f4b2011-12-13 13:22:37 -05005450 }
James Smart1ba981f2014-02-20 09:56:45 -05005451
5452 if (phba->cfg_fof)
James Smartb71413d2018-02-22 08:18:40 -08005453 sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005454}
5455
5456/**
James Smart6d368e52011-05-24 11:44:12 -04005457 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5458 * @phba: Pointer to HBA context object.
5459 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005460 * @extnt_count: buffer to hold port available extent count.
5461 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005462 *
James Smartb76f2dc2011-07-22 18:37:42 -04005463 * This function calls the port and retrievs the number of available
5464 * extents and their size for a particular extent type.
5465 *
5466 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005467 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005468int
James Smart6d368e52011-05-24 11:44:12 -04005469lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5470 uint16_t *extnt_count, uint16_t *extnt_size)
5471{
5472 int rc = 0;
5473 uint32_t length;
5474 uint32_t mbox_tmo;
5475 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5476 LPFC_MBOXQ_t *mbox;
5477
5478 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5479 if (!mbox)
5480 return -ENOMEM;
5481
5482 /* Find out how many extents are available for this resource type */
5483 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5484 sizeof(struct lpfc_sli4_cfg_mhdr));
5485 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5486 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5487 length, LPFC_SLI4_MBX_EMBED);
5488
5489 /* Send an extents count of 0 - the GET doesn't use it. */
5490 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5491 LPFC_SLI4_MBX_EMBED);
5492 if (unlikely(rc)) {
5493 rc = -EIO;
5494 goto err_exit;
5495 }
5496
5497 if (!phba->sli4_hba.intr_enable)
5498 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5499 else {
James Smarta183a152011-10-10 21:32:43 -04005500 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005501 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5502 }
5503 if (unlikely(rc)) {
5504 rc = -EIO;
5505 goto err_exit;
5506 }
5507
5508 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5509 if (bf_get(lpfc_mbox_hdr_status,
5510 &rsrc_info->header.cfg_shdr.response)) {
5511 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5512 "2930 Failed to get resource extents "
5513 "Status 0x%x Add'l Status 0x%x\n",
5514 bf_get(lpfc_mbox_hdr_status,
5515 &rsrc_info->header.cfg_shdr.response),
5516 bf_get(lpfc_mbox_hdr_add_status,
5517 &rsrc_info->header.cfg_shdr.response));
5518 rc = -EIO;
5519 goto err_exit;
5520 }
5521
5522 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5523 &rsrc_info->u.rsp);
5524 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5525 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005526
5527 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5528 "3162 Retrieved extents type-%d from port: count:%d, "
5529 "size:%d\n", type, *extnt_count, *extnt_size);
5530
5531err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005532 mempool_free(mbox, phba->mbox_mem_pool);
5533 return rc;
5534}
5535
5536/**
5537 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5538 * @phba: Pointer to HBA context object.
5539 * @type: The extent type to check.
5540 *
5541 * This function reads the current available extents from the port and checks
5542 * if the extent count or extent size has changed since the last access.
5543 * Callers use this routine post port reset to understand if there is a
5544 * extent reprovisioning requirement.
5545 *
5546 * Returns:
5547 * -Error: error indicates problem.
5548 * 1: Extent count or size has changed.
5549 * 0: No changes.
5550 **/
5551static int
5552lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5553{
5554 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5555 uint16_t size_diff, rsrc_ext_size;
5556 int rc = 0;
5557 struct lpfc_rsrc_blks *rsrc_entry;
5558 struct list_head *rsrc_blk_list = NULL;
5559
5560 size_diff = 0;
5561 curr_ext_cnt = 0;
5562 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5563 &rsrc_ext_cnt,
5564 &rsrc_ext_size);
5565 if (unlikely(rc))
5566 return -EIO;
5567
5568 switch (type) {
5569 case LPFC_RSC_TYPE_FCOE_RPI:
5570 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5571 break;
5572 case LPFC_RSC_TYPE_FCOE_VPI:
5573 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5574 break;
5575 case LPFC_RSC_TYPE_FCOE_XRI:
5576 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5577 break;
5578 case LPFC_RSC_TYPE_FCOE_VFI:
5579 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5580 break;
5581 default:
5582 break;
5583 }
5584
5585 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5586 curr_ext_cnt++;
5587 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5588 size_diff++;
5589 }
5590
5591 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5592 rc = 1;
5593
5594 return rc;
5595}
5596
5597/**
5598 * lpfc_sli4_cfg_post_extnts -
5599 * @phba: Pointer to HBA context object.
5600 * @extnt_cnt - number of available extents.
5601 * @type - the extent type (rpi, xri, vfi, vpi).
5602 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5603 * @mbox - pointer to the caller's allocated mailbox structure.
5604 *
5605 * This function executes the extents allocation request. It also
5606 * takes care of the amount of memory needed to allocate or get the
5607 * allocated extents. It is the caller's responsibility to evaluate
5608 * the response.
5609 *
5610 * Returns:
5611 * -Error: Error value describes the condition found.
5612 * 0: if successful
5613 **/
5614static int
James Smart8a9d2e82012-05-09 21:16:12 -04005615lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005616 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5617{
5618 int rc = 0;
5619 uint32_t req_len;
5620 uint32_t emb_len;
5621 uint32_t alloc_len, mbox_tmo;
5622
5623 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005624 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005625
5626 /*
5627 * Calculate the size of an embedded mailbox. The uint32_t
5628 * accounts for extents-specific word.
5629 */
5630 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5631 sizeof(uint32_t);
5632
5633 /*
5634 * Presume the allocation and response will fit into an embedded
5635 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5636 */
5637 *emb = LPFC_SLI4_MBX_EMBED;
5638 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005639 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005640 sizeof(union lpfc_sli4_cfg_shdr) +
5641 sizeof(uint32_t);
5642 *emb = LPFC_SLI4_MBX_NEMBED;
5643 }
5644
5645 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5646 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5647 req_len, *emb);
5648 if (alloc_len < req_len) {
5649 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005650 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005651 "less than the requested DMA memory "
5652 "size (x%x)\n", alloc_len, req_len);
5653 return -ENOMEM;
5654 }
James Smart8a9d2e82012-05-09 21:16:12 -04005655 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005656 if (unlikely(rc))
5657 return -EIO;
5658
5659 if (!phba->sli4_hba.intr_enable)
5660 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5661 else {
James Smarta183a152011-10-10 21:32:43 -04005662 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005663 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5664 }
5665
5666 if (unlikely(rc))
5667 rc = -EIO;
5668 return rc;
5669}
5670
5671/**
5672 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5673 * @phba: Pointer to HBA context object.
5674 * @type: The resource extent type to allocate.
5675 *
5676 * This function allocates the number of elements for the specified
5677 * resource type.
5678 **/
5679static int
5680lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5681{
5682 bool emb = false;
5683 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5684 uint16_t rsrc_id, rsrc_start, j, k;
5685 uint16_t *ids;
5686 int i, rc;
5687 unsigned long longs;
5688 unsigned long *bmask;
5689 struct lpfc_rsrc_blks *rsrc_blks;
5690 LPFC_MBOXQ_t *mbox;
5691 uint32_t length;
5692 struct lpfc_id_range *id_array = NULL;
5693 void *virtaddr = NULL;
5694 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5695 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5696 struct list_head *ext_blk_list;
5697
5698 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5699 &rsrc_cnt,
5700 &rsrc_size);
5701 if (unlikely(rc))
5702 return -EIO;
5703
5704 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5705 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5706 "3009 No available Resource Extents "
5707 "for resource type 0x%x: Count: 0x%x, "
5708 "Size 0x%x\n", type, rsrc_cnt,
5709 rsrc_size);
5710 return -ENOMEM;
5711 }
5712
James Smart8a9d2e82012-05-09 21:16:12 -04005713 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5714 "2903 Post resource extents type-0x%x: "
5715 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005716
5717 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5718 if (!mbox)
5719 return -ENOMEM;
5720
James Smart8a9d2e82012-05-09 21:16:12 -04005721 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005722 if (unlikely(rc)) {
5723 rc = -EIO;
5724 goto err_exit;
5725 }
5726
5727 /*
5728 * Figure out where the response is located. Then get local pointers
5729 * to the response data. The port does not guarantee to respond to
5730 * all extents counts request so update the local variable with the
5731 * allocated count from the port.
5732 */
5733 if (emb == LPFC_SLI4_MBX_EMBED) {
5734 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5735 id_array = &rsrc_ext->u.rsp.id[0];
5736 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5737 } else {
5738 virtaddr = mbox->sge_array->addr[0];
5739 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5740 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5741 id_array = &n_rsrc->id;
5742 }
5743
5744 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5745 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5746
5747 /*
5748 * Based on the resource size and count, correct the base and max
5749 * resource values.
5750 */
5751 length = sizeof(struct lpfc_rsrc_blks);
5752 switch (type) {
5753 case LPFC_RSC_TYPE_FCOE_RPI:
5754 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5755 sizeof(unsigned long),
5756 GFP_KERNEL);
5757 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5758 rc = -ENOMEM;
5759 goto err_exit;
5760 }
5761 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5762 sizeof(uint16_t),
5763 GFP_KERNEL);
5764 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5765 kfree(phba->sli4_hba.rpi_bmask);
5766 rc = -ENOMEM;
5767 goto err_exit;
5768 }
5769
5770 /*
5771 * The next_rpi was initialized with the maximum available
5772 * count but the port may allocate a smaller number. Catch
5773 * that case and update the next_rpi.
5774 */
5775 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5776
5777 /* Initialize local ptrs for common extent processing later. */
5778 bmask = phba->sli4_hba.rpi_bmask;
5779 ids = phba->sli4_hba.rpi_ids;
5780 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5781 break;
5782 case LPFC_RSC_TYPE_FCOE_VPI:
5783 phba->vpi_bmask = kzalloc(longs *
5784 sizeof(unsigned long),
5785 GFP_KERNEL);
5786 if (unlikely(!phba->vpi_bmask)) {
5787 rc = -ENOMEM;
5788 goto err_exit;
5789 }
5790 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5791 sizeof(uint16_t),
5792 GFP_KERNEL);
5793 if (unlikely(!phba->vpi_ids)) {
5794 kfree(phba->vpi_bmask);
5795 rc = -ENOMEM;
5796 goto err_exit;
5797 }
5798
5799 /* Initialize local ptrs for common extent processing later. */
5800 bmask = phba->vpi_bmask;
5801 ids = phba->vpi_ids;
5802 ext_blk_list = &phba->lpfc_vpi_blk_list;
5803 break;
5804 case LPFC_RSC_TYPE_FCOE_XRI:
5805 phba->sli4_hba.xri_bmask = kzalloc(longs *
5806 sizeof(unsigned long),
5807 GFP_KERNEL);
5808 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5809 rc = -ENOMEM;
5810 goto err_exit;
5811 }
James Smart8a9d2e82012-05-09 21:16:12 -04005812 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005813 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5814 sizeof(uint16_t),
5815 GFP_KERNEL);
5816 if (unlikely(!phba->sli4_hba.xri_ids)) {
5817 kfree(phba->sli4_hba.xri_bmask);
5818 rc = -ENOMEM;
5819 goto err_exit;
5820 }
5821
5822 /* Initialize local ptrs for common extent processing later. */
5823 bmask = phba->sli4_hba.xri_bmask;
5824 ids = phba->sli4_hba.xri_ids;
5825 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5826 break;
5827 case LPFC_RSC_TYPE_FCOE_VFI:
5828 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5829 sizeof(unsigned long),
5830 GFP_KERNEL);
5831 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5832 rc = -ENOMEM;
5833 goto err_exit;
5834 }
5835 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5836 sizeof(uint16_t),
5837 GFP_KERNEL);
5838 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5839 kfree(phba->sli4_hba.vfi_bmask);
5840 rc = -ENOMEM;
5841 goto err_exit;
5842 }
5843
5844 /* Initialize local ptrs for common extent processing later. */
5845 bmask = phba->sli4_hba.vfi_bmask;
5846 ids = phba->sli4_hba.vfi_ids;
5847 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5848 break;
5849 default:
5850 /* Unsupported Opcode. Fail call. */
5851 id_array = NULL;
5852 bmask = NULL;
5853 ids = NULL;
5854 ext_blk_list = NULL;
5855 goto err_exit;
5856 }
5857
5858 /*
5859 * Complete initializing the extent configuration with the
5860 * allocated ids assigned to this function. The bitmask serves
5861 * as an index into the array and manages the available ids. The
5862 * array just stores the ids communicated to the port via the wqes.
5863 */
5864 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5865 if ((i % 2) == 0)
5866 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5867 &id_array[k]);
5868 else
5869 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5870 &id_array[k]);
5871
5872 rsrc_blks = kzalloc(length, GFP_KERNEL);
5873 if (unlikely(!rsrc_blks)) {
5874 rc = -ENOMEM;
5875 kfree(bmask);
5876 kfree(ids);
5877 goto err_exit;
5878 }
5879 rsrc_blks->rsrc_start = rsrc_id;
5880 rsrc_blks->rsrc_size = rsrc_size;
5881 list_add_tail(&rsrc_blks->list, ext_blk_list);
5882 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005883 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005884 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005885 lpfc_sli4_get_iocb_cnt(phba);
5886 phba->sli4_hba.nvme_xri_start =
5887 phba->sli4_hba.scsi_xri_start +
5888 phba->sli4_hba.scsi_xri_max;
5889 }
James Smart6d368e52011-05-24 11:44:12 -04005890
5891 while (rsrc_id < (rsrc_start + rsrc_size)) {
5892 ids[j] = rsrc_id;
5893 rsrc_id++;
5894 j++;
5895 }
5896 /* Entire word processed. Get next word.*/
5897 if ((i % 2) == 1)
5898 k++;
5899 }
5900 err_exit:
5901 lpfc_sli4_mbox_cmd_free(phba, mbox);
5902 return rc;
5903}
5904
James Smart895427b2017-02-12 13:52:30 -08005905
5906
James Smart6d368e52011-05-24 11:44:12 -04005907/**
5908 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5909 * @phba: Pointer to HBA context object.
5910 * @type: the extent's type.
5911 *
5912 * This function deallocates all extents of a particular resource type.
5913 * SLI4 does not allow for deallocating a particular extent range. It
5914 * is the caller's responsibility to release all kernel memory resources.
5915 **/
5916static int
5917lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5918{
5919 int rc;
5920 uint32_t length, mbox_tmo = 0;
5921 LPFC_MBOXQ_t *mbox;
5922 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5923 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5924
5925 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5926 if (!mbox)
5927 return -ENOMEM;
5928
5929 /*
5930 * This function sends an embedded mailbox because it only sends the
5931 * the resource type. All extents of this type are released by the
5932 * port.
5933 */
5934 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5935 sizeof(struct lpfc_sli4_cfg_mhdr));
5936 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5937 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5938 length, LPFC_SLI4_MBX_EMBED);
5939
5940 /* Send an extents count of 0 - the dealloc doesn't use it. */
5941 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5942 LPFC_SLI4_MBX_EMBED);
5943 if (unlikely(rc)) {
5944 rc = -EIO;
5945 goto out_free_mbox;
5946 }
5947 if (!phba->sli4_hba.intr_enable)
5948 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5949 else {
James Smarta183a152011-10-10 21:32:43 -04005950 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005951 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5952 }
5953 if (unlikely(rc)) {
5954 rc = -EIO;
5955 goto out_free_mbox;
5956 }
5957
5958 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5959 if (bf_get(lpfc_mbox_hdr_status,
5960 &dealloc_rsrc->header.cfg_shdr.response)) {
5961 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5962 "2919 Failed to release resource extents "
5963 "for type %d - Status 0x%x Add'l Status 0x%x. "
5964 "Resource memory not released.\n",
5965 type,
5966 bf_get(lpfc_mbox_hdr_status,
5967 &dealloc_rsrc->header.cfg_shdr.response),
5968 bf_get(lpfc_mbox_hdr_add_status,
5969 &dealloc_rsrc->header.cfg_shdr.response));
5970 rc = -EIO;
5971 goto out_free_mbox;
5972 }
5973
5974 /* Release kernel memory resources for the specific type. */
5975 switch (type) {
5976 case LPFC_RSC_TYPE_FCOE_VPI:
5977 kfree(phba->vpi_bmask);
5978 kfree(phba->vpi_ids);
5979 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5980 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5981 &phba->lpfc_vpi_blk_list, list) {
5982 list_del_init(&rsrc_blk->list);
5983 kfree(rsrc_blk);
5984 }
James Smart16a3a202013-04-17 20:14:38 -04005985 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005986 break;
5987 case LPFC_RSC_TYPE_FCOE_XRI:
5988 kfree(phba->sli4_hba.xri_bmask);
5989 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005990 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5991 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5992 list_del_init(&rsrc_blk->list);
5993 kfree(rsrc_blk);
5994 }
5995 break;
5996 case LPFC_RSC_TYPE_FCOE_VFI:
5997 kfree(phba->sli4_hba.vfi_bmask);
5998 kfree(phba->sli4_hba.vfi_ids);
5999 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6000 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6001 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
6002 list_del_init(&rsrc_blk->list);
6003 kfree(rsrc_blk);
6004 }
6005 break;
6006 case LPFC_RSC_TYPE_FCOE_RPI:
6007 /* RPI bitmask and physical id array are cleaned up earlier. */
6008 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
6009 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
6010 list_del_init(&rsrc_blk->list);
6011 kfree(rsrc_blk);
6012 }
6013 break;
6014 default:
6015 break;
6016 }
6017
6018 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6019
6020 out_free_mbox:
6021 mempool_free(mbox, phba->mbox_mem_pool);
6022 return rc;
6023}
6024
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08006025static void
James Smart7bdedb32016-07-06 12:36:00 -07006026lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
6027 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07006028{
James Smart65791f12016-07-06 12:35:56 -07006029 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07006030
James Smart65791f12016-07-06 12:35:56 -07006031 len = sizeof(struct lpfc_mbx_set_feature) -
6032 sizeof(struct lpfc_sli4_cfg_mhdr);
6033 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6034 LPFC_MBOX_OPCODE_SET_FEATURES, len,
6035 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07006036
James Smart7bdedb32016-07-06 12:36:00 -07006037 switch (feature) {
6038 case LPFC_SET_UE_RECOVERY:
6039 bf_set(lpfc_mbx_set_feature_UER,
6040 &mbox->u.mqe.un.set_feature, 1);
6041 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
6042 mbox->u.mqe.un.set_feature.param_len = 8;
6043 break;
6044 case LPFC_SET_MDS_DIAGS:
6045 bf_set(lpfc_mbx_set_feature_mds,
6046 &mbox->u.mqe.un.set_feature, 1);
6047 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07006048 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07006049 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
6050 mbox->u.mqe.un.set_feature.param_len = 8;
6051 break;
James Smart65791f12016-07-06 12:35:56 -07006052 }
James Smart7bdedb32016-07-06 12:36:00 -07006053
6054 return;
James Smart65791f12016-07-06 12:35:56 -07006055}
6056
James Smart6d368e52011-05-24 11:44:12 -04006057/**
6058 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
6059 * @phba: Pointer to HBA context object.
6060 *
6061 * This function allocates all SLI4 resource identifiers.
6062 **/
6063int
6064lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
6065{
6066 int i, rc, error = 0;
6067 uint16_t count, base;
6068 unsigned long longs;
6069
James Smartff78d8f2011-12-13 13:21:35 -05006070 if (!phba->sli4_hba.rpi_hdrs_in_use)
6071 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04006072 if (phba->sli4_hba.extents_in_use) {
6073 /*
6074 * The port supports resource extents. The XRI, VPI, VFI, RPI
6075 * resource extent count must be read and allocated before
6076 * provisioning the resource id arrays.
6077 */
6078 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
6079 LPFC_IDX_RSRC_RDY) {
6080 /*
6081 * Extent-based resources are set - the driver could
6082 * be in a port reset. Figure out if any corrective
6083 * actions need to be taken.
6084 */
6085 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6086 LPFC_RSC_TYPE_FCOE_VFI);
6087 if (rc != 0)
6088 error++;
6089 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6090 LPFC_RSC_TYPE_FCOE_VPI);
6091 if (rc != 0)
6092 error++;
6093 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6094 LPFC_RSC_TYPE_FCOE_XRI);
6095 if (rc != 0)
6096 error++;
6097 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
6098 LPFC_RSC_TYPE_FCOE_RPI);
6099 if (rc != 0)
6100 error++;
6101
6102 /*
6103 * It's possible that the number of resources
6104 * provided to this port instance changed between
6105 * resets. Detect this condition and reallocate
6106 * resources. Otherwise, there is no action.
6107 */
6108 if (error) {
6109 lpfc_printf_log(phba, KERN_INFO,
6110 LOG_MBOX | LOG_INIT,
6111 "2931 Detected extent resource "
6112 "change. Reallocating all "
6113 "extents.\n");
6114 rc = lpfc_sli4_dealloc_extent(phba,
6115 LPFC_RSC_TYPE_FCOE_VFI);
6116 rc = lpfc_sli4_dealloc_extent(phba,
6117 LPFC_RSC_TYPE_FCOE_VPI);
6118 rc = lpfc_sli4_dealloc_extent(phba,
6119 LPFC_RSC_TYPE_FCOE_XRI);
6120 rc = lpfc_sli4_dealloc_extent(phba,
6121 LPFC_RSC_TYPE_FCOE_RPI);
6122 } else
6123 return 0;
6124 }
6125
6126 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6127 if (unlikely(rc))
6128 goto err_exit;
6129
6130 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6131 if (unlikely(rc))
6132 goto err_exit;
6133
6134 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6135 if (unlikely(rc))
6136 goto err_exit;
6137
6138 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6139 if (unlikely(rc))
6140 goto err_exit;
6141 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6142 LPFC_IDX_RSRC_RDY);
6143 return rc;
6144 } else {
6145 /*
6146 * The port does not support resource extents. The XRI, VPI,
6147 * VFI, RPI resource ids were determined from READ_CONFIG.
6148 * Just allocate the bitmasks and provision the resource id
6149 * arrays. If a port reset is active, the resources don't
6150 * need any action - just exit.
6151 */
6152 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006153 LPFC_IDX_RSRC_RDY) {
6154 lpfc_sli4_dealloc_resource_identifiers(phba);
6155 lpfc_sli4_remove_rpis(phba);
6156 }
James Smart6d368e52011-05-24 11:44:12 -04006157 /* RPIs. */
6158 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006159 if (count <= 0) {
6160 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6161 "3279 Invalid provisioning of "
6162 "rpi:%d\n", count);
6163 rc = -EINVAL;
6164 goto err_exit;
6165 }
James Smart6d368e52011-05-24 11:44:12 -04006166 base = phba->sli4_hba.max_cfg_param.rpi_base;
6167 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6168 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6169 sizeof(unsigned long),
6170 GFP_KERNEL);
6171 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6172 rc = -ENOMEM;
6173 goto err_exit;
6174 }
6175 phba->sli4_hba.rpi_ids = kzalloc(count *
6176 sizeof(uint16_t),
6177 GFP_KERNEL);
6178 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6179 rc = -ENOMEM;
6180 goto free_rpi_bmask;
6181 }
6182
6183 for (i = 0; i < count; i++)
6184 phba->sli4_hba.rpi_ids[i] = base + i;
6185
6186 /* VPIs. */
6187 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006188 if (count <= 0) {
6189 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6190 "3280 Invalid provisioning of "
6191 "vpi:%d\n", count);
6192 rc = -EINVAL;
6193 goto free_rpi_ids;
6194 }
James Smart6d368e52011-05-24 11:44:12 -04006195 base = phba->sli4_hba.max_cfg_param.vpi_base;
6196 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6197 phba->vpi_bmask = kzalloc(longs *
6198 sizeof(unsigned long),
6199 GFP_KERNEL);
6200 if (unlikely(!phba->vpi_bmask)) {
6201 rc = -ENOMEM;
6202 goto free_rpi_ids;
6203 }
6204 phba->vpi_ids = kzalloc(count *
6205 sizeof(uint16_t),
6206 GFP_KERNEL);
6207 if (unlikely(!phba->vpi_ids)) {
6208 rc = -ENOMEM;
6209 goto free_vpi_bmask;
6210 }
6211
6212 for (i = 0; i < count; i++)
6213 phba->vpi_ids[i] = base + i;
6214
6215 /* XRIs. */
6216 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006217 if (count <= 0) {
6218 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6219 "3281 Invalid provisioning of "
6220 "xri:%d\n", count);
6221 rc = -EINVAL;
6222 goto free_vpi_ids;
6223 }
James Smart6d368e52011-05-24 11:44:12 -04006224 base = phba->sli4_hba.max_cfg_param.xri_base;
6225 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6226 phba->sli4_hba.xri_bmask = kzalloc(longs *
6227 sizeof(unsigned long),
6228 GFP_KERNEL);
6229 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6230 rc = -ENOMEM;
6231 goto free_vpi_ids;
6232 }
James Smart41899be2012-03-01 22:34:19 -05006233 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006234 phba->sli4_hba.xri_ids = kzalloc(count *
6235 sizeof(uint16_t),
6236 GFP_KERNEL);
6237 if (unlikely(!phba->sli4_hba.xri_ids)) {
6238 rc = -ENOMEM;
6239 goto free_xri_bmask;
6240 }
6241
6242 for (i = 0; i < count; i++)
6243 phba->sli4_hba.xri_ids[i] = base + i;
6244
6245 /* VFIs. */
6246 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006247 if (count <= 0) {
6248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6249 "3282 Invalid provisioning of "
6250 "vfi:%d\n", count);
6251 rc = -EINVAL;
6252 goto free_xri_ids;
6253 }
James Smart6d368e52011-05-24 11:44:12 -04006254 base = phba->sli4_hba.max_cfg_param.vfi_base;
6255 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6256 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6257 sizeof(unsigned long),
6258 GFP_KERNEL);
6259 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6260 rc = -ENOMEM;
6261 goto free_xri_ids;
6262 }
6263 phba->sli4_hba.vfi_ids = kzalloc(count *
6264 sizeof(uint16_t),
6265 GFP_KERNEL);
6266 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6267 rc = -ENOMEM;
6268 goto free_vfi_bmask;
6269 }
6270
6271 for (i = 0; i < count; i++)
6272 phba->sli4_hba.vfi_ids[i] = base + i;
6273
6274 /*
6275 * Mark all resources ready. An HBA reset doesn't need
6276 * to reset the initialization.
6277 */
6278 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6279 LPFC_IDX_RSRC_RDY);
6280 return 0;
6281 }
6282
6283 free_vfi_bmask:
6284 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006285 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006286 free_xri_ids:
6287 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006288 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006289 free_xri_bmask:
6290 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006291 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006292 free_vpi_ids:
6293 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006294 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006295 free_vpi_bmask:
6296 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006297 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006298 free_rpi_ids:
6299 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006300 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006301 free_rpi_bmask:
6302 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006303 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006304 err_exit:
6305 return rc;
6306}
6307
6308/**
6309 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6310 * @phba: Pointer to HBA context object.
6311 *
6312 * This function allocates the number of elements for the specified
6313 * resource type.
6314 **/
6315int
6316lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6317{
6318 if (phba->sli4_hba.extents_in_use) {
6319 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6320 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6321 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6322 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6323 } else {
6324 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006325 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006326 kfree(phba->vpi_ids);
6327 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6328 kfree(phba->sli4_hba.xri_bmask);
6329 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006330 kfree(phba->sli4_hba.vfi_bmask);
6331 kfree(phba->sli4_hba.vfi_ids);
6332 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6333 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6334 }
6335
6336 return 0;
6337}
6338
6339/**
James Smartb76f2dc2011-07-22 18:37:42 -04006340 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6341 * @phba: Pointer to HBA context object.
6342 * @type: The resource extent type.
6343 * @extnt_count: buffer to hold port extent count response
6344 * @extnt_size: buffer to hold port extent size response.
6345 *
6346 * This function calls the port to read the host allocated extents
6347 * for a particular type.
6348 **/
6349int
6350lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6351 uint16_t *extnt_cnt, uint16_t *extnt_size)
6352{
6353 bool emb;
6354 int rc = 0;
6355 uint16_t curr_blks = 0;
6356 uint32_t req_len, emb_len;
6357 uint32_t alloc_len, mbox_tmo;
6358 struct list_head *blk_list_head;
6359 struct lpfc_rsrc_blks *rsrc_blk;
6360 LPFC_MBOXQ_t *mbox;
6361 void *virtaddr = NULL;
6362 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6363 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6364 union lpfc_sli4_cfg_shdr *shdr;
6365
6366 switch (type) {
6367 case LPFC_RSC_TYPE_FCOE_VPI:
6368 blk_list_head = &phba->lpfc_vpi_blk_list;
6369 break;
6370 case LPFC_RSC_TYPE_FCOE_XRI:
6371 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6372 break;
6373 case LPFC_RSC_TYPE_FCOE_VFI:
6374 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6375 break;
6376 case LPFC_RSC_TYPE_FCOE_RPI:
6377 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6378 break;
6379 default:
6380 return -EIO;
6381 }
6382
6383 /* Count the number of extents currently allocatd for this type. */
6384 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6385 if (curr_blks == 0) {
6386 /*
6387 * The GET_ALLOCATED mailbox does not return the size,
6388 * just the count. The size should be just the size
6389 * stored in the current allocated block and all sizes
6390 * for an extent type are the same so set the return
6391 * value now.
6392 */
6393 *extnt_size = rsrc_blk->rsrc_size;
6394 }
6395 curr_blks++;
6396 }
6397
James Smartb76f2dc2011-07-22 18:37:42 -04006398 /*
6399 * Calculate the size of an embedded mailbox. The uint32_t
6400 * accounts for extents-specific word.
6401 */
6402 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6403 sizeof(uint32_t);
6404
6405 /*
6406 * Presume the allocation and response will fit into an embedded
6407 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6408 */
6409 emb = LPFC_SLI4_MBX_EMBED;
6410 req_len = emb_len;
6411 if (req_len > emb_len) {
6412 req_len = curr_blks * sizeof(uint16_t) +
6413 sizeof(union lpfc_sli4_cfg_shdr) +
6414 sizeof(uint32_t);
6415 emb = LPFC_SLI4_MBX_NEMBED;
6416 }
6417
6418 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6419 if (!mbox)
6420 return -ENOMEM;
6421 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6422
6423 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6424 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6425 req_len, emb);
6426 if (alloc_len < req_len) {
6427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6428 "2983 Allocated DMA memory size (x%x) is "
6429 "less than the requested DMA memory "
6430 "size (x%x)\n", alloc_len, req_len);
6431 rc = -ENOMEM;
6432 goto err_exit;
6433 }
6434 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6435 if (unlikely(rc)) {
6436 rc = -EIO;
6437 goto err_exit;
6438 }
6439
6440 if (!phba->sli4_hba.intr_enable)
6441 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6442 else {
James Smarta183a152011-10-10 21:32:43 -04006443 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006444 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6445 }
6446
6447 if (unlikely(rc)) {
6448 rc = -EIO;
6449 goto err_exit;
6450 }
6451
6452 /*
6453 * Figure out where the response is located. Then get local pointers
6454 * to the response data. The port does not guarantee to respond to
6455 * all extents counts request so update the local variable with the
6456 * allocated count from the port.
6457 */
6458 if (emb == LPFC_SLI4_MBX_EMBED) {
6459 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6460 shdr = &rsrc_ext->header.cfg_shdr;
6461 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6462 } else {
6463 virtaddr = mbox->sge_array->addr[0];
6464 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6465 shdr = &n_rsrc->cfg_shdr;
6466 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6467 }
6468
6469 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6470 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6471 "2984 Failed to read allocated resources "
6472 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6473 type,
6474 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6475 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6476 rc = -EIO;
6477 goto err_exit;
6478 }
6479 err_exit:
6480 lpfc_sli4_mbox_cmd_free(phba, mbox);
6481 return rc;
6482}
6483
6484/**
James Smart0ef69962017-04-21 16:04:50 -07006485 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006486 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006487 * @pring: Pointer to driver SLI ring object.
6488 * @sgl_list: linked link of sgl buffers to post
6489 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006490 *
James Smart895427b2017-02-12 13:52:30 -08006491 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006492 * repost them to the port by using SGL block post. This is needed after a
6493 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006494 * of buffer sgls which contains contiguous xris and uses the non-embedded
6495 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006496 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6497 * mailbox command for posting.
6498 *
6499 * Returns: 0 = success, non-zero failure.
6500 **/
6501static int
James Smart895427b2017-02-12 13:52:30 -08006502lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6503 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006504{
6505 struct lpfc_sglq *sglq_entry = NULL;
6506 struct lpfc_sglq *sglq_entry_next = NULL;
6507 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006508 int status, total_cnt;
6509 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006510 int last_xritag = NO_XRI;
6511 LIST_HEAD(prep_sgl_list);
6512 LIST_HEAD(blck_sgl_list);
6513 LIST_HEAD(allc_sgl_list);
6514 LIST_HEAD(post_sgl_list);
6515 LIST_HEAD(free_sgl_list);
6516
James Smart38c20672013-03-01 16:37:44 -05006517 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006518 spin_lock(&phba->sli4_hba.sgl_list_lock);
6519 list_splice_init(sgl_list, &allc_sgl_list);
6520 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006521 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006522
James Smart895427b2017-02-12 13:52:30 -08006523 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006524 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6525 &allc_sgl_list, list) {
6526 list_del_init(&sglq_entry->list);
6527 block_cnt++;
6528 if ((last_xritag != NO_XRI) &&
6529 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6530 /* a hole in xri block, form a sgl posting block */
6531 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6532 post_cnt = block_cnt - 1;
6533 /* prepare list for next posting block */
6534 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6535 block_cnt = 1;
6536 } else {
6537 /* prepare list for next posting block */
6538 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6539 /* enough sgls for non-embed sgl mbox command */
6540 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6541 list_splice_init(&prep_sgl_list,
6542 &blck_sgl_list);
6543 post_cnt = block_cnt;
6544 block_cnt = 0;
6545 }
6546 }
6547 num_posted++;
6548
6549 /* keep track of last sgl's xritag */
6550 last_xritag = sglq_entry->sli4_xritag;
6551
James Smart895427b2017-02-12 13:52:30 -08006552 /* end of repost sgl list condition for buffers */
6553 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006554 if (post_cnt == 0) {
6555 list_splice_init(&prep_sgl_list,
6556 &blck_sgl_list);
6557 post_cnt = block_cnt;
6558 } else if (block_cnt == 1) {
6559 status = lpfc_sli4_post_sgl(phba,
6560 sglq_entry->phys, 0,
6561 sglq_entry->sli4_xritag);
6562 if (!status) {
6563 /* successful, put sgl to posted list */
6564 list_add_tail(&sglq_entry->list,
6565 &post_sgl_list);
6566 } else {
6567 /* Failure, put sgl to free list */
6568 lpfc_printf_log(phba, KERN_WARNING,
6569 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006570 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006571 "sgl, xritag:x%x\n",
6572 sglq_entry->sli4_xritag);
6573 list_add_tail(&sglq_entry->list,
6574 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006575 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006576 }
6577 }
6578 }
6579
6580 /* continue until a nembed page worth of sgls */
6581 if (post_cnt == 0)
6582 continue;
6583
James Smart895427b2017-02-12 13:52:30 -08006584 /* post the buffer list sgls as a block */
6585 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6586 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006587
6588 if (!status) {
6589 /* success, put sgl list to posted sgl list */
6590 list_splice_init(&blck_sgl_list, &post_sgl_list);
6591 } else {
6592 /* Failure, put sgl list to free sgl list */
6593 sglq_entry_first = list_first_entry(&blck_sgl_list,
6594 struct lpfc_sglq,
6595 list);
6596 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006597 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006598 "xritag:x%x-x%x\n",
6599 sglq_entry_first->sli4_xritag,
6600 (sglq_entry_first->sli4_xritag +
6601 post_cnt - 1));
6602 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006603 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006604 }
6605
6606 /* don't reset xirtag due to hole in xri block */
6607 if (block_cnt == 0)
6608 last_xritag = NO_XRI;
6609
James Smart895427b2017-02-12 13:52:30 -08006610 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006611 post_cnt = 0;
6612 }
6613
James Smart895427b2017-02-12 13:52:30 -08006614 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006615 lpfc_free_sgl_list(phba, &free_sgl_list);
6616
James Smart895427b2017-02-12 13:52:30 -08006617 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006618 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006619 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006620 spin_lock(&phba->sli4_hba.sgl_list_lock);
6621 list_splice_init(&post_sgl_list, sgl_list);
6622 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006623 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006624 } else {
6625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006626 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006627 return -EIO;
6628 }
James Smart895427b2017-02-12 13:52:30 -08006629
6630 /* return the number of XRIs actually posted */
6631 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006632}
6633
James Smart61bda8f2016-10-13 15:06:05 -07006634void
6635lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6636{
6637 uint32_t len;
6638
6639 len = sizeof(struct lpfc_mbx_set_host_data) -
6640 sizeof(struct lpfc_sli4_cfg_mhdr);
6641 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6642 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6643 LPFC_SLI4_MBX_EMBED);
6644
6645 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006646 mbox->u.mqe.un.set_host_data.param_len =
6647 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006648 snprintf(mbox->u.mqe.un.set_host_data.data,
6649 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6650 "Linux %s v"LPFC_DRIVER_VERSION,
6651 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6652}
6653
James Smarta8cf5df2017-05-15 15:20:46 -07006654int
James Smart6c621a22017-05-15 15:20:45 -07006655lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006656 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006657{
6658 int rc, i;
6659 struct lpfc_rqe hrqe;
6660 struct lpfc_rqe drqe;
6661 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08006662 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07006663 struct rqb_dmabuf *rqb_buffer;
6664 LIST_HEAD(rqb_buf_list);
6665
James Smart411de512018-01-30 15:58:52 -08006666 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006667 rqbp = hrq->rqbp;
6668 for (i = 0; i < count; i++) {
6669 /* IF RQ is already full, don't bother */
6670 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6671 break;
6672 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6673 if (!rqb_buffer)
6674 break;
6675 rqb_buffer->hrq = hrq;
6676 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006677 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006678 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6679 }
6680 while (!list_empty(&rqb_buf_list)) {
6681 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6682 hbuf.list);
6683
6684 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6685 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6686 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6687 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6688 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6689 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08006690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6691 "6421 Cannot post to HRQ %d: %x %x %x "
6692 "DRQ %x %x\n",
6693 hrq->queue_id,
6694 hrq->host_index,
6695 hrq->hba_index,
6696 hrq->entry_count,
6697 drq->host_index,
6698 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07006699 rqbp->rqb_free_buffer(phba, rqb_buffer);
6700 } else {
6701 list_add_tail(&rqb_buffer->hbuf.list,
6702 &rqbp->rqb_buffer_list);
6703 rqbp->buffer_count++;
6704 }
6705 }
James Smart411de512018-01-30 15:58:52 -08006706 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006707 return 1;
6708}
6709
James Smart8a9d2e82012-05-09 21:16:12 -04006710/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006711 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006712 * @phba: Pointer to HBA context object.
6713 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006714 * This function is the main SLI4 device initialization PCI function. This
6715 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006716 * HBA error attention handler code. Caller is not required to hold any
6717 * locks.
6718 **/
6719int
6720lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6721{
James Smart6c621a22017-05-15 15:20:45 -07006722 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006723 LPFC_MBOXQ_t *mboxq;
6724 struct lpfc_mqe *mqe;
6725 uint8_t *vpd;
6726 uint32_t vpd_size;
6727 uint32_t ftr_rsp = 0;
6728 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6729 struct lpfc_vport *vport = phba->pport;
6730 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006731 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006732
6733 /* Perform a PCI function reset to start from clean */
6734 rc = lpfc_pci_function_reset(phba);
6735 if (unlikely(rc))
6736 return -ENODEV;
6737
6738 /* Check the HBA Host Status Register for readyness */
6739 rc = lpfc_sli4_post_status_check(phba);
6740 if (unlikely(rc))
6741 return -ENODEV;
6742 else {
6743 spin_lock_irq(&phba->hbalock);
6744 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6745 spin_unlock_irq(&phba->hbalock);
6746 }
6747
6748 /*
6749 * Allocate a single mailbox container for initializing the
6750 * port.
6751 */
6752 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6753 if (!mboxq)
6754 return -ENOMEM;
6755
James Smartda0436e2009-05-22 14:51:39 -04006756 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006757 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006758 vpd = kzalloc(vpd_size, GFP_KERNEL);
6759 if (!vpd) {
6760 rc = -ENOMEM;
6761 goto out_free_mbox;
6762 }
6763
6764 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006765 if (unlikely(rc)) {
6766 kfree(vpd);
6767 goto out_free_mbox;
6768 }
James Smart572709e2013-07-15 18:32:43 -04006769
James Smartda0436e2009-05-22 14:51:39 -04006770 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006771 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006772 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006773 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006774 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6775 } else {
James Smart76a95d72010-11-20 23:11:48 -05006776 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006777 }
James Smart45ed1192009-10-02 15:17:02 -04006778
6779 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6780 LPFC_DCBX_CEE_MODE)
6781 phba->hba_flag |= HBA_FIP_SUPPORT;
6782 else
6783 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6784
James Smart4f2e66c2012-05-09 21:17:07 -04006785 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6786
James Smartc31098c2011-04-16 11:03:33 -04006787 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006788 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6789 "0376 READ_REV Error. SLI Level %d "
6790 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006791 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006792 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006793 kfree(vpd);
6794 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006795 }
James Smartcd1c8302011-10-10 21:33:25 -04006796
6797 /*
James Smartff78d8f2011-12-13 13:21:35 -05006798 * Continue initialization with default values even if driver failed
6799 * to read FCoE param config regions, only read parameters if the
6800 * board is FCoE
6801 */
6802 if (phba->hba_flag & HBA_FCOE_MODE &&
6803 lpfc_sli4_read_fcoe_params(phba))
6804 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6805 "2570 Failed to read FCoE parameters\n");
6806
6807 /*
James Smartcd1c8302011-10-10 21:33:25 -04006808 * Retrieve sli4 device physical port name, failure of doing it
6809 * is considered as non-fatal.
6810 */
6811 rc = lpfc_sli4_retrieve_pport_name(phba);
6812 if (!rc)
6813 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6814 "3080 Successful retrieving SLI4 device "
6815 "physical port name: %s.\n", phba->Port);
6816
James Smartda0436e2009-05-22 14:51:39 -04006817 /*
6818 * Evaluate the read rev and vpd data. Populate the driver
6819 * state with the results. If this routine fails, the failure
6820 * is not fatal as the driver will use generic values.
6821 */
6822 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6823 if (unlikely(!rc)) {
6824 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6825 "0377 Error %d parsing vpd. "
6826 "Using defaults.\n", rc);
6827 rc = 0;
6828 }
James Smart76a95d72010-11-20 23:11:48 -05006829 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006830
James Smartf1126682009-06-10 17:22:44 -04006831 /* Save information as VPD data */
6832 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6833 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6834 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6835 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6836 &mqe->un.read_rev);
6837 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6838 &mqe->un.read_rev);
6839 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6840 &mqe->un.read_rev);
6841 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6842 &mqe->un.read_rev);
6843 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6844 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6845 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6846 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6847 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6848 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6849 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6850 "(%d):0380 READ_REV Status x%x "
6851 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6852 mboxq->vport ? mboxq->vport->vpi : 0,
6853 bf_get(lpfc_mqe_status, mqe),
6854 phba->vpd.rev.opFwName,
6855 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6856 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006857
James Smart572709e2013-07-15 18:32:43 -04006858 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6859 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6860 if (phba->pport->cfg_lun_queue_depth > rc) {
6861 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6862 "3362 LUN queue depth changed from %d to %d\n",
6863 phba->pport->cfg_lun_queue_depth, rc);
6864 phba->pport->cfg_lun_queue_depth = rc;
6865 }
6866
James Smart65791f12016-07-06 12:35:56 -07006867 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006868 LPFC_SLI_INTF_IF_TYPE_0) {
6869 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6870 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6871 if (rc == MBX_SUCCESS) {
6872 phba->hba_flag |= HBA_RECOVERABLE_UE;
6873 /* Set 1Sec interval to detect UE */
6874 phba->eratt_poll_interval = 1;
6875 phba->sli4_hba.ue_to_sr = bf_get(
6876 lpfc_mbx_set_feature_UESR,
6877 &mboxq->u.mqe.un.set_feature);
6878 phba->sli4_hba.ue_to_rp = bf_get(
6879 lpfc_mbx_set_feature_UERP,
6880 &mboxq->u.mqe.un.set_feature);
6881 }
6882 }
6883
6884 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6885 /* Enable MDS Diagnostics only if the SLI Port supports it */
6886 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6887 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6888 if (rc != MBX_SUCCESS)
6889 phba->mds_diags_support = 0;
6890 }
James Smart572709e2013-07-15 18:32:43 -04006891
James Smartda0436e2009-05-22 14:51:39 -04006892 /*
6893 * Discover the port's supported feature set and match it against the
6894 * hosts requests.
6895 */
6896 lpfc_request_features(phba, mboxq);
6897 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6898 if (unlikely(rc)) {
6899 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006900 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006901 }
6902
6903 /*
6904 * The port must support FCP initiator mode as this is the
6905 * only mode running in the host.
6906 */
6907 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6908 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6909 "0378 No support for fcpi mode.\n");
6910 ftr_rsp++;
6911 }
James Smartfedd3b72011-02-16 12:39:24 -05006912 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6913 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6914 else
6915 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006916 /*
6917 * If the port cannot support the host's requested features
6918 * then turn off the global config parameters to disable the
6919 * feature in the driver. This is not a fatal error.
6920 */
James Smartbf086112011-08-21 21:48:13 -04006921 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6922 if (phba->cfg_enable_bg) {
6923 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6924 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6925 else
6926 ftr_rsp++;
6927 }
James Smartda0436e2009-05-22 14:51:39 -04006928
6929 if (phba->max_vpi && phba->cfg_enable_npiv &&
6930 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6931 ftr_rsp++;
6932
6933 if (ftr_rsp) {
6934 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6935 "0379 Feature Mismatch Data: x%08x %08x "
6936 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6937 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6938 phba->cfg_enable_npiv, phba->max_vpi);
6939 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6940 phba->cfg_enable_bg = 0;
6941 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6942 phba->cfg_enable_npiv = 0;
6943 }
6944
6945 /* These SLI3 features are assumed in SLI4 */
6946 spin_lock_irq(&phba->hbalock);
6947 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6948 spin_unlock_irq(&phba->hbalock);
6949
James Smart6d368e52011-05-24 11:44:12 -04006950 /*
6951 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6952 * calls depends on these resources to complete port setup.
6953 */
6954 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6955 if (rc) {
6956 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6957 "2920 Failed to alloc Resource IDs "
6958 "rc = x%x\n", rc);
6959 goto out_free_mbox;
6960 }
6961
James Smart61bda8f2016-10-13 15:06:05 -07006962 lpfc_set_host_data(phba, mboxq);
6963
6964 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6965 if (rc) {
6966 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6967 "2134 Failed to set host os driver version %x",
6968 rc);
6969 }
6970
James Smartda0436e2009-05-22 14:51:39 -04006971 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006972 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6973 if (rc) {
6974 phba->link_state = LPFC_HBA_ERROR;
6975 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006976 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006977 }
6978
James Smartda0436e2009-05-22 14:51:39 -04006979 mboxq->vport = vport;
6980 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6981 mp = (struct lpfc_dmabuf *) mboxq->context1;
6982 if (rc == MBX_SUCCESS) {
6983 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6984 rc = 0;
6985 }
6986
6987 /*
6988 * This memory was allocated by the lpfc_read_sparam routine. Release
6989 * it to the mbuf pool.
6990 */
6991 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6992 kfree(mp);
6993 mboxq->context1 = NULL;
6994 if (unlikely(rc)) {
6995 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6996 "0382 READ_SPARAM command failed "
6997 "status %d, mbxStatus x%x\n",
6998 rc, bf_get(lpfc_mqe_status, mqe));
6999 phba->link_state = LPFC_HBA_ERROR;
7000 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05007001 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04007002 }
7003
James Smart05580562011-05-24 11:40:48 -04007004 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04007005
7006 /* Update the fc_host data structures with new wwn. */
7007 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
7008 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
7009
James Smart895427b2017-02-12 13:52:30 -08007010 /* Create all the SLI4 queues */
7011 rc = lpfc_sli4_queue_create(phba);
7012 if (rc) {
7013 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7014 "3089 Failed to allocate queues\n");
7015 rc = -ENODEV;
7016 goto out_free_mbox;
7017 }
7018 /* Set up all the queues to the device */
7019 rc = lpfc_sli4_queue_setup(phba);
7020 if (unlikely(rc)) {
7021 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7022 "0381 Error %d during queue setup.\n ", rc);
7023 goto out_stop_timers;
7024 }
7025 /* Initialize the driver internal SLI layer lists. */
7026 lpfc_sli4_setup(phba);
7027 lpfc_sli4_queue_init(phba);
7028
7029 /* update host els xri-sgl sizes and mappings */
7030 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04007031 if (unlikely(rc)) {
7032 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7033 "1400 Failed to update xri-sgl size and "
7034 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08007035 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007036 }
7037
James Smart8a9d2e82012-05-09 21:16:12 -04007038 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08007039 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
7040 phba->sli4_hba.els_xri_cnt);
7041 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04007042 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7043 "0582 Error %d during els sgl post "
7044 "operation\n", rc);
7045 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007046 goto out_destroy_queue;
7047 }
7048 phba->sli4_hba.els_xri_cnt = rc;
7049
James Smartf358dd02017-02-12 13:52:34 -08007050 if (phba->nvmet_support) {
7051 /* update host nvmet xri-sgl sizes and mappings */
7052 rc = lpfc_sli4_nvmet_sgl_update(phba);
7053 if (unlikely(rc)) {
7054 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7055 "6308 Failed to update nvmet-sgl size "
7056 "and mapping: %d\n", rc);
7057 goto out_destroy_queue;
7058 }
7059
7060 /* register the nvmet sgl pool to the port */
7061 rc = lpfc_sli4_repost_sgl_list(
7062 phba,
7063 &phba->sli4_hba.lpfc_nvmet_sgl_list,
7064 phba->sli4_hba.nvmet_xri_cnt);
7065 if (unlikely(rc < 0)) {
7066 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7067 "3117 Error %d during nvmet "
7068 "sgl post\n", rc);
7069 rc = -ENODEV;
7070 goto out_destroy_queue;
7071 }
7072 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07007073
7074 cnt = phba->cfg_iocb_cnt * 1024;
7075 /* We need 1 iocbq for every SGL, for IO processing */
7076 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08007077 } else {
James Smart895427b2017-02-12 13:52:30 -08007078 /* update host scsi xri-sgl sizes and mappings */
7079 rc = lpfc_sli4_scsi_sgl_update(phba);
7080 if (unlikely(rc)) {
7081 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7082 "6309 Failed to update scsi-sgl size "
7083 "and mapping: %d\n", rc);
7084 goto out_destroy_queue;
7085 }
7086
7087 /* update host nvme xri-sgl sizes and mappings */
7088 rc = lpfc_sli4_nvme_sgl_update(phba);
7089 if (unlikely(rc)) {
7090 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7091 "6082 Failed to update nvme-sgl size "
7092 "and mapping: %d\n", rc);
7093 goto out_destroy_queue;
7094 }
James Smart6c621a22017-05-15 15:20:45 -07007095
7096 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07007097 }
7098
7099 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07007100 /* Initialize and populate the iocb list per host */
7101 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007102 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07007103 phba->cfg_iocb_cnt, cnt);
7104 rc = lpfc_init_iocb_list(phba, cnt);
7105 if (rc) {
7106 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07007107 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07007108 goto out_destroy_queue;
7109 }
James Smart8a9d2e82012-05-09 21:16:12 -04007110 }
7111
James Smart11e644e2017-06-15 22:56:48 -07007112 if (phba->nvmet_support)
7113 lpfc_nvmet_create_targetport(phba);
7114
James Smart2d7dbc42017-02-12 13:52:35 -08007115 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007116 /* Post initial buffers to all RQs created */
7117 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7118 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7119 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7120 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7121 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007122 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007123 rqbp->buffer_count = 0;
7124
James Smart2d7dbc42017-02-12 13:52:35 -08007125 lpfc_post_rq_buffer(
7126 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7127 phba->sli4_hba.nvmet_mrq_data[i],
James Smarta8cf5df2017-05-15 15:20:46 -07007128 LPFC_NVMET_RQE_DEF_COUNT, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007129 }
7130 }
7131
James Smart895427b2017-02-12 13:52:30 -08007132 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7133 /* register the allocated scsi sgl pool to the port */
7134 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7135 if (unlikely(rc)) {
7136 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7137 "0383 Error %d during scsi sgl post "
7138 "operation\n", rc);
7139 /* Some Scsi buffers were moved to abort scsi list */
7140 /* A pci function reset will repost them */
7141 rc = -ENODEV;
7142 goto out_destroy_queue;
7143 }
James Smartda0436e2009-05-22 14:51:39 -04007144 }
7145
James Smart01649562017-02-12 13:52:32 -08007146 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7147 (phba->nvmet_support == 0)) {
7148
7149 /* register the allocated nvme sgl pool to the port */
7150 rc = lpfc_repost_nvme_sgl_list(phba);
7151 if (unlikely(rc)) {
7152 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7153 "6116 Error %d during nvme sgl post "
7154 "operation\n", rc);
7155 /* Some NVME buffers were moved to abort nvme list */
7156 /* A pci function reset will repost them */
7157 rc = -ENODEV;
7158 goto out_destroy_queue;
7159 }
James Smartda0436e2009-05-22 14:51:39 -04007160 }
7161
7162 /* Post the rpi header region to the device. */
7163 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7164 if (unlikely(rc)) {
7165 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7166 "0393 Error %d during rpi post operation\n",
7167 rc);
7168 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007169 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007170 }
James Smart97f2ecf2012-03-01 22:35:23 -05007171 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007172
James Smart895427b2017-02-12 13:52:30 -08007173 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007174 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007175 /*
7176 * The FC Port needs to register FCFI (index 0)
7177 */
7178 lpfc_reg_fcfi(phba, mboxq);
7179 mboxq->vport = phba->pport;
7180 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7181 if (rc != MBX_SUCCESS)
7182 goto out_unset_queue;
7183 rc = 0;
7184 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7185 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007186 } else {
7187 /* We are a NVME Target mode with MRQ > 1 */
7188
7189 /* First register the FCFI */
7190 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7191 mboxq->vport = phba->pport;
7192 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7193 if (rc != MBX_SUCCESS)
7194 goto out_unset_queue;
7195 rc = 0;
7196 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7197 &mboxq->u.mqe.un.reg_fcfi_mrq);
7198
7199 /* Next register the MRQs */
7200 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7201 mboxq->vport = phba->pport;
7202 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7203 if (rc != MBX_SUCCESS)
7204 goto out_unset_queue;
7205 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007206 }
7207 /* Check if the port is configured to be disabled */
7208 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007209 }
7210
7211 /* Arm the CQs and then EQs on device */
7212 lpfc_sli4_arm_cqeq_intr(phba);
7213
7214 /* Indicate device interrupt mode */
7215 phba->sli4_hba.intr_enable = 1;
7216
7217 /* Allow asynchronous mailbox command to go through */
7218 spin_lock_irq(&phba->hbalock);
7219 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7220 spin_unlock_irq(&phba->hbalock);
7221
7222 /* Post receive buffers to the device */
7223 lpfc_sli4_rb_setup(phba);
7224
James Smartfc2b9892010-02-26 14:15:29 -05007225 /* Reset HBA FCF states after HBA reset */
7226 phba->fcf.fcf_flag = 0;
7227 phba->fcf.current_rec.flag = 0;
7228
James Smartda0436e2009-05-22 14:51:39 -04007229 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007230 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007231 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007232
7233 /* Start heart beat timer */
7234 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007235 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007236 phba->hb_outstanding = 0;
7237 phba->last_completion_time = jiffies;
7238
7239 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007240 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007241 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007242
James Smart75baf692010-06-08 18:31:21 -04007243 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7244 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7245 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7246 if (!rc) {
7247 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7248 "2829 This device supports "
7249 "Advanced Error Reporting (AER)\n");
7250 spin_lock_irq(&phba->hbalock);
7251 phba->hba_flag |= HBA_AER_ENABLED;
7252 spin_unlock_irq(&phba->hbalock);
7253 } else {
7254 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7255 "2830 This device does not support "
7256 "Advanced Error Reporting (AER)\n");
7257 phba->cfg_aer_support = 0;
7258 }
James Smart0a96e972011-07-22 18:37:28 -04007259 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007260 }
7261
James Smartda0436e2009-05-22 14:51:39 -04007262 /*
7263 * The port is ready, set the host's link state to LINK_DOWN
7264 * in preparation for link interrupts.
7265 */
James Smartda0436e2009-05-22 14:51:39 -04007266 spin_lock_irq(&phba->hbalock);
7267 phba->link_state = LPFC_LINK_DOWN;
7268 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007269 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7270 (phba->hba_flag & LINK_DISABLED)) {
7271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7272 "3103 Adapter Link is disabled.\n");
7273 lpfc_down_link(phba, mboxq);
7274 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7275 if (rc != MBX_SUCCESS) {
7276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7277 "3104 Adapter failed to issue "
7278 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7279 goto out_unset_queue;
7280 }
7281 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007282 /* don't perform init_link on SLI4 FC port loopback test */
7283 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7284 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7285 if (rc)
7286 goto out_unset_queue;
7287 }
James Smart5350d872011-10-10 21:33:49 -04007288 }
7289 mempool_free(mboxq, phba->mbox_mem_pool);
7290 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007291out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007292 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007293 lpfc_sli4_queue_unset(phba);
7294out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007295 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007296 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007297out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007298 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007299out_free_mbox:
7300 mempool_free(mboxq, phba->mbox_mem_pool);
7301 return rc;
7302}
James Smarte59058c2008-08-24 21:49:00 -04007303
7304/**
James Smart3621a712009-04-06 18:47:14 -04007305 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007306 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007307 *
James Smarte59058c2008-08-24 21:49:00 -04007308 * This is the callback function for mailbox timer. The mailbox
7309 * timer is armed when a new mailbox command is issued and the timer
7310 * is deleted when the mailbox complete. The function is called by
7311 * the kernel timer code when a mailbox does not complete within
7312 * expected time. This function wakes up the worker thread to
7313 * process the mailbox timeout and returns. All the processing is
7314 * done by the worker thread function lpfc_mbox_timeout_handler.
7315 **/
dea31012005-04-17 16:05:31 -05007316void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007317lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007318{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007319 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007320 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007321 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007322
James Smart2e0fef82007-06-17 19:56:36 -05007323 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007324 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007325 if (!tmo_posted)
7326 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7327 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7328
James Smart5e9d9b82008-06-14 22:52:53 -04007329 if (!tmo_posted)
7330 lpfc_worker_wake_up(phba);
7331 return;
dea31012005-04-17 16:05:31 -05007332}
7333
James Smarte8d3c3b2013-10-10 12:21:30 -04007334/**
7335 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7336 * are pending
7337 * @phba: Pointer to HBA context object.
7338 *
7339 * This function checks if any mailbox completions are present on the mailbox
7340 * completion queue.
7341 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007342static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007343lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7344{
7345
7346 uint32_t idx;
7347 struct lpfc_queue *mcq;
7348 struct lpfc_mcqe *mcqe;
7349 bool pending_completions = false;
7350
7351 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7352 return false;
7353
7354 /* Check for completions on mailbox completion queue */
7355
7356 mcq = phba->sli4_hba.mbx_cq;
7357 idx = mcq->hba_index;
7358 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7359 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7360 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7361 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7362 pending_completions = true;
7363 break;
7364 }
7365 idx = (idx + 1) % mcq->entry_count;
7366 if (mcq->hba_index == idx)
7367 break;
7368 }
7369 return pending_completions;
7370
7371}
7372
7373/**
7374 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7375 * that were missed.
7376 * @phba: Pointer to HBA context object.
7377 *
7378 * For sli4, it is possible to miss an interrupt. As such mbox completions
7379 * maybe missed causing erroneous mailbox timeouts to occur. This function
7380 * checks to see if mbox completions are on the mailbox completion queue
7381 * and will process all the completions associated with the eq for the
7382 * mailbox completion queue.
7383 **/
7384bool
7385lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7386{
James Smartb71413d2018-02-22 08:18:40 -08007387 struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
James Smarte8d3c3b2013-10-10 12:21:30 -04007388 uint32_t eqidx;
7389 struct lpfc_queue *fpeq = NULL;
7390 struct lpfc_eqe *eqe;
7391 bool mbox_pending;
7392
7393 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7394 return false;
7395
7396 /* Find the eq associated with the mcq */
7397
James Smartb71413d2018-02-22 08:18:40 -08007398 if (sli4_hba->hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007399 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smartb71413d2018-02-22 08:18:40 -08007400 if (sli4_hba->hba_eq[eqidx]->queue_id ==
7401 sli4_hba->mbx_cq->assoc_qid) {
7402 fpeq = sli4_hba->hba_eq[eqidx];
James Smarte8d3c3b2013-10-10 12:21:30 -04007403 break;
7404 }
7405 if (!fpeq)
7406 return false;
7407
7408 /* Turn off interrupts from this EQ */
7409
James Smartb71413d2018-02-22 08:18:40 -08007410 sli4_hba->sli4_eq_clr_intr(fpeq);
James Smarte8d3c3b2013-10-10 12:21:30 -04007411
7412 /* Check to see if a mbox completion is pending */
7413
7414 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7415
7416 /*
7417 * If a mbox completion is pending, process all the events on EQ
7418 * associated with the mbox completion queue (this could include
7419 * mailbox commands, async events, els commands, receive queue data
7420 * and fcp commands)
7421 */
7422
7423 if (mbox_pending)
7424 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7425 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7426 fpeq->EQ_processed++;
7427 }
7428
7429 /* Always clear and re-arm the EQ */
7430
James Smartb71413d2018-02-22 08:18:40 -08007431 sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smarte8d3c3b2013-10-10 12:21:30 -04007432
7433 return mbox_pending;
7434
7435}
James Smarte59058c2008-08-24 21:49:00 -04007436
7437/**
James Smart3621a712009-04-06 18:47:14 -04007438 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007439 * @phba: Pointer to HBA context object.
7440 *
7441 * This function is called from worker thread when a mailbox command times out.
7442 * The caller is not required to hold any locks. This function will reset the
7443 * HBA and recover all the pending commands.
7444 **/
dea31012005-04-17 16:05:31 -05007445void
7446lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7447{
James Smart2e0fef82007-06-17 19:56:36 -05007448 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007449 MAILBOX_t *mb = NULL;
7450
James Smart1dcb58e2007-04-25 09:51:30 -04007451 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007452
James Smarte8d3c3b2013-10-10 12:21:30 -04007453 /* If the mailbox completed, process the completion and return */
7454 if (lpfc_sli4_process_missed_mbox_completions(phba))
7455 return;
7456
James Smarteb016562014-09-03 12:58:06 -04007457 if (pmbox != NULL)
7458 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007459 /* Check the pmbox pointer first. There is a race condition
7460 * between the mbox timeout handler getting executed in the
7461 * worklist and the mailbox actually completing. When this
7462 * race condition occurs, the mbox_active will be NULL.
7463 */
7464 spin_lock_irq(&phba->hbalock);
7465 if (pmbox == NULL) {
7466 lpfc_printf_log(phba, KERN_WARNING,
7467 LOG_MBOX | LOG_SLI,
7468 "0353 Active Mailbox cleared - mailbox timeout "
7469 "exiting\n");
7470 spin_unlock_irq(&phba->hbalock);
7471 return;
7472 }
7473
dea31012005-04-17 16:05:31 -05007474 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007475 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007476 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007477 mb->mbxCommand,
7478 phba->pport->port_state,
7479 phba->sli.sli_flag,
7480 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007481 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007482
James Smart1dcb58e2007-04-25 09:51:30 -04007483 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7484 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007485 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007486 */
James Smart2e0fef82007-06-17 19:56:36 -05007487 spin_lock_irq(&phba->pport->work_port_lock);
7488 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7489 spin_unlock_irq(&phba->pport->work_port_lock);
7490 spin_lock_irq(&phba->hbalock);
7491 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007492 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007493 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007494
James Smartdb55fba2014-04-04 13:52:02 -04007495 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007496
7497 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007498 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007499
7500 /* Reset the HBA device */
7501 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007502}
7503
James Smarte59058c2008-08-24 21:49:00 -04007504/**
James Smart3772a992009-05-22 14:50:54 -04007505 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007506 * @phba: Pointer to HBA context object.
7507 * @pmbox: Pointer to mailbox object.
7508 * @flag: Flag indicating how the mailbox need to be processed.
7509 *
7510 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007511 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7512 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007513 * The mailbox command can be submitted in polling mode, in which case
7514 * this function will wait in a polling loop for the completion of the
7515 * mailbox.
7516 * If the mailbox is submitted in no_wait mode (not polling) the
7517 * function will submit the command and returns immediately without waiting
7518 * for the mailbox completion. The no_wait is supported only when HBA
7519 * is in SLI2/SLI3 mode - interrupts are enabled.
7520 * The SLI interface allows only one mailbox pending at a time. If the
7521 * mailbox is issued in polling mode and there is already a mailbox
7522 * pending, then the function will return an error. If the mailbox is issued
7523 * in NO_WAIT mode and there is a mailbox pending already, the function
7524 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7525 * The sli layer owns the mailbox object until the completion of mailbox
7526 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7527 * return codes the caller owns the mailbox command after the return of
7528 * the function.
7529 **/
James Smart3772a992009-05-22 14:50:54 -04007530static int
7531lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7532 uint32_t flag)
dea31012005-04-17 16:05:31 -05007533{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007534 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007535 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007536 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007537 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007538 int i;
James Smart09372822008-01-11 01:52:54 -05007539 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007540 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007541 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007542 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007543 int processing_queue = 0;
7544
7545 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7546 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007547 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007548 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007549 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7550 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7551 return MBX_SUCCESS;
7552 }
James Smart58da1ff2008-04-07 10:15:56 -04007553 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007554 pmbox = lpfc_mbox_get(phba);
7555 if (!pmbox) {
7556 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7557 return MBX_SUCCESS;
7558 }
7559 }
dea31012005-04-17 16:05:31 -05007560
James Smarted957682007-06-17 19:56:37 -05007561 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007562 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007563 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007564 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007565 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007566 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007567 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007568 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007569 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007570 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007571 }
7572 }
7573
Linas Vepstas8d63f372007-02-14 14:28:36 -06007574 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007575 if (unlikely(pci_channel_offline(phba->pcidev))) {
7576 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7577 goto out_not_finished;
7578 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007579
James Smarta257bf92009-04-06 18:48:10 -04007580 /* If HBA has a deferred error attention, fail the iocb. */
7581 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7582 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7583 goto out_not_finished;
7584 }
7585
dea31012005-04-17 16:05:31 -05007586 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007587
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007588 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007589 status = MBX_SUCCESS;
7590
James Smart2e0fef82007-06-17 19:56:36 -05007591 if (phba->link_state == LPFC_HBA_ERROR) {
7592 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007593
7594 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007595 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7596 "(%d):0311 Mailbox command x%x cannot "
7597 "issue Data: x%x x%x\n",
7598 pmbox->vport ? pmbox->vport->vpi : 0,
7599 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007600 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007601 }
7602
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007603 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007604 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7605 !(hc_copy & HC_MBINT_ENA)) {
7606 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7607 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007608 "(%d):2528 Mailbox command x%x cannot "
7609 "issue Data: x%x x%x\n",
7610 pmbox->vport ? pmbox->vport->vpi : 0,
7611 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007612 goto out_not_finished;
7613 }
James Smart92908312006-03-07 15:04:13 -05007614 }
7615
dea31012005-04-17 16:05:31 -05007616 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7617 /* Polling for a mbox command when another one is already active
7618 * is not allowed in SLI. Also, the driver must have established
7619 * SLI2 mode to queue and process multiple mbox commands.
7620 */
7621
7622 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007623 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007624
7625 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007626 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7627 "(%d):2529 Mailbox command x%x "
7628 "cannot issue Data: x%x x%x\n",
7629 pmbox->vport ? pmbox->vport->vpi : 0,
7630 pmbox->u.mb.mbxCommand,
7631 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007632 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007633 }
7634
James Smart3772a992009-05-22 14:50:54 -04007635 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007636 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007637 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007638 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7639 "(%d):2530 Mailbox command x%x "
7640 "cannot issue Data: x%x x%x\n",
7641 pmbox->vport ? pmbox->vport->vpi : 0,
7642 pmbox->u.mb.mbxCommand,
7643 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007644 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007645 }
7646
dea31012005-04-17 16:05:31 -05007647 /* Another mailbox command is still being processed, queue this
7648 * command to be processed later.
7649 */
7650 lpfc_mbox_put(phba, pmbox);
7651
7652 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007653 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007654 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007655 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007656 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07007657 mbx->mbxCommand,
7658 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007659 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007660
7661 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007662 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007663
James Smart858c9f62007-06-17 19:56:39 -05007664 if (pmbox->vport) {
7665 lpfc_debugfs_disc_trc(pmbox->vport,
7666 LPFC_DISC_TRC_MBOX_VPORT,
7667 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007668 (uint32_t)mbx->mbxCommand,
7669 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007670 }
7671 else {
7672 lpfc_debugfs_disc_trc(phba->pport,
7673 LPFC_DISC_TRC_MBOX,
7674 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007675 (uint32_t)mbx->mbxCommand,
7676 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007677 }
7678
James Smart2e0fef82007-06-17 19:56:36 -05007679 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007680 }
7681
dea31012005-04-17 16:05:31 -05007682 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7683
7684 /* If we are not polling, we MUST be in SLI2 mode */
7685 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007686 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007687 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007688 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007689 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007690 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007691 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7692 "(%d):2531 Mailbox command x%x "
7693 "cannot issue Data: x%x x%x\n",
7694 pmbox->vport ? pmbox->vport->vpi : 0,
7695 pmbox->u.mb.mbxCommand,
7696 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007697 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007698 }
7699 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007700 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7701 1000);
7702 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007703 }
7704
7705 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007706 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007707 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007708 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007709 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07007710 mbx->mbxCommand,
7711 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007712 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007713
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007714 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007715 if (pmbox->vport) {
7716 lpfc_debugfs_disc_trc(pmbox->vport,
7717 LPFC_DISC_TRC_MBOX_VPORT,
7718 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007719 (uint32_t)mbx->mbxCommand,
7720 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007721 }
7722 else {
7723 lpfc_debugfs_disc_trc(phba->pport,
7724 LPFC_DISC_TRC_MBOX,
7725 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007726 (uint32_t)mbx->mbxCommand,
7727 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007728 }
7729 }
7730
dea31012005-04-17 16:05:31 -05007731 psli->slistat.mbox_cmd++;
7732 evtctr = psli->slistat.mbox_event;
7733
7734 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007735 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007736
James Smart3772a992009-05-22 14:50:54 -04007737 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007738 /* Populate mbox extension offset word. */
7739 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007740 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007741 = (uint8_t *)phba->mbox_ext
7742 - (uint8_t *)phba->mbox;
7743 }
7744
7745 /* Copy the mailbox extension data */
7746 if (pmbox->in_ext_byte_len && pmbox->context2) {
7747 lpfc_sli_pcimem_bcopy(pmbox->context2,
7748 (uint8_t *)phba->mbox_ext,
7749 pmbox->in_ext_byte_len);
7750 }
7751 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007752 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007753 } else {
James Smart7a470272010-03-15 11:25:20 -04007754 /* Populate mbox extension offset word. */
7755 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007756 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007757 = MAILBOX_HBA_EXT_OFFSET;
7758
7759 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007760 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007761 lpfc_memcpy_to_slim(phba->MBslimaddr +
7762 MAILBOX_HBA_EXT_OFFSET,
7763 pmbox->context2, pmbox->in_ext_byte_len);
7764
James Smart895427b2017-02-12 13:52:30 -08007765 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007766 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007767 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7768 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007769
7770 /* First copy mbox command data to HBA SLIM, skip past first
7771 word */
7772 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007773 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007774 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7775
7776 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007777 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007778 to_slim = phba->MBslimaddr;
7779 writel(ldata, to_slim);
7780 readl(to_slim); /* flush */
7781
James Smart895427b2017-02-12 13:52:30 -08007782 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007783 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007784 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007785 }
7786
7787 wmb();
dea31012005-04-17 16:05:31 -05007788
7789 switch (flag) {
7790 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007791 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007792 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007793 /* Interrupt board to do it */
7794 writel(CA_MBATT, phba->CAregaddr);
7795 readl(phba->CAregaddr); /* flush */
7796 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007797 break;
7798
7799 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007800 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007801 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007802 /* Interrupt board to do it */
7803 writel(CA_MBATT, phba->CAregaddr);
7804 readl(phba->CAregaddr); /* flush */
7805
James Smart3772a992009-05-22 14:50:54 -04007806 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007807 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007808 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007809 word0 = le32_to_cpu(word0);
7810 } else {
7811 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007812 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7813 spin_unlock_irqrestore(&phba->hbalock,
7814 drvr_flag);
7815 goto out_not_finished;
7816 }
dea31012005-04-17 16:05:31 -05007817 }
7818
7819 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007820 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7821 spin_unlock_irqrestore(&phba->hbalock,
7822 drvr_flag);
7823 goto out_not_finished;
7824 }
James Smarta183a152011-10-10 21:32:43 -04007825 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7826 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007827 i = 0;
dea31012005-04-17 16:05:31 -05007828 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007829 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7830 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007831 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007832 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007833 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007834 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007835 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007836 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007837 }
7838
7839 /* Check if we took a mbox interrupt while we were
7840 polling */
7841 if (((word0 & OWN_CHIP) != OWN_CHIP)
7842 && (evtctr != psli->slistat.mbox_event))
7843 break;
7844
James Smart09372822008-01-11 01:52:54 -05007845 if (i++ > 10) {
7846 spin_unlock_irqrestore(&phba->hbalock,
7847 drvr_flag);
7848 msleep(1);
7849 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7850 }
dea31012005-04-17 16:05:31 -05007851
James Smart3772a992009-05-22 14:50:54 -04007852 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007853 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007854 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007855 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007856 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007857 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007858 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007859 /* Check real SLIM for any errors */
7860 slimword0 = readl(phba->MBslimaddr);
7861 slimmb = (MAILBOX_t *) & slimword0;
7862 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7863 && slimmb->mbxStatus) {
7864 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007865 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007866 word0 = slimword0;
7867 }
7868 }
7869 } else {
7870 /* First copy command data */
7871 word0 = readl(phba->MBslimaddr);
7872 }
7873 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007874 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7875 spin_unlock_irqrestore(&phba->hbalock,
7876 drvr_flag);
7877 goto out_not_finished;
7878 }
dea31012005-04-17 16:05:31 -05007879 }
7880
James Smart3772a992009-05-22 14:50:54 -04007881 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007882 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007883 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7884 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007885 /* Copy the mailbox extension data */
7886 if (pmbox->out_ext_byte_len && pmbox->context2) {
7887 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7888 pmbox->context2,
7889 pmbox->out_ext_byte_len);
7890 }
dea31012005-04-17 16:05:31 -05007891 } else {
7892 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007893 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007894 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007895 /* Copy the mailbox extension data */
7896 if (pmbox->out_ext_byte_len && pmbox->context2) {
7897 lpfc_memcpy_from_slim(pmbox->context2,
7898 phba->MBslimaddr +
7899 MAILBOX_HBA_EXT_OFFSET,
7900 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007901 }
7902 }
7903
7904 writel(HA_MBATT, phba->HAregaddr);
7905 readl(phba->HAregaddr); /* flush */
7906
7907 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007908 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007909 }
7910
James Smart2e0fef82007-06-17 19:56:36 -05007911 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7912 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007913
7914out_not_finished:
7915 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007916 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007917 lpfc_mbox_cmpl_put(phba, pmbox);
7918 }
7919 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007920}
7921
James Smarte59058c2008-08-24 21:49:00 -04007922/**
James Smartf1126682009-06-10 17:22:44 -04007923 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7924 * @phba: Pointer to HBA context object.
7925 *
7926 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7927 * the driver internal pending mailbox queue. It will then try to wait out the
7928 * possible outstanding mailbox command before return.
7929 *
7930 * Returns:
7931 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7932 * the outstanding mailbox command timed out.
7933 **/
7934static int
7935lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7936{
7937 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007938 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007939 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007940
7941 /* Mark the asynchronous mailbox command posting as blocked */
7942 spin_lock_irq(&phba->hbalock);
7943 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007944 /* Determine how long we might wait for the active mailbox
7945 * command to be gracefully completed by firmware.
7946 */
James Smarta183a152011-10-10 21:32:43 -04007947 if (phba->sli.mbox_active)
7948 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7949 phba->sli.mbox_active) *
7950 1000) + jiffies;
7951 spin_unlock_irq(&phba->hbalock);
7952
James Smarte8d3c3b2013-10-10 12:21:30 -04007953 /* Make sure the mailbox is really active */
7954 if (timeout)
7955 lpfc_sli4_process_missed_mbox_completions(phba);
7956
James Smartf1126682009-06-10 17:22:44 -04007957 /* Wait for the outstnading mailbox command to complete */
7958 while (phba->sli.mbox_active) {
7959 /* Check active mailbox complete status every 2ms */
7960 msleep(2);
7961 if (time_after(jiffies, timeout)) {
7962 /* Timeout, marked the outstanding cmd not complete */
7963 rc = 1;
7964 break;
7965 }
7966 }
7967
7968 /* Can not cleanly block async mailbox command, fails it */
7969 if (rc) {
7970 spin_lock_irq(&phba->hbalock);
7971 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7972 spin_unlock_irq(&phba->hbalock);
7973 }
7974 return rc;
7975}
7976
7977/**
7978 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7979 * @phba: Pointer to HBA context object.
7980 *
7981 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7982 * commands from the driver internal pending mailbox queue. It makes sure
7983 * that there is no outstanding mailbox command before resuming posting
7984 * asynchronous mailbox commands. If, for any reason, there is outstanding
7985 * mailbox command, it will try to wait it out before resuming asynchronous
7986 * mailbox command posting.
7987 **/
7988static void
7989lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7990{
7991 struct lpfc_sli *psli = &phba->sli;
7992
7993 spin_lock_irq(&phba->hbalock);
7994 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7995 /* Asynchronous mailbox posting is not blocked, do nothing */
7996 spin_unlock_irq(&phba->hbalock);
7997 return;
7998 }
7999
8000 /* Outstanding synchronous mailbox command is guaranteed to be done,
8001 * successful or timeout, after timing-out the outstanding mailbox
8002 * command shall always be removed, so just unblock posting async
8003 * mailbox command and resume
8004 */
8005 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
8006 spin_unlock_irq(&phba->hbalock);
8007
8008 /* wake up worker thread to post asynchronlous mailbox command */
8009 lpfc_worker_wake_up(phba);
8010}
8011
8012/**
James Smart2d843ed2012-09-29 11:29:06 -04008013 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
8014 * @phba: Pointer to HBA context object.
8015 * @mboxq: Pointer to mailbox object.
8016 *
8017 * The function waits for the bootstrap mailbox register ready bit from
8018 * port for twice the regular mailbox command timeout value.
8019 *
8020 * 0 - no timeout on waiting for bootstrap mailbox register ready.
8021 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
8022 **/
8023static int
8024lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8025{
8026 uint32_t db_ready;
8027 unsigned long timeout;
8028 struct lpfc_register bmbx_reg;
8029
8030 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
8031 * 1000) + jiffies;
8032
8033 do {
8034 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
8035 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
8036 if (!db_ready)
8037 msleep(2);
8038
8039 if (time_after(jiffies, timeout))
8040 return MBXERR_ERROR;
8041 } while (!db_ready);
8042
8043 return 0;
8044}
8045
8046/**
James Smartda0436e2009-05-22 14:51:39 -04008047 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
8048 * @phba: Pointer to HBA context object.
8049 * @mboxq: Pointer to mailbox object.
8050 *
8051 * The function posts a mailbox to the port. The mailbox is expected
8052 * to be comletely filled in and ready for the port to operate on it.
8053 * This routine executes a synchronous completion operation on the
8054 * mailbox by polling for its completion.
8055 *
8056 * The caller must not be holding any locks when calling this routine.
8057 *
8058 * Returns:
8059 * MBX_SUCCESS - mailbox posted successfully
8060 * Any of the MBX error values.
8061 **/
8062static int
8063lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8064{
8065 int rc = MBX_SUCCESS;
8066 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04008067 uint32_t mcqe_status;
8068 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04008069 struct lpfc_sli *psli = &phba->sli;
8070 struct lpfc_mqe *mb = &mboxq->u.mqe;
8071 struct lpfc_bmbx_create *mbox_rgn;
8072 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04008073
8074 /*
8075 * Only one mailbox can be active to the bootstrap mailbox region
8076 * at a time and there is no queueing provided.
8077 */
8078 spin_lock_irqsave(&phba->hbalock, iflag);
8079 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8080 spin_unlock_irqrestore(&phba->hbalock, iflag);
8081 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008082 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008083 "cannot issue Data: x%x x%x\n",
8084 mboxq->vport ? mboxq->vport->vpi : 0,
8085 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008086 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8087 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008088 psli->sli_flag, MBX_POLL);
8089 return MBXERR_ERROR;
8090 }
8091 /* The server grabs the token and owns it until release */
8092 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8093 phba->sli.mbox_active = mboxq;
8094 spin_unlock_irqrestore(&phba->hbalock, iflag);
8095
James Smart2d843ed2012-09-29 11:29:06 -04008096 /* wait for bootstrap mbox register for readyness */
8097 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8098 if (rc)
8099 goto exit;
8100
James Smartda0436e2009-05-22 14:51:39 -04008101 /*
8102 * Initialize the bootstrap memory region to avoid stale data areas
8103 * in the mailbox post. Then copy the caller's mailbox contents to
8104 * the bmbx mailbox region.
8105 */
8106 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
8107 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
8108 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
8109 sizeof(struct lpfc_mqe));
8110
8111 /* Post the high mailbox dma address to the port and wait for ready. */
8112 dma_address = &phba->sli4_hba.bmbx.dma_address;
8113 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8114
James Smart2d843ed2012-09-29 11:29:06 -04008115 /* wait for bootstrap mbox register for hi-address write done */
8116 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8117 if (rc)
8118 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008119
8120 /* Post the low mailbox dma address to the port. */
8121 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008122
James Smart2d843ed2012-09-29 11:29:06 -04008123 /* wait for bootstrap mbox register for low address write done */
8124 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8125 if (rc)
8126 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008127
8128 /*
8129 * Read the CQ to ensure the mailbox has completed.
8130 * If so, update the mailbox status so that the upper layers
8131 * can complete the request normally.
8132 */
8133 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8134 sizeof(struct lpfc_mqe));
8135 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8136 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8137 sizeof(struct lpfc_mcqe));
8138 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008139 /*
8140 * When the CQE status indicates a failure and the mailbox status
8141 * indicates success then copy the CQE status into the mailbox status
8142 * (and prefix it with x4000).
8143 */
James Smartda0436e2009-05-22 14:51:39 -04008144 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008145 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8146 bf_set(lpfc_mqe_status, mb,
8147 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008148 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008149 } else
8150 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008151
8152 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008153 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008154 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8155 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008156 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8157 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8158 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008159 bf_get(lpfc_mqe_status, mb),
8160 mb->un.mb_words[0], mb->un.mb_words[1],
8161 mb->un.mb_words[2], mb->un.mb_words[3],
8162 mb->un.mb_words[4], mb->un.mb_words[5],
8163 mb->un.mb_words[6], mb->un.mb_words[7],
8164 mb->un.mb_words[8], mb->un.mb_words[9],
8165 mb->un.mb_words[10], mb->un.mb_words[11],
8166 mb->un.mb_words[12], mboxq->mcqe.word0,
8167 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8168 mboxq->mcqe.trailer);
8169exit:
8170 /* We are holding the token, no needed for lock when release */
8171 spin_lock_irqsave(&phba->hbalock, iflag);
8172 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8173 phba->sli.mbox_active = NULL;
8174 spin_unlock_irqrestore(&phba->hbalock, iflag);
8175 return rc;
8176}
8177
8178/**
8179 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8180 * @phba: Pointer to HBA context object.
8181 * @pmbox: Pointer to mailbox object.
8182 * @flag: Flag indicating how the mailbox need to be processed.
8183 *
8184 * This function is called by discovery code and HBA management code to submit
8185 * a mailbox command to firmware with SLI-4 interface spec.
8186 *
8187 * Return codes the caller owns the mailbox command after the return of the
8188 * function.
8189 **/
8190static int
8191lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8192 uint32_t flag)
8193{
8194 struct lpfc_sli *psli = &phba->sli;
8195 unsigned long iflags;
8196 int rc;
8197
James Smartb76f2dc2011-07-22 18:37:42 -04008198 /* dump from issue mailbox command if setup */
8199 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8200
James Smart8fa38512009-07-19 10:01:03 -04008201 rc = lpfc_mbox_dev_check(phba);
8202 if (unlikely(rc)) {
8203 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008204 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008205 "cannot issue Data: x%x x%x\n",
8206 mboxq->vport ? mboxq->vport->vpi : 0,
8207 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008208 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8209 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008210 psli->sli_flag, flag);
8211 goto out_not_finished;
8212 }
8213
James Smartda0436e2009-05-22 14:51:39 -04008214 /* Detect polling mode and jump to a handler */
8215 if (!phba->sli4_hba.intr_enable) {
8216 if (flag == MBX_POLL)
8217 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8218 else
8219 rc = -EIO;
8220 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008221 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008222 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008223 "(x%x/x%x) failure: "
8224 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8225 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008226 mboxq->vport ? mboxq->vport->vpi : 0,
8227 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008228 lpfc_sli_config_mbox_subsys_get(phba,
8229 mboxq),
8230 lpfc_sli_config_mbox_opcode_get(phba,
8231 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008232 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8233 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8234 bf_get(lpfc_mcqe_ext_status,
8235 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008236 psli->sli_flag, flag);
8237 return rc;
8238 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008239 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8240 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04008241 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04008242 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008243 mboxq->vport ? mboxq->vport->vpi : 0,
8244 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008245 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8246 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008247 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008248 /* Try to block the asynchronous mailbox posting */
8249 rc = lpfc_sli4_async_mbox_block(phba);
8250 if (!rc) {
8251 /* Successfully blocked, now issue sync mbox cmd */
8252 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8253 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008254 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008255 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008256 "(%d):2597 Sync Mailbox command "
8257 "x%x (x%x/x%x) failure: "
8258 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8259 "Data: x%x x%x\n,",
8260 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008261 mboxq->u.mb.mbxCommand,
8262 lpfc_sli_config_mbox_subsys_get(phba,
8263 mboxq),
8264 lpfc_sli_config_mbox_opcode_get(phba,
8265 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008266 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8267 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8268 bf_get(lpfc_mcqe_ext_status,
8269 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008270 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008271 /* Unblock the async mailbox posting afterward */
8272 lpfc_sli4_async_mbox_unblock(phba);
8273 }
8274 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008275 }
8276
8277 /* Now, interrupt mode asynchrous mailbox command */
8278 rc = lpfc_mbox_cmd_check(phba, mboxq);
8279 if (rc) {
8280 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008281 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008282 "cannot issue Data: x%x x%x\n",
8283 mboxq->vport ? mboxq->vport->vpi : 0,
8284 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008285 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8286 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008287 psli->sli_flag, flag);
8288 goto out_not_finished;
8289 }
James Smartda0436e2009-05-22 14:51:39 -04008290
8291 /* Put the mailbox command to the driver internal FIFO */
8292 psli->slistat.mbox_busy++;
8293 spin_lock_irqsave(&phba->hbalock, iflags);
8294 lpfc_mbox_put(phba, mboxq);
8295 spin_unlock_irqrestore(&phba->hbalock, iflags);
8296 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8297 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008298 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008299 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8300 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008301 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8302 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008303 phba->pport->port_state,
8304 psli->sli_flag, MBX_NOWAIT);
8305 /* Wake up worker thread to transport mailbox command from head */
8306 lpfc_worker_wake_up(phba);
8307
8308 return MBX_BUSY;
8309
8310out_not_finished:
8311 return MBX_NOT_FINISHED;
8312}
8313
8314/**
8315 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8316 * @phba: Pointer to HBA context object.
8317 *
8318 * This function is called by worker thread to send a mailbox command to
8319 * SLI4 HBA firmware.
8320 *
8321 **/
8322int
8323lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8324{
8325 struct lpfc_sli *psli = &phba->sli;
8326 LPFC_MBOXQ_t *mboxq;
8327 int rc = MBX_SUCCESS;
8328 unsigned long iflags;
8329 struct lpfc_mqe *mqe;
8330 uint32_t mbx_cmnd;
8331
8332 /* Check interrupt mode before post async mailbox command */
8333 if (unlikely(!phba->sli4_hba.intr_enable))
8334 return MBX_NOT_FINISHED;
8335
8336 /* Check for mailbox command service token */
8337 spin_lock_irqsave(&phba->hbalock, iflags);
8338 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8339 spin_unlock_irqrestore(&phba->hbalock, iflags);
8340 return MBX_NOT_FINISHED;
8341 }
8342 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8343 spin_unlock_irqrestore(&phba->hbalock, iflags);
8344 return MBX_NOT_FINISHED;
8345 }
8346 if (unlikely(phba->sli.mbox_active)) {
8347 spin_unlock_irqrestore(&phba->hbalock, iflags);
8348 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8349 "0384 There is pending active mailbox cmd\n");
8350 return MBX_NOT_FINISHED;
8351 }
8352 /* Take the mailbox command service token */
8353 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8354
8355 /* Get the next mailbox command from head of queue */
8356 mboxq = lpfc_mbox_get(phba);
8357
8358 /* If no more mailbox command waiting for post, we're done */
8359 if (!mboxq) {
8360 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8361 spin_unlock_irqrestore(&phba->hbalock, iflags);
8362 return MBX_SUCCESS;
8363 }
8364 phba->sli.mbox_active = mboxq;
8365 spin_unlock_irqrestore(&phba->hbalock, iflags);
8366
8367 /* Check device readiness for posting mailbox command */
8368 rc = lpfc_mbox_dev_check(phba);
8369 if (unlikely(rc))
8370 /* Driver clean routine will clean up pending mailbox */
8371 goto out_not_finished;
8372
8373 /* Prepare the mbox command to be posted */
8374 mqe = &mboxq->u.mqe;
8375 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8376
8377 /* Start timer for the mbox_tmo and log some mailbox post messages */
8378 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008379 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008380
8381 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008382 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008383 "x%x x%x\n",
8384 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008385 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8386 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008387 phba->pport->port_state, psli->sli_flag);
8388
8389 if (mbx_cmnd != MBX_HEARTBEAT) {
8390 if (mboxq->vport) {
8391 lpfc_debugfs_disc_trc(mboxq->vport,
8392 LPFC_DISC_TRC_MBOX_VPORT,
8393 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8394 mbx_cmnd, mqe->un.mb_words[0],
8395 mqe->un.mb_words[1]);
8396 } else {
8397 lpfc_debugfs_disc_trc(phba->pport,
8398 LPFC_DISC_TRC_MBOX,
8399 "MBOX Send: cmd:x%x mb:x%x x%x",
8400 mbx_cmnd, mqe->un.mb_words[0],
8401 mqe->un.mb_words[1]);
8402 }
8403 }
8404 psli->slistat.mbox_cmd++;
8405
8406 /* Post the mailbox command to the port */
8407 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8408 if (rc != MBX_SUCCESS) {
8409 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008410 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008411 "cannot issue Data: x%x x%x\n",
8412 mboxq->vport ? mboxq->vport->vpi : 0,
8413 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008414 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8415 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008416 psli->sli_flag, MBX_NOWAIT);
8417 goto out_not_finished;
8418 }
8419
8420 return rc;
8421
8422out_not_finished:
8423 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008424 if (phba->sli.mbox_active) {
8425 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8426 __lpfc_mbox_cmpl_put(phba, mboxq);
8427 /* Release the token */
8428 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8429 phba->sli.mbox_active = NULL;
8430 }
James Smartda0436e2009-05-22 14:51:39 -04008431 spin_unlock_irqrestore(&phba->hbalock, iflags);
8432
8433 return MBX_NOT_FINISHED;
8434}
8435
8436/**
8437 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8438 * @phba: Pointer to HBA context object.
8439 * @pmbox: Pointer to mailbox object.
8440 * @flag: Flag indicating how the mailbox need to be processed.
8441 *
8442 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8443 * the API jump table function pointer from the lpfc_hba struct.
8444 *
8445 * Return codes the caller owns the mailbox command after the return of the
8446 * function.
8447 **/
8448int
8449lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8450{
8451 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8452}
8453
8454/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008455 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008456 * @phba: The hba struct for which this call is being executed.
8457 * @dev_grp: The HBA PCI-Device group number.
8458 *
8459 * This routine sets up the mbox interface API function jump table in @phba
8460 * struct.
8461 * Returns: 0 - success, -ENODEV - failure.
8462 **/
8463int
8464lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8465{
8466
8467 switch (dev_grp) {
8468 case LPFC_PCI_DEV_LP:
8469 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8470 phba->lpfc_sli_handle_slow_ring_event =
8471 lpfc_sli_handle_slow_ring_event_s3;
8472 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8473 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8474 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8475 break;
8476 case LPFC_PCI_DEV_OC:
8477 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8478 phba->lpfc_sli_handle_slow_ring_event =
8479 lpfc_sli_handle_slow_ring_event_s4;
8480 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8481 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8482 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8483 break;
8484 default:
8485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8486 "1420 Invalid HBA PCI-device group: 0x%x\n",
8487 dev_grp);
8488 return -ENODEV;
8489 break;
8490 }
8491 return 0;
8492}
8493
8494/**
James Smart3621a712009-04-06 18:47:14 -04008495 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008496 * @phba: Pointer to HBA context object.
8497 * @pring: Pointer to driver SLI ring object.
8498 * @piocb: Pointer to address of newly added command iocb.
8499 *
8500 * This function is called with hbalock held to add a command
8501 * iocb to the txq when SLI layer cannot submit the command iocb
8502 * to the ring.
8503 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008504void
James Smart92d7f7b2007-06-17 19:56:38 -05008505__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008506 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008507{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008508 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008509 /* Insert the caller's iocb in the txq tail for later processing. */
8510 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008511}
8512
James Smarte59058c2008-08-24 21:49:00 -04008513/**
James Smart3621a712009-04-06 18:47:14 -04008514 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008515 * @phba: Pointer to HBA context object.
8516 * @pring: Pointer to driver SLI ring object.
8517 * @piocb: Pointer to address of newly added command iocb.
8518 *
8519 * This function is called with hbalock held before a new
8520 * iocb is submitted to the firmware. This function checks
8521 * txq to flush the iocbs in txq to Firmware before
8522 * submitting new iocbs to the Firmware.
8523 * If there are iocbs in the txq which need to be submitted
8524 * to firmware, lpfc_sli_next_iocb returns the first element
8525 * of the txq after dequeuing it from txq.
8526 * If there is no iocb in the txq then the function will return
8527 * *piocb and *piocb is set to NULL. Caller needs to check
8528 * *piocb to find if there are more commands in the txq.
8529 **/
dea31012005-04-17 16:05:31 -05008530static struct lpfc_iocbq *
8531lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008532 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008533{
8534 struct lpfc_iocbq * nextiocb;
8535
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008536 lockdep_assert_held(&phba->hbalock);
8537
dea31012005-04-17 16:05:31 -05008538 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8539 if (!nextiocb) {
8540 nextiocb = *piocb;
8541 *piocb = NULL;
8542 }
8543
8544 return nextiocb;
8545}
8546
James Smarte59058c2008-08-24 21:49:00 -04008547/**
James Smart3772a992009-05-22 14:50:54 -04008548 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008549 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008550 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008551 * @piocb: Pointer to command iocb.
8552 * @flag: Flag indicating if this command can be put into txq.
8553 *
James Smart3772a992009-05-22 14:50:54 -04008554 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8555 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8556 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8557 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8558 * this function allows only iocbs for posting buffers. This function finds
8559 * next available slot in the command ring and posts the command to the
8560 * available slot and writes the port attention register to request HBA start
8561 * processing new iocb. If there is no slot available in the ring and
8562 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8563 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008564 *
James Smart3772a992009-05-22 14:50:54 -04008565 * This function is called with hbalock held. The function will return success
8566 * after it successfully submit the iocb to firmware or after adding to the
8567 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008568 **/
James Smart98c9ea52007-10-27 13:37:33 -04008569static int
James Smart3772a992009-05-22 14:50:54 -04008570__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008571 struct lpfc_iocbq *piocb, uint32_t flag)
8572{
8573 struct lpfc_iocbq *nextiocb;
8574 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008575 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008576
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008577 lockdep_assert_held(&phba->hbalock);
8578
James Smart92d7f7b2007-06-17 19:56:38 -05008579 if (piocb->iocb_cmpl && (!piocb->vport) &&
8580 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8581 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8582 lpfc_printf_log(phba, KERN_ERR,
8583 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008584 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008585 piocb->iocb.ulpCommand);
8586 dump_stack();
8587 return IOCB_ERROR;
8588 }
8589
8590
Linas Vepstas8d63f372007-02-14 14:28:36 -06008591 /* If the PCI channel is in offline state, do not post iocbs. */
8592 if (unlikely(pci_channel_offline(phba->pcidev)))
8593 return IOCB_ERROR;
8594
James Smarta257bf92009-04-06 18:48:10 -04008595 /* If HBA has a deferred error attention, fail the iocb. */
8596 if (unlikely(phba->hba_flag & DEFER_ERATT))
8597 return IOCB_ERROR;
8598
dea31012005-04-17 16:05:31 -05008599 /*
8600 * We should never get an IOCB if we are in a < LINK_DOWN state
8601 */
James Smart2e0fef82007-06-17 19:56:36 -05008602 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008603 return IOCB_ERROR;
8604
8605 /*
8606 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008607 * outstanding event.
dea31012005-04-17 16:05:31 -05008608 */
James Smart0b727fe2007-10-27 13:37:25 -04008609 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008610 goto iocb_busy;
8611
James Smart2e0fef82007-06-17 19:56:36 -05008612 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008613 /*
James Smart2680eea2007-04-25 09:52:55 -04008614 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008615 * can be issued if the link is not up.
8616 */
8617 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008618 case CMD_GEN_REQUEST64_CR:
8619 case CMD_GEN_REQUEST64_CX:
8620 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8621 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008622 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008623 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8624 MENLO_TRANSPORT_TYPE))
8625
8626 goto iocb_busy;
8627 break;
dea31012005-04-17 16:05:31 -05008628 case CMD_QUE_RING_BUF_CN:
8629 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008630 /*
8631 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8632 * completion, iocb_cmpl MUST be 0.
8633 */
8634 if (piocb->iocb_cmpl)
8635 piocb->iocb_cmpl = NULL;
8636 /*FALLTHROUGH*/
8637 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008638 case CMD_CLOSE_XRI_CN:
8639 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008640 break;
8641 default:
8642 goto iocb_busy;
8643 }
8644
8645 /*
8646 * For FCP commands, we must be in a state where we can process link
8647 * attention events.
8648 */
James Smart895427b2017-02-12 13:52:30 -08008649 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008650 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008651 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008652 }
dea31012005-04-17 16:05:31 -05008653
dea31012005-04-17 16:05:31 -05008654 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8655 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8656 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8657
8658 if (iocb)
8659 lpfc_sli_update_ring(phba, pring);
8660 else
8661 lpfc_sli_update_full_ring(phba, pring);
8662
8663 if (!piocb)
8664 return IOCB_SUCCESS;
8665
8666 goto out_busy;
8667
8668 iocb_busy:
8669 pring->stats.iocb_cmd_delay++;
8670
8671 out_busy:
8672
8673 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008674 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008675 return IOCB_SUCCESS;
8676 }
8677
8678 return IOCB_BUSY;
8679}
8680
James Smart3772a992009-05-22 14:50:54 -04008681/**
James Smart4f774512009-05-22 14:52:35 -04008682 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8683 * @phba: Pointer to HBA context object.
8684 * @piocb: Pointer to command iocb.
8685 * @sglq: Pointer to the scatter gather queue object.
8686 *
8687 * This routine converts the bpl or bde that is in the IOCB
8688 * to a sgl list for the sli4 hardware. The physical address
8689 * of the bpl/bde is converted back to a virtual address.
8690 * If the IOCB contains a BPL then the list of BDE's is
8691 * converted to sli4_sge's. If the IOCB contains a single
8692 * BDE then it is converted to a single sli_sge.
8693 * The IOCB is still in cpu endianess so the contents of
8694 * the bpl can be used without byte swapping.
8695 *
8696 * Returns valid XRI = Success, NO_XRI = Failure.
8697**/
8698static uint16_t
8699lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8700 struct lpfc_sglq *sglq)
8701{
8702 uint16_t xritag = NO_XRI;
8703 struct ulp_bde64 *bpl = NULL;
8704 struct ulp_bde64 bde;
8705 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008706 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008707 IOCB_t *icmd;
8708 int numBdes = 0;
8709 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008710 uint32_t offset = 0; /* accumulated offset in the sg request list */
8711 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008712
8713 if (!piocbq || !sglq)
8714 return xritag;
8715
8716 sgl = (struct sli4_sge *)sglq->sgl;
8717 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008718 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8719 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008720 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8721 numBdes = icmd->un.genreq64.bdl.bdeSize /
8722 sizeof(struct ulp_bde64);
8723 /* The addrHigh and addrLow fields within the IOCB
8724 * have not been byteswapped yet so there is no
8725 * need to swap them back.
8726 */
James Smart1b511972011-12-13 13:23:09 -05008727 if (piocbq->context3)
8728 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8729 else
8730 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008731
James Smart1b511972011-12-13 13:23:09 -05008732 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008733 if (!bpl)
8734 return xritag;
8735
8736 for (i = 0; i < numBdes; i++) {
8737 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008738 sgl->addr_hi = bpl->addrHigh;
8739 sgl->addr_lo = bpl->addrLow;
8740
James Smart05580562011-05-24 11:40:48 -04008741 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008742 if ((i+1) == numBdes)
8743 bf_set(lpfc_sli4_sge_last, sgl, 1);
8744 else
8745 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008746 /* swap the size field back to the cpu so we
8747 * can assign it to the sgl.
8748 */
8749 bde.tus.w = le32_to_cpu(bpl->tus.w);
8750 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008751 /* The offsets in the sgl need to be accumulated
8752 * separately for the request and reply lists.
8753 * The request is always first, the reply follows.
8754 */
8755 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8756 /* add up the reply sg entries */
8757 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8758 inbound++;
8759 /* first inbound? reset the offset */
8760 if (inbound == 1)
8761 offset = 0;
8762 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008763 bf_set(lpfc_sli4_sge_type, sgl,
8764 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008765 offset += bde.tus.f.bdeSize;
8766 }
James Smart546fc852011-03-11 16:06:29 -05008767 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008768 bpl++;
8769 sgl++;
8770 }
8771 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8772 /* The addrHigh and addrLow fields of the BDE have not
8773 * been byteswapped yet so they need to be swapped
8774 * before putting them in the sgl.
8775 */
8776 sgl->addr_hi =
8777 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8778 sgl->addr_lo =
8779 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008780 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008781 bf_set(lpfc_sli4_sge_last, sgl, 1);
8782 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008783 sgl->sge_len =
8784 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008785 }
8786 return sglq->sli4_xritag;
8787}
8788
8789/**
James Smart4f774512009-05-22 14:52:35 -04008790 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8791 * @phba: Pointer to HBA context object.
8792 * @piocb: Pointer to command iocb.
8793 * @wqe: Pointer to the work queue entry.
8794 *
8795 * This routine converts the iocb command to its Work Queue Entry
8796 * equivalent. The wqe pointer should not have any fields set when
8797 * this routine is called because it will memcpy over them.
8798 * This routine does not set the CQ_ID or the WQEC bits in the
8799 * wqe.
8800 *
8801 * Returns: 0 = Success, IOCB_ERROR = Failure.
8802 **/
8803static int
8804lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8805 union lpfc_wqe *wqe)
8806{
James Smart5ffc2662009-11-18 15:39:44 -05008807 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008808 uint8_t ct = 0;
8809 uint32_t fip;
8810 uint32_t abort_tag;
8811 uint8_t command_type = ELS_COMMAND_NON_FIP;
8812 uint8_t cmnd;
8813 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008814 uint16_t abrt_iotag;
8815 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008816 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008817 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008818 int numBdes, i;
8819 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008820 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008821 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008822 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008823
James Smart45ed1192009-10-02 15:17:02 -04008824 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008825 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008826 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008827 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008828 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008829 command_type = ELS_COMMAND_FIP;
8830 else
8831 command_type = ELS_COMMAND_NON_FIP;
8832
James Smartb5c53952016-03-31 14:12:30 -07008833 if (phba->fcp_embed_io)
8834 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008835 /* Some of the fields are in the right position already */
8836 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008837 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8838 /* The ct field has moved so reset */
8839 wqe->generic.wqe_com.word7 = 0;
8840 wqe->generic.wqe_com.word10 = 0;
8841 }
James Smartb5c53952016-03-31 14:12:30 -07008842
8843 abort_tag = (uint32_t) iocbq->iotag;
8844 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008845 /* words0-2 bpl convert bde */
8846 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008847 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8848 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008849 bpl = (struct ulp_bde64 *)
8850 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8851 if (!bpl)
8852 return IOCB_ERROR;
8853
8854 /* Should already be byte swapped. */
8855 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8856 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8857 /* swap the size field back to the cpu so we
8858 * can assign it to the sgl.
8859 */
8860 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008861 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8862 total_len = 0;
8863 for (i = 0; i < numBdes; i++) {
8864 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8865 total_len += bde.tus.f.bdeSize;
8866 }
James Smart4f774512009-05-22 14:52:35 -04008867 } else
James Smart5ffc2662009-11-18 15:39:44 -05008868 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008869
8870 iocbq->iocb.ulpIoTag = iocbq->iotag;
8871 cmnd = iocbq->iocb.ulpCommand;
8872
8873 switch (iocbq->iocb.ulpCommand) {
8874 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008875 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8876 ndlp = iocbq->context_un.ndlp;
8877 else
8878 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008879 if (!iocbq->iocb.ulpLe) {
8880 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8881 "2007 Only Limited Edition cmd Format"
8882 " supported 0x%x\n",
8883 iocbq->iocb.ulpCommand);
8884 return IOCB_ERROR;
8885 }
James Smartff78d8f2011-12-13 13:21:35 -05008886
James Smart5ffc2662009-11-18 15:39:44 -05008887 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008888 /* Els_reguest64 has a TMO */
8889 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8890 iocbq->iocb.ulpTimeout);
8891 /* Need a VF for word 4 set the vf bit*/
8892 bf_set(els_req64_vf, &wqe->els_req, 0);
8893 /* And a VFID for word 12 */
8894 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008895 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008896 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8897 iocbq->iocb.ulpContext);
8898 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8899 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008900 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008901 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008902 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8903 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008904 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8905 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008906 if_type = bf_get(lpfc_sli_intf_if_type,
8907 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08008908 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008909 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008910 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008911 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008912 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008913 *pcmd == ELS_CMD_PLOGI)) {
8914 bf_set(els_req64_sp, &wqe->els_req, 1);
8915 bf_set(els_req64_sid, &wqe->els_req,
8916 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008917 if ((*pcmd == ELS_CMD_FLOGI) &&
8918 !(phba->fc_topology ==
8919 LPFC_TOPOLOGY_LOOP))
8920 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008921 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8922 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008923 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008924 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008925 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8926 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8927 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8928 }
James Smartc8685952009-11-18 15:39:16 -05008929 }
James Smart6d368e52011-05-24 11:44:12 -04008930 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8931 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008932 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8933 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8934 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8935 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8936 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8937 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008938 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008939 break;
James Smart5ffc2662009-11-18 15:39:44 -05008940 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008941 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8942 iocbq->iocb.un.ulpWord[3]);
8943 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008944 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008945 /* The entire sequence is transmitted for this IOCB */
8946 xmit_len = total_len;
8947 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008948 if (phba->link_flag & LS_LOOPBACK_MODE)
8949 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008950 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008951 /* word3 iocb=io_tag32 wqe=reserved */
8952 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008953 /* word4 relative_offset memcpy */
8954 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008955 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8956 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8957 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8958 LPFC_WQE_IOD_WRITE);
8959 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8960 LPFC_WQE_LENLOC_WORD12);
8961 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008962 wqe->xmit_sequence.xmit_len = xmit_len;
8963 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008964 break;
James Smart4f774512009-05-22 14:52:35 -04008965 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008966 /* word3 iocb=iotag32 wqe=seq_payload_len */
8967 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008968 /* word4 iocb=rsvd wqe=rsvd */
8969 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8970 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008971 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008972 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008973 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8974 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8975 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8976 LPFC_WQE_LENLOC_WORD3);
8977 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008978 break;
James Smart4f774512009-05-22 14:52:35 -04008979 case CMD_FCP_IWRITE64_CR:
8980 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008981 /* word3 iocb=iotag wqe=payload_offset_len */
8982 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008983 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8984 xmit_len + sizeof(struct fcp_rsp));
8985 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8986 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008987 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8988 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8989 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8990 iocbq->iocb.ulpFCP2Rcvy);
8991 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8992 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008993 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8994 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8995 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008996 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008997 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008998 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8999 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009000 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
9001 if (iocbq->priority) {
9002 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9003 (iocbq->priority << 1));
9004 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009005 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
9006 (phba->cfg_XLanePriority << 1));
9007 }
9008 }
James Smartb5c53952016-03-31 14:12:30 -07009009 /* Note, word 10 is already initialized to 0 */
9010
9011 if (phba->fcp_embed_io) {
9012 struct lpfc_scsi_buf *lpfc_cmd;
9013 struct sli4_sge *sgl;
9014 union lpfc_wqe128 *wqe128;
9015 struct fcp_cmnd *fcp_cmnd;
9016 uint32_t *ptr;
9017
9018 /* 128 byte wqe support here */
9019 wqe128 = (union lpfc_wqe128 *)wqe;
9020
9021 lpfc_cmd = iocbq->context1;
9022 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9023 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9024
9025 /* Word 0-2 - FCP_CMND */
9026 wqe128->generic.bde.tus.f.bdeFlags =
9027 BUFF_TYPE_BDE_IMMED;
9028 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9029 wqe128->generic.bde.addrHigh = 0;
9030 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9031
9032 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
9033
9034 /* Word 22-29 FCP CMND Payload */
9035 ptr = &wqe128->words[22];
9036 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9037 }
James Smart7851fe22011-07-22 18:36:52 -04009038 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04009039 case CMD_FCP_IREAD64_CR:
9040 /* word3 iocb=iotag wqe=payload_offset_len */
9041 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04009042 bf_set(payload_offset_len, &wqe->fcp_iread,
9043 xmit_len + sizeof(struct fcp_rsp));
9044 bf_set(cmd_buff_len, &wqe->fcp_iread,
9045 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009046 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
9047 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
9048 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
9049 iocbq->iocb.ulpFCP2Rcvy);
9050 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04009051 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009052 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
9053 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
9054 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04009055 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05009056 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05009057 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9058 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009059 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
9060 if (iocbq->priority) {
9061 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9062 (iocbq->priority << 1));
9063 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009064 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
9065 (phba->cfg_XLanePriority << 1));
9066 }
9067 }
James Smartb5c53952016-03-31 14:12:30 -07009068 /* Note, word 10 is already initialized to 0 */
9069
9070 if (phba->fcp_embed_io) {
9071 struct lpfc_scsi_buf *lpfc_cmd;
9072 struct sli4_sge *sgl;
9073 union lpfc_wqe128 *wqe128;
9074 struct fcp_cmnd *fcp_cmnd;
9075 uint32_t *ptr;
9076
9077 /* 128 byte wqe support here */
9078 wqe128 = (union lpfc_wqe128 *)wqe;
9079
9080 lpfc_cmd = iocbq->context1;
9081 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9082 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9083
9084 /* Word 0-2 - FCP_CMND */
9085 wqe128->generic.bde.tus.f.bdeFlags =
9086 BUFF_TYPE_BDE_IMMED;
9087 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9088 wqe128->generic.bde.addrHigh = 0;
9089 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9090
9091 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
9092
9093 /* Word 22-29 FCP CMND Payload */
9094 ptr = &wqe128->words[22];
9095 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9096 }
James Smart7851fe22011-07-22 18:36:52 -04009097 break;
James Smartf1126682009-06-10 17:22:44 -04009098 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04009099 /* word3 iocb=iotag wqe=payload_offset_len */
9100 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
9101 bf_set(payload_offset_len, &wqe->fcp_icmd,
9102 xmit_len + sizeof(struct fcp_rsp));
9103 bf_set(cmd_buff_len, &wqe->fcp_icmd,
9104 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04009105 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04009106 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04009107 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04009108 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
9109 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
9110 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
9111 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
9112 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04009113 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9114 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009115 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9116 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009117 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9118 if (iocbq->priority) {
9119 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9120 (iocbq->priority << 1));
9121 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009122 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9123 (phba->cfg_XLanePriority << 1));
9124 }
9125 }
James Smartb5c53952016-03-31 14:12:30 -07009126 /* Note, word 10 is already initialized to 0 */
9127
9128 if (phba->fcp_embed_io) {
9129 struct lpfc_scsi_buf *lpfc_cmd;
9130 struct sli4_sge *sgl;
9131 union lpfc_wqe128 *wqe128;
9132 struct fcp_cmnd *fcp_cmnd;
9133 uint32_t *ptr;
9134
9135 /* 128 byte wqe support here */
9136 wqe128 = (union lpfc_wqe128 *)wqe;
9137
9138 lpfc_cmd = iocbq->context1;
9139 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9140 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9141
9142 /* Word 0-2 - FCP_CMND */
9143 wqe128->generic.bde.tus.f.bdeFlags =
9144 BUFF_TYPE_BDE_IMMED;
9145 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9146 wqe128->generic.bde.addrHigh = 0;
9147 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9148
9149 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9150
9151 /* Word 22-29 FCP CMND Payload */
9152 ptr = &wqe128->words[22];
9153 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9154 }
James Smart7851fe22011-07-22 18:36:52 -04009155 break;
James Smart4f774512009-05-22 14:52:35 -04009156 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009157 /* For this command calculate the xmit length of the
9158 * request bde.
9159 */
9160 xmit_len = 0;
9161 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9162 sizeof(struct ulp_bde64);
9163 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009164 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009165 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9166 break;
James Smart63e801c2010-11-20 23:14:19 -05009167 xmit_len += bde.tus.f.bdeSize;
9168 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009169 /* word3 iocb=IO_TAG wqe=request_payload_len */
9170 wqe->gen_req.request_payload_len = xmit_len;
9171 /* word4 iocb=parameter wqe=relative_offset memcpy */
9172 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009173 /* word6 context tag copied in memcpy */
9174 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9175 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9176 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9177 "2015 Invalid CT %x command 0x%x\n",
9178 ct, iocbq->iocb.ulpCommand);
9179 return IOCB_ERROR;
9180 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009181 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9182 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9183 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9184 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9185 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9186 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9187 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9188 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009189 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009190 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009191 break;
James Smart4f774512009-05-22 14:52:35 -04009192 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009193 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009194 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009195 /* word3 iocb=iotag32 wqe=response_payload_len */
9196 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009197 /* word4 */
9198 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009199 /* word5 iocb=rsvd wge=did */
9200 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009201 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9202
9203 if_type = bf_get(lpfc_sli_intf_if_type,
9204 &phba->sli4_hba.sli_intf);
James Smart27d6ac02018-02-22 08:18:42 -08009205 if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
James Smart939723a2012-05-09 21:19:03 -04009206 if (iocbq->vport->fc_flag & FC_PT2PT) {
9207 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9208 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9209 iocbq->vport->fc_myDID);
9210 if (iocbq->vport->fc_myDID == Fabric_DID) {
9211 bf_set(wqe_els_did,
9212 &wqe->xmit_els_rsp.wqe_dest, 0);
9213 }
9214 }
9215 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009216 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9217 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9218 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9219 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009220 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009221 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009222 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009223 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009224 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9225 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9226 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9227 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9228 LPFC_WQE_LENLOC_WORD3);
9229 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009230 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9231 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009232 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9233 iocbq->context2)->virt);
9234 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009235 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9236 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009237 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009238 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9239 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009240 phba->vpi_ids[phba->pport->vpi]);
9241 }
James Smart4f774512009-05-22 14:52:35 -04009242 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009243 break;
James Smart4f774512009-05-22 14:52:35 -04009244 case CMD_CLOSE_XRI_CN:
9245 case CMD_ABORT_XRI_CN:
9246 case CMD_ABORT_XRI_CX:
9247 /* words 0-2 memcpy should be 0 rserved */
9248 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009249 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9250 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9251 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9252 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9253 } else
9254 fip = 0;
9255
9256 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009257 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009258 * The link is down, or the command was ELS_FIP
9259 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009260 * on the wire.
9261 */
9262 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9263 else
9264 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9265 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009266 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9267 wqe->abort_cmd.rsrvd5 = 0;
9268 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009269 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9270 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009271 /*
9272 * The abort handler will send us CMD_ABORT_XRI_CN or
9273 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9274 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009275 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9276 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9277 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9278 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009279 cmnd = CMD_ABORT_XRI_CX;
9280 command_type = OTHER_COMMAND;
9281 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009282 break;
James Smart6669f9b2009-10-02 15:16:45 -04009283 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009284 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009285 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009286 * we re-construct this WQE here based on information in
9287 * iocbq from scratch.
9288 */
9289 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009290 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009291 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009292 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9293 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009294 LPFC_ABTS_UNSOL_INT) {
9295 /* ABTS sent by initiator to CT exchange, the
9296 * RX_ID field will be filled with the newly
9297 * allocated responder XRI.
9298 */
9299 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9300 iocbq->sli4_xritag);
9301 } else {
9302 /* ABTS sent by responder to CT exchange, the
9303 * RX_ID field will be filled with the responder
9304 * RX_ID from ABTS.
9305 */
9306 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009307 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009308 }
James Smart6669f9b2009-10-02 15:16:45 -04009309 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9310 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009311
9312 /* Use CT=VPI */
9313 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9314 ndlp->nlp_DID);
9315 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9316 iocbq->iocb.ulpContext);
9317 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009318 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009319 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009320 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9321 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9322 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009323 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9324 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009325 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9326 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9327 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9328 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9329 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9330 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9331 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9332 }
9333
James Smart7851fe22011-07-22 18:36:52 -04009334 break;
James Smartae9e28f2017-05-15 15:20:51 -07009335 case CMD_SEND_FRAME:
9336 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9337 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9338 return 0;
James Smart4f774512009-05-22 14:52:35 -04009339 case CMD_XRI_ABORTED_CX:
9340 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009341 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9342 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9343 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9344 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9345 default:
9346 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9347 "2014 Invalid command 0x%x\n",
9348 iocbq->iocb.ulpCommand);
9349 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009350 break;
James Smart4f774512009-05-22 14:52:35 -04009351 }
James Smart6d368e52011-05-24 11:44:12 -04009352
James Smart8012cc32012-10-31 14:44:49 -04009353 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9354 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9355 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9356 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9357 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9358 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9359 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9360 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009361 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9362 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9363 wqe->generic.wqe_com.abort_tag = abort_tag;
9364 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9365 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9366 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9367 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009368 return 0;
9369}
9370
9371/**
9372 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9373 * @phba: Pointer to HBA context object.
9374 * @ring_number: SLI ring number to issue iocb on.
9375 * @piocb: Pointer to command iocb.
9376 * @flag: Flag indicating if this command can be put into txq.
9377 *
9378 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9379 * an iocb command to an HBA with SLI-4 interface spec.
9380 *
9381 * This function is called with hbalock held. The function will return success
9382 * after it successfully submit the iocb to firmware or after adding to the
9383 * txq.
9384 **/
9385static int
9386__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9387 struct lpfc_iocbq *piocb, uint32_t flag)
9388{
9389 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009390 union lpfc_wqe *wqe;
9391 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009392 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009393 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009394
James Smart895427b2017-02-12 13:52:30 -08009395 /* Get the WQ */
9396 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9397 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9398 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9399 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9400 else
9401 wq = phba->sli4_hba.oas_wq;
9402 } else {
9403 wq = phba->sli4_hba.els_wq;
9404 }
9405
9406 /* Get corresponding ring */
9407 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009408
James Smartb5c53952016-03-31 14:12:30 -07009409 /*
9410 * The WQE can be either 64 or 128 bytes,
9411 * so allocate space on the stack assuming the largest.
9412 */
9413 wqe = (union lpfc_wqe *)&wqe128;
9414
James Smart895427b2017-02-12 13:52:30 -08009415 lockdep_assert_held(&phba->hbalock);
9416
James Smart4f774512009-05-22 14:52:35 -04009417 if (piocb->sli4_xritag == NO_XRI) {
9418 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009419 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009420 sglq = NULL;
9421 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009422 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009423 if (!(flag & SLI_IOCB_RET_IOCB)) {
9424 __lpfc_sli_ringtx_put(phba,
9425 pring, piocb);
9426 return IOCB_SUCCESS;
9427 } else {
9428 return IOCB_BUSY;
9429 }
9430 } else {
James Smart895427b2017-02-12 13:52:30 -08009431 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009432 if (!sglq) {
9433 if (!(flag & SLI_IOCB_RET_IOCB)) {
9434 __lpfc_sli_ringtx_put(phba,
9435 pring,
9436 piocb);
9437 return IOCB_SUCCESS;
9438 } else
9439 return IOCB_BUSY;
9440 }
9441 }
James Smart4f774512009-05-22 14:52:35 -04009442 }
James Smart2ea259e2017-02-12 13:52:27 -08009443 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009444 /* These IO's already have an XRI and a mapped sgl. */
9445 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009446 else {
James Smart6d368e52011-05-24 11:44:12 -04009447 /*
9448 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009449 * sglq is on the active list
9450 */
James Smartedccdc12013-01-03 15:43:45 -05009451 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009452 if (!sglq)
9453 return IOCB_ERROR;
9454 }
9455
9456 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009457 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009458 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009459 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009460 return IOCB_ERROR;
9461 }
9462
James Smartb5c53952016-03-31 14:12:30 -07009463 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009464 return IOCB_ERROR;
9465
James Smart895427b2017-02-12 13:52:30 -08009466 if (lpfc_sli4_wq_put(wq, wqe))
9467 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009468 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9469
9470 return 0;
9471}
9472
9473/**
James Smart3772a992009-05-22 14:50:54 -04009474 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9475 *
9476 * This routine wraps the actual lockless version for issusing IOCB function
9477 * pointer from the lpfc_hba struct.
9478 *
9479 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009480 * IOCB_ERROR - Error
9481 * IOCB_SUCCESS - Success
9482 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009483 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009484int
James Smart3772a992009-05-22 14:50:54 -04009485__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9486 struct lpfc_iocbq *piocb, uint32_t flag)
9487{
9488 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9489}
9490
9491/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009492 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009493 * @phba: The hba struct for which this call is being executed.
9494 * @dev_grp: The HBA PCI-Device group number.
9495 *
9496 * This routine sets up the SLI interface API function jump table in @phba
9497 * struct.
9498 * Returns: 0 - success, -ENODEV - failure.
9499 **/
9500int
9501lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9502{
9503
9504 switch (dev_grp) {
9505 case LPFC_PCI_DEV_LP:
9506 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9507 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9508 break;
James Smart4f774512009-05-22 14:52:35 -04009509 case LPFC_PCI_DEV_OC:
9510 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9511 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9512 break;
James Smart3772a992009-05-22 14:50:54 -04009513 default:
9514 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9515 "1419 Invalid HBA PCI-device group: 0x%x\n",
9516 dev_grp);
9517 return -ENODEV;
9518 break;
9519 }
9520 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9521 return 0;
9522}
James Smart92d7f7b2007-06-17 19:56:38 -05009523
James Smarta1efe162015-05-21 13:55:20 -04009524/**
James Smart895427b2017-02-12 13:52:30 -08009525 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009526 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009527 * @piocb: Pointer to command iocb.
9528 *
James Smart895427b2017-02-12 13:52:30 -08009529 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9530 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009531 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009532 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009533 */
James Smart895427b2017-02-12 13:52:30 -08009534struct lpfc_sli_ring *
9535lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009536{
James Smart895427b2017-02-12 13:52:30 -08009537 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009538 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009539 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009540 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009541 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009542 /*
James Smart895427b2017-02-12 13:52:30 -08009543 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009544 * be setup based on what work queue we used.
9545 */
Dick Kennedy8e036a92017-09-29 17:34:35 -07009546 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart895427b2017-02-12 13:52:30 -08009547 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009548 lpfc_sli4_scmd_to_wqidx_distr(phba,
9549 piocb->context1);
Dick Kennedy8e036a92017-09-29 17:34:35 -07009550 piocb->hba_wqidx = piocb->hba_wqidx %
9551 phba->cfg_fcp_io_channel;
9552 }
James Smart895427b2017-02-12 13:52:30 -08009553 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009554 } else {
9555 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009556 return NULL;
9557 piocb->hba_wqidx = 0;
9558 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009559 }
James Smart895427b2017-02-12 13:52:30 -08009560 } else {
9561 if (unlikely(!phba->sli4_hba.els_wq))
9562 return NULL;
9563 piocb->hba_wqidx = 0;
9564 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009565 }
James Smart9bd2bff52014-09-03 12:57:30 -04009566}
9567
James Smarte59058c2008-08-24 21:49:00 -04009568/**
James Smart3621a712009-04-06 18:47:14 -04009569 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009570 * @phba: Pointer to HBA context object.
9571 * @pring: Pointer to driver SLI ring object.
9572 * @piocb: Pointer to command iocb.
9573 * @flag: Flag indicating if this command can be put into txq.
9574 *
9575 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9576 * function. This function gets the hbalock and calls
9577 * __lpfc_sli_issue_iocb function and will return the error returned
9578 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9579 * functions which do not hold hbalock.
9580 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009581int
James Smart3772a992009-05-22 14:50:54 -04009582lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009583 struct lpfc_iocbq *piocb, uint32_t flag)
9584{
James Smart895427b2017-02-12 13:52:30 -08009585 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009586 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009587 struct lpfc_queue *fpeq;
9588 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009589 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009590 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009591
James Smart7e56aa22012-08-03 12:35:34 -04009592 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009593 pring = lpfc_sli4_calc_ring(phba, piocb);
9594 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009595 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009596
James Smart9bd2bff52014-09-03 12:57:30 -04009597 spin_lock_irqsave(&pring->ring_lock, iflags);
9598 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9599 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009600
James Smart9bd2bff52014-09-03 12:57:30 -04009601 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009602 idx = piocb->hba_wqidx;
9603 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009604
James Smart895427b2017-02-12 13:52:30 -08009605 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009606
James Smart9bd2bff52014-09-03 12:57:30 -04009607 /* Get associated EQ with this index */
9608 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009609
James Smart9bd2bff52014-09-03 12:57:30 -04009610 /* Turn off interrupts from this EQ */
James Smartb71413d2018-02-22 08:18:40 -08009611 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009612
James Smart9bd2bff52014-09-03 12:57:30 -04009613 /*
9614 * Process all the events on FCP EQ
9615 */
9616 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9617 lpfc_sli4_hba_handle_eqe(phba,
9618 eqe, idx);
9619 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009620 }
James Smartba20c852012-08-03 12:36:52 -04009621
James Smart9bd2bff52014-09-03 12:57:30 -04009622 /* Always clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -08009623 phba->sli4_hba.sli4_eq_release(fpeq,
James Smart9bd2bff52014-09-03 12:57:30 -04009624 LPFC_QUEUE_REARM);
9625 }
James Smart895427b2017-02-12 13:52:30 -08009626 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009627 }
James Smart7e56aa22012-08-03 12:35:34 -04009628 } else {
9629 /* For now, SLI2/3 will still use hbalock */
9630 spin_lock_irqsave(&phba->hbalock, iflags);
9631 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9632 spin_unlock_irqrestore(&phba->hbalock, iflags);
9633 }
James Smart92d7f7b2007-06-17 19:56:38 -05009634 return rc;
9635}
9636
James Smarte59058c2008-08-24 21:49:00 -04009637/**
James Smart3621a712009-04-06 18:47:14 -04009638 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009639 * @phba: Pointer to HBA context object.
9640 *
9641 * This function is called while driver attaches with the
9642 * HBA to setup the extra ring. The extra ring is used
9643 * only when driver needs to support target mode functionality
9644 * or IP over FC functionalities.
9645 *
James Smart895427b2017-02-12 13:52:30 -08009646 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009647 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009648static int
9649lpfc_extra_ring_setup( struct lpfc_hba *phba)
9650{
9651 struct lpfc_sli *psli;
9652 struct lpfc_sli_ring *pring;
9653
9654 psli = &phba->sli;
9655
9656 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009657
9658 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009659 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009660 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9661 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9662 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9663 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009664
James Smarta4bc3372006-12-02 13:34:16 -05009665 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009666 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009667
James Smart7e56aa22012-08-03 12:35:34 -04009668 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9669 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9670 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9671 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009672
9673 /* Setup default profile for this ring */
9674 pring->iotag_max = 4096;
9675 pring->num_mask = 1;
9676 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009677 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9678 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009679 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9680 return 0;
9681}
9682
James Smartcb69f7d2011-12-13 13:21:57 -05009683/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9684 * @phba: Pointer to HBA context object.
9685 * @iocbq: Pointer to iocb object.
9686 *
9687 * The async_event handler calls this routine when it receives
9688 * an ASYNC_STATUS_CN event from the port. The port generates
9689 * this event when an Abort Sequence request to an rport fails
9690 * twice in succession. The abort could be originated by the
9691 * driver or by the port. The ABTS could have been for an ELS
9692 * or FCP IO. The port only generates this event when an ABTS
9693 * fails to complete after one retry.
9694 */
9695static void
9696lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9697 struct lpfc_iocbq *iocbq)
9698{
9699 struct lpfc_nodelist *ndlp = NULL;
9700 uint16_t rpi = 0, vpi = 0;
9701 struct lpfc_vport *vport = NULL;
9702
9703 /* The rpi in the ulpContext is vport-sensitive. */
9704 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9705 rpi = iocbq->iocb.ulpContext;
9706
9707 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9708 "3092 Port generated ABTS async event "
9709 "on vpi %d rpi %d status 0x%x\n",
9710 vpi, rpi, iocbq->iocb.ulpStatus);
9711
9712 vport = lpfc_find_vport_by_vpid(phba, vpi);
9713 if (!vport)
9714 goto err_exit;
9715 ndlp = lpfc_findnode_rpi(vport, rpi);
9716 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9717 goto err_exit;
9718
9719 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9720 lpfc_sli_abts_recover_port(vport, ndlp);
9721 return;
9722
9723 err_exit:
9724 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9725 "3095 Event Context not found, no "
9726 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9727 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9728 vpi, rpi);
9729}
9730
9731/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9732 * @phba: pointer to HBA context object.
9733 * @ndlp: nodelist pointer for the impacted rport.
9734 * @axri: pointer to the wcqe containing the failed exchange.
9735 *
9736 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9737 * port. The port generates this event when an abort exchange request to an
9738 * rport fails twice in succession with no reply. The abort could be originated
9739 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9740 */
9741void
9742lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9743 struct lpfc_nodelist *ndlp,
9744 struct sli4_wcqe_xri_aborted *axri)
9745{
9746 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009747 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009748
James Smart6b5151f2012-01-18 16:24:06 -05009749 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009750 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9751 "3115 Node Context not found, driver "
9752 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009753 return;
9754 }
9755
James Smartcb69f7d2011-12-13 13:21:57 -05009756 vport = ndlp->vport;
9757 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9758 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009759 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009760 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009761 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009762 bf_get(lpfc_wcqe_xa_status, axri),
9763 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009764
James Smart5c1db2a2012-03-01 22:34:36 -05009765 /*
9766 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9767 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9768 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9769 */
James Smarte3d2b802012-08-14 14:25:43 -04009770 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009771 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9772 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009773 lpfc_sli_abts_recover_port(vport, ndlp);
9774}
9775
James Smarte59058c2008-08-24 21:49:00 -04009776/**
James Smart3621a712009-04-06 18:47:14 -04009777 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009778 * @phba: Pointer to HBA context object.
9779 * @pring: Pointer to driver SLI ring object.
9780 * @iocbq: Pointer to iocb object.
9781 *
9782 * This function is called by the slow ring event handler
9783 * function when there is an ASYNC event iocb in the ring.
9784 * This function is called with no lock held.
9785 * Currently this function handles only temperature related
9786 * ASYNC events. The function decodes the temperature sensor
9787 * event message and posts events for the management applications.
9788 **/
James Smart98c9ea52007-10-27 13:37:33 -04009789static void
James Smart57127f12007-10-27 13:37:05 -04009790lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9791 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9792{
9793 IOCB_t *icmd;
9794 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009795 struct temp_event temp_event_data;
9796 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009797 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009798
9799 icmd = &iocbq->iocb;
9800 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009801
James Smartcb69f7d2011-12-13 13:21:57 -05009802 switch (evt_code) {
9803 case ASYNC_TEMP_WARN:
9804 case ASYNC_TEMP_SAFE:
9805 temp_event_data.data = (uint32_t) icmd->ulpContext;
9806 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9807 if (evt_code == ASYNC_TEMP_WARN) {
9808 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9809 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9810 "0347 Adapter is very hot, please take "
9811 "corrective action. temperature : %d Celsius\n",
9812 (uint32_t) icmd->ulpContext);
9813 } else {
9814 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9815 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9816 "0340 Adapter temperature is OK now. "
9817 "temperature : %d Celsius\n",
9818 (uint32_t) icmd->ulpContext);
9819 }
9820
9821 /* Send temperature change event to applications */
9822 shost = lpfc_shost_from_vport(phba->pport);
9823 fc_host_post_vendor_event(shost, fc_get_event_number(),
9824 sizeof(temp_event_data), (char *) &temp_event_data,
9825 LPFC_NL_VENDOR_ID);
9826 break;
9827 case ASYNC_STATUS_CN:
9828 lpfc_sli_abts_err_handler(phba, iocbq);
9829 break;
9830 default:
James Smarta257bf92009-04-06 18:48:10 -04009831 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009832 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009833 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009834 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009835 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9836 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9837 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9838 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009839 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009840 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9841 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9842 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9843 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9844
James Smartcb69f7d2011-12-13 13:21:57 -05009845 break;
James Smart57127f12007-10-27 13:37:05 -04009846 }
James Smart57127f12007-10-27 13:37:05 -04009847}
9848
9849
James Smarte59058c2008-08-24 21:49:00 -04009850/**
James Smart895427b2017-02-12 13:52:30 -08009851 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009852 * @phba: Pointer to HBA context object.
9853 *
9854 * lpfc_sli_setup sets up rings of the SLI interface with
9855 * number of iocbs per ring and iotags. This function is
9856 * called while driver attach to the HBA and before the
9857 * interrupts are enabled. So there is no need for locking.
9858 *
9859 * This function always returns 0.
9860 **/
dea31012005-04-17 16:05:31 -05009861int
James Smart895427b2017-02-12 13:52:30 -08009862lpfc_sli4_setup(struct lpfc_hba *phba)
9863{
9864 struct lpfc_sli_ring *pring;
9865
9866 pring = phba->sli4_hba.els_wq->pring;
9867 pring->num_mask = LPFC_MAX_RING_MASK;
9868 pring->prt[0].profile = 0; /* Mask 0 */
9869 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9870 pring->prt[0].type = FC_TYPE_ELS;
9871 pring->prt[0].lpfc_sli_rcv_unsol_event =
9872 lpfc_els_unsol_event;
9873 pring->prt[1].profile = 0; /* Mask 1 */
9874 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9875 pring->prt[1].type = FC_TYPE_ELS;
9876 pring->prt[1].lpfc_sli_rcv_unsol_event =
9877 lpfc_els_unsol_event;
9878 pring->prt[2].profile = 0; /* Mask 2 */
9879 /* NameServer Inquiry */
9880 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9881 /* NameServer */
9882 pring->prt[2].type = FC_TYPE_CT;
9883 pring->prt[2].lpfc_sli_rcv_unsol_event =
9884 lpfc_ct_unsol_event;
9885 pring->prt[3].profile = 0; /* Mask 3 */
9886 /* NameServer response */
9887 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9888 /* NameServer */
9889 pring->prt[3].type = FC_TYPE_CT;
9890 pring->prt[3].lpfc_sli_rcv_unsol_event =
9891 lpfc_ct_unsol_event;
9892 return 0;
9893}
9894
9895/**
9896 * lpfc_sli_setup - SLI ring setup function
9897 * @phba: Pointer to HBA context object.
9898 *
9899 * lpfc_sli_setup sets up rings of the SLI interface with
9900 * number of iocbs per ring and iotags. This function is
9901 * called while driver attach to the HBA and before the
9902 * interrupts are enabled. So there is no need for locking.
9903 *
9904 * This function always returns 0. SLI3 only.
9905 **/
9906int
dea31012005-04-17 16:05:31 -05009907lpfc_sli_setup(struct lpfc_hba *phba)
9908{
James Smarted957682007-06-17 19:56:37 -05009909 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009910 struct lpfc_sli *psli = &phba->sli;
9911 struct lpfc_sli_ring *pring;
9912
James Smart2a76a282012-08-03 12:35:54 -04009913 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009914 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009915
James Bottomley604a3e32005-10-29 10:28:33 -05009916 psli->iocbq_lookup = NULL;
9917 psli->iocbq_lookup_len = 0;
9918 psli->last_iotag = 0;
9919
dea31012005-04-17 16:05:31 -05009920 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009921 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009922 switch (i) {
9923 case LPFC_FCP_RING: /* ring 0 - FCP */
9924 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009925 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9926 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9927 pring->sli.sli3.numCiocb +=
9928 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9929 pring->sli.sli3.numRiocb +=
9930 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9931 pring->sli.sli3.numCiocb +=
9932 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9933 pring->sli.sli3.numRiocb +=
9934 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9935 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009936 SLI3_IOCB_CMD_SIZE :
9937 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009938 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009939 SLI3_IOCB_RSP_SIZE :
9940 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009941 pring->iotag_ctr = 0;
9942 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009943 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009944 pring->fast_iotag = pring->iotag_max;
9945 pring->num_mask = 0;
9946 break;
James Smarta4bc3372006-12-02 13:34:16 -05009947 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009948 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009949 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9950 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9951 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009952 SLI3_IOCB_CMD_SIZE :
9953 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009954 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009955 SLI3_IOCB_RSP_SIZE :
9956 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009957 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009958 pring->num_mask = 0;
9959 break;
9960 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9961 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009962 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9963 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9964 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009965 SLI3_IOCB_CMD_SIZE :
9966 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009967 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009968 SLI3_IOCB_RSP_SIZE :
9969 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009970 pring->fast_iotag = 0;
9971 pring->iotag_ctr = 0;
9972 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009973 pring->lpfc_sli_rcv_async_status =
9974 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009975 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009976 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009977 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9978 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009979 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009980 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009981 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009982 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9983 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009984 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009985 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009986 pring->prt[2].profile = 0; /* Mask 2 */
9987 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009988 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009989 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009990 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009991 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009992 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009993 pring->prt[3].profile = 0; /* Mask 3 */
9994 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009995 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009996 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009997 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009998 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009999 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -050010000 break;
10001 }
James Smart7e56aa22012-08-03 12:35:34 -040010002 totiocbsize += (pring->sli.sli3.numCiocb *
10003 pring->sli.sli3.sizeCiocb) +
10004 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -050010005 }
James Smarted957682007-06-17 19:56:37 -050010006 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -050010007 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -040010008 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
10009 "SLI2 SLIM Data: x%x x%lx\n",
10010 phba->brd_no, totiocbsize,
10011 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -050010012 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -050010013 if (phba->cfg_multi_ring_support == 2)
10014 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -050010015
10016 return 0;
10017}
10018
James Smarte59058c2008-08-24 21:49:00 -040010019/**
James Smart895427b2017-02-12 13:52:30 -080010020 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -040010021 * @phba: Pointer to HBA context object.
10022 *
James Smart895427b2017-02-12 13:52:30 -080010023 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -040010024 * ring. This function also initializes ring indices of each ring.
10025 * This function is called during the initialization of the SLI
10026 * interface of an HBA.
10027 * This function is called with no lock held and always returns
10028 * 1.
10029 **/
James Smart895427b2017-02-12 13:52:30 -080010030void
10031lpfc_sli4_queue_init(struct lpfc_hba *phba)
10032{
10033 struct lpfc_sli *psli;
10034 struct lpfc_sli_ring *pring;
10035 int i;
10036
10037 psli = &phba->sli;
10038 spin_lock_irq(&phba->hbalock);
10039 INIT_LIST_HEAD(&psli->mboxq);
10040 INIT_LIST_HEAD(&psli->mboxq_cmpl);
10041 /* Initialize list headers for txq and txcmplq as double linked lists */
10042 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
10043 pring = phba->sli4_hba.fcp_wq[i]->pring;
10044 pring->flag = 0;
10045 pring->ringno = LPFC_FCP_RING;
10046 INIT_LIST_HEAD(&pring->txq);
10047 INIT_LIST_HEAD(&pring->txcmplq);
10048 INIT_LIST_HEAD(&pring->iocb_continueq);
10049 spin_lock_init(&pring->ring_lock);
10050 }
10051 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
10052 pring = phba->sli4_hba.nvme_wq[i]->pring;
10053 pring->flag = 0;
10054 pring->ringno = LPFC_FCP_RING;
10055 INIT_LIST_HEAD(&pring->txq);
10056 INIT_LIST_HEAD(&pring->txcmplq);
10057 INIT_LIST_HEAD(&pring->iocb_continueq);
10058 spin_lock_init(&pring->ring_lock);
10059 }
10060 pring = phba->sli4_hba.els_wq->pring;
10061 pring->flag = 0;
10062 pring->ringno = LPFC_ELS_RING;
10063 INIT_LIST_HEAD(&pring->txq);
10064 INIT_LIST_HEAD(&pring->txcmplq);
10065 INIT_LIST_HEAD(&pring->iocb_continueq);
10066 spin_lock_init(&pring->ring_lock);
10067
10068 if (phba->cfg_nvme_io_channel) {
10069 pring = phba->sli4_hba.nvmels_wq->pring;
10070 pring->flag = 0;
10071 pring->ringno = LPFC_ELS_RING;
10072 INIT_LIST_HEAD(&pring->txq);
10073 INIT_LIST_HEAD(&pring->txcmplq);
10074 INIT_LIST_HEAD(&pring->iocb_continueq);
10075 spin_lock_init(&pring->ring_lock);
10076 }
10077
10078 if (phba->cfg_fof) {
10079 pring = phba->sli4_hba.oas_wq->pring;
10080 pring->flag = 0;
10081 pring->ringno = LPFC_FCP_RING;
10082 INIT_LIST_HEAD(&pring->txq);
10083 INIT_LIST_HEAD(&pring->txcmplq);
10084 INIT_LIST_HEAD(&pring->iocb_continueq);
10085 spin_lock_init(&pring->ring_lock);
10086 }
10087
10088 spin_unlock_irq(&phba->hbalock);
10089}
10090
10091/**
10092 * lpfc_sli_queue_init - Queue initialization function
10093 * @phba: Pointer to HBA context object.
10094 *
10095 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
10096 * ring. This function also initializes ring indices of each ring.
10097 * This function is called during the initialization of the SLI
10098 * interface of an HBA.
10099 * This function is called with no lock held and always returns
10100 * 1.
10101 **/
10102void
10103lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010104{
10105 struct lpfc_sli *psli;
10106 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -050010107 int i;
dea31012005-04-17 16:05:31 -050010108
10109 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -050010110 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010111 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -050010112 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -050010113 /* Initialize list headers for txq and txcmplq as double linked lists */
10114 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010115 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010116 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010117 pring->sli.sli3.next_cmdidx = 0;
10118 pring->sli.sli3.local_getidx = 0;
10119 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010120 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010121 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010122 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010123 pring->flag = 0;
10124 INIT_LIST_HEAD(&pring->txq);
10125 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010126 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010127 }
James Smart2e0fef82007-06-17 19:56:36 -050010128 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010129}
10130
James Smarte59058c2008-08-24 21:49:00 -040010131/**
James Smart04c68492009-05-22 14:52:52 -040010132 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10133 * @phba: Pointer to HBA context object.
10134 *
10135 * This routine flushes the mailbox command subsystem. It will unconditionally
10136 * flush all the mailbox commands in the three possible stages in the mailbox
10137 * command sub-system: pending mailbox command queue; the outstanding mailbox
10138 * command; and completed mailbox command queue. It is caller's responsibility
10139 * to make sure that the driver is in the proper state to flush the mailbox
10140 * command sub-system. Namely, the posting of mailbox commands into the
10141 * pending mailbox command queue from the various clients must be stopped;
10142 * either the HBA is in a state that it will never works on the outstanding
10143 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10144 * mailbox command has been completed.
10145 **/
10146static void
10147lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10148{
10149 LIST_HEAD(completions);
10150 struct lpfc_sli *psli = &phba->sli;
10151 LPFC_MBOXQ_t *pmb;
10152 unsigned long iflag;
10153
10154 /* Flush all the mailbox commands in the mbox system */
10155 spin_lock_irqsave(&phba->hbalock, iflag);
10156 /* The pending mailbox command queue */
10157 list_splice_init(&phba->sli.mboxq, &completions);
10158 /* The outstanding active mailbox command */
10159 if (psli->mbox_active) {
10160 list_add_tail(&psli->mbox_active->list, &completions);
10161 psli->mbox_active = NULL;
10162 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10163 }
10164 /* The completed mailbox command queue */
10165 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10166 spin_unlock_irqrestore(&phba->hbalock, iflag);
10167
10168 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10169 while (!list_empty(&completions)) {
10170 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10171 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10172 if (pmb->mbox_cmpl)
10173 pmb->mbox_cmpl(phba, pmb);
10174 }
10175}
10176
10177/**
James Smart3621a712009-04-06 18:47:14 -040010178 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010179 * @vport: Pointer to virtual port object.
10180 *
10181 * lpfc_sli_host_down is called to clean up the resources
10182 * associated with a vport before destroying virtual
10183 * port data structures.
10184 * This function does following operations:
10185 * - Free discovery resources associated with this virtual
10186 * port.
10187 * - Free iocbs associated with this virtual port in
10188 * the txq.
10189 * - Send abort for all iocb commands associated with this
10190 * vport in txcmplq.
10191 *
10192 * This function is called with no lock held and always returns 1.
10193 **/
dea31012005-04-17 16:05:31 -050010194int
James Smart92d7f7b2007-06-17 19:56:38 -050010195lpfc_sli_host_down(struct lpfc_vport *vport)
10196{
James Smart858c9f62007-06-17 19:56:39 -050010197 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010198 struct lpfc_hba *phba = vport->phba;
10199 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010200 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010201 struct lpfc_sli_ring *pring;
10202 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010203 int i;
10204 unsigned long flags = 0;
10205 uint16_t prev_pring_flag;
10206
10207 lpfc_cleanup_discovery_resources(vport);
10208
10209 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010210
James Smart895427b2017-02-12 13:52:30 -080010211 /*
10212 * Error everything on the txq since these iocbs
10213 * have not been given to the FW yet.
10214 * Also issue ABTS for everything on the txcmplq
10215 */
10216 if (phba->sli_rev != LPFC_SLI_REV4) {
10217 for (i = 0; i < psli->num_rings; i++) {
10218 pring = &psli->sli3_ring[i];
10219 prev_pring_flag = pring->flag;
10220 /* Only slow rings */
10221 if (pring->ringno == LPFC_ELS_RING) {
10222 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10223 /* Set the lpfc data pending flag */
10224 set_bit(LPFC_DATA_READY, &phba->data_flags);
10225 }
10226 list_for_each_entry_safe(iocb, next_iocb,
10227 &pring->txq, list) {
10228 if (iocb->vport != vport)
10229 continue;
10230 list_move_tail(&iocb->list, &completions);
10231 }
10232 list_for_each_entry_safe(iocb, next_iocb,
10233 &pring->txcmplq, list) {
10234 if (iocb->vport != vport)
10235 continue;
10236 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10237 }
10238 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010239 }
James Smart895427b2017-02-12 13:52:30 -080010240 } else {
10241 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10242 pring = qp->pring;
10243 if (!pring)
10244 continue;
10245 if (pring == phba->sli4_hba.els_wq->pring) {
10246 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10247 /* Set the lpfc data pending flag */
10248 set_bit(LPFC_DATA_READY, &phba->data_flags);
10249 }
10250 prev_pring_flag = pring->flag;
10251 spin_lock_irq(&pring->ring_lock);
10252 list_for_each_entry_safe(iocb, next_iocb,
10253 &pring->txq, list) {
10254 if (iocb->vport != vport)
10255 continue;
10256 list_move_tail(&iocb->list, &completions);
10257 }
10258 spin_unlock_irq(&pring->ring_lock);
10259 list_for_each_entry_safe(iocb, next_iocb,
10260 &pring->txcmplq, list) {
10261 if (iocb->vport != vport)
10262 continue;
10263 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10264 }
10265 pring->flag = prev_pring_flag;
10266 }
James Smart92d7f7b2007-06-17 19:56:38 -050010267 }
James Smart92d7f7b2007-06-17 19:56:38 -050010268 spin_unlock_irqrestore(&phba->hbalock, flags);
10269
James Smarta257bf92009-04-06 18:48:10 -040010270 /* Cancel all the IOCBs from the completions list */
10271 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10272 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010273 return 1;
10274}
10275
James Smarte59058c2008-08-24 21:49:00 -040010276/**
James Smart3621a712009-04-06 18:47:14 -040010277 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010278 * @phba: Pointer to HBA context object.
10279 *
10280 * This function cleans up all iocb, buffers, mailbox commands
10281 * while shutting down the HBA. This function is called with no
10282 * lock held and always returns 1.
10283 * This function does the following to cleanup driver resources:
10284 * - Free discovery resources for each virtual port
10285 * - Cleanup any pending fabric iocbs
10286 * - Iterate through the iocb txq and free each entry
10287 * in the list.
10288 * - Free up any buffer posted to the HBA
10289 * - Free mailbox commands in the mailbox queue.
10290 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010291int
James Smart2e0fef82007-06-17 19:56:36 -050010292lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010293{
James Smart2534ba72007-04-25 09:52:20 -040010294 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010295 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010296 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010297 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010298 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010299 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010300 int i;
10301
10302 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010303 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010304
dea31012005-04-17 16:05:31 -050010305 lpfc_hba_down_prep(phba);
10306
James Smart92d7f7b2007-06-17 19:56:38 -050010307 lpfc_fabric_abort_hba(phba);
10308
James Smart2e0fef82007-06-17 19:56:36 -050010309 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010310
James Smart895427b2017-02-12 13:52:30 -080010311 /*
10312 * Error everything on the txq since these iocbs
10313 * have not been given to the FW yet.
10314 */
10315 if (phba->sli_rev != LPFC_SLI_REV4) {
10316 for (i = 0; i < psli->num_rings; i++) {
10317 pring = &psli->sli3_ring[i];
10318 /* Only slow rings */
10319 if (pring->ringno == LPFC_ELS_RING) {
10320 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10321 /* Set the lpfc data pending flag */
10322 set_bit(LPFC_DATA_READY, &phba->data_flags);
10323 }
10324 list_splice_init(&pring->txq, &completions);
10325 }
10326 } else {
10327 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10328 pring = qp->pring;
10329 if (!pring)
10330 continue;
10331 spin_lock_irq(&pring->ring_lock);
10332 list_splice_init(&pring->txq, &completions);
10333 spin_unlock_irq(&pring->ring_lock);
10334 if (pring == phba->sli4_hba.els_wq->pring) {
10335 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10336 /* Set the lpfc data pending flag */
10337 set_bit(LPFC_DATA_READY, &phba->data_flags);
10338 }
10339 }
dea31012005-04-17 16:05:31 -050010340 }
James Smart2e0fef82007-06-17 19:56:36 -050010341 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010342
James Smarta257bf92009-04-06 18:48:10 -040010343 /* Cancel all the IOCBs from the completions list */
10344 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10345 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010346
James Smart0ff10d42008-01-11 01:52:36 -050010347 spin_lock_irqsave(&phba->hbalock, flags);
10348 list_splice_init(&phba->elsbuf, &completions);
10349 phba->elsbuf_cnt = 0;
10350 phba->elsbuf_prev_cnt = 0;
10351 spin_unlock_irqrestore(&phba->hbalock, flags);
10352
10353 while (!list_empty(&completions)) {
10354 list_remove_head(&completions, buf_ptr,
10355 struct lpfc_dmabuf, list);
10356 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10357 kfree(buf_ptr);
10358 }
10359
dea31012005-04-17 16:05:31 -050010360 /* Return any active mbox cmds */
10361 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010362
James Smartda0436e2009-05-22 14:51:39 -040010363 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010364 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010365 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010366
James Smartda0436e2009-05-22 14:51:39 -040010367 return 1;
10368}
James Smart92d7f7b2007-06-17 19:56:38 -050010369
James Smartda0436e2009-05-22 14:51:39 -040010370/**
James Smart3621a712009-04-06 18:47:14 -040010371 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010372 * @srcp: Source memory pointer.
10373 * @destp: Destination memory pointer.
10374 * @cnt: Number of words required to be copied.
10375 *
10376 * This function is used for copying data between driver memory
10377 * and the SLI memory. This function also changes the endianness
10378 * of each word if native endianness is different from SLI
10379 * endianness. This function can be called with or without
10380 * lock.
10381 **/
dea31012005-04-17 16:05:31 -050010382void
10383lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10384{
10385 uint32_t *src = srcp;
10386 uint32_t *dest = destp;
10387 uint32_t ldata;
10388 int i;
10389
10390 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10391 ldata = *src;
10392 ldata = le32_to_cpu(ldata);
10393 *dest = ldata;
10394 src++;
10395 dest++;
10396 }
10397}
10398
James Smarte59058c2008-08-24 21:49:00 -040010399
10400/**
James Smarta0c87cb2009-07-19 10:01:10 -040010401 * lpfc_sli_bemem_bcopy - SLI memory copy function
10402 * @srcp: Source memory pointer.
10403 * @destp: Destination memory pointer.
10404 * @cnt: Number of words required to be copied.
10405 *
10406 * This function is used for copying data between a data structure
10407 * with big endian representation to local endianness.
10408 * This function can be called with or without lock.
10409 **/
10410void
10411lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10412{
10413 uint32_t *src = srcp;
10414 uint32_t *dest = destp;
10415 uint32_t ldata;
10416 int i;
10417
10418 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10419 ldata = *src;
10420 ldata = be32_to_cpu(ldata);
10421 *dest = ldata;
10422 src++;
10423 dest++;
10424 }
10425}
10426
10427/**
James Smart3621a712009-04-06 18:47:14 -040010428 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010429 * @phba: Pointer to HBA context object.
10430 * @pring: Pointer to driver SLI ring object.
10431 * @mp: Pointer to driver buffer object.
10432 *
10433 * This function is called with no lock held.
10434 * It always return zero after adding the buffer to the postbufq
10435 * buffer list.
10436 **/
dea31012005-04-17 16:05:31 -050010437int
James Smart2e0fef82007-06-17 19:56:36 -050010438lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10439 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010440{
10441 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10442 later */
James Smart2e0fef82007-06-17 19:56:36 -050010443 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010444 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010445 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010446 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010447 return 0;
10448}
10449
James Smarte59058c2008-08-24 21:49:00 -040010450/**
James Smart3621a712009-04-06 18:47:14 -040010451 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010452 * @phba: Pointer to HBA context object.
10453 *
10454 * When HBQ is enabled, buffers are searched based on tags. This function
10455 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10456 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10457 * does not conflict with tags of buffer posted for unsolicited events.
10458 * The function returns the allocated tag. The function is called with
10459 * no locks held.
10460 **/
James Smart76bb24e2007-10-27 13:38:00 -040010461uint32_t
10462lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10463{
10464 spin_lock_irq(&phba->hbalock);
10465 phba->buffer_tag_count++;
10466 /*
10467 * Always set the QUE_BUFTAG_BIT to distiguish between
10468 * a tag assigned by HBQ.
10469 */
10470 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10471 spin_unlock_irq(&phba->hbalock);
10472 return phba->buffer_tag_count;
10473}
10474
James Smarte59058c2008-08-24 21:49:00 -040010475/**
James Smart3621a712009-04-06 18:47:14 -040010476 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010477 * @phba: Pointer to HBA context object.
10478 * @pring: Pointer to driver SLI ring object.
10479 * @tag: Buffer tag.
10480 *
10481 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10482 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10483 * iocb is posted to the response ring with the tag of the buffer.
10484 * This function searches the pring->postbufq list using the tag
10485 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10486 * iocb. If the buffer is found then lpfc_dmabuf object of the
10487 * buffer is returned to the caller else NULL is returned.
10488 * This function is called with no lock held.
10489 **/
James Smart76bb24e2007-10-27 13:38:00 -040010490struct lpfc_dmabuf *
10491lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10492 uint32_t tag)
10493{
10494 struct lpfc_dmabuf *mp, *next_mp;
10495 struct list_head *slp = &pring->postbufq;
10496
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010497 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010498 spin_lock_irq(&phba->hbalock);
10499 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10500 if (mp->buffer_tag == tag) {
10501 list_del_init(&mp->list);
10502 pring->postbufq_cnt--;
10503 spin_unlock_irq(&phba->hbalock);
10504 return mp;
10505 }
10506 }
10507
10508 spin_unlock_irq(&phba->hbalock);
10509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010510 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010511 "ring %d Data x%lx x%p x%p x%x\n",
10512 pring->ringno, (unsigned long) tag,
10513 slp->next, slp->prev, pring->postbufq_cnt);
10514
10515 return NULL;
10516}
dea31012005-04-17 16:05:31 -050010517
James Smarte59058c2008-08-24 21:49:00 -040010518/**
James Smart3621a712009-04-06 18:47:14 -040010519 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010520 * @phba: Pointer to HBA context object.
10521 * @pring: Pointer to driver SLI ring object.
10522 * @phys: DMA address of the buffer.
10523 *
10524 * This function searches the buffer list using the dma_address
10525 * of unsolicited event to find the driver's lpfc_dmabuf object
10526 * corresponding to the dma_address. The function returns the
10527 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10528 * This function is called by the ct and els unsolicited event
10529 * handlers to get the buffer associated with the unsolicited
10530 * event.
10531 *
10532 * This function is called with no lock held.
10533 **/
dea31012005-04-17 16:05:31 -050010534struct lpfc_dmabuf *
10535lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10536 dma_addr_t phys)
10537{
10538 struct lpfc_dmabuf *mp, *next_mp;
10539 struct list_head *slp = &pring->postbufq;
10540
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010541 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010542 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010543 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10544 if (mp->phys == phys) {
10545 list_del_init(&mp->list);
10546 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010547 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010548 return mp;
10549 }
10550 }
10551
James Smart2e0fef82007-06-17 19:56:36 -050010552 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010554 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010555 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010556 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010557 slp->next, slp->prev, pring->postbufq_cnt);
10558 return NULL;
10559}
10560
James Smarte59058c2008-08-24 21:49:00 -040010561/**
James Smart3621a712009-04-06 18:47:14 -040010562 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010563 * @phba: Pointer to HBA context object.
10564 * @cmdiocb: Pointer to driver command iocb object.
10565 * @rspiocb: Pointer to driver response iocb object.
10566 *
10567 * This function is the completion handler for the abort iocbs for
10568 * ELS commands. This function is called from the ELS ring event
10569 * handler with no lock held. This function frees memory resources
10570 * associated with the abort iocb.
10571 **/
dea31012005-04-17 16:05:31 -050010572static void
James Smart2e0fef82007-06-17 19:56:36 -050010573lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10574 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010575{
James Smart2e0fef82007-06-17 19:56:36 -050010576 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010577 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010578 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010579
10580 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010581
10582 /*
10583 * Assume that the port already completed and returned, or
10584 * will return the iocb. Just Log the message.
10585 */
James Smart2680eea2007-04-25 09:52:55 -040010586 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10587 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10588
James Smart2e0fef82007-06-17 19:56:36 -050010589 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010590 if (phba->sli_rev < LPFC_SLI_REV4) {
10591 if (abort_iotag != 0 &&
10592 abort_iotag <= phba->sli.last_iotag)
10593 abort_iocb =
10594 phba->sli.iocbq_lookup[abort_iotag];
10595 } else
10596 /* For sli4 the abort_tag is the XRI,
10597 * so the abort routine puts the iotag of the iocb
10598 * being aborted in the context field of the abort
10599 * IOCB.
10600 */
10601 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010602
James Smart2a9bf3d2010-06-07 15:24:45 -040010603 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10604 "0327 Cannot abort els iocb %p "
10605 "with tag %x context %x, abort status %x, "
10606 "abort code %x\n",
10607 abort_iocb, abort_iotag, abort_context,
10608 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010609
James Smartff78d8f2011-12-13 13:21:35 -050010610 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010611 }
James Bottomley604a3e32005-10-29 10:28:33 -050010612 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010613 return;
10614}
10615
James Smarte59058c2008-08-24 21:49:00 -040010616/**
James Smart3621a712009-04-06 18:47:14 -040010617 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010618 * @phba: Pointer to HBA context object.
10619 * @cmdiocb: Pointer to driver command iocb object.
10620 * @rspiocb: Pointer to driver response iocb object.
10621 *
10622 * The function is called from SLI ring event handler with no
10623 * lock held. This function is the completion handler for ELS commands
10624 * which are aborted. The function frees memory resources used for
10625 * the aborted ELS commands.
10626 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010627static void
10628lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10629 struct lpfc_iocbq *rspiocb)
10630{
10631 IOCB_t *irsp = &rspiocb->iocb;
10632
10633 /* ELS cmd tag <ulpIoTag> completes */
10634 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010635 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010636 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010637 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010638 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010639 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10640 lpfc_ct_free_iocb(phba, cmdiocb);
10641 else
10642 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010643 return;
10644}
10645
James Smarte59058c2008-08-24 21:49:00 -040010646/**
James Smart5af5eee2010-10-22 11:06:38 -040010647 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010648 * @phba: Pointer to HBA context object.
10649 * @pring: Pointer to driver SLI ring object.
10650 * @cmdiocb: Pointer to driver command iocb object.
10651 *
James Smart5af5eee2010-10-22 11:06:38 -040010652 * This function issues an abort iocb for the provided command iocb down to
10653 * the port. Other than the case the outstanding command iocb is an abort
10654 * request, this function issues abort out unconditionally. This function is
10655 * called with hbalock held. The function returns 0 when it fails due to
10656 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010657 **/
James Smart5af5eee2010-10-22 11:06:38 -040010658static int
10659lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010660 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010661{
James Smart2e0fef82007-06-17 19:56:36 -050010662 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010663 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010664 IOCB_t *icmd = NULL;
10665 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010666 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010667 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010668
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010669 lockdep_assert_held(&phba->hbalock);
10670
James Smart92d7f7b2007-06-17 19:56:38 -050010671 /*
10672 * There are certain command types we don't want to abort. And we
10673 * don't want to abort commands that are already in the process of
10674 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010675 */
10676 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010677 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010678 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10679 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010680 return 0;
10681
dea31012005-04-17 16:05:31 -050010682 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010683 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010684 if (abtsiocbp == NULL)
10685 return 0;
dea31012005-04-17 16:05:31 -050010686
James Smart07951072007-04-25 09:51:38 -040010687 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010688 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010689 */
10690 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10691
dea31012005-04-17 16:05:31 -050010692 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010693 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10694 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010695 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010696 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010697 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10698 }
James Smartda0436e2009-05-22 14:51:39 -040010699 else
10700 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010701 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010702 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010703
James Smart5ffc2662009-11-18 15:39:44 -050010704 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010705 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010706 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10707 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010708 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10709 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010710
James Smart2e0fef82007-06-17 19:56:36 -050010711 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010712 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10713 else
10714 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10715
10716 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010717 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010718
James Smarte8b62012007-08-02 11:10:09 -040010719 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10720 "0339 Abort xri x%x, original iotag x%x, "
10721 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010722 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010723 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010724 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010725
10726 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010727 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10728 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010729 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010730 /* Note: both hbalock and ring_lock need to be set here */
10731 spin_lock_irqsave(&pring->ring_lock, iflags);
10732 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10733 abtsiocbp, 0);
10734 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10735 } else {
10736 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10737 abtsiocbp, 0);
10738 }
James Smart07951072007-04-25 09:51:38 -040010739
James Smartd7c255b2008-08-24 21:50:00 -040010740 if (retval)
10741 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010742
10743 /*
10744 * Caller to this routine should check for IOCB_ERROR
10745 * and handle it properly. This routine no longer removes
10746 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10747 */
10748 return retval;
10749}
10750
10751/**
10752 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10753 * @phba: Pointer to HBA context object.
10754 * @pring: Pointer to driver SLI ring object.
10755 * @cmdiocb: Pointer to driver command iocb object.
10756 *
10757 * This function issues an abort iocb for the provided command iocb. In case
10758 * of unloading, the abort iocb will not be issued to commands on the ELS
10759 * ring. Instead, the callback function shall be changed to those commands
10760 * so that nothing happens when them finishes. This function is called with
10761 * hbalock held. The function returns 0 when the command iocb is an abort
10762 * request.
10763 **/
10764int
10765lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10766 struct lpfc_iocbq *cmdiocb)
10767{
10768 struct lpfc_vport *vport = cmdiocb->vport;
10769 int retval = IOCB_ERROR;
10770 IOCB_t *icmd = NULL;
10771
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010772 lockdep_assert_held(&phba->hbalock);
10773
James Smart5af5eee2010-10-22 11:06:38 -040010774 /*
10775 * There are certain command types we don't want to abort. And we
10776 * don't want to abort commands that are already in the process of
10777 * being aborted.
10778 */
10779 icmd = &cmdiocb->iocb;
10780 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10781 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10782 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10783 return 0;
10784
Dick Kennedy1234a6d2017-09-29 17:34:29 -070010785 if (!pring) {
10786 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10787 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10788 else
10789 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10790 goto abort_iotag_exit;
10791 }
10792
James Smart5af5eee2010-10-22 11:06:38 -040010793 /*
10794 * If we're unloading, don't abort iocb on the ELS ring, but change
10795 * the callback so that nothing happens when it finishes.
10796 */
10797 if ((vport->load_flag & FC_UNLOADING) &&
10798 (pring->ringno == LPFC_ELS_RING)) {
10799 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10800 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10801 else
10802 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10803 goto abort_iotag_exit;
10804 }
10805
10806 /* Now, we try to issue the abort to the cmdiocb out */
10807 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10808
James Smart07951072007-04-25 09:51:38 -040010809abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010810 /*
10811 * Caller to this routine should check for IOCB_ERROR
10812 * and handle it properly. This routine no longer removes
10813 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010814 */
James Smart2e0fef82007-06-17 19:56:36 -050010815 return retval;
dea31012005-04-17 16:05:31 -050010816}
10817
James Smarte59058c2008-08-24 21:49:00 -040010818/**
James Smart895427b2017-02-12 13:52:30 -080010819 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10820 * @phba: Pointer to HBA context object.
10821 * @pring: Pointer to driver SLI ring object.
10822 * @cmdiocb: Pointer to driver command iocb object.
10823 *
10824 * This function issues an abort iocb for the provided command iocb down to
10825 * the port. Other than the case the outstanding command iocb is an abort
10826 * request, this function issues abort out unconditionally. This function is
10827 * called with hbalock held. The function returns 0 when it fails due to
10828 * memory allocation failure or when the command iocb is an abort request.
10829 **/
10830static int
10831lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10832 struct lpfc_iocbq *cmdiocb)
10833{
10834 struct lpfc_vport *vport = cmdiocb->vport;
10835 struct lpfc_iocbq *abtsiocbp;
10836 union lpfc_wqe *abts_wqe;
10837 int retval;
10838
10839 /*
10840 * There are certain command types we don't want to abort. And we
10841 * don't want to abort commands that are already in the process of
10842 * being aborted.
10843 */
10844 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10845 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10846 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10847 return 0;
10848
10849 /* issue ABTS for this io based on iotag */
10850 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10851 if (abtsiocbp == NULL)
10852 return 0;
10853
10854 /* This signals the response to set the correct status
10855 * before calling the completion handler
10856 */
10857 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10858
10859 /* Complete prepping the abort wqe and issue to the FW. */
10860 abts_wqe = &abtsiocbp->wqe;
10861 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10862 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10863
10864 /* Explicitly set reserved fields to zero.*/
10865 abts_wqe->abort_cmd.rsrvd4 = 0;
10866 abts_wqe->abort_cmd.rsrvd5 = 0;
10867
10868 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10869 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10870 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10871
10872 /* word 7 */
10873 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10874 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10875 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10876 cmdiocb->iocb.ulpClass);
10877
10878 /* word 8 - tell the FW to abort the IO associated with this
10879 * outstanding exchange ID.
10880 */
10881 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10882
10883 /* word 9 - this is the iotag for the abts_wqe completion. */
10884 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10885 abtsiocbp->iotag);
10886
10887 /* word 10 */
10888 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10889 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10890 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10891
10892 /* word 11 */
10893 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10894 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10895 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10896
10897 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10898 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10899 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010900 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010901 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070010902 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080010903 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10904 "6147 Failed abts issue_wqe with status x%x "
10905 "for oxid x%x\n",
10906 retval, cmdiocb->sli4_xritag);
10907 lpfc_sli_release_iocbq(phba, abtsiocbp);
10908 return retval;
10909 }
10910
10911 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10912 "6148 Drv Abort NVME Request Issued for "
10913 "ox_id x%x on reqtag x%x\n",
10914 cmdiocb->sli4_xritag,
10915 abtsiocbp->iotag);
10916
10917 return retval;
10918}
10919
10920/**
James Smart5af5eee2010-10-22 11:06:38 -040010921 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10922 * @phba: pointer to lpfc HBA data structure.
10923 *
10924 * This routine will abort all pending and outstanding iocbs to an HBA.
10925 **/
10926void
10927lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10928{
10929 struct lpfc_sli *psli = &phba->sli;
10930 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010931 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010932 int i;
10933
James Smart895427b2017-02-12 13:52:30 -080010934 if (phba->sli_rev != LPFC_SLI_REV4) {
10935 for (i = 0; i < psli->num_rings; i++) {
10936 pring = &psli->sli3_ring[i];
10937 lpfc_sli_abort_iocb_ring(phba, pring);
10938 }
10939 return;
10940 }
10941 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10942 pring = qp->pring;
10943 if (!pring)
10944 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010945 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010946 }
10947}
10948
10949/**
James Smart3621a712009-04-06 18:47:14 -040010950 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010951 * @iocbq: Pointer to driver iocb object.
10952 * @vport: Pointer to driver virtual port object.
10953 * @tgt_id: SCSI ID of the target.
10954 * @lun_id: LUN ID of the scsi device.
10955 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10956 *
James Smart3621a712009-04-06 18:47:14 -040010957 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010958 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10959 * 0 if the filtering criteria is met for the given iocb and will return
10960 * 1 if the filtering criteria is not met.
10961 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10962 * given iocb is for the SCSI device specified by vport, tgt_id and
10963 * lun_id parameter.
10964 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10965 * given iocb is for the SCSI target specified by vport and tgt_id
10966 * parameters.
10967 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10968 * given iocb is for the SCSI host associated with the given vport.
10969 * This function is called with no locks held.
10970 **/
dea31012005-04-17 16:05:31 -050010971static int
James Smart51ef4c22007-08-02 11:10:31 -040010972lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10973 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010974 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010975{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010976 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010977 int rc = 1;
10978
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010979 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10980 return rc;
10981
James Smart51ef4c22007-08-02 11:10:31 -040010982 if (iocbq->vport != vport)
10983 return rc;
10984
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010985 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010986
James Smart495a7142008-06-14 22:52:59 -040010987 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010988 return rc;
10989
10990 switch (ctx_cmd) {
10991 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010992 if ((lpfc_cmd->rdata->pnode) &&
10993 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10994 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010995 rc = 0;
10996 break;
10997 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010998 if ((lpfc_cmd->rdata->pnode) &&
10999 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050011000 rc = 0;
11001 break;
dea31012005-04-17 16:05:31 -050011002 case LPFC_CTX_HOST:
11003 rc = 0;
11004 break;
11005 default:
11006 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070011007 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050011008 break;
11009 }
11010
11011 return rc;
11012}
11013
James Smarte59058c2008-08-24 21:49:00 -040011014/**
James Smart3621a712009-04-06 18:47:14 -040011015 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040011016 * @vport: Pointer to virtual port.
11017 * @tgt_id: SCSI ID of the target.
11018 * @lun_id: LUN ID of the scsi device.
11019 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11020 *
11021 * This function returns number of FCP commands pending for the vport.
11022 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
11023 * commands pending on the vport associated with SCSI device specified
11024 * by tgt_id and lun_id parameters.
11025 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
11026 * commands pending on the vport associated with SCSI target specified
11027 * by tgt_id parameter.
11028 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
11029 * commands pending on the vport.
11030 * This function returns the number of iocbs which satisfy the filter.
11031 * This function is called without any lock held.
11032 **/
dea31012005-04-17 16:05:31 -050011033int
James Smart51ef4c22007-08-02 11:10:31 -040011034lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
11035 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050011036{
James Smart51ef4c22007-08-02 11:10:31 -040011037 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011038 struct lpfc_iocbq *iocbq;
11039 int sum, i;
dea31012005-04-17 16:05:31 -050011040
Johannes Thumshirn31979002016-07-18 16:06:03 +020011041 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011042 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
11043 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011044
James Smart51ef4c22007-08-02 11:10:31 -040011045 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
11046 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011047 sum++;
dea31012005-04-17 16:05:31 -050011048 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020011049 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011050
dea31012005-04-17 16:05:31 -050011051 return sum;
11052}
11053
James Smarte59058c2008-08-24 21:49:00 -040011054/**
James Smart3621a712009-04-06 18:47:14 -040011055 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040011056 * @phba: Pointer to HBA context object
11057 * @cmdiocb: Pointer to command iocb object.
11058 * @rspiocb: Pointer to response iocb object.
11059 *
11060 * This function is called when an aborted FCP iocb completes. This
11061 * function is called by the ring event handler with no lock held.
11062 * This function frees the iocb.
11063 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011064void
James Smart2e0fef82007-06-17 19:56:36 -050011065lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
11066 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011067{
James Smartcb69f7d2011-12-13 13:21:57 -050011068 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040011069 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050011070 "original iotag x%x, abort cmd iotag x%x "
11071 "status 0x%x, reason 0x%x\n",
11072 cmdiocb->iocb.un.acxri.abortContextTag,
11073 cmdiocb->iocb.un.acxri.abortIoTag,
11074 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
11075 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050011076 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011077 return;
11078}
11079
James Smarte59058c2008-08-24 21:49:00 -040011080/**
James Smart3621a712009-04-06 18:47:14 -040011081 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040011082 * @vport: Pointer to virtual port.
11083 * @pring: Pointer to driver SLI ring object.
11084 * @tgt_id: SCSI ID of the target.
11085 * @lun_id: LUN ID of the scsi device.
11086 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11087 *
11088 * This function sends an abort command for every SCSI command
11089 * associated with the given virtual port pending on the ring
11090 * filtered by lpfc_sli_validate_fcp_iocb function.
11091 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
11092 * FCP iocbs associated with lun specified by tgt_id and lun_id
11093 * parameters
11094 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
11095 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11096 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
11097 * FCP iocbs associated with virtual port.
11098 * This function returns number of iocbs it failed to abort.
11099 * This function is called with no locks held.
11100 **/
dea31012005-04-17 16:05:31 -050011101int
James Smart51ef4c22007-08-02 11:10:31 -040011102lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11103 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050011104{
James Smart51ef4c22007-08-02 11:10:31 -040011105 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011106 struct lpfc_iocbq *iocbq;
11107 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070011108 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050011109 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050011110 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011111 int i;
dea31012005-04-17 16:05:31 -050011112
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011113 for (i = 1; i <= phba->sli.last_iotag; i++) {
11114 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011115
James Smart51ef4c22007-08-02 11:10:31 -040011116 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011117 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011118 continue;
11119
James Smartafbd8d82013-09-06 12:22:13 -040011120 /*
11121 * If the iocbq is already being aborted, don't take a second
11122 * action, but do count it.
11123 */
11124 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11125 continue;
11126
dea31012005-04-17 16:05:31 -050011127 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011128 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011129 if (abtsiocb == NULL) {
11130 errcnt++;
11131 continue;
11132 }
dea31012005-04-17 16:05:31 -050011133
James Smartafbd8d82013-09-06 12:22:13 -040011134 /* indicate the IO is being aborted by the driver. */
11135 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11136
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011137 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011138 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11139 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011140 if (phba->sli_rev == LPFC_SLI_REV4)
11141 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11142 else
11143 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011144 abtsiocb->iocb.ulpLe = 1;
11145 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011146 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011147
James Smart5ffc2662009-11-18 15:39:44 -050011148 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011149 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011150 if (iocbq->iocb_flag & LPFC_IO_FCP)
11151 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011152 if (iocbq->iocb_flag & LPFC_IO_FOF)
11153 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011154
James Smart2e0fef82007-06-17 19:56:36 -050011155 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011156 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11157 else
11158 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11159
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011160 /* Setup callback routine and issue the command. */
11161 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011162 if (phba->sli_rev == LPFC_SLI_REV4) {
11163 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11164 if (!pring_s4)
11165 continue;
11166 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11167 abtsiocb, 0);
11168 } else
11169 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11170 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011171 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011172 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011173 errcnt++;
11174 continue;
11175 }
11176 }
11177
11178 return errcnt;
11179}
11180
James Smarte59058c2008-08-24 21:49:00 -040011181/**
James Smart98912dda2014-04-04 13:52:31 -040011182 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11183 * @vport: Pointer to virtual port.
11184 * @pring: Pointer to driver SLI ring object.
11185 * @tgt_id: SCSI ID of the target.
11186 * @lun_id: LUN ID of the scsi device.
11187 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11188 *
11189 * This function sends an abort command for every SCSI command
11190 * associated with the given virtual port pending on the ring
11191 * filtered by lpfc_sli_validate_fcp_iocb function.
11192 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11193 * FCP iocbs associated with lun specified by tgt_id and lun_id
11194 * parameters
11195 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11196 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11197 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11198 * FCP iocbs associated with virtual port.
11199 * This function returns number of iocbs it aborted .
11200 * This function is called with no locks held right after a taskmgmt
11201 * command is sent.
11202 **/
11203int
11204lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11205 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11206{
11207 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011208 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011209 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011210 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011211 struct lpfc_iocbq *iocbq;
11212 IOCB_t *icmd;
11213 int sum, i, ret_val;
11214 unsigned long iflags;
11215 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011216
11217 spin_lock_irq(&phba->hbalock);
11218
11219 /* all I/Os are in process of being flushed */
11220 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11221 spin_unlock_irq(&phba->hbalock);
11222 return 0;
11223 }
11224 sum = 0;
11225
11226 for (i = 1; i <= phba->sli.last_iotag; i++) {
11227 iocbq = phba->sli.iocbq_lookup[i];
11228
11229 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11230 cmd) != 0)
11231 continue;
11232
11233 /*
11234 * If the iocbq is already being aborted, don't take a second
11235 * action, but do count it.
11236 */
11237 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11238 continue;
11239
11240 /* issue ABTS for this IOCB based on iotag */
11241 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11242 if (abtsiocbq == NULL)
11243 continue;
11244
11245 icmd = &iocbq->iocb;
11246 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11247 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11248 if (phba->sli_rev == LPFC_SLI_REV4)
11249 abtsiocbq->iocb.un.acxri.abortIoTag =
11250 iocbq->sli4_xritag;
11251 else
11252 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11253 abtsiocbq->iocb.ulpLe = 1;
11254 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11255 abtsiocbq->vport = vport;
11256
11257 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011258 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011259 if (iocbq->iocb_flag & LPFC_IO_FCP)
11260 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011261 if (iocbq->iocb_flag & LPFC_IO_FOF)
11262 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011263
James Smart8c50d252014-09-03 12:58:16 -040011264 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11265 ndlp = lpfc_cmd->rdata->pnode;
11266
11267 if (lpfc_is_link_up(phba) &&
11268 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011269 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11270 else
11271 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11272
11273 /* Setup callback routine and issue the command. */
11274 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11275
11276 /*
11277 * Indicate the IO is being aborted by the driver and set
11278 * the caller's flag into the aborted IO.
11279 */
11280 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11281
11282 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011283 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11284 if (pring_s4 == NULL)
11285 continue;
James Smart98912dda2014-04-04 13:52:31 -040011286 /* Note: both hbalock and ring_lock must be set here */
11287 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11288 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11289 abtsiocbq, 0);
11290 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11291 } else {
11292 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11293 abtsiocbq, 0);
11294 }
11295
11296
11297 if (ret_val == IOCB_ERROR)
11298 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11299 else
11300 sum++;
11301 }
11302 spin_unlock_irq(&phba->hbalock);
11303 return sum;
11304}
11305
11306/**
James Smart3621a712009-04-06 18:47:14 -040011307 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011308 * @phba: Pointer to HBA context object.
11309 * @cmdiocbq: Pointer to command iocb.
11310 * @rspiocbq: Pointer to response iocb.
11311 *
11312 * This function is the completion handler for iocbs issued using
11313 * lpfc_sli_issue_iocb_wait function. This function is called by the
11314 * ring event handler function without any lock held. This function
11315 * can be called from both worker thread context and interrupt
11316 * context. This function also can be called from other thread which
11317 * cleans up the SLI layer objects.
11318 * This function copy the contents of the response iocb to the
11319 * response iocb memory object provided by the caller of
11320 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11321 * sleeps for the iocb completion.
11322 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011323static void
11324lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11325 struct lpfc_iocbq *cmdiocbq,
11326 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011327{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011328 wait_queue_head_t *pdone_q;
11329 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011330 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011331
James Smart2e0fef82007-06-17 19:56:36 -050011332 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011333 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11334
11335 /*
11336 * A time out has occurred for the iocb. If a time out
11337 * completion handler has been supplied, call it. Otherwise,
11338 * just free the iocbq.
11339 */
11340
11341 spin_unlock_irqrestore(&phba->hbalock, iflags);
11342 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11343 cmdiocbq->wait_iocb_cmpl = NULL;
11344 if (cmdiocbq->iocb_cmpl)
11345 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11346 else
11347 lpfc_sli_release_iocbq(phba, cmdiocbq);
11348 return;
11349 }
11350
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011351 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11352 if (cmdiocbq->context2 && rspiocbq)
11353 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11354 &rspiocbq->iocb, sizeof(IOCB_t));
11355
James Smart0f65ff62010-02-26 14:14:23 -050011356 /* Set the exchange busy flag for task management commands */
11357 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11358 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11359 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11360 cur_iocbq);
11361 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11362 }
11363
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011364 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011365 if (pdone_q)
11366 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011367 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011368 return;
11369}
11370
James Smarte59058c2008-08-24 21:49:00 -040011371/**
James Smartd11e31d2009-06-10 17:23:06 -040011372 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11373 * @phba: Pointer to HBA context object..
11374 * @piocbq: Pointer to command iocb.
11375 * @flag: Flag to test.
11376 *
11377 * This routine grabs the hbalock and then test the iocb_flag to
11378 * see if the passed in flag is set.
11379 * Returns:
11380 * 1 if flag is set.
11381 * 0 if flag is not set.
11382 **/
11383static int
11384lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11385 struct lpfc_iocbq *piocbq, uint32_t flag)
11386{
11387 unsigned long iflags;
11388 int ret;
11389
11390 spin_lock_irqsave(&phba->hbalock, iflags);
11391 ret = piocbq->iocb_flag & flag;
11392 spin_unlock_irqrestore(&phba->hbalock, iflags);
11393 return ret;
11394
11395}
11396
11397/**
James Smart3621a712009-04-06 18:47:14 -040011398 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011399 * @phba: Pointer to HBA context object..
11400 * @pring: Pointer to sli ring.
11401 * @piocb: Pointer to command iocb.
11402 * @prspiocbq: Pointer to response iocb.
11403 * @timeout: Timeout in number of seconds.
11404 *
11405 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011406 * iocb to complete. The iocb_cmpl field of the shall be used
11407 * to handle iocbs which time out. If the field is NULL, the
11408 * function shall free the iocbq structure. If more clean up is
11409 * needed, the caller is expected to provide a completion function
11410 * that will provide the needed clean up. If the iocb command is
11411 * not completed within timeout seconds, the function will either
11412 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11413 * completion function set in the iocb_cmpl field and then return
11414 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11415 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011416 * The function waits for the iocb completion using an
11417 * non-interruptible wait.
11418 * This function will sleep while waiting for iocb completion.
11419 * So, this function should not be called from any context which
11420 * does not allow sleeping. Due to the same reason, this function
11421 * cannot be called with interrupt disabled.
11422 * This function assumes that the iocb completions occur while
11423 * this function sleep. So, this function cannot be called from
11424 * the thread which process iocb completion for this ring.
11425 * This function clears the iocb_flag of the iocb object before
11426 * issuing the iocb and the iocb completion handler sets this
11427 * flag and wakes this thread when the iocb completes.
11428 * The contents of the response iocb will be copied to prspiocbq
11429 * by the completion handler when the command completes.
11430 * This function returns IOCB_SUCCESS when success.
11431 * This function is called with no lock held.
11432 **/
dea31012005-04-17 16:05:31 -050011433int
James Smart2e0fef82007-06-17 19:56:36 -050011434lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011435 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011436 struct lpfc_iocbq *piocb,
11437 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011438 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011439{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011440 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011441 long timeleft, timeout_req = 0;
11442 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011443 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011444 struct lpfc_iocbq *iocb;
11445 int txq_cnt = 0;
11446 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011447 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011448 unsigned long iflags;
11449 bool iocb_completed = true;
11450
James Smart895427b2017-02-12 13:52:30 -080011451 if (phba->sli_rev >= LPFC_SLI_REV4)
11452 pring = lpfc_sli4_calc_ring(phba, piocb);
11453 else
11454 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011455 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011456 * If the caller has provided a response iocbq buffer, then context2
11457 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011458 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011459 if (prspiocbq) {
11460 if (piocb->context2)
11461 return IOCB_ERROR;
11462 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011463 }
11464
James Smart5a0916b2013-07-15 18:31:42 -040011465 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011466 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11467 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011468 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011469
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011470 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011471 if (lpfc_readl(phba->HCregaddr, &creg_val))
11472 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011473 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11474 writel(creg_val, phba->HCregaddr);
11475 readl(phba->HCregaddr); /* flush */
11476 }
11477
James Smart2a9bf3d2010-06-07 15:24:45 -040011478 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11479 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011480 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011481 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011482 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011483 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011484 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011485 spin_lock_irqsave(&phba->hbalock, iflags);
11486 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011487
James Smart5a0916b2013-07-15 18:31:42 -040011488 /*
11489 * IOCB timed out. Inform the wake iocb wait
11490 * completion function and set local status
11491 */
11492
11493 iocb_completed = false;
11494 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11495 }
11496 spin_unlock_irqrestore(&phba->hbalock, iflags);
11497 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011498 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011499 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011500 /* Note: we are not indicating if the IOCB has a success
11501 * status or not - that's for the caller to check.
11502 * IOCB_SUCCESS means just that the command was sent and
11503 * completed. Not that it completed successfully.
11504 * */
James Smart7054a602007-04-25 09:52:34 -040011505 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011506 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011507 "0338 IOCB wait timeout error - no "
11508 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011509 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011510 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011512 "0330 IOCB wake NOT set, "
11513 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011514 timeout, (timeleft / jiffies));
11515 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011516 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011517 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011518 if (phba->cfg_log_verbose & LOG_SLI) {
11519 list_for_each_entry(iocb, &pring->txq, list) {
11520 txq_cnt++;
11521 }
11522 list_for_each_entry(iocb, &pring->txcmplq, list) {
11523 txcmplq_cnt++;
11524 }
11525 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11526 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11527 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11528 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011529 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011530 } else {
11531 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011532 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011533 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011534 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011535 }
11536
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011537 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011538 if (lpfc_readl(phba->HCregaddr, &creg_val))
11539 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011540 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11541 writel(creg_val, phba->HCregaddr);
11542 readl(phba->HCregaddr); /* flush */
11543 }
11544
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011545 if (prspiocbq)
11546 piocb->context2 = NULL;
11547
11548 piocb->context_un.wait_queue = NULL;
11549 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011550 return retval;
11551}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011552
James Smarte59058c2008-08-24 21:49:00 -040011553/**
James Smart3621a712009-04-06 18:47:14 -040011554 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011555 * @phba: Pointer to HBA context object.
11556 * @pmboxq: Pointer to driver mailbox object.
11557 * @timeout: Timeout in number of seconds.
11558 *
11559 * This function issues the mailbox to firmware and waits for the
11560 * mailbox command to complete. If the mailbox command is not
11561 * completed within timeout seconds, it returns MBX_TIMEOUT.
11562 * The function waits for the mailbox completion using an
11563 * interruptible wait. If the thread is woken up due to a
11564 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11565 * should not free the mailbox resources, if this function returns
11566 * MBX_TIMEOUT.
11567 * This function will sleep while waiting for mailbox completion.
11568 * So, this function should not be called from any context which
11569 * does not allow sleeping. Due to the same reason, this function
11570 * cannot be called with interrupt disabled.
11571 * This function assumes that the mailbox completion occurs while
11572 * this function sleep. So, this function cannot be called from
11573 * the worker thread which processes mailbox completion.
11574 * This function is called in the context of HBA management
11575 * applications.
11576 * This function returns MBX_SUCCESS when successful.
11577 * This function is called with no lock held.
11578 **/
dea31012005-04-17 16:05:31 -050011579int
James Smart2e0fef82007-06-17 19:56:36 -050011580lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011581 uint32_t timeout)
11582{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011583 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011584 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011585 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011586 unsigned long flag;
dea31012005-04-17 16:05:31 -050011587
James Smartb230b8a2013-05-31 17:05:27 -040011588 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011589 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011590 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011591
James Smart495a7142008-06-14 22:52:59 -040011592 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011593 /* setup wake call as IOCB callback */
11594 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11595 /* setup context field to pass wait_queue pointer to wake function */
11596 pmboxq->context1 = &done_q;
11597
dea31012005-04-17 16:05:31 -050011598 /* now issue the command */
11599 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011600 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011601 wait_event_interruptible_timeout(done_q,
11602 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011603 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011604
James Smart858c9f62007-06-17 19:56:39 -050011605 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011606 /* restore the possible extended buffer for free resource */
11607 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011608 /*
11609 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11610 * else do not free the resources.
11611 */
James Smartd7c47992010-06-08 18:31:54 -040011612 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011613 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011614 } else {
James Smart7054a602007-04-25 09:52:34 -040011615 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011616 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11617 }
11618 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011619 } else {
11620 /* restore the possible extended buffer for free resource */
11621 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011622 }
11623
dea31012005-04-17 16:05:31 -050011624 return retval;
11625}
11626
James Smarte59058c2008-08-24 21:49:00 -040011627/**
James Smart3772a992009-05-22 14:50:54 -040011628 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011629 * @phba: Pointer to HBA context.
11630 *
James Smart3772a992009-05-22 14:50:54 -040011631 * This function is called to shutdown the driver's mailbox sub-system.
11632 * It first marks the mailbox sub-system is in a block state to prevent
11633 * the asynchronous mailbox command from issued off the pending mailbox
11634 * command queue. If the mailbox command sub-system shutdown is due to
11635 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11636 * the mailbox sub-system flush routine to forcefully bring down the
11637 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11638 * as with offline or HBA function reset), this routine will wait for the
11639 * outstanding mailbox command to complete before invoking the mailbox
11640 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011641 **/
James Smart3772a992009-05-22 14:50:54 -040011642void
James Smart618a5232012-06-12 13:54:36 -040011643lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011644{
James Smart3772a992009-05-22 14:50:54 -040011645 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011646 unsigned long timeout;
11647
James Smart618a5232012-06-12 13:54:36 -040011648 if (mbx_action == LPFC_MBX_NO_WAIT) {
11649 /* delay 100ms for port state */
11650 msleep(100);
11651 lpfc_sli_mbox_sys_flush(phba);
11652 return;
11653 }
James Smarta183a152011-10-10 21:32:43 -040011654 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011655
James Smart3772a992009-05-22 14:50:54 -040011656 spin_lock_irq(&phba->hbalock);
11657 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011658
11659 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011660 /* Determine how long we might wait for the active mailbox
11661 * command to be gracefully completed by firmware.
11662 */
James Smarta183a152011-10-10 21:32:43 -040011663 if (phba->sli.mbox_active)
11664 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11665 phba->sli.mbox_active) *
11666 1000) + jiffies;
11667 spin_unlock_irq(&phba->hbalock);
11668
James Smart3772a992009-05-22 14:50:54 -040011669 while (phba->sli.mbox_active) {
11670 /* Check active mailbox complete status every 2ms */
11671 msleep(2);
11672 if (time_after(jiffies, timeout))
11673 /* Timeout, let the mailbox flush routine to
11674 * forcefully release active mailbox command
11675 */
11676 break;
11677 }
James Smartd7069f02012-03-01 22:36:29 -050011678 } else
11679 spin_unlock_irq(&phba->hbalock);
11680
James Smart3772a992009-05-22 14:50:54 -040011681 lpfc_sli_mbox_sys_flush(phba);
11682}
11683
11684/**
11685 * lpfc_sli_eratt_read - read sli-3 error attention events
11686 * @phba: Pointer to HBA context.
11687 *
11688 * This function is called to read the SLI3 device error attention registers
11689 * for possible error attention events. The caller must hold the hostlock
11690 * with spin_lock_irq().
11691 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011692 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011693 * Register and returns 0 otherwise.
11694 **/
11695static int
11696lpfc_sli_eratt_read(struct lpfc_hba *phba)
11697{
James Smarted957682007-06-17 19:56:37 -050011698 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011699
James Smart3772a992009-05-22 14:50:54 -040011700 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011701 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11702 goto unplug_err;
11703
James Smart3772a992009-05-22 14:50:54 -040011704 if (ha_copy & HA_ERATT) {
11705 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011706 if (lpfc_sli_read_hs(phba))
11707 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011708
James Smart3772a992009-05-22 14:50:54 -040011709 /* Check if there is a deferred error condition is active */
11710 if ((HS_FFER1 & phba->work_hs) &&
11711 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011712 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011713 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011714 /* Clear all interrupt enable conditions */
11715 writel(0, phba->HCregaddr);
11716 readl(phba->HCregaddr);
11717 }
11718
11719 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011720 phba->work_ha |= HA_ERATT;
11721 /* Indicate polling handles this ERATT */
11722 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011723 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011724 }
James Smart3772a992009-05-22 14:50:54 -040011725 return 0;
James Smart9940b972011-03-11 16:06:12 -050011726
11727unplug_err:
11728 /* Set the driver HS work bitmap */
11729 phba->work_hs |= UNPLUG_ERR;
11730 /* Set the driver HA work bitmap */
11731 phba->work_ha |= HA_ERATT;
11732 /* Indicate polling handles this ERATT */
11733 phba->hba_flag |= HBA_ERATT_HANDLED;
11734 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011735}
11736
James Smarte59058c2008-08-24 21:49:00 -040011737/**
James Smartda0436e2009-05-22 14:51:39 -040011738 * lpfc_sli4_eratt_read - read sli-4 error attention events
11739 * @phba: Pointer to HBA context.
11740 *
11741 * This function is called to read the SLI4 device error attention registers
11742 * for possible error attention events. The caller must hold the hostlock
11743 * with spin_lock_irq().
11744 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011745 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011746 * Register and returns 0 otherwise.
11747 **/
11748static int
11749lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11750{
11751 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011752 uint32_t if_type, portsmphr;
11753 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011754
James Smart2fcee4b2010-12-15 17:57:46 -050011755 /*
11756 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011757 * registers for error attention. This can be changed later.
11758 */
James Smart2fcee4b2010-12-15 17:57:46 -050011759 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11760 switch (if_type) {
11761 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011762 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11763 &uerr_sta_lo) ||
11764 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11765 &uerr_sta_hi)) {
11766 phba->work_hs |= UNPLUG_ERR;
11767 phba->work_ha |= HA_ERATT;
11768 phba->hba_flag |= HBA_ERATT_HANDLED;
11769 return 1;
11770 }
James Smart2fcee4b2010-12-15 17:57:46 -050011771 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11772 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11774 "1423 HBA Unrecoverable error: "
11775 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11776 "ue_mask_lo_reg=0x%x, "
11777 "ue_mask_hi_reg=0x%x\n",
11778 uerr_sta_lo, uerr_sta_hi,
11779 phba->sli4_hba.ue_mask_lo,
11780 phba->sli4_hba.ue_mask_hi);
11781 phba->work_status[0] = uerr_sta_lo;
11782 phba->work_status[1] = uerr_sta_hi;
11783 phba->work_ha |= HA_ERATT;
11784 phba->hba_flag |= HBA_ERATT_HANDLED;
11785 return 1;
11786 }
11787 break;
11788 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart27d6ac02018-02-22 08:18:42 -080011789 case LPFC_SLI_INTF_IF_TYPE_6:
James Smart9940b972011-03-11 16:06:12 -050011790 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11791 &portstat_reg.word0) ||
11792 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11793 &portsmphr)){
11794 phba->work_hs |= UNPLUG_ERR;
11795 phba->work_ha |= HA_ERATT;
11796 phba->hba_flag |= HBA_ERATT_HANDLED;
11797 return 1;
11798 }
James Smart2fcee4b2010-12-15 17:57:46 -050011799 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11800 phba->work_status[0] =
11801 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11802 phba->work_status[1] =
11803 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11804 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011805 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011806 "port status reg 0x%x, "
11807 "port smphr reg 0x%x, "
11808 "error 1=0x%x, error 2=0x%x\n",
11809 portstat_reg.word0,
11810 portsmphr,
11811 phba->work_status[0],
11812 phba->work_status[1]);
11813 phba->work_ha |= HA_ERATT;
11814 phba->hba_flag |= HBA_ERATT_HANDLED;
11815 return 1;
11816 }
11817 break;
11818 case LPFC_SLI_INTF_IF_TYPE_1:
11819 default:
James Smarta747c9c2009-11-18 15:41:10 -050011820 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011821 "2886 HBA Error Attention on unsupported "
11822 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011823 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011824 }
James Smart2fcee4b2010-12-15 17:57:46 -050011825
James Smartda0436e2009-05-22 14:51:39 -040011826 return 0;
11827}
11828
11829/**
James Smart3621a712009-04-06 18:47:14 -040011830 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011831 * @phba: Pointer to HBA context.
11832 *
James Smart3772a992009-05-22 14:50:54 -040011833 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011834 * error attention register bit for error attention events.
11835 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011836 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011837 * Register and returns 0 otherwise.
11838 **/
11839int
11840lpfc_sli_check_eratt(struct lpfc_hba *phba)
11841{
11842 uint32_t ha_copy;
11843
11844 /* If somebody is waiting to handle an eratt, don't process it
11845 * here. The brdkill function will do this.
11846 */
11847 if (phba->link_flag & LS_IGNORE_ERATT)
11848 return 0;
11849
11850 /* Check if interrupt handler handles this ERATT */
11851 spin_lock_irq(&phba->hbalock);
11852 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11853 /* Interrupt handler has handled ERATT */
11854 spin_unlock_irq(&phba->hbalock);
11855 return 0;
11856 }
11857
James Smarta257bf92009-04-06 18:48:10 -040011858 /*
11859 * If there is deferred error attention, do not check for error
11860 * attention
11861 */
11862 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11863 spin_unlock_irq(&phba->hbalock);
11864 return 0;
11865 }
11866
James Smart3772a992009-05-22 14:50:54 -040011867 /* If PCI channel is offline, don't process it */
11868 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011869 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011870 return 0;
11871 }
11872
11873 switch (phba->sli_rev) {
11874 case LPFC_SLI_REV2:
11875 case LPFC_SLI_REV3:
11876 /* Read chip Host Attention (HA) register */
11877 ha_copy = lpfc_sli_eratt_read(phba);
11878 break;
James Smartda0436e2009-05-22 14:51:39 -040011879 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011880 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011881 ha_copy = lpfc_sli4_eratt_read(phba);
11882 break;
James Smart3772a992009-05-22 14:50:54 -040011883 default:
11884 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11885 "0299 Invalid SLI revision (%d)\n",
11886 phba->sli_rev);
11887 ha_copy = 0;
11888 break;
James Smart93996272008-08-24 21:50:30 -040011889 }
11890 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011891
11892 return ha_copy;
11893}
11894
11895/**
11896 * lpfc_intr_state_check - Check device state for interrupt handling
11897 * @phba: Pointer to HBA context.
11898 *
11899 * This inline routine checks whether a device or its PCI slot is in a state
11900 * that the interrupt should be handled.
11901 *
11902 * This function returns 0 if the device or the PCI slot is in a state that
11903 * interrupt should be handled, otherwise -EIO.
11904 */
11905static inline int
11906lpfc_intr_state_check(struct lpfc_hba *phba)
11907{
11908 /* If the pci channel is offline, ignore all the interrupts */
11909 if (unlikely(pci_channel_offline(phba->pcidev)))
11910 return -EIO;
11911
11912 /* Update device level interrupt statistics */
11913 phba->sli.slistat.sli_intr++;
11914
11915 /* Ignore all interrupts during initialization. */
11916 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11917 return -EIO;
11918
James Smart93996272008-08-24 21:50:30 -040011919 return 0;
11920}
11921
11922/**
James Smart3772a992009-05-22 14:50:54 -040011923 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011924 * @irq: Interrupt number.
11925 * @dev_id: The device context pointer.
11926 *
James Smart93996272008-08-24 21:50:30 -040011927 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011928 * service routine when device with SLI-3 interface spec is enabled with
11929 * MSI-X multi-message interrupt mode and there are slow-path events in
11930 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11931 * interrupt mode, this function is called as part of the device-level
11932 * interrupt handler. When the PCI slot is in error recovery or the HBA
11933 * is undergoing initialization, the interrupt handler will not process
11934 * the interrupt. The link attention and ELS ring attention events are
11935 * handled by the worker thread. The interrupt handler signals the worker
11936 * thread and returns for these events. This function is called without
11937 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011938 * structures.
11939 *
11940 * This function returns IRQ_HANDLED when interrupt is handled else it
11941 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011942 **/
dea31012005-04-17 16:05:31 -050011943irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011944lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011945{
James Smart2e0fef82007-06-17 19:56:36 -050011946 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011947 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011948 uint32_t work_ha_copy;
11949 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011950 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011951 uint32_t control;
11952
James Smart92d7f7b2007-06-17 19:56:38 -050011953 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011954 struct lpfc_vport *vport;
11955 struct lpfc_nodelist *ndlp;
11956 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011957 LPFC_MBOXQ_t *pmb;
11958 int rc;
11959
dea31012005-04-17 16:05:31 -050011960 /*
11961 * Get the driver's phba structure from the dev_id and
11962 * assume the HBA is not interrupting.
11963 */
James Smart93996272008-08-24 21:50:30 -040011964 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011965
11966 if (unlikely(!phba))
11967 return IRQ_NONE;
11968
dea31012005-04-17 16:05:31 -050011969 /*
James Smart93996272008-08-24 21:50:30 -040011970 * Stuff needs to be attented to when this function is invoked as an
11971 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011972 */
James Smart93996272008-08-24 21:50:30 -040011973 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011974 /* Check device state for handling interrupt */
11975 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011976 return IRQ_NONE;
11977 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011978 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011979 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11980 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011981 /* If somebody is waiting to handle an eratt don't process it
11982 * here. The brdkill function will do this.
11983 */
11984 if (phba->link_flag & LS_IGNORE_ERATT)
11985 ha_copy &= ~HA_ERATT;
11986 /* Check the need for handling ERATT in interrupt handler */
11987 if (ha_copy & HA_ERATT) {
11988 if (phba->hba_flag & HBA_ERATT_HANDLED)
11989 /* ERATT polling has handled ERATT */
11990 ha_copy &= ~HA_ERATT;
11991 else
11992 /* Indicate interrupt handler handles ERATT */
11993 phba->hba_flag |= HBA_ERATT_HANDLED;
11994 }
James Smarta257bf92009-04-06 18:48:10 -040011995
11996 /*
11997 * If there is deferred error attention, do not check for any
11998 * interrupt.
11999 */
12000 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012001 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012002 return IRQ_NONE;
12003 }
12004
James Smart93996272008-08-24 21:50:30 -040012005 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050012006 if (lpfc_readl(phba->HCregaddr, &hc_copy))
12007 goto unplug_error;
12008
James Smarta747c9c2009-11-18 15:41:10 -050012009 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
12010 HC_LAINT_ENA | HC_ERINT_ENA),
12011 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012012 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
12013 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012014 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012015 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012016 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012017 } else
12018 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050012019
dea31012005-04-17 16:05:31 -050012020 work_ha_copy = ha_copy & phba->work_ha_mask;
12021
James Smart93996272008-08-24 21:50:30 -040012022 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050012023 if (work_ha_copy & HA_LATT) {
12024 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
12025 /*
12026 * Turn off Link Attention interrupts
12027 * until CLEAR_LA done
12028 */
James Smart5b75da22008-12-04 22:39:35 -050012029 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012030 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050012031 if (lpfc_readl(phba->HCregaddr, &control))
12032 goto unplug_error;
dea31012005-04-17 16:05:31 -050012033 control &= ~HC_LAINT_ENA;
12034 writel(control, phba->HCregaddr);
12035 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012036 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012037 }
12038 else
12039 work_ha_copy &= ~HA_LATT;
12040 }
12041
James Smart93996272008-08-24 21:50:30 -040012042 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050012043 /*
12044 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
12045 * the only slow ring.
12046 */
12047 status = (work_ha_copy &
12048 (HA_RXMASK << (4*LPFC_ELS_RING)));
12049 status >>= (4*LPFC_ELS_RING);
12050 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050012051 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050012052 if (lpfc_readl(phba->HCregaddr, &control))
12053 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040012054
12055 lpfc_debugfs_slow_ring_trc(phba,
12056 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
12057 control, status,
12058 (uint32_t)phba->sli.slistat.sli_intr);
12059
James Smart858c9f62007-06-17 19:56:39 -050012060 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040012061 lpfc_debugfs_slow_ring_trc(phba,
12062 "ISR Disable ring:"
12063 "pwork:x%x hawork:x%x wait:x%x",
12064 phba->work_ha, work_ha_copy,
12065 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012066 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012067
James Smart858c9f62007-06-17 19:56:39 -050012068 control &=
12069 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050012070 writel(control, phba->HCregaddr);
12071 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050012072 }
James Smarta58cbd52007-08-02 11:09:43 -040012073 else {
12074 lpfc_debugfs_slow_ring_trc(phba,
12075 "ISR slow ring: pwork:"
12076 "x%x hawork:x%x wait:x%x",
12077 phba->work_ha, work_ha_copy,
12078 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040012079 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040012080 }
James Smart5b75da22008-12-04 22:39:35 -050012081 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012082 }
12083 }
James Smart5b75da22008-12-04 22:39:35 -050012084 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012085 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050012086 if (lpfc_sli_read_hs(phba))
12087 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040012088 /*
12089 * Check if there is a deferred error condition
12090 * is active
12091 */
12092 if ((HS_FFER1 & phba->work_hs) &&
12093 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040012094 HS_FFER6 | HS_FFER7 | HS_FFER8) &
12095 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040012096 phba->hba_flag |= DEFER_ERATT;
12097 /* Clear all interrupt enable conditions */
12098 writel(0, phba->HCregaddr);
12099 readl(phba->HCregaddr);
12100 }
12101 }
12102
James Smart93996272008-08-24 21:50:30 -040012103 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050012104 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040012105 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040012106 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050012107 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050012108
12109 /* First check out the status word */
12110 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
12111 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050012112 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050012113 /*
12114 * Stray Mailbox Interrupt, mbxCommand <cmd>
12115 * mbxStatus <status>
12116 */
James Smart09372822008-01-11 01:52:54 -050012117 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012118 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012119 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012120 "Interrupt mbxCommand x%x "
12121 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012122 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012123 pmbox->mbxCommand,
12124 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012125 /* clear mailbox attention bit */
12126 work_ha_copy &= ~HA_MBATT;
12127 } else {
James Smart97eab632008-04-07 10:16:05 -040012128 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012129 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012130 phba->last_completion_time = jiffies;
12131 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012132 if (pmb->mbox_cmpl) {
12133 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12134 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012135 if (pmb->out_ext_byte_len &&
12136 pmb->context2)
12137 lpfc_sli_pcimem_bcopy(
12138 phba->mbox_ext,
12139 pmb->context2,
12140 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012141 }
James Smart09372822008-01-11 01:52:54 -050012142 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12143 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12144
12145 lpfc_debugfs_disc_trc(vport,
12146 LPFC_DISC_TRC_MBOX_VPORT,
12147 "MBOX dflt rpi: : "
12148 "status:x%x rpi:x%x",
12149 (uint32_t)pmbox->mbxStatus,
12150 pmbox->un.varWords[0], 0);
12151
12152 if (!pmbox->mbxStatus) {
12153 mp = (struct lpfc_dmabuf *)
12154 (pmb->context1);
12155 ndlp = (struct lpfc_nodelist *)
12156 pmb->context2;
12157
12158 /* Reg_LOGIN of dflt RPI was
12159 * successful. new lets get
12160 * rid of the RPI using the
12161 * same mbox buffer.
12162 */
12163 lpfc_unreg_login(phba,
12164 vport->vpi,
12165 pmbox->un.varWords[0],
12166 pmb);
12167 pmb->mbox_cmpl =
12168 lpfc_mbx_cmpl_dflt_rpi;
12169 pmb->context1 = mp;
12170 pmb->context2 = ndlp;
12171 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012172 rc = lpfc_sli_issue_mbox(phba,
12173 pmb,
12174 MBX_NOWAIT);
12175 if (rc != MBX_BUSY)
12176 lpfc_printf_log(phba,
12177 KERN_ERR,
12178 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012179 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012180 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012181 if (rc != MBX_NOT_FINISHED)
12182 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012183 }
12184 }
James Smart5b75da22008-12-04 22:39:35 -050012185 spin_lock_irqsave(
12186 &phba->pport->work_port_lock,
12187 iflag);
James Smart09372822008-01-11 01:52:54 -050012188 phba->pport->work_port_events &=
12189 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012190 spin_unlock_irqrestore(
12191 &phba->pport->work_port_lock,
12192 iflag);
James Smart09372822008-01-11 01:52:54 -050012193 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012194 }
James Smart97eab632008-04-07 10:16:05 -040012195 } else
James Smart5b75da22008-12-04 22:39:35 -050012196 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012197
James Smart92d7f7b2007-06-17 19:56:38 -050012198 if ((work_ha_copy & HA_MBATT) &&
12199 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012200send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012201 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012202 do {
12203 rc = lpfc_sli_issue_mbox(phba, NULL,
12204 MBX_NOWAIT);
12205 } while (rc == MBX_NOT_FINISHED);
12206 if (rc != MBX_SUCCESS)
12207 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12208 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012209 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012210 }
12211
James Smart5b75da22008-12-04 22:39:35 -050012212 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012213 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012214 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012215 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012216 }
James Smart93996272008-08-24 21:50:30 -040012217 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012218unplug_error:
12219 spin_unlock_irqrestore(&phba->hbalock, iflag);
12220 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012221
James Smart3772a992009-05-22 14:50:54 -040012222} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012223
12224/**
James Smart3772a992009-05-22 14:50:54 -040012225 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012226 * @irq: Interrupt number.
12227 * @dev_id: The device context pointer.
12228 *
12229 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012230 * service routine when device with SLI-3 interface spec is enabled with
12231 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12232 * ring event in the HBA. However, when the device is enabled with either
12233 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12234 * device-level interrupt handler. When the PCI slot is in error recovery
12235 * or the HBA is undergoing initialization, the interrupt handler will not
12236 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12237 * the intrrupt context. This function is called without any lock held.
12238 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012239 *
12240 * This function returns IRQ_HANDLED when interrupt is handled else it
12241 * returns IRQ_NONE.
12242 **/
12243irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012244lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012245{
12246 struct lpfc_hba *phba;
12247 uint32_t ha_copy;
12248 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012249 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012250 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012251
12252 /* Get the driver's phba structure from the dev_id and
12253 * assume the HBA is not interrupting.
12254 */
12255 phba = (struct lpfc_hba *) dev_id;
12256
12257 if (unlikely(!phba))
12258 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012259
12260 /*
James Smart93996272008-08-24 21:50:30 -040012261 * Stuff needs to be attented to when this function is invoked as an
12262 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012263 */
James Smart93996272008-08-24 21:50:30 -040012264 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012265 /* Check device state for handling interrupt */
12266 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012267 return IRQ_NONE;
12268 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012269 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12270 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012271 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012272 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012273 /*
12274 * If there is deferred error attention, do not check for
12275 * any interrupt.
12276 */
12277 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012278 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012279 return IRQ_NONE;
12280 }
James Smart93996272008-08-24 21:50:30 -040012281 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12282 phba->HAregaddr);
12283 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012284 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012285 } else
12286 ha_copy = phba->ha_copy;
12287
12288 /*
12289 * Process all events on FCP ring. Take the optimized path for FCP IO.
12290 */
12291 ha_copy &= ~(phba->work_ha_mask);
12292
12293 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012294 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012295 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012296 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012297 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012298
12299 if (phba->cfg_multi_ring_support == 2) {
12300 /*
James Smart93996272008-08-24 21:50:30 -040012301 * Process all events on extra ring. Take the optimized path
12302 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012303 */
James Smart93996272008-08-24 21:50:30 -040012304 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012305 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012306 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012307 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012308 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012309 status);
12310 }
12311 }
dea31012005-04-17 16:05:31 -050012312 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012313} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012314
James Smart93996272008-08-24 21:50:30 -040012315/**
James Smart3772a992009-05-22 14:50:54 -040012316 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012317 * @irq: Interrupt number.
12318 * @dev_id: The device context pointer.
12319 *
James Smart3772a992009-05-22 14:50:54 -040012320 * This function is the HBA device-level interrupt handler to device with
12321 * SLI-3 interface spec, called from the PCI layer when either MSI or
12322 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12323 * requires driver attention. This function invokes the slow-path interrupt
12324 * attention handling function and fast-path interrupt attention handling
12325 * function in turn to process the relevant HBA attention events. This
12326 * function is called without any lock held. It gets the hbalock to access
12327 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012328 *
12329 * This function returns IRQ_HANDLED when interrupt is handled, else it
12330 * returns IRQ_NONE.
12331 **/
12332irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012333lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012334{
12335 struct lpfc_hba *phba;
12336 irqreturn_t sp_irq_rc, fp_irq_rc;
12337 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012338 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012339
12340 /*
12341 * Get the driver's phba structure from the dev_id and
12342 * assume the HBA is not interrupting.
12343 */
12344 phba = (struct lpfc_hba *) dev_id;
12345
12346 if (unlikely(!phba))
12347 return IRQ_NONE;
12348
James Smart3772a992009-05-22 14:50:54 -040012349 /* Check device state for handling interrupt */
12350 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012351 return IRQ_NONE;
12352
12353 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012354 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12355 spin_unlock(&phba->hbalock);
12356 return IRQ_HANDLED;
12357 }
12358
James Smart93996272008-08-24 21:50:30 -040012359 if (unlikely(!phba->ha_copy)) {
12360 spin_unlock(&phba->hbalock);
12361 return IRQ_NONE;
12362 } else if (phba->ha_copy & HA_ERATT) {
12363 if (phba->hba_flag & HBA_ERATT_HANDLED)
12364 /* ERATT polling has handled ERATT */
12365 phba->ha_copy &= ~HA_ERATT;
12366 else
12367 /* Indicate interrupt handler handles ERATT */
12368 phba->hba_flag |= HBA_ERATT_HANDLED;
12369 }
12370
James Smarta257bf92009-04-06 18:48:10 -040012371 /*
12372 * If there is deferred error attention, do not check for any interrupt.
12373 */
12374 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012375 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012376 return IRQ_NONE;
12377 }
12378
James Smart93996272008-08-24 21:50:30 -040012379 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012380 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12381 spin_unlock(&phba->hbalock);
12382 return IRQ_HANDLED;
12383 }
James Smarta747c9c2009-11-18 15:41:10 -050012384 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12385 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12386 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012387 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012388 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012389 readl(phba->HAregaddr); /* flush */
12390 spin_unlock(&phba->hbalock);
12391
12392 /*
12393 * Invokes slow-path host attention interrupt handling as appropriate.
12394 */
12395
12396 /* status of events with mailbox and link attention */
12397 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12398
12399 /* status of events with ELS ring */
12400 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12401 status2 >>= (4*LPFC_ELS_RING);
12402
12403 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012404 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012405 else
12406 sp_irq_rc = IRQ_NONE;
12407
12408 /*
12409 * Invoke fast-path host attention interrupt handling as appropriate.
12410 */
12411
12412 /* status of events with FCP ring */
12413 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12414 status1 >>= (4*LPFC_FCP_RING);
12415
12416 /* status of events with extra ring */
12417 if (phba->cfg_multi_ring_support == 2) {
12418 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12419 status2 >>= (4*LPFC_EXTRA_RING);
12420 } else
12421 status2 = 0;
12422
12423 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012424 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012425 else
12426 fp_irq_rc = IRQ_NONE;
12427
12428 /* Return device-level interrupt handling status */
12429 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012430} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012431
12432/**
12433 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12434 * @phba: pointer to lpfc hba data structure.
12435 *
12436 * This routine is invoked by the worker thread to process all the pending
12437 * SLI4 FCP abort XRI events.
12438 **/
12439void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12440{
12441 struct lpfc_cq_event *cq_event;
12442
12443 /* First, declare the fcp xri abort event has been handled */
12444 spin_lock_irq(&phba->hbalock);
12445 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12446 spin_unlock_irq(&phba->hbalock);
12447 /* Now, handle all the fcp xri abort events */
12448 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12449 /* Get the first event from the head of the event queue */
12450 spin_lock_irq(&phba->hbalock);
12451 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12452 cq_event, struct lpfc_cq_event, list);
12453 spin_unlock_irq(&phba->hbalock);
12454 /* Notify aborted XRI for FCP work queue */
12455 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12456 /* Free the event processed back to the free pool */
12457 lpfc_sli4_cq_event_release(phba, cq_event);
12458 }
12459}
12460
12461/**
12462 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12463 * @phba: pointer to lpfc hba data structure.
12464 *
12465 * This routine is invoked by the worker thread to process all the pending
12466 * SLI4 els abort xri events.
12467 **/
12468void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12469{
12470 struct lpfc_cq_event *cq_event;
12471
12472 /* First, declare the els xri abort event has been handled */
12473 spin_lock_irq(&phba->hbalock);
12474 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12475 spin_unlock_irq(&phba->hbalock);
12476 /* Now, handle all the els xri abort events */
12477 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12478 /* Get the first event from the head of the event queue */
12479 spin_lock_irq(&phba->hbalock);
12480 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12481 cq_event, struct lpfc_cq_event, list);
12482 spin_unlock_irq(&phba->hbalock);
12483 /* Notify aborted XRI for ELS work queue */
12484 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12485 /* Free the event processed back to the free pool */
12486 lpfc_sli4_cq_event_release(phba, cq_event);
12487 }
12488}
12489
James Smart341af102010-01-26 23:07:37 -050012490/**
12491 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12492 * @phba: pointer to lpfc hba data structure
12493 * @pIocbIn: pointer to the rspiocbq
12494 * @pIocbOut: pointer to the cmdiocbq
12495 * @wcqe: pointer to the complete wcqe
12496 *
12497 * This routine transfers the fields of a command iocbq to a response iocbq
12498 * by copying all the IOCB fields from command iocbq and transferring the
12499 * completion status information from the complete wcqe.
12500 **/
James Smart4f774512009-05-22 14:52:35 -040012501static void
James Smart341af102010-01-26 23:07:37 -050012502lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12503 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012504 struct lpfc_iocbq *pIocbOut,
12505 struct lpfc_wcqe_complete *wcqe)
12506{
James Smartaf227412013-10-10 12:23:10 -040012507 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012508 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012509 uint32_t status, max_response;
12510 struct lpfc_dmabuf *dmabuf;
12511 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012512 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12513
12514 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12515 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012516 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012517 status = bf_get(lpfc_wcqe_c_status, wcqe);
12518 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012519 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12520 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12521 pIocbIn->iocb.un.fcpi.fcpi_parm =
12522 pIocbOut->iocb.un.fcpi.fcpi_parm -
12523 wcqe->total_data_placed;
12524 else
12525 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012526 else {
James Smart4f774512009-05-22 14:52:35 -040012527 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012528 switch (pIocbOut->iocb.ulpCommand) {
12529 case CMD_ELS_REQUEST64_CR:
12530 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12531 bpl = (struct ulp_bde64 *)dmabuf->virt;
12532 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12533 max_response = bde.tus.f.bdeSize;
12534 break;
12535 case CMD_GEN_REQUEST64_CR:
12536 max_response = 0;
12537 if (!pIocbOut->context3)
12538 break;
12539 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12540 sizeof(struct ulp_bde64);
12541 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12542 bpl = (struct ulp_bde64 *)dmabuf->virt;
12543 for (i = 0; i < numBdes; i++) {
12544 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12545 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12546 max_response += bde.tus.f.bdeSize;
12547 }
12548 break;
12549 default:
12550 max_response = wcqe->total_data_placed;
12551 break;
12552 }
12553 if (max_response < wcqe->total_data_placed)
12554 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12555 else
12556 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12557 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012558 }
James Smart341af102010-01-26 23:07:37 -050012559
James Smartacd68592012-01-18 16:25:09 -050012560 /* Convert BG errors for completion status */
12561 if (status == CQE_STATUS_DI_ERROR) {
12562 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12563
12564 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12565 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12566 else
12567 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12568
12569 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12570 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12571 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12572 BGS_GUARD_ERR_MASK;
12573 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12574 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12575 BGS_APPTAG_ERR_MASK;
12576 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12577 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12578 BGS_REFTAG_ERR_MASK;
12579
12580 /* Check to see if there was any good data before the error */
12581 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12582 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12583 BGS_HI_WATER_MARK_PRESENT_MASK;
12584 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12585 wcqe->total_data_placed;
12586 }
12587
12588 /*
12589 * Set ALL the error bits to indicate we don't know what
12590 * type of error it is.
12591 */
12592 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12593 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12594 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12595 BGS_GUARD_ERR_MASK);
12596 }
12597
James Smart341af102010-01-26 23:07:37 -050012598 /* Pick up HBA exchange busy condition */
12599 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12600 spin_lock_irqsave(&phba->hbalock, iflags);
12601 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12602 spin_unlock_irqrestore(&phba->hbalock, iflags);
12603 }
James Smart4f774512009-05-22 14:52:35 -040012604}
12605
12606/**
James Smart45ed1192009-10-02 15:17:02 -040012607 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12608 * @phba: Pointer to HBA context object.
12609 * @wcqe: Pointer to work-queue completion queue entry.
12610 *
12611 * This routine handles an ELS work-queue completion event and construct
12612 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12613 * discovery engine to handle.
12614 *
12615 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12616 **/
12617static struct lpfc_iocbq *
12618lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12619 struct lpfc_iocbq *irspiocbq)
12620{
James Smart895427b2017-02-12 13:52:30 -080012621 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012622 struct lpfc_iocbq *cmdiocbq;
12623 struct lpfc_wcqe_complete *wcqe;
12624 unsigned long iflags;
12625
James Smart895427b2017-02-12 13:52:30 -080012626 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012627 if (unlikely(!pring))
12628 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012629
James Smart45ed1192009-10-02 15:17:02 -040012630 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012631 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012632 pring->stats.iocb_event++;
12633 /* Look up the ELS command IOCB and create pseudo response IOCB */
12634 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12635 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012636 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012637 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012638 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12639 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012640 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12641 wcqe->word0, wcqe->total_data_placed,
12642 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012643 lpfc_sli_release_iocbq(phba, irspiocbq);
12644 return NULL;
12645 }
12646
Dick Kennedy401bb412017-09-29 17:34:28 -070012647 /* Put the iocb back on the txcmplq */
12648 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12649 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12650
James Smart45ed1192009-10-02 15:17:02 -040012651 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012652 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012653
12654 return irspiocbq;
12655}
12656
James Smart8a5ca102017-11-20 16:00:30 -080012657inline struct lpfc_cq_event *
12658lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12659{
12660 struct lpfc_cq_event *cq_event;
12661
12662 /* Allocate a new internal CQ_EVENT entry */
12663 cq_event = lpfc_sli4_cq_event_alloc(phba);
12664 if (!cq_event) {
12665 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12666 "0602 Failed to alloc CQ_EVENT entry\n");
12667 return NULL;
12668 }
12669
12670 /* Move the CQE into the event */
12671 memcpy(&cq_event->cqe, entry, size);
12672 return cq_event;
12673}
12674
James Smart45ed1192009-10-02 15:17:02 -040012675/**
James Smart04c68492009-05-22 14:52:52 -040012676 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12677 * @phba: Pointer to HBA context object.
12678 * @cqe: Pointer to mailbox completion queue entry.
12679 *
12680 * This routine process a mailbox completion queue entry with asynchrous
12681 * event.
12682 *
12683 * Return: true if work posted to worker thread, otherwise false.
12684 **/
12685static bool
12686lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12687{
12688 struct lpfc_cq_event *cq_event;
12689 unsigned long iflags;
12690
12691 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12692 "0392 Async Event: word0:x%x, word1:x%x, "
12693 "word2:x%x, word3:x%x\n", mcqe->word0,
12694 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12695
James Smart8a5ca102017-11-20 16:00:30 -080012696 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12697 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040012698 return false;
James Smart04c68492009-05-22 14:52:52 -040012699 spin_lock_irqsave(&phba->hbalock, iflags);
12700 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12701 /* Set the async event flag */
12702 phba->hba_flag |= ASYNC_EVENT;
12703 spin_unlock_irqrestore(&phba->hbalock, iflags);
12704
12705 return true;
12706}
12707
12708/**
12709 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12710 * @phba: Pointer to HBA context object.
12711 * @cqe: Pointer to mailbox completion queue entry.
12712 *
12713 * This routine process a mailbox completion queue entry with mailbox
12714 * completion event.
12715 *
12716 * Return: true if work posted to worker thread, otherwise false.
12717 **/
12718static bool
12719lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12720{
12721 uint32_t mcqe_status;
12722 MAILBOX_t *mbox, *pmbox;
12723 struct lpfc_mqe *mqe;
12724 struct lpfc_vport *vport;
12725 struct lpfc_nodelist *ndlp;
12726 struct lpfc_dmabuf *mp;
12727 unsigned long iflags;
12728 LPFC_MBOXQ_t *pmb;
12729 bool workposted = false;
12730 int rc;
12731
12732 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12733 if (!bf_get(lpfc_trailer_completed, mcqe))
12734 goto out_no_mqe_complete;
12735
12736 /* Get the reference to the active mbox command */
12737 spin_lock_irqsave(&phba->hbalock, iflags);
12738 pmb = phba->sli.mbox_active;
12739 if (unlikely(!pmb)) {
12740 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12741 "1832 No pending MBOX command to handle\n");
12742 spin_unlock_irqrestore(&phba->hbalock, iflags);
12743 goto out_no_mqe_complete;
12744 }
12745 spin_unlock_irqrestore(&phba->hbalock, iflags);
12746 mqe = &pmb->u.mqe;
12747 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12748 mbox = phba->mbox;
12749 vport = pmb->vport;
12750
12751 /* Reset heartbeat timer */
12752 phba->last_completion_time = jiffies;
12753 del_timer(&phba->sli.mbox_tmo);
12754
12755 /* Move mbox data to caller's mailbox region, do endian swapping */
12756 if (pmb->mbox_cmpl && mbox)
12757 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012758
James Smart73d91e52011-10-10 21:32:10 -040012759 /*
12760 * For mcqe errors, conditionally move a modified error code to
12761 * the mbox so that the error will not be missed.
12762 */
12763 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12764 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12765 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12766 bf_set(lpfc_mqe_status, mqe,
12767 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12768 }
James Smart04c68492009-05-22 14:52:52 -040012769 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12770 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12771 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12772 "MBOX dflt rpi: status:x%x rpi:x%x",
12773 mcqe_status,
12774 pmbox->un.varWords[0], 0);
12775 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12776 mp = (struct lpfc_dmabuf *)(pmb->context1);
12777 ndlp = (struct lpfc_nodelist *)pmb->context2;
12778 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12779 * RID of the PPI using the same mbox buffer.
12780 */
12781 lpfc_unreg_login(phba, vport->vpi,
12782 pmbox->un.varWords[0], pmb);
12783 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12784 pmb->context1 = mp;
12785 pmb->context2 = ndlp;
12786 pmb->vport = vport;
12787 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12788 if (rc != MBX_BUSY)
12789 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12790 LOG_SLI, "0385 rc should "
12791 "have been MBX_BUSY\n");
12792 if (rc != MBX_NOT_FINISHED)
12793 goto send_current_mbox;
12794 }
12795 }
12796 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12797 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12798 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12799
12800 /* There is mailbox completion work to do */
12801 spin_lock_irqsave(&phba->hbalock, iflags);
12802 __lpfc_mbox_cmpl_put(phba, pmb);
12803 phba->work_ha |= HA_MBATT;
12804 spin_unlock_irqrestore(&phba->hbalock, iflags);
12805 workposted = true;
12806
12807send_current_mbox:
12808 spin_lock_irqsave(&phba->hbalock, iflags);
12809 /* Release the mailbox command posting token */
12810 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12811 /* Setting active mailbox pointer need to be in sync to flag clear */
12812 phba->sli.mbox_active = NULL;
12813 spin_unlock_irqrestore(&phba->hbalock, iflags);
12814 /* Wake up worker thread to post the next pending mailbox command */
12815 lpfc_worker_wake_up(phba);
12816out_no_mqe_complete:
12817 if (bf_get(lpfc_trailer_consumed, mcqe))
12818 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12819 return workposted;
12820}
12821
12822/**
12823 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12824 * @phba: Pointer to HBA context object.
12825 * @cqe: Pointer to mailbox completion queue entry.
12826 *
12827 * This routine process a mailbox completion queue entry, it invokes the
12828 * proper mailbox complete handling or asynchrous event handling routine
12829 * according to the MCQE's async bit.
12830 *
12831 * Return: true if work posted to worker thread, otherwise false.
12832 **/
12833static bool
12834lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12835{
12836 struct lpfc_mcqe mcqe;
12837 bool workposted;
12838
12839 /* Copy the mailbox MCQE and convert endian order as needed */
12840 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12841
12842 /* Invoke the proper event handling routine */
12843 if (!bf_get(lpfc_trailer_async, &mcqe))
12844 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12845 else
12846 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12847 return workposted;
12848}
12849
12850/**
James Smart4f774512009-05-22 14:52:35 -040012851 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12852 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012853 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012854 * @wcqe: Pointer to work-queue completion queue entry.
12855 *
12856 * This routine handles an ELS work-queue completion event.
12857 *
12858 * Return: true if work posted to worker thread, otherwise false.
12859 **/
12860static bool
James Smart2a76a282012-08-03 12:35:54 -040012861lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012862 struct lpfc_wcqe_complete *wcqe)
12863{
James Smart4f774512009-05-22 14:52:35 -040012864 struct lpfc_iocbq *irspiocbq;
12865 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012866 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012867 int txq_cnt = 0;
12868 int txcmplq_cnt = 0;
12869 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012870
James Smart45ed1192009-10-02 15:17:02 -040012871 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012872 irspiocbq = lpfc_sli_get_iocbq(phba);
12873 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012874 if (!list_empty(&pring->txq))
12875 txq_cnt++;
12876 if (!list_empty(&pring->txcmplq))
12877 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012879 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12880 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012881 txq_cnt, phba->iocb_cnt,
12882 fcp_txcmplq_cnt,
12883 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012884 return false;
James Smart4f774512009-05-22 14:52:35 -040012885 }
James Smart4f774512009-05-22 14:52:35 -040012886
James Smart45ed1192009-10-02 15:17:02 -040012887 /* Save off the slow-path queue event for work thread to process */
12888 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012889 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012890 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012891 &phba->sli4_hba.sp_queue_event);
12892 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012893 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012894
James Smart45ed1192009-10-02 15:17:02 -040012895 return true;
James Smart4f774512009-05-22 14:52:35 -040012896}
12897
12898/**
12899 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12900 * @phba: Pointer to HBA context object.
12901 * @wcqe: Pointer to work-queue completion queue entry.
12902 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012903 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012904 * proper WQ release routine to the slow-path WQ.
12905 **/
12906static void
12907lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12908 struct lpfc_wcqe_release *wcqe)
12909{
James Smart2e90f4b2011-12-13 13:22:37 -050012910 /* sanity check on queue memory */
12911 if (unlikely(!phba->sli4_hba.els_wq))
12912 return;
James Smart4f774512009-05-22 14:52:35 -040012913 /* Check for the slow-path ELS work queue */
12914 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12915 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12916 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12917 else
12918 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12919 "2579 Slow-path wqe consume event carries "
12920 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12921 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12922 phba->sli4_hba.els_wq->queue_id);
12923}
12924
12925/**
12926 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12927 * @phba: Pointer to HBA context object.
12928 * @cq: Pointer to a WQ completion queue.
12929 * @wcqe: Pointer to work-queue completion queue entry.
12930 *
12931 * This routine handles an XRI abort event.
12932 *
12933 * Return: true if work posted to worker thread, otherwise false.
12934 **/
12935static bool
12936lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12937 struct lpfc_queue *cq,
12938 struct sli4_wcqe_xri_aborted *wcqe)
12939{
12940 bool workposted = false;
12941 struct lpfc_cq_event *cq_event;
12942 unsigned long iflags;
12943
James Smart4f774512009-05-22 14:52:35 -040012944 switch (cq->subtype) {
12945 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080012946 cq_event = lpfc_cq_event_setup(
12947 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12948 if (!cq_event)
12949 return false;
James Smart4f774512009-05-22 14:52:35 -040012950 spin_lock_irqsave(&phba->hbalock, iflags);
12951 list_add_tail(&cq_event->list,
12952 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12953 /* Set the fcp xri abort event flag */
12954 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12955 spin_unlock_irqrestore(&phba->hbalock, iflags);
12956 workposted = true;
12957 break;
James Smart422c4cb2017-11-20 16:00:32 -080012958 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040012959 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080012960 cq_event = lpfc_cq_event_setup(
12961 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12962 if (!cq_event)
12963 return false;
James Smart4f774512009-05-22 14:52:35 -040012964 spin_lock_irqsave(&phba->hbalock, iflags);
12965 list_add_tail(&cq_event->list,
12966 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12967 /* Set the els xri abort event flag */
12968 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12969 spin_unlock_irqrestore(&phba->hbalock, iflags);
12970 workposted = true;
12971 break;
James Smart318083a2017-03-04 09:30:30 -080012972 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080012973 /* Notify aborted XRI for NVME work queue */
12974 if (phba->nvmet_support)
12975 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
12976 else
12977 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
12978
12979 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080012980 break;
James Smart4f774512009-05-22 14:52:35 -040012981 default:
12982 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080012983 "0603 Invalid CQ subtype %d: "
12984 "%08x %08x %08x %08x\n",
12985 cq->subtype, wcqe->word0, wcqe->parameter,
12986 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040012987 workposted = false;
12988 break;
12989 }
12990 return workposted;
12991}
12992
12993/**
James Smart4d9ab992009-10-02 15:16:39 -040012994 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012995 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012996 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012997 *
James Smart4d9ab992009-10-02 15:16:39 -040012998 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012999 *
13000 * Return: true if work posted to worker thread, otherwise false.
13001 **/
13002static bool
James Smart4d9ab992009-10-02 15:16:39 -040013003lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
13004{
13005 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080013006 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040013007 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
13008 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070013009 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040013010 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040013011 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040013012 unsigned long iflags;
13013
James Smart2e90f4b2011-12-13 13:22:37 -050013014 /* sanity check on queue memory */
13015 if (unlikely(!hrq) || unlikely(!drq))
13016 return workposted;
13017
James Smart7851fe22011-07-22 18:36:52 -040013018 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13019 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13020 else
13021 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13022 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040013023 goto out;
13024
13025 status = bf_get(lpfc_rcqe_status, rcqe);
13026 switch (status) {
13027 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13028 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13029 "2537 Receive Frame Truncated!!\n");
13030 case FC_STATUS_RQ_SUCCESS:
13031 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013032 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040013033 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
13034 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040013035 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040013036 spin_unlock_irqrestore(&phba->hbalock, iflags);
13037 goto out;
13038 }
James Smartb84daac2012-08-03 12:35:13 -040013039 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013040 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040013041 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080013042
13043 /* If a NVME LS event (type 0x28), treat it as Fast path */
13044 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13045
James Smart4d9ab992009-10-02 15:16:39 -040013046 /* save off the frame for the word thread to process */
13047 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040013048 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040013049 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040013050 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040013051 spin_unlock_irqrestore(&phba->hbalock, iflags);
13052 workposted = true;
13053 break;
James Smart4d9ab992009-10-02 15:16:39 -040013054 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013055 if (phba->nvmet_support) {
13056 tgtp = phba->targetport->private;
13057 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13058 "6402 RQE Error x%x, posted %d err_cnt "
13059 "%d: %x %x %x\n",
13060 status, hrq->RQ_buf_posted,
13061 hrq->RQ_no_posted_buf,
13062 atomic_read(&tgtp->rcv_fcp_cmd_in),
13063 atomic_read(&tgtp->rcv_fcp_cmd_out),
13064 atomic_read(&tgtp->xmt_fcp_release));
13065 }
13066 /* fallthrough */
13067
13068 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040013069 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040013070 /* Post more buffers if possible */
13071 spin_lock_irqsave(&phba->hbalock, iflags);
13072 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
13073 spin_unlock_irqrestore(&phba->hbalock, iflags);
13074 workposted = true;
13075 break;
13076 }
13077out:
13078 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040013079}
13080
13081/**
13082 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
13083 * @phba: Pointer to HBA context object.
13084 * @cq: Pointer to the completion queue.
13085 * @wcqe: Pointer to a completion queue entry.
13086 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030013087 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040013088 * entry.
13089 *
13090 * Return: true if work posted to worker thread, otherwise false.
13091 **/
13092static bool
13093lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013094 struct lpfc_cqe *cqe)
13095{
James Smart45ed1192009-10-02 15:17:02 -040013096 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040013097 bool workposted = false;
13098
13099 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040013100 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040013101
13102 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040013103 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040013104 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040013105 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040013106 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040013107 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013108 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013109 break;
13110 case CQE_CODE_RELEASE_WQE:
13111 /* Process the WQ release event */
13112 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013113 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013114 break;
13115 case CQE_CODE_XRI_ABORTED:
13116 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013117 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013118 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013119 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013120 break;
James Smart4d9ab992009-10-02 15:16:39 -040013121 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013122 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013123 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013124 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013125 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013126 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013127 break;
James Smart4f774512009-05-22 14:52:35 -040013128 default:
13129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13130 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013131 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013132 break;
13133 }
13134 return workposted;
13135}
13136
13137/**
James Smart4f774512009-05-22 14:52:35 -040013138 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13139 * @phba: Pointer to HBA context object.
13140 * @eqe: Pointer to fast-path event queue entry.
13141 *
13142 * This routine process a event queue entry from the slow-path event queue.
13143 * It will check the MajorCode and MinorCode to determine this is for a
13144 * completion event on a completion queue, if not, an error shall be logged
13145 * and just return. Otherwise, it will get to the corresponding completion
13146 * queue and process all the entries on that completion queue, rearm the
13147 * completion queue, and then return.
13148 *
13149 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013150static void
James Smart67d12732012-08-03 12:36:13 -040013151lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13152 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013153{
James Smart67d12732012-08-03 12:36:13 -040013154 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013155 uint16_t cqid;
13156
James Smart4f774512009-05-22 14:52:35 -040013157 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013158 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013159
James Smart4f774512009-05-22 14:52:35 -040013160 list_for_each_entry(childq, &speq->child_list, list) {
13161 if (childq->queue_id == cqid) {
13162 cq = childq;
13163 break;
13164 }
13165 }
13166 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013167 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13168 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13169 "0365 Slow-path CQ identifier "
13170 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013171 return;
James Smart4f774512009-05-22 14:52:35 -040013172 }
13173
James Smart895427b2017-02-12 13:52:30 -080013174 /* Save EQ associated with this CQ */
13175 cq->assoc_qp = speq;
13176
Dick Kennedyf485c182017-09-29 17:34:34 -070013177 if (!queue_work(phba->wq, &cq->spwork))
13178 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13179 "0390 Cannot schedule soft IRQ "
13180 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13181 cqid, cq->queue_id, smp_processor_id());
13182}
13183
13184/**
13185 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13186 * @phba: Pointer to HBA context object.
13187 *
13188 * This routine process a event queue entry from the slow-path event queue.
13189 * It will check the MajorCode and MinorCode to determine this is for a
13190 * completion event on a completion queue, if not, an error shall be logged
13191 * and just return. Otherwise, it will get to the corresponding completion
13192 * queue and process all the entries on that completion queue, rearm the
13193 * completion queue, and then return.
13194 *
13195 **/
13196static void
13197lpfc_sli4_sp_process_cq(struct work_struct *work)
13198{
13199 struct lpfc_queue *cq =
13200 container_of(work, struct lpfc_queue, spwork);
13201 struct lpfc_hba *phba = cq->phba;
13202 struct lpfc_cqe *cqe;
13203 bool workposted = false;
13204 int ccount = 0;
13205
James Smart4f774512009-05-22 14:52:35 -040013206 /* Process all the entries to the CQ */
13207 switch (cq->type) {
13208 case LPFC_MCQ:
13209 while ((cqe = lpfc_sli4_cq_get(cq))) {
13210 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013211 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013212 break;
James Smartb84daac2012-08-03 12:35:13 -040013213 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013214 }
13215 break;
13216 case LPFC_WCQ:
13217 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013218 if (cq->subtype == LPFC_FCP ||
13219 cq->subtype == LPFC_NVME) {
13220#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13221 if (phba->ktime_on)
13222 cq->isr_timestamp = ktime_get_ns();
13223 else
13224 cq->isr_timestamp = 0;
13225#endif
James Smart895427b2017-02-12 13:52:30 -080013226 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013227 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013228 } else {
James Smart05580562011-05-24 11:40:48 -040013229 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13230 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013231 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013232 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013233 break;
James Smart4f774512009-05-22 14:52:35 -040013234 }
James Smartb84daac2012-08-03 12:35:13 -040013235
13236 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013237 if (ccount > cq->CQ_max_cqe)
13238 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013239 break;
13240 default:
13241 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13242 "0370 Invalid completion queue type (%d)\n",
13243 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013244 return;
James Smart4f774512009-05-22 14:52:35 -040013245 }
13246
13247 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013248 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013249 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13250 "0371 No entry from the CQ: identifier "
13251 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13252
13253 /* In any case, flash and re-arm the RCQ */
James Smartb71413d2018-02-22 08:18:40 -080013254 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013255
13256 /* wake up worker thread if there are works to be done */
13257 if (workposted)
13258 lpfc_worker_wake_up(phba);
13259}
13260
13261/**
13262 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013263 * @phba: Pointer to HBA context object.
13264 * @cq: Pointer to associated CQ
13265 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013266 *
13267 * This routine process a fast-path work queue completion entry from fast-path
13268 * event queue for FCP command response completion.
13269 **/
13270static void
James Smart2a76a282012-08-03 12:35:54 -040013271lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013272 struct lpfc_wcqe_complete *wcqe)
13273{
James Smart2a76a282012-08-03 12:35:54 -040013274 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013275 struct lpfc_iocbq *cmdiocbq;
13276 struct lpfc_iocbq irspiocbq;
13277 unsigned long iflags;
13278
James Smart4f774512009-05-22 14:52:35 -040013279 /* Check for response status */
13280 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13281 /* If resource errors reported from HBA, reduce queue
13282 * depth of the SCSI device.
13283 */
James Smarte3d2b802012-08-14 14:25:43 -040013284 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13285 IOSTAT_LOCAL_REJECT)) &&
13286 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13287 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013288 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013289
James Smart4f774512009-05-22 14:52:35 -040013290 /* Log the error status */
13291 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13292 "0373 FCP complete error: status=x%x, "
13293 "hw_status=x%x, total_data_specified=%d, "
13294 "parameter=x%x, word3=x%x\n",
13295 bf_get(lpfc_wcqe_c_status, wcqe),
13296 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13297 wcqe->total_data_placed, wcqe->parameter,
13298 wcqe->word3);
13299 }
13300
13301 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013302 spin_lock_irqsave(&pring->ring_lock, iflags);
13303 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013304 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13305 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013306 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013307 if (unlikely(!cmdiocbq)) {
13308 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13309 "0374 FCP complete with no corresponding "
13310 "cmdiocb: iotag (%d)\n",
13311 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13312 return;
13313 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013314#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13315 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13316#endif
James Smart895427b2017-02-12 13:52:30 -080013317 if (cmdiocbq->iocb_cmpl == NULL) {
13318 if (cmdiocbq->wqe_cmpl) {
13319 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13320 spin_lock_irqsave(&phba->hbalock, iflags);
13321 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13322 spin_unlock_irqrestore(&phba->hbalock, iflags);
13323 }
13324
13325 /* Pass the cmd_iocb and the wcqe to the upper layer */
13326 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13327 return;
13328 }
James Smart4f774512009-05-22 14:52:35 -040013329 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13330 "0375 FCP cmdiocb not callback function "
13331 "iotag: (%d)\n",
13332 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13333 return;
13334 }
13335
13336 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013337 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013338
James Smart0f65ff62010-02-26 14:14:23 -050013339 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13340 spin_lock_irqsave(&phba->hbalock, iflags);
13341 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13342 spin_unlock_irqrestore(&phba->hbalock, iflags);
13343 }
13344
James Smart4f774512009-05-22 14:52:35 -040013345 /* Pass the cmd_iocb and the rsp state to the upper layer */
13346 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13347}
13348
13349/**
13350 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13351 * @phba: Pointer to HBA context object.
13352 * @cq: Pointer to completion queue.
13353 * @wcqe: Pointer to work-queue completion queue entry.
13354 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013355 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013356 * proper WQ release routine to the slow-path WQ.
13357 **/
13358static void
13359lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13360 struct lpfc_wcqe_release *wcqe)
13361{
13362 struct lpfc_queue *childwq;
13363 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013364 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013365
13366 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013367 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013368 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013369 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013370 lpfc_sli4_wq_release(childwq,
13371 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013372 if (childwq->q_flag & HBA_NVMET_WQFULL)
13373 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013374 wqid_matched = true;
13375 break;
13376 }
13377 }
13378 /* Report warning log message if no match found */
13379 if (wqid_matched != true)
13380 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13381 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013382 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013383}
13384
13385/**
James Smart2d7dbc42017-02-12 13:52:35 -080013386 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13387 * @phba: Pointer to HBA context object.
13388 * @rcqe: Pointer to receive-queue completion queue entry.
13389 *
13390 * This routine process a receive-queue completion queue entry.
13391 *
13392 * Return: true if work posted to worker thread, otherwise false.
13393 **/
13394static bool
13395lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13396 struct lpfc_rcqe *rcqe)
13397{
13398 bool workposted = false;
13399 struct lpfc_queue *hrq;
13400 struct lpfc_queue *drq;
13401 struct rqb_dmabuf *dma_buf;
13402 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013403 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013404 uint32_t status, rq_id;
13405 unsigned long iflags;
13406 uint32_t fctl, idx;
13407
13408 if ((phba->nvmet_support == 0) ||
13409 (phba->sli4_hba.nvmet_cqset == NULL))
13410 return workposted;
13411
13412 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13413 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13414 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13415
13416 /* sanity check on queue memory */
13417 if (unlikely(!hrq) || unlikely(!drq))
13418 return workposted;
13419
13420 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13421 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13422 else
13423 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13424
13425 if ((phba->nvmet_support == 0) ||
13426 (rq_id != hrq->queue_id))
13427 return workposted;
13428
13429 status = bf_get(lpfc_rcqe_status, rcqe);
13430 switch (status) {
13431 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13433 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013434 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013435 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013436 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013437 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013438 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13439 if (!dma_buf) {
13440 hrq->RQ_no_buf_found++;
13441 spin_unlock_irqrestore(&phba->hbalock, iflags);
13442 goto out;
13443 }
13444 spin_unlock_irqrestore(&phba->hbalock, iflags);
13445 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013446 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013447 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13448
13449 /* Just some basic sanity checks on FCP Command frame */
13450 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13451 fc_hdr->fh_f_ctl[1] << 8 |
13452 fc_hdr->fh_f_ctl[2]);
13453 if (((fctl &
13454 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13455 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13456 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13457 goto drop;
13458
13459 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13460 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013461 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013462 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013463 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013464 return false;
13465 }
13466drop:
13467 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13468 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013469 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013470 if (phba->nvmet_support) {
13471 tgtp = phba->targetport->private;
13472 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13473 "6401 RQE Error x%x, posted %d err_cnt "
13474 "%d: %x %x %x\n",
13475 status, hrq->RQ_buf_posted,
13476 hrq->RQ_no_posted_buf,
13477 atomic_read(&tgtp->rcv_fcp_cmd_in),
13478 atomic_read(&tgtp->rcv_fcp_cmd_out),
13479 atomic_read(&tgtp->xmt_fcp_release));
13480 }
13481 /* fallthrough */
13482
13483 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013484 hrq->RQ_no_posted_buf++;
13485 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013486 break;
13487 }
13488out:
13489 return workposted;
13490}
13491
13492/**
James Smart895427b2017-02-12 13:52:30 -080013493 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013494 * @cq: Pointer to the completion queue.
13495 * @eqe: Pointer to fast-path completion queue entry.
13496 *
13497 * This routine process a fast-path work queue completion entry from fast-path
13498 * event queue for FCP command response completion.
13499 **/
13500static int
James Smart895427b2017-02-12 13:52:30 -080013501lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013502 struct lpfc_cqe *cqe)
13503{
13504 struct lpfc_wcqe_release wcqe;
13505 bool workposted = false;
13506
13507 /* Copy the work queue CQE and convert endian order if needed */
13508 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13509
13510 /* Check and process for different type of WCQE and dispatch */
13511 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13512 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013513 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013514 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013515 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013516 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013517 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13518 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13519 (struct lpfc_wcqe_complete *)&wcqe);
13520 if (cq->subtype == LPFC_NVME_LS)
13521 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013522 (struct lpfc_wcqe_complete *)&wcqe);
13523 break;
13524 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013525 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013526 /* Process the WQ release event */
13527 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13528 (struct lpfc_wcqe_release *)&wcqe);
13529 break;
13530 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013531 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013532 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013533 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013534 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13535 (struct sli4_wcqe_xri_aborted *)&wcqe);
13536 break;
James Smart895427b2017-02-12 13:52:30 -080013537 case CQE_CODE_RECEIVE_V1:
13538 case CQE_CODE_RECEIVE:
13539 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013540 if (cq->subtype == LPFC_NVMET) {
13541 workposted = lpfc_sli4_nvmet_handle_rcqe(
13542 phba, cq, (struct lpfc_rcqe *)&wcqe);
13543 }
James Smart895427b2017-02-12 13:52:30 -080013544 break;
James Smart4f774512009-05-22 14:52:35 -040013545 default:
13546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013547 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013548 bf_get(lpfc_wcqe_c_code, &wcqe));
13549 break;
13550 }
13551 return workposted;
13552}
13553
13554/**
James Smart67d12732012-08-03 12:36:13 -040013555 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013556 * @phba: Pointer to HBA context object.
13557 * @eqe: Pointer to fast-path event queue entry.
13558 *
13559 * This routine process a event queue entry from the fast-path event queue.
13560 * It will check the MajorCode and MinorCode to determine this is for a
13561 * completion event on a completion queue, if not, an error shall be logged
13562 * and just return. Otherwise, it will get to the corresponding completion
13563 * queue and process all the entries on the completion queue, rearm the
13564 * completion queue, and then return.
13565 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013566static void
James Smart67d12732012-08-03 12:36:13 -040013567lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13568 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013569{
James Smart895427b2017-02-12 13:52:30 -080013570 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013571 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013572
James Smartcb5172e2010-03-15 11:25:07 -040013573 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013574 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013575 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013576 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013577 bf_get_le32(lpfc_eqe_major_code, eqe),
13578 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013579 return;
James Smart4f774512009-05-22 14:52:35 -040013580 }
13581
James Smart67d12732012-08-03 12:36:13 -040013582 /* Get the reference to the corresponding CQ */
13583 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13584
James Smart2d7dbc42017-02-12 13:52:35 -080013585 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13586 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13587 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13588 /* Process NVMET unsol rcv */
13589 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13590 goto process_cq;
13591 }
13592 }
13593
James Smart895427b2017-02-12 13:52:30 -080013594 if (phba->sli4_hba.nvme_cq_map &&
13595 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013596 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013597 cq = phba->sli4_hba.nvme_cq[qidx];
13598 goto process_cq;
13599 }
13600
13601 if (phba->sli4_hba.fcp_cq_map &&
13602 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13603 /* Process FCP command completion */
13604 cq = phba->sli4_hba.fcp_cq[qidx];
13605 goto process_cq;
13606 }
13607
13608 if (phba->sli4_hba.nvmels_cq &&
13609 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13610 /* Process NVME unsol rcv */
13611 cq = phba->sli4_hba.nvmels_cq;
13612 }
13613
13614 /* Otherwise this is a Slow path event */
13615 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013616 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13617 return;
James Smart67d12732012-08-03 12:36:13 -040013618 }
13619
James Smart895427b2017-02-12 13:52:30 -080013620process_cq:
James Smart4f774512009-05-22 14:52:35 -040013621 if (unlikely(cqid != cq->queue_id)) {
13622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13623 "0368 Miss-matched fast-path completion "
13624 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13625 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070013626 return;
James Smart4f774512009-05-22 14:52:35 -040013627 }
13628
James Smart895427b2017-02-12 13:52:30 -080013629 /* Save EQ associated with this CQ */
13630 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13631
Dick Kennedyf485c182017-09-29 17:34:34 -070013632 if (!queue_work(phba->wq, &cq->irqwork))
13633 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13634 "0363 Cannot schedule soft IRQ "
13635 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13636 cqid, cq->queue_id, smp_processor_id());
13637}
13638
13639/**
13640 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13641 * @phba: Pointer to HBA context object.
13642 * @eqe: Pointer to fast-path event queue entry.
13643 *
13644 * This routine process a event queue entry from the fast-path event queue.
13645 * It will check the MajorCode and MinorCode to determine this is for a
13646 * completion event on a completion queue, if not, an error shall be logged
13647 * and just return. Otherwise, it will get to the corresponding completion
13648 * queue and process all the entries on the completion queue, rearm the
13649 * completion queue, and then return.
13650 **/
13651static void
13652lpfc_sli4_hba_process_cq(struct work_struct *work)
13653{
13654 struct lpfc_queue *cq =
13655 container_of(work, struct lpfc_queue, irqwork);
13656 struct lpfc_hba *phba = cq->phba;
13657 struct lpfc_cqe *cqe;
13658 bool workposted = false;
13659 int ccount = 0;
13660
James Smart4f774512009-05-22 14:52:35 -040013661 /* Process all the entries to the CQ */
13662 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013663#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13664 if (phba->ktime_on)
13665 cq->isr_timestamp = ktime_get_ns();
13666 else
13667 cq->isr_timestamp = 0;
13668#endif
James Smart895427b2017-02-12 13:52:30 -080013669 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013670 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013671 break;
James Smart4f774512009-05-22 14:52:35 -040013672 }
13673
James Smartb84daac2012-08-03 12:35:13 -040013674 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013675 if (ccount > cq->CQ_max_cqe)
13676 cq->CQ_max_cqe = ccount;
13677 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040013678
James Smart4f774512009-05-22 14:52:35 -040013679 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070013680 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013681 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13682 "0369 No entry from fast-path completion "
13683 "queue fcpcqid=%d\n", cq->queue_id);
13684
13685 /* In any case, flash and re-arm the CQ */
James Smartb71413d2018-02-22 08:18:40 -080013686 phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013687
13688 /* wake up worker thread if there are works to be done */
13689 if (workposted)
13690 lpfc_worker_wake_up(phba);
13691}
13692
13693static void
13694lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13695{
13696 struct lpfc_eqe *eqe;
13697
13698 /* walk all the EQ entries and drop on the floor */
13699 while ((eqe = lpfc_sli4_eq_get(eq)))
13700 ;
13701
13702 /* Clear and re-arm the EQ */
James Smartb71413d2018-02-22 08:18:40 -080013703 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013704}
13705
James Smart1ba981f2014-02-20 09:56:45 -050013706
13707/**
13708 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13709 * entry
13710 * @phba: Pointer to HBA context object.
13711 * @eqe: Pointer to fast-path event queue entry.
13712 *
13713 * This routine process a event queue entry from the Flash Optimized Fabric
13714 * event queue. It will check the MajorCode and MinorCode to determine this
13715 * is for a completion event on a completion queue, if not, an error shall be
13716 * logged and just return. Otherwise, it will get to the corresponding
13717 * completion queue and process all the entries on the completion queue, rearm
13718 * the completion queue, and then return.
13719 **/
13720static void
13721lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13722{
13723 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050013724 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050013725
13726 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13727 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13728 "9147 Not a valid completion "
13729 "event: majorcode=x%x, minorcode=x%x\n",
13730 bf_get_le32(lpfc_eqe_major_code, eqe),
13731 bf_get_le32(lpfc_eqe_minor_code, eqe));
13732 return;
13733 }
13734
13735 /* Get the reference to the corresponding CQ */
13736 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13737
13738 /* Next check for OAS */
13739 cq = phba->sli4_hba.oas_cq;
13740 if (unlikely(!cq)) {
13741 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13742 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13743 "9148 OAS completion queue "
13744 "does not exist\n");
13745 return;
13746 }
13747
13748 if (unlikely(cqid != cq->queue_id)) {
13749 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13750 "9149 Miss-matched fast-path compl "
13751 "queue id: eqcqid=%d, fcpcqid=%d\n",
13752 cqid, cq->queue_id);
13753 return;
13754 }
13755
James Smartd41b65b2017-06-15 22:56:41 -070013756 /* Save EQ associated with this CQ */
13757 cq->assoc_qp = phba->sli4_hba.fof_eq;
13758
Dick Kennedyf485c182017-09-29 17:34:34 -070013759 /* CQ work will be processed on CPU affinitized to this IRQ */
13760 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050013761 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070013762 "0367 Cannot schedule soft IRQ "
13763 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13764 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050013765}
13766
13767/**
13768 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13769 * @irq: Interrupt number.
13770 * @dev_id: The device context pointer.
13771 *
13772 * This function is directly called from the PCI layer as an interrupt
13773 * service routine when device with SLI-4 interface spec is enabled with
13774 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13775 * IOCB ring event in the HBA. However, when the device is enabled with either
13776 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13777 * device-level interrupt handler. When the PCI slot is in error recovery
13778 * or the HBA is undergoing initialization, the interrupt handler will not
13779 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13780 * the intrrupt context. This function is called without any lock held.
13781 * It gets the hbalock to access and update SLI data structures. Note that,
13782 * the EQ to CQ are one-to-one map such that the EQ index is
13783 * equal to that of CQ index.
13784 *
13785 * This function returns IRQ_HANDLED when interrupt is handled else it
13786 * returns IRQ_NONE.
13787 **/
13788irqreturn_t
13789lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13790{
13791 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013792 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013793 struct lpfc_queue *eq;
13794 struct lpfc_eqe *eqe;
13795 unsigned long iflag;
13796 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013797
13798 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013799 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13800 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013801
13802 if (unlikely(!phba))
13803 return IRQ_NONE;
13804
13805 /* Get to the EQ struct associated with this vector */
13806 eq = phba->sli4_hba.fof_eq;
13807 if (unlikely(!eq))
13808 return IRQ_NONE;
13809
13810 /* Check device state for handling interrupt */
13811 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013812 /* Check again for link_state with lock held */
13813 spin_lock_irqsave(&phba->hbalock, iflag);
13814 if (phba->link_state < LPFC_LINK_DOWN)
13815 /* Flush, clear interrupt, and rearm the EQ */
13816 lpfc_sli4_eq_flush(phba, eq);
13817 spin_unlock_irqrestore(&phba->hbalock, iflag);
13818 return IRQ_NONE;
13819 }
13820
13821 /*
13822 * Process all the event on FCP fast-path EQ
13823 */
13824 while ((eqe = lpfc_sli4_eq_get(eq))) {
13825 lpfc_sli4_fof_handle_eqe(phba, eqe);
13826 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013827 break;
James Smart1ba981f2014-02-20 09:56:45 -050013828 eq->EQ_processed++;
13829 }
13830
13831 /* Track the max number of EQEs processed in 1 intr */
13832 if (ecount > eq->EQ_max_eqe)
13833 eq->EQ_max_eqe = ecount;
13834
13835
13836 if (unlikely(ecount == 0)) {
13837 eq->EQ_no_entry++;
13838
13839 if (phba->intr_type == MSIX)
13840 /* MSI-X treated interrupt served as no EQ share INT */
13841 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13842 "9145 MSI-X interrupt with no EQE\n");
13843 else {
13844 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13845 "9146 ISR interrupt with no EQE\n");
13846 /* Non MSI-X treated on interrupt as EQ share INT */
13847 return IRQ_NONE;
13848 }
13849 }
13850 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080013851 phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -050013852 return IRQ_HANDLED;
13853}
13854
James Smart4f774512009-05-22 14:52:35 -040013855/**
James Smart67d12732012-08-03 12:36:13 -040013856 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013857 * @irq: Interrupt number.
13858 * @dev_id: The device context pointer.
13859 *
13860 * This function is directly called from the PCI layer as an interrupt
13861 * service routine when device with SLI-4 interface spec is enabled with
13862 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13863 * ring event in the HBA. However, when the device is enabled with either
13864 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13865 * device-level interrupt handler. When the PCI slot is in error recovery
13866 * or the HBA is undergoing initialization, the interrupt handler will not
13867 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13868 * the intrrupt context. This function is called without any lock held.
13869 * It gets the hbalock to access and update SLI data structures. Note that,
13870 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13871 * equal to that of FCP CQ index.
13872 *
James Smart67d12732012-08-03 12:36:13 -040013873 * The link attention and ELS ring attention events are handled
13874 * by the worker thread. The interrupt handler signals the worker thread
13875 * and returns for these events. This function is called without any lock
13876 * held. It gets the hbalock to access and update SLI data structures.
13877 *
James Smart4f774512009-05-22 14:52:35 -040013878 * This function returns IRQ_HANDLED when interrupt is handled else it
13879 * returns IRQ_NONE.
13880 **/
13881irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013882lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013883{
13884 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013885 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013886 struct lpfc_queue *fpeq;
13887 struct lpfc_eqe *eqe;
13888 unsigned long iflag;
13889 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013890 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013891
13892 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013893 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13894 phba = hba_eq_hdl->phba;
13895 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013896
13897 if (unlikely(!phba))
13898 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013899 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013900 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013901
13902 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013903 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013904 if (unlikely(!fpeq))
13905 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013906
James Smartba20c852012-08-03 12:36:52 -040013907 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013908 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartb71413d2018-02-22 08:18:40 -080013909 phba->sli4_hba.sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -040013910 else {
James Smart895427b2017-02-12 13:52:30 -080013911 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013912 return IRQ_NONE;
13913 }
13914 }
13915
James Smart4f774512009-05-22 14:52:35 -040013916 /* Check device state for handling interrupt */
13917 if (unlikely(lpfc_intr_state_check(phba))) {
13918 /* Check again for link_state with lock held */
13919 spin_lock_irqsave(&phba->hbalock, iflag);
13920 if (phba->link_state < LPFC_LINK_DOWN)
13921 /* Flush, clear interrupt, and rearm the EQ */
13922 lpfc_sli4_eq_flush(phba, fpeq);
13923 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013924 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013925 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013926 return IRQ_NONE;
13927 }
13928
13929 /*
13930 * Process all the event on FCP fast-path EQ
13931 */
13932 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013933 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13934 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013935 break;
James Smartb84daac2012-08-03 12:35:13 -040013936 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013937 }
13938
James Smartb84daac2012-08-03 12:35:13 -040013939 /* Track the max number of EQEs processed in 1 intr */
13940 if (ecount > fpeq->EQ_max_eqe)
13941 fpeq->EQ_max_eqe = ecount;
13942
James Smart4f774512009-05-22 14:52:35 -040013943 /* Always clear and re-arm the fast-path EQ */
James Smartb71413d2018-02-22 08:18:40 -080013944 phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
James Smart4f774512009-05-22 14:52:35 -040013945
13946 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013947 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013948
13949 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013950 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013951 return IRQ_NONE;
13952 }
13953
James Smart4f774512009-05-22 14:52:35 -040013954 if (phba->intr_type == MSIX)
13955 /* MSI-X treated interrupt served as no EQ share INT */
13956 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13957 "0358 MSI-X interrupt with no EQE\n");
13958 else
13959 /* Non MSI-X treated on interrupt as EQ share INT */
13960 return IRQ_NONE;
13961 }
13962
James Smartba20c852012-08-03 12:36:52 -040013963 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013964 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13965
James Smart4f774512009-05-22 14:52:35 -040013966 return IRQ_HANDLED;
13967} /* lpfc_sli4_fp_intr_handler */
13968
13969/**
13970 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13971 * @irq: Interrupt number.
13972 * @dev_id: The device context pointer.
13973 *
13974 * This function is the device-level interrupt handler to device with SLI-4
13975 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13976 * interrupt mode is enabled and there is an event in the HBA which requires
13977 * driver attention. This function invokes the slow-path interrupt attention
13978 * handling function and fast-path interrupt attention handling function in
13979 * turn to process the relevant HBA attention events. This function is called
13980 * without any lock held. It gets the hbalock to access and update SLI data
13981 * structures.
13982 *
13983 * This function returns IRQ_HANDLED when interrupt is handled, else it
13984 * returns IRQ_NONE.
13985 **/
13986irqreturn_t
13987lpfc_sli4_intr_handler(int irq, void *dev_id)
13988{
13989 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013990 irqreturn_t hba_irq_rc;
13991 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013992 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013993
13994 /* Get the driver's phba structure from the dev_id */
13995 phba = (struct lpfc_hba *)dev_id;
13996
13997 if (unlikely(!phba))
13998 return IRQ_NONE;
13999
14000 /*
James Smart4f774512009-05-22 14:52:35 -040014001 * Invoke fast-path host attention interrupt handling as appropriate.
14002 */
James Smart895427b2017-02-12 13:52:30 -080014003 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040014004 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014005 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040014006 if (hba_irq_rc == IRQ_HANDLED)
14007 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040014008 }
14009
James Smart1ba981f2014-02-20 09:56:45 -050014010 if (phba->cfg_fof) {
14011 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080014012 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050014013 if (hba_irq_rc == IRQ_HANDLED)
14014 hba_handled |= true;
14015 }
14016
James Smart67d12732012-08-03 12:36:13 -040014017 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040014018} /* lpfc_sli4_intr_handler */
14019
14020/**
14021 * lpfc_sli4_queue_free - free a queue structure and associated memory
14022 * @queue: The queue structure to free.
14023 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040014024 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040014025 * the host resident queue. This function must be called after destroying the
14026 * queue on the HBA.
14027 **/
14028void
14029lpfc_sli4_queue_free(struct lpfc_queue *queue)
14030{
14031 struct lpfc_dmabuf *dmabuf;
14032
14033 if (!queue)
14034 return;
14035
14036 while (!list_empty(&queue->page_list)) {
14037 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
14038 list);
James Smart81b96ed2017-11-20 16:00:29 -080014039 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040014040 dmabuf->virt, dmabuf->phys);
14041 kfree(dmabuf);
14042 }
James Smart895427b2017-02-12 13:52:30 -080014043 if (queue->rqbp) {
14044 lpfc_free_rq_buffer(queue->phba, queue);
14045 kfree(queue->rqbp);
14046 }
James Smartd1f525a2017-04-21 16:04:55 -070014047
14048 if (!list_empty(&queue->wq_list))
14049 list_del(&queue->wq_list);
14050
James Smart4f774512009-05-22 14:52:35 -040014051 kfree(queue);
14052 return;
14053}
14054
14055/**
14056 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
14057 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080014058 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040014059 * @entry_size: The size of each queue entry for this queue.
14060 * @entry count: The number of entries that this queue will handle.
14061 *
14062 * This function allocates a queue structure and the DMAable memory used for
14063 * the host resident queue. This function must be called before creating the
14064 * queue on the HBA.
14065 **/
14066struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080014067lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
14068 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040014069{
14070 struct lpfc_queue *queue;
14071 struct lpfc_dmabuf *dmabuf;
14072 int x, total_qe_count;
14073 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040014074 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040014075
James Smartcb5172e2010-03-15 11:25:07 -040014076 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014077 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040014078
James Smart4f774512009-05-22 14:52:35 -040014079 queue = kzalloc(sizeof(struct lpfc_queue) +
14080 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
14081 if (!queue)
14082 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040014083 queue->page_count = (ALIGN(entry_size * entry_count,
14084 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080014085
14086 /* If needed, Adjust page count to match the max the adapter supports */
14087 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
14088 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
14089
James Smart4f774512009-05-22 14:52:35 -040014090 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080014091 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080014092 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040014093 INIT_LIST_HEAD(&queue->page_list);
14094 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080014095
14096 /* Set queue parameters now. If the system cannot provide memory
14097 * resources, the free routine needs to know what was allocated.
14098 */
14099 queue->entry_size = entry_size;
14100 queue->entry_count = entry_count;
14101 queue->page_size = hw_page_size;
14102 queue->phba = phba;
14103
James Smart4f774512009-05-22 14:52:35 -040014104 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
14105 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
14106 if (!dmabuf)
14107 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040014108 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
14109 hw_page_size, &dmabuf->phys,
14110 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040014111 if (!dmabuf->virt) {
14112 kfree(dmabuf);
14113 goto out_fail;
14114 }
14115 dmabuf->buffer_tag = x;
14116 list_add_tail(&dmabuf->list, &queue->page_list);
14117 /* initialize queue's entry array */
14118 dma_pointer = dmabuf->virt;
14119 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014120 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014121 total_qe_count++, dma_pointer += entry_size) {
14122 queue->qe[total_qe_count].address = dma_pointer;
14123 }
14124 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014125 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14126 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014127
James Smart64eb4dc2017-05-15 15:20:49 -070014128 /* entry_repost will be set during q creation */
14129
James Smart4f774512009-05-22 14:52:35 -040014130 return queue;
14131out_fail:
14132 lpfc_sli4_queue_free(queue);
14133 return NULL;
14134}
14135
14136/**
James Smart962bc512013-01-03 15:44:00 -050014137 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14138 * @phba: HBA structure that indicates port to create a queue on.
14139 * @pci_barset: PCI BAR set flag.
14140 *
14141 * This function shall perform iomap of the specified PCI BAR address to host
14142 * memory address if not already done so and return it. The returned host
14143 * memory address can be NULL.
14144 */
14145static void __iomem *
14146lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14147{
James Smart962bc512013-01-03 15:44:00 -050014148 if (!phba->pcidev)
14149 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014150
14151 switch (pci_barset) {
14152 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014153 return phba->pci_bar0_memmap_p;
14154 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014155 return phba->pci_bar2_memmap_p;
14156 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014157 return phba->pci_bar4_memmap_p;
14158 default:
14159 break;
14160 }
14161 return NULL;
14162}
14163
14164/**
James Smart895427b2017-02-12 13:52:30 -080014165 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014166 * @phba: HBA structure that indicates port to create a queue on.
14167 * @startq: The starting FCP EQ to modify
14168 *
14169 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014170 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14171 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014172 *
14173 * The @phba struct is used to send mailbox command to HBA. The @startq
14174 * is used to get the starting FCP EQ to change.
14175 * This function is asynchronous and will wait for the mailbox
14176 * command to finish before continuing.
14177 *
14178 * On success this function will return a zero. If unable to allocate enough
14179 * memory this function will return -ENOMEM. If the queue create mailbox command
14180 * fails this function will return -ENXIO.
14181 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014182int
James Smart0cf07f842017-06-01 21:07:10 -070014183lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14184 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014185{
14186 struct lpfc_mbx_modify_eq_delay *eq_delay;
14187 LPFC_MBOXQ_t *mbox;
14188 struct lpfc_queue *eq;
14189 int cnt, rc, length, status = 0;
14190 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014191 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014192 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014193 union lpfc_sli4_cfg_shdr *shdr;
14194 uint16_t dmult;
14195
James Smart895427b2017-02-12 13:52:30 -080014196 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014197 return 0;
14198
14199 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14200 if (!mbox)
14201 return -ENOMEM;
14202 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14203 sizeof(struct lpfc_sli4_cfg_mhdr));
14204 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14205 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14206 length, LPFC_SLI4_MBX_EMBED);
14207 eq_delay = &mbox->u.mqe.un.eq_delay;
14208
14209 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014210 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014211 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014212 dmult = 0;
14213 else
14214 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014215 if (dmult > LPFC_DMULT_MAX)
14216 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014217
14218 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014219 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14220 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014221 if (!eq)
14222 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014223 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014224 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14225 eq_delay->u.request.eq[cnt].phase = 0;
14226 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14227 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014228
14229 /* q_mode is only used for auto_imax */
14230 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14231 /* Use EQ Delay Register method for q_mode */
14232
14233 /* Convert for EQ Delay register */
14234 val = phba->cfg_fcp_imax;
14235 if (val) {
14236 /* First, interrupts per sec per EQ */
14237 val = phba->cfg_fcp_imax /
14238 phba->io_channel_irqs;
14239
14240 /* us delay between each interrupt */
14241 val = LPFC_SEC_TO_USEC / val;
14242 }
14243 eq->q_mode = val;
14244 } else {
14245 eq->q_mode = imax;
14246 }
14247
14248 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014249 break;
14250 }
14251 eq_delay->u.request.num_eq = cnt;
14252
14253 mbox->vport = phba->pport;
14254 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14255 mbox->context1 = NULL;
14256 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14257 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14258 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14259 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14260 if (shdr_status || shdr_add_status || rc) {
14261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14262 "2512 MODIFY_EQ_DELAY mailbox failed with "
14263 "status x%x add_status x%x, mbx status x%x\n",
14264 shdr_status, shdr_add_status, rc);
14265 status = -ENXIO;
14266 }
14267 mempool_free(mbox, phba->mbox_mem_pool);
14268 return status;
14269}
14270
14271/**
James Smart4f774512009-05-22 14:52:35 -040014272 * lpfc_eq_create - Create an Event Queue on the HBA
14273 * @phba: HBA structure that indicates port to create a queue on.
14274 * @eq: The queue structure to use to create the event queue.
14275 * @imax: The maximum interrupt per second limit.
14276 *
14277 * This function creates an event queue, as detailed in @eq, on a port,
14278 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14279 *
14280 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14281 * is used to get the entry count and entry size that are necessary to
14282 * determine the number of pages to allocate and use for this queue. This
14283 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14284 * event queue. This function is asynchronous and will wait for the mailbox
14285 * command to finish before continuing.
14286 *
14287 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014288 * memory this function will return -ENOMEM. If the queue create mailbox command
14289 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014290 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014291int
James Smartee020062012-09-29 11:28:52 -040014292lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014293{
14294 struct lpfc_mbx_eq_create *eq_create;
14295 LPFC_MBOXQ_t *mbox;
14296 int rc, length, status = 0;
14297 struct lpfc_dmabuf *dmabuf;
14298 uint32_t shdr_status, shdr_add_status;
14299 union lpfc_sli4_cfg_shdr *shdr;
14300 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014301 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14302
James Smart2e90f4b2011-12-13 13:22:37 -050014303 /* sanity check on queue memory */
14304 if (!eq)
14305 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014306 if (!phba->sli4_hba.pc_sli4_params.supported)
14307 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014308
14309 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14310 if (!mbox)
14311 return -ENOMEM;
14312 length = (sizeof(struct lpfc_mbx_eq_create) -
14313 sizeof(struct lpfc_sli4_cfg_mhdr));
14314 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14315 LPFC_MBOX_OPCODE_EQ_CREATE,
14316 length, LPFC_SLI4_MBX_EMBED);
14317 eq_create = &mbox->u.mqe.un.eq_create;
14318 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14319 eq->page_count);
14320 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14321 LPFC_EQE_SIZE);
14322 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040014323 /* don't setup delay multiplier using EQ_CREATE */
14324 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014325 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14326 dmult);
14327 switch (eq->entry_count) {
14328 default:
14329 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14330 "0360 Unsupported EQ count. (%d)\n",
14331 eq->entry_count);
14332 if (eq->entry_count < 256)
14333 return -EINVAL;
14334 /* otherwise default to smallest count (drop through) */
14335 case 256:
14336 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14337 LPFC_EQ_CNT_256);
14338 break;
14339 case 512:
14340 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14341 LPFC_EQ_CNT_512);
14342 break;
14343 case 1024:
14344 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14345 LPFC_EQ_CNT_1024);
14346 break;
14347 case 2048:
14348 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14349 LPFC_EQ_CNT_2048);
14350 break;
14351 case 4096:
14352 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14353 LPFC_EQ_CNT_4096);
14354 break;
14355 }
14356 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014357 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014358 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14359 putPaddrLow(dmabuf->phys);
14360 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14361 putPaddrHigh(dmabuf->phys);
14362 }
14363 mbox->vport = phba->pport;
14364 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14365 mbox->context1 = NULL;
14366 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14367 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14368 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14369 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14370 if (shdr_status || shdr_add_status || rc) {
14371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14372 "2500 EQ_CREATE mailbox failed with "
14373 "status x%x add_status x%x, mbx status x%x\n",
14374 shdr_status, shdr_add_status, rc);
14375 status = -ENXIO;
14376 }
14377 eq->type = LPFC_EQ;
14378 eq->subtype = LPFC_NONE;
14379 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14380 if (eq->queue_id == 0xFFFF)
14381 status = -ENXIO;
14382 eq->host_index = 0;
14383 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014384 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014385
James Smart8fa38512009-07-19 10:01:03 -040014386 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014387 return status;
14388}
14389
14390/**
14391 * lpfc_cq_create - Create a Completion Queue on the HBA
14392 * @phba: HBA structure that indicates port to create a queue on.
14393 * @cq: The queue structure to use to create the completion queue.
14394 * @eq: The event queue to bind this completion queue to.
14395 *
14396 * This function creates a completion queue, as detailed in @wq, on a port,
14397 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14398 *
14399 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14400 * is used to get the entry count and entry size that are necessary to
14401 * determine the number of pages to allocate and use for this queue. The @eq
14402 * is used to indicate which event queue to bind this completion queue to. This
14403 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14404 * completion queue. This function is asynchronous and will wait for the mailbox
14405 * command to finish before continuing.
14406 *
14407 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014408 * memory this function will return -ENOMEM. If the queue create mailbox command
14409 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014410 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014411int
James Smart4f774512009-05-22 14:52:35 -040014412lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14413 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14414{
14415 struct lpfc_mbx_cq_create *cq_create;
14416 struct lpfc_dmabuf *dmabuf;
14417 LPFC_MBOXQ_t *mbox;
14418 int rc, length, status = 0;
14419 uint32_t shdr_status, shdr_add_status;
14420 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014421 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14422
James Smart2e90f4b2011-12-13 13:22:37 -050014423 /* sanity check on queue memory */
14424 if (!cq || !eq)
14425 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014426 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014427 hw_page_size = cq->page_size;
James Smart49198b32010-04-06 15:04:33 -040014428
James Smart4f774512009-05-22 14:52:35 -040014429 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14430 if (!mbox)
14431 return -ENOMEM;
14432 length = (sizeof(struct lpfc_mbx_cq_create) -
14433 sizeof(struct lpfc_sli4_cfg_mhdr));
14434 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14435 LPFC_MBOX_OPCODE_CQ_CREATE,
14436 length, LPFC_SLI4_MBX_EMBED);
14437 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014438 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014439 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14440 cq->page_count);
14441 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14442 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014443 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14444 phba->sli4_hba.pc_sli4_params.cqv);
14445 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014446 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14447 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014448 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14449 eq->queue_id);
14450 } else {
14451 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14452 eq->queue_id);
14453 }
James Smart4f774512009-05-22 14:52:35 -040014454 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014455 case 2048:
14456 case 4096:
14457 if (phba->sli4_hba.pc_sli4_params.cqv ==
14458 LPFC_Q_CREATE_VERSION_2) {
14459 cq_create->u.request.context.lpfc_cq_context_count =
14460 cq->entry_count;
14461 bf_set(lpfc_cq_context_count,
14462 &cq_create->u.request.context,
14463 LPFC_CQ_CNT_WORD7);
14464 break;
14465 }
14466 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014467 default:
14468 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014469 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014470 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014471 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014472 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014473 if (cq->entry_count < 256) {
14474 status = -EINVAL;
14475 goto out;
14476 }
James Smart4f774512009-05-22 14:52:35 -040014477 /* otherwise default to smallest count (drop through) */
14478 case 256:
14479 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14480 LPFC_CQ_CNT_256);
14481 break;
14482 case 512:
14483 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14484 LPFC_CQ_CNT_512);
14485 break;
14486 case 1024:
14487 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14488 LPFC_CQ_CNT_1024);
14489 break;
14490 }
14491 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014492 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014493 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14494 putPaddrLow(dmabuf->phys);
14495 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14496 putPaddrHigh(dmabuf->phys);
14497 }
14498 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14499
14500 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014501 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14502 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14503 if (shdr_status || shdr_add_status || rc) {
14504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14505 "2501 CQ_CREATE mailbox failed with "
14506 "status x%x add_status x%x, mbx status x%x\n",
14507 shdr_status, shdr_add_status, rc);
14508 status = -ENXIO;
14509 goto out;
14510 }
14511 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14512 if (cq->queue_id == 0xFFFF) {
14513 status = -ENXIO;
14514 goto out;
14515 }
14516 /* link the cq onto the parent eq child list */
14517 list_add_tail(&cq->list, &eq->child_list);
14518 /* Set up completion queue's type and subtype */
14519 cq->type = type;
14520 cq->subtype = subtype;
14521 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014522 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014523 cq->host_index = 0;
14524 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014525 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014526
James Smart8fa38512009-07-19 10:01:03 -040014527out:
14528 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014529 return status;
14530}
14531
14532/**
James Smart2d7dbc42017-02-12 13:52:35 -080014533 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14534 * @phba: HBA structure that indicates port to create a queue on.
14535 * @cqp: The queue structure array to use to create the completion queues.
14536 * @eqp: The event queue array to bind these completion queues to.
14537 *
14538 * This function creates a set of completion queue, s to support MRQ
14539 * as detailed in @cqp, on a port,
14540 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14541 *
14542 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14543 * is used to get the entry count and entry size that are necessary to
14544 * determine the number of pages to allocate and use for this queue. The @eq
14545 * is used to indicate which event queue to bind this completion queue to. This
14546 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14547 * completion queue. This function is asynchronous and will wait for the mailbox
14548 * command to finish before continuing.
14549 *
14550 * On success this function will return a zero. If unable to allocate enough
14551 * memory this function will return -ENOMEM. If the queue create mailbox command
14552 * fails this function will return -ENXIO.
14553 **/
14554int
14555lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14556 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14557{
14558 struct lpfc_queue *cq;
14559 struct lpfc_queue *eq;
14560 struct lpfc_mbx_cq_create_set *cq_set;
14561 struct lpfc_dmabuf *dmabuf;
14562 LPFC_MBOXQ_t *mbox;
14563 int rc, length, alloclen, status = 0;
14564 int cnt, idx, numcq, page_idx = 0;
14565 uint32_t shdr_status, shdr_add_status;
14566 union lpfc_sli4_cfg_shdr *shdr;
14567 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14568
14569 /* sanity check on queue memory */
14570 numcq = phba->cfg_nvmet_mrq;
14571 if (!cqp || !eqp || !numcq)
14572 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014573
14574 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14575 if (!mbox)
14576 return -ENOMEM;
14577
14578 length = sizeof(struct lpfc_mbx_cq_create_set);
14579 length += ((numcq * cqp[0]->page_count) *
14580 sizeof(struct dma_address));
14581 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14582 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14583 LPFC_SLI4_MBX_NEMBED);
14584 if (alloclen < length) {
14585 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14586 "3098 Allocated DMA memory size (%d) is "
14587 "less than the requested DMA memory size "
14588 "(%d)\n", alloclen, length);
14589 status = -ENOMEM;
14590 goto out;
14591 }
14592 cq_set = mbox->sge_array->addr[0];
14593 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14594 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14595
14596 for (idx = 0; idx < numcq; idx++) {
14597 cq = cqp[idx];
14598 eq = eqp[idx];
14599 if (!cq || !eq) {
14600 status = -ENOMEM;
14601 goto out;
14602 }
James Smart81b96ed2017-11-20 16:00:29 -080014603 if (!phba->sli4_hba.pc_sli4_params.supported)
14604 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014605
14606 switch (idx) {
14607 case 0:
14608 bf_set(lpfc_mbx_cq_create_set_page_size,
14609 &cq_set->u.request,
14610 (hw_page_size / SLI4_PAGE_SIZE));
14611 bf_set(lpfc_mbx_cq_create_set_num_pages,
14612 &cq_set->u.request, cq->page_count);
14613 bf_set(lpfc_mbx_cq_create_set_evt,
14614 &cq_set->u.request, 1);
14615 bf_set(lpfc_mbx_cq_create_set_valid,
14616 &cq_set->u.request, 1);
14617 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14618 &cq_set->u.request, 0);
14619 bf_set(lpfc_mbx_cq_create_set_num_cq,
14620 &cq_set->u.request, numcq);
14621 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014622 case 2048:
14623 case 4096:
14624 if (phba->sli4_hba.pc_sli4_params.cqv ==
14625 LPFC_Q_CREATE_VERSION_2) {
14626 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14627 &cq_set->u.request,
14628 cq->entry_count);
14629 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14630 &cq_set->u.request,
14631 LPFC_CQ_CNT_WORD7);
14632 break;
14633 }
14634 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014635 default:
14636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14637 "3118 Bad CQ count. (%d)\n",
14638 cq->entry_count);
14639 if (cq->entry_count < 256) {
14640 status = -EINVAL;
14641 goto out;
14642 }
14643 /* otherwise default to smallest (drop thru) */
14644 case 256:
14645 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14646 &cq_set->u.request, LPFC_CQ_CNT_256);
14647 break;
14648 case 512:
14649 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14650 &cq_set->u.request, LPFC_CQ_CNT_512);
14651 break;
14652 case 1024:
14653 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14654 &cq_set->u.request, LPFC_CQ_CNT_1024);
14655 break;
14656 }
14657 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14658 &cq_set->u.request, eq->queue_id);
14659 break;
14660 case 1:
14661 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14662 &cq_set->u.request, eq->queue_id);
14663 break;
14664 case 2:
14665 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14666 &cq_set->u.request, eq->queue_id);
14667 break;
14668 case 3:
14669 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14670 &cq_set->u.request, eq->queue_id);
14671 break;
14672 case 4:
14673 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14674 &cq_set->u.request, eq->queue_id);
14675 break;
14676 case 5:
14677 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14678 &cq_set->u.request, eq->queue_id);
14679 break;
14680 case 6:
14681 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14682 &cq_set->u.request, eq->queue_id);
14683 break;
14684 case 7:
14685 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14686 &cq_set->u.request, eq->queue_id);
14687 break;
14688 case 8:
14689 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14690 &cq_set->u.request, eq->queue_id);
14691 break;
14692 case 9:
14693 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14694 &cq_set->u.request, eq->queue_id);
14695 break;
14696 case 10:
14697 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14698 &cq_set->u.request, eq->queue_id);
14699 break;
14700 case 11:
14701 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14702 &cq_set->u.request, eq->queue_id);
14703 break;
14704 case 12:
14705 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14706 &cq_set->u.request, eq->queue_id);
14707 break;
14708 case 13:
14709 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14710 &cq_set->u.request, eq->queue_id);
14711 break;
14712 case 14:
14713 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14714 &cq_set->u.request, eq->queue_id);
14715 break;
14716 case 15:
14717 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14718 &cq_set->u.request, eq->queue_id);
14719 break;
14720 }
14721
14722 /* link the cq onto the parent eq child list */
14723 list_add_tail(&cq->list, &eq->child_list);
14724 /* Set up completion queue's type and subtype */
14725 cq->type = type;
14726 cq->subtype = subtype;
14727 cq->assoc_qid = eq->queue_id;
14728 cq->host_index = 0;
14729 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014730 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080014731 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080014732
14733 rc = 0;
14734 list_for_each_entry(dmabuf, &cq->page_list, list) {
14735 memset(dmabuf->virt, 0, hw_page_size);
14736 cnt = page_idx + dmabuf->buffer_tag;
14737 cq_set->u.request.page[cnt].addr_lo =
14738 putPaddrLow(dmabuf->phys);
14739 cq_set->u.request.page[cnt].addr_hi =
14740 putPaddrHigh(dmabuf->phys);
14741 rc++;
14742 }
14743 page_idx += rc;
14744 }
14745
14746 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14747
14748 /* The IOCTL status is embedded in the mailbox subheader. */
14749 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14750 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14751 if (shdr_status || shdr_add_status || rc) {
14752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14753 "3119 CQ_CREATE_SET mailbox failed with "
14754 "status x%x add_status x%x, mbx status x%x\n",
14755 shdr_status, shdr_add_status, rc);
14756 status = -ENXIO;
14757 goto out;
14758 }
14759 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14760 if (rc == 0xFFFF) {
14761 status = -ENXIO;
14762 goto out;
14763 }
14764
14765 for (idx = 0; idx < numcq; idx++) {
14766 cq = cqp[idx];
14767 cq->queue_id = rc + idx;
14768 }
14769
14770out:
14771 lpfc_sli4_mbox_cmd_free(phba, mbox);
14772 return status;
14773}
14774
14775/**
James Smartb19a0612010-04-06 14:48:51 -040014776 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014777 * @phba: HBA structure that indicates port to create a queue on.
14778 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014779 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14780 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014781 *
James Smartb19a0612010-04-06 14:48:51 -040014782 * This function provides failback (fb) functionality when the
14783 * mq_create_ext fails on older FW generations. It's purpose is identical
14784 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014785 *
James Smartb19a0612010-04-06 14:48:51 -040014786 * This routine cannot fail as all attributes were previously accessed and
14787 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014788 **/
James Smartb19a0612010-04-06 14:48:51 -040014789static void
14790lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14791 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014792{
14793 struct lpfc_mbx_mq_create *mq_create;
14794 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014795 int length;
James Smart04c68492009-05-22 14:52:52 -040014796
James Smart04c68492009-05-22 14:52:52 -040014797 length = (sizeof(struct lpfc_mbx_mq_create) -
14798 sizeof(struct lpfc_sli4_cfg_mhdr));
14799 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14800 LPFC_MBOX_OPCODE_MQ_CREATE,
14801 length, LPFC_SLI4_MBX_EMBED);
14802 mq_create = &mbox->u.mqe.un.mq_create;
14803 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014804 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014805 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014806 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014807 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14808 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014809 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014810 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14811 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014812 break;
14813 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014814 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14815 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014816 break;
14817 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014818 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14819 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014820 break;
14821 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014822 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14823 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014824 break;
14825 }
14826 list_for_each_entry(dmabuf, &mq->page_list, list) {
14827 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014828 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014829 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014830 putPaddrHigh(dmabuf->phys);
14831 }
14832}
14833
14834/**
14835 * lpfc_mq_create - Create a mailbox Queue on the HBA
14836 * @phba: HBA structure that indicates port to create a queue on.
14837 * @mq: The queue structure to use to create the mailbox queue.
14838 * @cq: The completion queue to associate with this cq.
14839 * @subtype: The queue's subtype.
14840 *
14841 * This function creates a mailbox queue, as detailed in @mq, on a port,
14842 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14843 *
14844 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14845 * is used to get the entry count and entry size that are necessary to
14846 * determine the number of pages to allocate and use for this queue. This
14847 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14848 * mailbox queue. This function is asynchronous and will wait for the mailbox
14849 * command to finish before continuing.
14850 *
14851 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014852 * memory this function will return -ENOMEM. If the queue create mailbox command
14853 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014854 **/
14855int32_t
14856lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14857 struct lpfc_queue *cq, uint32_t subtype)
14858{
14859 struct lpfc_mbx_mq_create *mq_create;
14860 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14861 struct lpfc_dmabuf *dmabuf;
14862 LPFC_MBOXQ_t *mbox;
14863 int rc, length, status = 0;
14864 uint32_t shdr_status, shdr_add_status;
14865 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014866 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014867
James Smart2e90f4b2011-12-13 13:22:37 -050014868 /* sanity check on queue memory */
14869 if (!mq || !cq)
14870 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014871 if (!phba->sli4_hba.pc_sli4_params.supported)
14872 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014873
14874 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14875 if (!mbox)
14876 return -ENOMEM;
14877 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14878 sizeof(struct lpfc_sli4_cfg_mhdr));
14879 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14880 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14881 length, LPFC_SLI4_MBX_EMBED);
14882
14883 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014884 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014885 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14886 &mq_create_ext->u.request, mq->page_count);
14887 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14888 &mq_create_ext->u.request, 1);
14889 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014890 &mq_create_ext->u.request, 1);
14891 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14892 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014893 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14894 &mq_create_ext->u.request, 1);
14895 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14896 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014897 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014898 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14899 phba->sli4_hba.pc_sli4_params.mqv);
14900 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14901 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14902 cq->queue_id);
14903 else
14904 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14905 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014906 switch (mq->entry_count) {
14907 default:
14908 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14909 "0362 Unsupported MQ count. (%d)\n",
14910 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014911 if (mq->entry_count < 16) {
14912 status = -EINVAL;
14913 goto out;
14914 }
James Smartb19a0612010-04-06 14:48:51 -040014915 /* otherwise default to smallest count (drop through) */
14916 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014917 bf_set(lpfc_mq_context_ring_size,
14918 &mq_create_ext->u.request.context,
14919 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014920 break;
14921 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014922 bf_set(lpfc_mq_context_ring_size,
14923 &mq_create_ext->u.request.context,
14924 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014925 break;
14926 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014927 bf_set(lpfc_mq_context_ring_size,
14928 &mq_create_ext->u.request.context,
14929 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014930 break;
14931 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014932 bf_set(lpfc_mq_context_ring_size,
14933 &mq_create_ext->u.request.context,
14934 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014935 break;
14936 }
14937 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014938 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014939 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14940 putPaddrLow(dmabuf->phys);
14941 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014942 putPaddrHigh(dmabuf->phys);
14943 }
14944 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014945 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14946 &mq_create_ext->u.response);
14947 if (rc != MBX_SUCCESS) {
14948 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14949 "2795 MQ_CREATE_EXT failed with "
14950 "status x%x. Failback to MQ_CREATE.\n",
14951 rc);
14952 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14953 mq_create = &mbox->u.mqe.un.mq_create;
14954 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14955 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14956 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14957 &mq_create->u.response);
14958 }
14959
James Smart04c68492009-05-22 14:52:52 -040014960 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014961 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14962 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14963 if (shdr_status || shdr_add_status || rc) {
14964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14965 "2502 MQ_CREATE mailbox failed with "
14966 "status x%x add_status x%x, mbx status x%x\n",
14967 shdr_status, shdr_add_status, rc);
14968 status = -ENXIO;
14969 goto out;
14970 }
James Smart04c68492009-05-22 14:52:52 -040014971 if (mq->queue_id == 0xFFFF) {
14972 status = -ENXIO;
14973 goto out;
14974 }
14975 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014976 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014977 mq->subtype = subtype;
14978 mq->host_index = 0;
14979 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014980 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040014981
14982 /* link the mq onto the parent cq child list */
14983 list_add_tail(&mq->list, &cq->child_list);
14984out:
James Smart8fa38512009-07-19 10:01:03 -040014985 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014986 return status;
14987}
14988
14989/**
James Smart4f774512009-05-22 14:52:35 -040014990 * lpfc_wq_create - Create a Work Queue on the HBA
14991 * @phba: HBA structure that indicates port to create a queue on.
14992 * @wq: The queue structure to use to create the work queue.
14993 * @cq: The completion queue to bind this work queue to.
14994 * @subtype: The subtype of the work queue indicating its functionality.
14995 *
14996 * This function creates a work queue, as detailed in @wq, on a port, described
14997 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14998 *
14999 * The @phba struct is used to send mailbox command to HBA. The @wq struct
15000 * is used to get the entry count and entry size that are necessary to
15001 * determine the number of pages to allocate and use for this queue. The @cq
15002 * is used to indicate which completion queue to bind this work queue to. This
15003 * function will send the WQ_CREATE mailbox command to the HBA to setup the
15004 * work queue. This function is asynchronous and will wait for the mailbox
15005 * command to finish before continuing.
15006 *
15007 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015008 * memory this function will return -ENOMEM. If the queue create mailbox command
15009 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015010 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015011int
James Smart4f774512009-05-22 14:52:35 -040015012lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
15013 struct lpfc_queue *cq, uint32_t subtype)
15014{
15015 struct lpfc_mbx_wq_create *wq_create;
15016 struct lpfc_dmabuf *dmabuf;
15017 LPFC_MBOXQ_t *mbox;
15018 int rc, length, status = 0;
15019 uint32_t shdr_status, shdr_add_status;
15020 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015021 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050015022 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050015023 void __iomem *bar_memmap_p;
15024 uint32_t db_offset;
15025 uint16_t pci_barset;
James Smart81b96ed2017-11-20 16:00:29 -080015026 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040015027
James Smart2e90f4b2011-12-13 13:22:37 -050015028 /* sanity check on queue memory */
15029 if (!wq || !cq)
15030 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015031 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080015032 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040015033
15034 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15035 if (!mbox)
15036 return -ENOMEM;
15037 length = (sizeof(struct lpfc_mbx_wq_create) -
15038 sizeof(struct lpfc_sli4_cfg_mhdr));
15039 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15040 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
15041 length, LPFC_SLI4_MBX_EMBED);
15042 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015043 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040015044 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
15045 wq->page_count);
15046 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
15047 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040015048
15049 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050015050 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15051 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050015052
James Smartc176ffa2018-01-30 15:58:46 -080015053 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
15054 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080015055 wq_create_version = LPFC_Q_CREATE_VERSION_1;
15056 else
15057 wq_create_version = LPFC_Q_CREATE_VERSION_0;
15058
15059 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040015060 case LPFC_Q_CREATE_VERSION_0:
15061 switch (wq->entry_size) {
15062 default:
15063 case 64:
15064 /* Nothing to do, version 0 ONLY supports 64 byte */
15065 page = wq_create->u.request.page;
15066 break;
15067 case 128:
15068 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
15069 LPFC_WQ_SZ128_SUPPORT)) {
15070 status = -ERANGE;
15071 goto out;
15072 }
15073 /* If we get here the HBA MUST also support V1 and
15074 * we MUST use it
15075 */
15076 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15077 LPFC_Q_CREATE_VERSION_1);
15078
15079 bf_set(lpfc_mbx_wq_create_wqe_count,
15080 &wq_create->u.request_1, wq->entry_count);
15081 bf_set(lpfc_mbx_wq_create_wqe_size,
15082 &wq_create->u.request_1,
15083 LPFC_WQ_WQE_SIZE_128);
15084 bf_set(lpfc_mbx_wq_create_page_size,
15085 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080015086 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040015087 page = wq_create->u.request_1.page;
15088 break;
15089 }
15090 break;
15091 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050015092 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
15093 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070015094 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15095 LPFC_Q_CREATE_VERSION_1);
15096
James Smart5a6f1332011-03-11 16:05:35 -050015097 switch (wq->entry_size) {
15098 default:
15099 case 64:
15100 bf_set(lpfc_mbx_wq_create_wqe_size,
15101 &wq_create->u.request_1,
15102 LPFC_WQ_WQE_SIZE_64);
15103 break;
15104 case 128:
James Smart0c651872013-07-15 18:33:23 -040015105 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
15106 LPFC_WQ_SZ128_SUPPORT)) {
15107 status = -ERANGE;
15108 goto out;
15109 }
James Smart5a6f1332011-03-11 16:05:35 -050015110 bf_set(lpfc_mbx_wq_create_wqe_size,
15111 &wq_create->u.request_1,
15112 LPFC_WQ_WQE_SIZE_128);
15113 break;
15114 }
James Smart8ea73db2017-02-12 13:52:25 -080015115 bf_set(lpfc_mbx_wq_create_page_size,
15116 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015117 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015118 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015119 break;
15120 default:
15121 status = -ERANGE;
15122 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050015123 }
James Smart0c651872013-07-15 18:33:23 -040015124
James Smart4f774512009-05-22 14:52:35 -040015125 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015126 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015127 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15128 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015129 }
James Smart962bc512013-01-03 15:44:00 -050015130
15131 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15132 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15133
James Smart4f774512009-05-22 14:52:35 -040015134 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15135 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015136 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15137 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15138 if (shdr_status || shdr_add_status || rc) {
15139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15140 "2503 WQ_CREATE mailbox failed with "
15141 "status x%x add_status x%x, mbx status x%x\n",
15142 shdr_status, shdr_add_status, rc);
15143 status = -ENXIO;
15144 goto out;
15145 }
15146 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
15147 if (wq->queue_id == 0xFFFF) {
15148 status = -ENXIO;
15149 goto out;
15150 }
James Smart962bc512013-01-03 15:44:00 -050015151 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15152 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15153 &wq_create->u.response);
15154 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15155 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15157 "3265 WQ[%d] doorbell format not "
15158 "supported: x%x\n", wq->queue_id,
15159 wq->db_format);
15160 status = -EINVAL;
15161 goto out;
15162 }
15163 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15164 &wq_create->u.response);
15165 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15166 if (!bar_memmap_p) {
15167 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15168 "3263 WQ[%d] failed to memmap pci "
15169 "barset:x%x\n", wq->queue_id,
15170 pci_barset);
15171 status = -ENOMEM;
15172 goto out;
15173 }
15174 db_offset = wq_create->u.response.doorbell_offset;
15175 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15176 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15178 "3252 WQ[%d] doorbell offset not "
15179 "supported: x%x\n", wq->queue_id,
15180 db_offset);
15181 status = -EINVAL;
15182 goto out;
15183 }
15184 wq->db_regaddr = bar_memmap_p + db_offset;
15185 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015186 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15187 "format:x%x\n", wq->queue_id, pci_barset,
15188 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015189 } else {
15190 wq->db_format = LPFC_DB_LIST_FORMAT;
15191 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15192 }
James Smart895427b2017-02-12 13:52:30 -080015193 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15194 if (wq->pring == NULL) {
15195 status = -ENOMEM;
15196 goto out;
15197 }
James Smart4f774512009-05-22 14:52:35 -040015198 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015199 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015200 wq->subtype = subtype;
15201 wq->host_index = 0;
15202 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015203 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015204
15205 /* link the wq onto the parent cq child list */
15206 list_add_tail(&wq->list, &cq->child_list);
15207out:
James Smart8fa38512009-07-19 10:01:03 -040015208 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015209 return status;
15210}
15211
15212/**
15213 * lpfc_rq_create - Create a Receive Queue on the HBA
15214 * @phba: HBA structure that indicates port to create a queue on.
15215 * @hrq: The queue structure to use to create the header receive queue.
15216 * @drq: The queue structure to use to create the data receive queue.
15217 * @cq: The completion queue to bind this work queue to.
15218 *
15219 * This function creates a receive buffer queue pair , as detailed in @hrq and
15220 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15221 * to the HBA.
15222 *
15223 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15224 * struct is used to get the entry count that is necessary to determine the
15225 * number of pages to use for this queue. The @cq is used to indicate which
15226 * completion queue to bind received buffers that are posted to these queues to.
15227 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15228 * receive queue pair. This function is asynchronous and will wait for the
15229 * mailbox command to finish before continuing.
15230 *
15231 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015232 * memory this function will return -ENOMEM. If the queue create mailbox command
15233 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015234 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015235int
James Smart4f774512009-05-22 14:52:35 -040015236lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15237 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15238{
15239 struct lpfc_mbx_rq_create *rq_create;
15240 struct lpfc_dmabuf *dmabuf;
15241 LPFC_MBOXQ_t *mbox;
15242 int rc, length, status = 0;
15243 uint32_t shdr_status, shdr_add_status;
15244 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015245 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015246 void __iomem *bar_memmap_p;
15247 uint32_t db_offset;
15248 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015249
James Smart2e90f4b2011-12-13 13:22:37 -050015250 /* sanity check on queue memory */
15251 if (!hrq || !drq || !cq)
15252 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015253 if (!phba->sli4_hba.pc_sli4_params.supported)
15254 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015255
15256 if (hrq->entry_count != drq->entry_count)
15257 return -EINVAL;
15258 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15259 if (!mbox)
15260 return -ENOMEM;
15261 length = (sizeof(struct lpfc_mbx_rq_create) -
15262 sizeof(struct lpfc_sli4_cfg_mhdr));
15263 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15264 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15265 length, LPFC_SLI4_MBX_EMBED);
15266 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015267 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15268 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15269 phba->sli4_hba.pc_sli4_params.rqv);
15270 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15271 bf_set(lpfc_rq_context_rqe_count_1,
15272 &rq_create->u.request.context,
15273 hrq->entry_count);
15274 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015275 bf_set(lpfc_rq_context_rqe_size,
15276 &rq_create->u.request.context,
15277 LPFC_RQE_SIZE_8);
15278 bf_set(lpfc_rq_context_page_size,
15279 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015280 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015281 } else {
15282 switch (hrq->entry_count) {
15283 default:
15284 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15285 "2535 Unsupported RQ count. (%d)\n",
15286 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015287 if (hrq->entry_count < 512) {
15288 status = -EINVAL;
15289 goto out;
15290 }
James Smart5a6f1332011-03-11 16:05:35 -050015291 /* otherwise default to smallest count (drop through) */
15292 case 512:
15293 bf_set(lpfc_rq_context_rqe_count,
15294 &rq_create->u.request.context,
15295 LPFC_RQ_RING_SIZE_512);
15296 break;
15297 case 1024:
15298 bf_set(lpfc_rq_context_rqe_count,
15299 &rq_create->u.request.context,
15300 LPFC_RQ_RING_SIZE_1024);
15301 break;
15302 case 2048:
15303 bf_set(lpfc_rq_context_rqe_count,
15304 &rq_create->u.request.context,
15305 LPFC_RQ_RING_SIZE_2048);
15306 break;
15307 case 4096:
15308 bf_set(lpfc_rq_context_rqe_count,
15309 &rq_create->u.request.context,
15310 LPFC_RQ_RING_SIZE_4096);
15311 break;
15312 }
15313 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15314 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015315 }
15316 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15317 cq->queue_id);
15318 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15319 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015320 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015321 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015322 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15323 putPaddrLow(dmabuf->phys);
15324 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15325 putPaddrHigh(dmabuf->phys);
15326 }
James Smart962bc512013-01-03 15:44:00 -050015327 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15328 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15329
James Smart4f774512009-05-22 14:52:35 -040015330 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15331 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015332 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15333 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15334 if (shdr_status || shdr_add_status || rc) {
15335 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15336 "2504 RQ_CREATE mailbox failed with "
15337 "status x%x add_status x%x, mbx status x%x\n",
15338 shdr_status, shdr_add_status, rc);
15339 status = -ENXIO;
15340 goto out;
15341 }
15342 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15343 if (hrq->queue_id == 0xFFFF) {
15344 status = -ENXIO;
15345 goto out;
15346 }
James Smart962bc512013-01-03 15:44:00 -050015347
15348 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15349 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15350 &rq_create->u.response);
15351 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15352 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15354 "3262 RQ [%d] doorbell format not "
15355 "supported: x%x\n", hrq->queue_id,
15356 hrq->db_format);
15357 status = -EINVAL;
15358 goto out;
15359 }
15360
15361 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15362 &rq_create->u.response);
15363 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15364 if (!bar_memmap_p) {
15365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15366 "3269 RQ[%d] failed to memmap pci "
15367 "barset:x%x\n", hrq->queue_id,
15368 pci_barset);
15369 status = -ENOMEM;
15370 goto out;
15371 }
15372
15373 db_offset = rq_create->u.response.doorbell_offset;
15374 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15375 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15377 "3270 RQ[%d] doorbell offset not "
15378 "supported: x%x\n", hrq->queue_id,
15379 db_offset);
15380 status = -EINVAL;
15381 goto out;
15382 }
15383 hrq->db_regaddr = bar_memmap_p + db_offset;
15384 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015385 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15386 "format:x%x\n", hrq->queue_id, pci_barset,
15387 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015388 } else {
15389 hrq->db_format = LPFC_DB_RING_FORMAT;
15390 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15391 }
James Smart4f774512009-05-22 14:52:35 -040015392 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015393 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015394 hrq->subtype = subtype;
15395 hrq->host_index = 0;
15396 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015397 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015398
15399 /* now create the data queue */
15400 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15401 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15402 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015403 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15404 phba->sli4_hba.pc_sli4_params.rqv);
15405 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15406 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015407 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015408 if (subtype == LPFC_NVMET)
15409 rq_create->u.request.context.buffer_size =
15410 LPFC_NVMET_DATA_BUF_SIZE;
15411 else
15412 rq_create->u.request.context.buffer_size =
15413 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015414 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15415 LPFC_RQE_SIZE_8);
15416 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15417 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015418 } else {
15419 switch (drq->entry_count) {
15420 default:
15421 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15422 "2536 Unsupported RQ count. (%d)\n",
15423 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015424 if (drq->entry_count < 512) {
15425 status = -EINVAL;
15426 goto out;
15427 }
James Smart5a6f1332011-03-11 16:05:35 -050015428 /* otherwise default to smallest count (drop through) */
15429 case 512:
15430 bf_set(lpfc_rq_context_rqe_count,
15431 &rq_create->u.request.context,
15432 LPFC_RQ_RING_SIZE_512);
15433 break;
15434 case 1024:
15435 bf_set(lpfc_rq_context_rqe_count,
15436 &rq_create->u.request.context,
15437 LPFC_RQ_RING_SIZE_1024);
15438 break;
15439 case 2048:
15440 bf_set(lpfc_rq_context_rqe_count,
15441 &rq_create->u.request.context,
15442 LPFC_RQ_RING_SIZE_2048);
15443 break;
15444 case 4096:
15445 bf_set(lpfc_rq_context_rqe_count,
15446 &rq_create->u.request.context,
15447 LPFC_RQ_RING_SIZE_4096);
15448 break;
15449 }
James Smart3c603be2017-05-15 15:20:44 -070015450 if (subtype == LPFC_NVMET)
15451 bf_set(lpfc_rq_context_buf_size,
15452 &rq_create->u.request.context,
15453 LPFC_NVMET_DATA_BUF_SIZE);
15454 else
15455 bf_set(lpfc_rq_context_buf_size,
15456 &rq_create->u.request.context,
15457 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015458 }
15459 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15460 cq->queue_id);
15461 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15462 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015463 list_for_each_entry(dmabuf, &drq->page_list, list) {
15464 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15465 putPaddrLow(dmabuf->phys);
15466 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15467 putPaddrHigh(dmabuf->phys);
15468 }
James Smart962bc512013-01-03 15:44:00 -050015469 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15470 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015471 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15472 /* The IOCTL status is embedded in the mailbox subheader. */
15473 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15474 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15475 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15476 if (shdr_status || shdr_add_status || rc) {
15477 status = -ENXIO;
15478 goto out;
15479 }
15480 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15481 if (drq->queue_id == 0xFFFF) {
15482 status = -ENXIO;
15483 goto out;
15484 }
15485 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015486 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015487 drq->subtype = subtype;
15488 drq->host_index = 0;
15489 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015490 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015491
15492 /* link the header and data RQs onto the parent cq child list */
15493 list_add_tail(&hrq->list, &cq->child_list);
15494 list_add_tail(&drq->list, &cq->child_list);
15495
15496out:
James Smart8fa38512009-07-19 10:01:03 -040015497 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015498 return status;
15499}
15500
15501/**
James Smart2d7dbc42017-02-12 13:52:35 -080015502 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15503 * @phba: HBA structure that indicates port to create a queue on.
15504 * @hrqp: The queue structure array to use to create the header receive queues.
15505 * @drqp: The queue structure array to use to create the data receive queues.
15506 * @cqp: The completion queue array to bind these receive queues to.
15507 *
15508 * This function creates a receive buffer queue pair , as detailed in @hrq and
15509 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15510 * to the HBA.
15511 *
15512 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15513 * struct is used to get the entry count that is necessary to determine the
15514 * number of pages to use for this queue. The @cq is used to indicate which
15515 * completion queue to bind received buffers that are posted to these queues to.
15516 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15517 * receive queue pair. This function is asynchronous and will wait for the
15518 * mailbox command to finish before continuing.
15519 *
15520 * On success this function will return a zero. If unable to allocate enough
15521 * memory this function will return -ENOMEM. If the queue create mailbox command
15522 * fails this function will return -ENXIO.
15523 **/
15524int
15525lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15526 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15527 uint32_t subtype)
15528{
15529 struct lpfc_queue *hrq, *drq, *cq;
15530 struct lpfc_mbx_rq_create_v2 *rq_create;
15531 struct lpfc_dmabuf *dmabuf;
15532 LPFC_MBOXQ_t *mbox;
15533 int rc, length, alloclen, status = 0;
15534 int cnt, idx, numrq, page_idx = 0;
15535 uint32_t shdr_status, shdr_add_status;
15536 union lpfc_sli4_cfg_shdr *shdr;
15537 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15538
15539 numrq = phba->cfg_nvmet_mrq;
15540 /* sanity check on array memory */
15541 if (!hrqp || !drqp || !cqp || !numrq)
15542 return -ENODEV;
15543 if (!phba->sli4_hba.pc_sli4_params.supported)
15544 hw_page_size = SLI4_PAGE_SIZE;
15545
15546 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15547 if (!mbox)
15548 return -ENOMEM;
15549
15550 length = sizeof(struct lpfc_mbx_rq_create_v2);
15551 length += ((2 * numrq * hrqp[0]->page_count) *
15552 sizeof(struct dma_address));
15553
15554 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15555 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15556 LPFC_SLI4_MBX_NEMBED);
15557 if (alloclen < length) {
15558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15559 "3099 Allocated DMA memory size (%d) is "
15560 "less than the requested DMA memory size "
15561 "(%d)\n", alloclen, length);
15562 status = -ENOMEM;
15563 goto out;
15564 }
15565
15566
15567
15568 rq_create = mbox->sge_array->addr[0];
15569 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15570
15571 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15572 cnt = 0;
15573
15574 for (idx = 0; idx < numrq; idx++) {
15575 hrq = hrqp[idx];
15576 drq = drqp[idx];
15577 cq = cqp[idx];
15578
James Smart2d7dbc42017-02-12 13:52:35 -080015579 /* sanity check on queue memory */
15580 if (!hrq || !drq || !cq) {
15581 status = -ENODEV;
15582 goto out;
15583 }
15584
James Smart7aabe842017-03-04 09:30:22 -080015585 if (hrq->entry_count != drq->entry_count) {
15586 status = -EINVAL;
15587 goto out;
15588 }
15589
James Smart2d7dbc42017-02-12 13:52:35 -080015590 if (idx == 0) {
15591 bf_set(lpfc_mbx_rq_create_num_pages,
15592 &rq_create->u.request,
15593 hrq->page_count);
15594 bf_set(lpfc_mbx_rq_create_rq_cnt,
15595 &rq_create->u.request, (numrq * 2));
15596 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15597 1);
15598 bf_set(lpfc_rq_context_base_cq,
15599 &rq_create->u.request.context,
15600 cq->queue_id);
15601 bf_set(lpfc_rq_context_data_size,
15602 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015603 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015604 bf_set(lpfc_rq_context_hdr_size,
15605 &rq_create->u.request.context,
15606 LPFC_HDR_BUF_SIZE);
15607 bf_set(lpfc_rq_context_rqe_count_1,
15608 &rq_create->u.request.context,
15609 hrq->entry_count);
15610 bf_set(lpfc_rq_context_rqe_size,
15611 &rq_create->u.request.context,
15612 LPFC_RQE_SIZE_8);
15613 bf_set(lpfc_rq_context_page_size,
15614 &rq_create->u.request.context,
15615 (PAGE_SIZE/SLI4_PAGE_SIZE));
15616 }
15617 rc = 0;
15618 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15619 memset(dmabuf->virt, 0, hw_page_size);
15620 cnt = page_idx + dmabuf->buffer_tag;
15621 rq_create->u.request.page[cnt].addr_lo =
15622 putPaddrLow(dmabuf->phys);
15623 rq_create->u.request.page[cnt].addr_hi =
15624 putPaddrHigh(dmabuf->phys);
15625 rc++;
15626 }
15627 page_idx += rc;
15628
15629 rc = 0;
15630 list_for_each_entry(dmabuf, &drq->page_list, list) {
15631 memset(dmabuf->virt, 0, hw_page_size);
15632 cnt = page_idx + dmabuf->buffer_tag;
15633 rq_create->u.request.page[cnt].addr_lo =
15634 putPaddrLow(dmabuf->phys);
15635 rq_create->u.request.page[cnt].addr_hi =
15636 putPaddrHigh(dmabuf->phys);
15637 rc++;
15638 }
15639 page_idx += rc;
15640
15641 hrq->db_format = LPFC_DB_RING_FORMAT;
15642 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15643 hrq->type = LPFC_HRQ;
15644 hrq->assoc_qid = cq->queue_id;
15645 hrq->subtype = subtype;
15646 hrq->host_index = 0;
15647 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015648 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015649
15650 drq->db_format = LPFC_DB_RING_FORMAT;
15651 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15652 drq->type = LPFC_DRQ;
15653 drq->assoc_qid = cq->queue_id;
15654 drq->subtype = subtype;
15655 drq->host_index = 0;
15656 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015657 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015658
15659 list_add_tail(&hrq->list, &cq->child_list);
15660 list_add_tail(&drq->list, &cq->child_list);
15661 }
15662
15663 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15664 /* The IOCTL status is embedded in the mailbox subheader. */
15665 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15666 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15667 if (shdr_status || shdr_add_status || rc) {
15668 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15669 "3120 RQ_CREATE mailbox failed with "
15670 "status x%x add_status x%x, mbx status x%x\n",
15671 shdr_status, shdr_add_status, rc);
15672 status = -ENXIO;
15673 goto out;
15674 }
15675 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15676 if (rc == 0xFFFF) {
15677 status = -ENXIO;
15678 goto out;
15679 }
15680
15681 /* Initialize all RQs with associated queue id */
15682 for (idx = 0; idx < numrq; idx++) {
15683 hrq = hrqp[idx];
15684 hrq->queue_id = rc + (2 * idx);
15685 drq = drqp[idx];
15686 drq->queue_id = rc + (2 * idx) + 1;
15687 }
15688
15689out:
15690 lpfc_sli4_mbox_cmd_free(phba, mbox);
15691 return status;
15692}
15693
15694/**
James Smart4f774512009-05-22 14:52:35 -040015695 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15696 * @eq: The queue structure associated with the queue to destroy.
15697 *
15698 * This function destroys a queue, as detailed in @eq by sending an mailbox
15699 * command, specific to the type of queue, to the HBA.
15700 *
15701 * The @eq struct is used to get the queue ID of the queue to destroy.
15702 *
15703 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015704 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015705 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015706int
James Smart4f774512009-05-22 14:52:35 -040015707lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15708{
15709 LPFC_MBOXQ_t *mbox;
15710 int rc, length, status = 0;
15711 uint32_t shdr_status, shdr_add_status;
15712 union lpfc_sli4_cfg_shdr *shdr;
15713
James Smart2e90f4b2011-12-13 13:22:37 -050015714 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015715 if (!eq)
15716 return -ENODEV;
15717 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15718 if (!mbox)
15719 return -ENOMEM;
15720 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15721 sizeof(struct lpfc_sli4_cfg_mhdr));
15722 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15723 LPFC_MBOX_OPCODE_EQ_DESTROY,
15724 length, LPFC_SLI4_MBX_EMBED);
15725 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15726 eq->queue_id);
15727 mbox->vport = eq->phba->pport;
15728 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15729
15730 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15731 /* The IOCTL status is embedded in the mailbox subheader. */
15732 shdr = (union lpfc_sli4_cfg_shdr *)
15733 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15734 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15735 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15736 if (shdr_status || shdr_add_status || rc) {
15737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15738 "2505 EQ_DESTROY mailbox failed with "
15739 "status x%x add_status x%x, mbx status x%x\n",
15740 shdr_status, shdr_add_status, rc);
15741 status = -ENXIO;
15742 }
15743
15744 /* Remove eq from any list */
15745 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015746 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015747 return status;
15748}
15749
15750/**
15751 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15752 * @cq: The queue structure associated with the queue to destroy.
15753 *
15754 * This function destroys a queue, as detailed in @cq by sending an mailbox
15755 * command, specific to the type of queue, to the HBA.
15756 *
15757 * The @cq struct is used to get the queue ID of the queue to destroy.
15758 *
15759 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015760 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015761 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015762int
James Smart4f774512009-05-22 14:52:35 -040015763lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15764{
15765 LPFC_MBOXQ_t *mbox;
15766 int rc, length, status = 0;
15767 uint32_t shdr_status, shdr_add_status;
15768 union lpfc_sli4_cfg_shdr *shdr;
15769
James Smart2e90f4b2011-12-13 13:22:37 -050015770 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015771 if (!cq)
15772 return -ENODEV;
15773 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15774 if (!mbox)
15775 return -ENOMEM;
15776 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15777 sizeof(struct lpfc_sli4_cfg_mhdr));
15778 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15779 LPFC_MBOX_OPCODE_CQ_DESTROY,
15780 length, LPFC_SLI4_MBX_EMBED);
15781 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15782 cq->queue_id);
15783 mbox->vport = cq->phba->pport;
15784 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15785 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15786 /* The IOCTL status is embedded in the mailbox subheader. */
15787 shdr = (union lpfc_sli4_cfg_shdr *)
15788 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15789 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15790 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15791 if (shdr_status || shdr_add_status || rc) {
15792 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15793 "2506 CQ_DESTROY mailbox failed with "
15794 "status x%x add_status x%x, mbx status x%x\n",
15795 shdr_status, shdr_add_status, rc);
15796 status = -ENXIO;
15797 }
15798 /* Remove cq from any list */
15799 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015800 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015801 return status;
15802}
15803
15804/**
James Smart04c68492009-05-22 14:52:52 -040015805 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15806 * @qm: The queue structure associated with the queue to destroy.
15807 *
15808 * This function destroys a queue, as detailed in @mq by sending an mailbox
15809 * command, specific to the type of queue, to the HBA.
15810 *
15811 * The @mq struct is used to get the queue ID of the queue to destroy.
15812 *
15813 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015814 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015815 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015816int
James Smart04c68492009-05-22 14:52:52 -040015817lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15818{
15819 LPFC_MBOXQ_t *mbox;
15820 int rc, length, status = 0;
15821 uint32_t shdr_status, shdr_add_status;
15822 union lpfc_sli4_cfg_shdr *shdr;
15823
James Smart2e90f4b2011-12-13 13:22:37 -050015824 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015825 if (!mq)
15826 return -ENODEV;
15827 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15828 if (!mbox)
15829 return -ENOMEM;
15830 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15831 sizeof(struct lpfc_sli4_cfg_mhdr));
15832 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15833 LPFC_MBOX_OPCODE_MQ_DESTROY,
15834 length, LPFC_SLI4_MBX_EMBED);
15835 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15836 mq->queue_id);
15837 mbox->vport = mq->phba->pport;
15838 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15839 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15840 /* The IOCTL status is embedded in the mailbox subheader. */
15841 shdr = (union lpfc_sli4_cfg_shdr *)
15842 &mbox->u.mqe.un.mq_destroy.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 (shdr_status || shdr_add_status || rc) {
15846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15847 "2507 MQ_DESTROY mailbox failed with "
15848 "status x%x add_status x%x, mbx status x%x\n",
15849 shdr_status, shdr_add_status, rc);
15850 status = -ENXIO;
15851 }
15852 /* Remove mq from any list */
15853 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015854 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015855 return status;
15856}
15857
15858/**
James Smart4f774512009-05-22 14:52:35 -040015859 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15860 * @wq: The queue structure associated with the queue to destroy.
15861 *
15862 * This function destroys a queue, as detailed in @wq by sending an mailbox
15863 * command, specific to the type of queue, to the HBA.
15864 *
15865 * The @wq struct is used to get the queue ID of the queue to destroy.
15866 *
15867 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015868 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015869 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015870int
James Smart4f774512009-05-22 14:52:35 -040015871lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15872{
15873 LPFC_MBOXQ_t *mbox;
15874 int rc, length, status = 0;
15875 uint32_t shdr_status, shdr_add_status;
15876 union lpfc_sli4_cfg_shdr *shdr;
15877
James Smart2e90f4b2011-12-13 13:22:37 -050015878 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015879 if (!wq)
15880 return -ENODEV;
15881 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15882 if (!mbox)
15883 return -ENOMEM;
15884 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15885 sizeof(struct lpfc_sli4_cfg_mhdr));
15886 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15887 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15888 length, LPFC_SLI4_MBX_EMBED);
15889 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15890 wq->queue_id);
15891 mbox->vport = wq->phba->pport;
15892 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15893 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15894 shdr = (union lpfc_sli4_cfg_shdr *)
15895 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15896 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15897 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15898 if (shdr_status || shdr_add_status || rc) {
15899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15900 "2508 WQ_DESTROY mailbox failed with "
15901 "status x%x add_status x%x, mbx status x%x\n",
15902 shdr_status, shdr_add_status, rc);
15903 status = -ENXIO;
15904 }
15905 /* Remove wq from any list */
15906 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070015907 kfree(wq->pring);
15908 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040015909 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015910 return status;
15911}
15912
15913/**
15914 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15915 * @rq: The queue structure associated with the queue to destroy.
15916 *
15917 * This function destroys a queue, as detailed in @rq by sending an mailbox
15918 * command, specific to the type of queue, to the HBA.
15919 *
15920 * The @rq struct is used to get the queue ID of the queue to destroy.
15921 *
15922 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015923 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015924 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015925int
James Smart4f774512009-05-22 14:52:35 -040015926lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15927 struct lpfc_queue *drq)
15928{
15929 LPFC_MBOXQ_t *mbox;
15930 int rc, length, status = 0;
15931 uint32_t shdr_status, shdr_add_status;
15932 union lpfc_sli4_cfg_shdr *shdr;
15933
James Smart2e90f4b2011-12-13 13:22:37 -050015934 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015935 if (!hrq || !drq)
15936 return -ENODEV;
15937 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15938 if (!mbox)
15939 return -ENOMEM;
15940 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050015941 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040015942 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15943 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15944 length, LPFC_SLI4_MBX_EMBED);
15945 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15946 hrq->queue_id);
15947 mbox->vport = hrq->phba->pport;
15948 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15949 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15950 /* The IOCTL status is embedded in the mailbox subheader. */
15951 shdr = (union lpfc_sli4_cfg_shdr *)
15952 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15953 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15954 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15955 if (shdr_status || shdr_add_status || rc) {
15956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15957 "2509 RQ_DESTROY mailbox failed with "
15958 "status x%x add_status x%x, mbx status x%x\n",
15959 shdr_status, shdr_add_status, rc);
15960 if (rc != MBX_TIMEOUT)
15961 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15962 return -ENXIO;
15963 }
15964 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15965 drq->queue_id);
15966 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15967 shdr = (union lpfc_sli4_cfg_shdr *)
15968 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15969 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15970 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15971 if (shdr_status || shdr_add_status || rc) {
15972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15973 "2510 RQ_DESTROY mailbox failed with "
15974 "status x%x add_status x%x, mbx status x%x\n",
15975 shdr_status, shdr_add_status, rc);
15976 status = -ENXIO;
15977 }
15978 list_del_init(&hrq->list);
15979 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040015980 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015981 return status;
15982}
15983
15984/**
15985 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15986 * @phba: The virtual port for which this call being executed.
15987 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15988 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15989 * @xritag: the xritag that ties this io to the SGL pages.
15990 *
15991 * This routine will post the sgl pages for the IO that has the xritag
15992 * that is in the iocbq structure. The xritag is assigned during iocbq
15993 * creation and persists for as long as the driver is loaded.
15994 * if the caller has fewer than 256 scatter gather segments to map then
15995 * pdma_phys_addr1 should be 0.
15996 * If the caller needs to map more than 256 scatter gather segment then
15997 * pdma_phys_addr1 should be a valid physical address.
15998 * physical address for SGLs must be 64 byte aligned.
15999 * If you are going to map 2 SGL's then the first one must have 256 entries
16000 * the second sgl can have between 1 and 256 entries.
16001 *
16002 * Return codes:
16003 * 0 - Success
16004 * -ENXIO, -ENOMEM - Failure
16005 **/
16006int
16007lpfc_sli4_post_sgl(struct lpfc_hba *phba,
16008 dma_addr_t pdma_phys_addr0,
16009 dma_addr_t pdma_phys_addr1,
16010 uint16_t xritag)
16011{
16012 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
16013 LPFC_MBOXQ_t *mbox;
16014 int rc;
16015 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040016016 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040016017 union lpfc_sli4_cfg_shdr *shdr;
16018
16019 if (xritag == NO_XRI) {
16020 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16021 "0364 Invalid param:\n");
16022 return -EINVAL;
16023 }
16024
16025 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16026 if (!mbox)
16027 return -ENOMEM;
16028
16029 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16030 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
16031 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050016032 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040016033
16034 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
16035 &mbox->u.mqe.un.post_sgl_pages;
16036 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
16037 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
16038
16039 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
16040 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
16041 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
16042 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
16043
16044 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
16045 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
16046 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
16047 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
16048 if (!phba->sli4_hba.intr_enable)
16049 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040016050 else {
James Smarta183a152011-10-10 21:32:43 -040016051 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040016052 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16053 }
James Smart4f774512009-05-22 14:52:35 -040016054 /* The IOCTL status is embedded in the mailbox subheader. */
16055 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
16056 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16057 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16058 if (rc != MBX_TIMEOUT)
16059 mempool_free(mbox, phba->mbox_mem_pool);
16060 if (shdr_status || shdr_add_status || rc) {
16061 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16062 "2511 POST_SGL mailbox failed with "
16063 "status x%x add_status x%x, mbx status x%x\n",
16064 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040016065 }
16066 return 0;
16067}
James Smart4f774512009-05-22 14:52:35 -040016068
16069/**
James Smart88a2cfb2011-07-22 18:36:33 -040016070 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040016071 * @phba: pointer to lpfc hba data structure.
16072 *
16073 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040016074 * HBA consistent with the SLI-4 interface spec. This routine
16075 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16076 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040016077 *
James Smart88a2cfb2011-07-22 18:36:33 -040016078 * Returns
16079 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
16080 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16081 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016082static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016083lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
16084{
16085 unsigned long xri;
16086
16087 /*
16088 * Fetch the next logical xri. Because this index is logical,
16089 * the driver starts at 0 each time.
16090 */
16091 spin_lock_irq(&phba->hbalock);
16092 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
16093 phba->sli4_hba.max_cfg_param.max_xri, 0);
16094 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
16095 spin_unlock_irq(&phba->hbalock);
16096 return NO_XRI;
16097 } else {
16098 set_bit(xri, phba->sli4_hba.xri_bmask);
16099 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040016100 }
James Smart6d368e52011-05-24 11:44:12 -040016101 spin_unlock_irq(&phba->hbalock);
16102 return xri;
16103}
16104
16105/**
16106 * lpfc_sli4_free_xri - Release an xri for reuse.
16107 * @phba: pointer to lpfc hba data structure.
16108 *
16109 * This routine is invoked to release an xri to the pool of
16110 * available rpis maintained by the driver.
16111 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016112static void
James Smart6d368e52011-05-24 11:44:12 -040016113__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16114{
16115 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016116 phba->sli4_hba.max_cfg_param.xri_used--;
16117 }
16118}
16119
16120/**
16121 * lpfc_sli4_free_xri - Release an xri for reuse.
16122 * @phba: pointer to lpfc hba data structure.
16123 *
16124 * This routine is invoked to release an xri to the pool of
16125 * available rpis maintained by the driver.
16126 **/
16127void
16128lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16129{
16130 spin_lock_irq(&phba->hbalock);
16131 __lpfc_sli4_free_xri(phba, xri);
16132 spin_unlock_irq(&phba->hbalock);
16133}
16134
16135/**
James Smart4f774512009-05-22 14:52:35 -040016136 * lpfc_sli4_next_xritag - Get an xritag for the io
16137 * @phba: Pointer to HBA context object.
16138 *
16139 * This function gets an xritag for the iocb. If there is no unused xritag
16140 * it will return 0xffff.
16141 * The function returns the allocated xritag if successful, else returns zero.
16142 * Zero is not a valid xritag.
16143 * The caller is not required to hold any lock.
16144 **/
16145uint16_t
16146lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16147{
James Smart6d368e52011-05-24 11:44:12 -040016148 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016149
James Smart6d368e52011-05-24 11:44:12 -040016150 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016151 if (xri_index == NO_XRI)
16152 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16153 "2004 Failed to allocate XRI.last XRITAG is %d"
16154 " Max XRI is %d, Used XRI is %d\n",
16155 xri_index,
16156 phba->sli4_hba.max_cfg_param.max_xri,
16157 phba->sli4_hba.max_cfg_param.xri_used);
16158 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016159}
16160
16161/**
James Smart895427b2017-02-12 13:52:30 -080016162 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016163 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016164 * @post_sgl_list: pointer to els sgl entry list.
16165 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016166 *
16167 * This routine is invoked to post a block of driver's sgl pages to the
16168 * HBA using non-embedded mailbox command. No Lock is held. This routine
16169 * is only called when the driver is loading and after all IO has been
16170 * stopped.
16171 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016172static int
James Smart895427b2017-02-12 13:52:30 -080016173lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016174 struct list_head *post_sgl_list,
16175 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016176{
James Smart8a9d2e82012-05-09 21:16:12 -040016177 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016178 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16179 struct sgl_page_pairs *sgl_pg_pairs;
16180 void *viraddr;
16181 LPFC_MBOXQ_t *mbox;
16182 uint32_t reqlen, alloclen, pg_pairs;
16183 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016184 uint16_t xritag_start = 0;
16185 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016186 uint32_t shdr_status, shdr_add_status;
16187 union lpfc_sli4_cfg_shdr *shdr;
16188
James Smart895427b2017-02-12 13:52:30 -080016189 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016190 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016191 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016192 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016193 "2559 Block sgl registration required DMA "
16194 "size (%d) great than a page\n", reqlen);
16195 return -ENOMEM;
16196 }
James Smart895427b2017-02-12 13:52:30 -080016197
James Smart4f774512009-05-22 14:52:35 -040016198 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016199 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016200 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016201
16202 /* Allocate DMA memory and set up the non-embedded mailbox command */
16203 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16204 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16205 LPFC_SLI4_MBX_NEMBED);
16206
16207 if (alloclen < reqlen) {
16208 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16209 "0285 Allocated DMA memory size (%d) is "
16210 "less than the requested DMA memory "
16211 "size (%d)\n", alloclen, reqlen);
16212 lpfc_sli4_mbox_cmd_free(phba, mbox);
16213 return -ENOMEM;
16214 }
James Smart4f774512009-05-22 14:52:35 -040016215 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016216 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016217 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16218 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16219
James Smart8a9d2e82012-05-09 21:16:12 -040016220 pg_pairs = 0;
16221 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016222 /* Set up the sge entry */
16223 sgl_pg_pairs->sgl_pg0_addr_lo =
16224 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16225 sgl_pg_pairs->sgl_pg0_addr_hi =
16226 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16227 sgl_pg_pairs->sgl_pg1_addr_lo =
16228 cpu_to_le32(putPaddrLow(0));
16229 sgl_pg_pairs->sgl_pg1_addr_hi =
16230 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016231
James Smart4f774512009-05-22 14:52:35 -040016232 /* Keep the first xritag on the list */
16233 if (pg_pairs == 0)
16234 xritag_start = sglq_entry->sli4_xritag;
16235 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016236 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016237 }
James Smart6d368e52011-05-24 11:44:12 -040016238
16239 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016240 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016241 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016242 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016243
James Smart4f774512009-05-22 14:52:35 -040016244 if (!phba->sli4_hba.intr_enable)
16245 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16246 else {
James Smarta183a152011-10-10 21:32:43 -040016247 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016248 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16249 }
16250 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16251 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16252 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16253 if (rc != MBX_TIMEOUT)
16254 lpfc_sli4_mbox_cmd_free(phba, mbox);
16255 if (shdr_status || shdr_add_status || rc) {
16256 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16257 "2513 POST_SGL_BLOCK mailbox command failed "
16258 "status x%x add_status x%x mbx status x%x\n",
16259 shdr_status, shdr_add_status, rc);
16260 rc = -ENXIO;
16261 }
16262 return rc;
16263}
16264
16265/**
16266 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16267 * @phba: pointer to lpfc hba data structure.
16268 * @sblist: pointer to scsi buffer list.
16269 * @count: number of scsi buffers on the list.
16270 *
16271 * This routine is invoked to post a block of @count scsi sgl pages from a
16272 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16273 * No Lock is held.
16274 *
16275 **/
16276int
James Smart8a9d2e82012-05-09 21:16:12 -040016277lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16278 struct list_head *sblist,
16279 int count)
James Smart4f774512009-05-22 14:52:35 -040016280{
16281 struct lpfc_scsi_buf *psb;
16282 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16283 struct sgl_page_pairs *sgl_pg_pairs;
16284 void *viraddr;
16285 LPFC_MBOXQ_t *mbox;
16286 uint32_t reqlen, alloclen, pg_pairs;
16287 uint32_t mbox_tmo;
16288 uint16_t xritag_start = 0;
16289 int rc = 0;
16290 uint32_t shdr_status, shdr_add_status;
16291 dma_addr_t pdma_phys_bpl1;
16292 union lpfc_sli4_cfg_shdr *shdr;
16293
16294 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016295 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016296 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016297 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016298 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16299 "0217 Block sgl registration required DMA "
16300 "size (%d) great than a page\n", reqlen);
16301 return -ENOMEM;
16302 }
16303 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16304 if (!mbox) {
16305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16306 "0283 Failed to allocate mbox cmd memory\n");
16307 return -ENOMEM;
16308 }
16309
16310 /* Allocate DMA memory and set up the non-embedded mailbox command */
16311 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16312 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16313 LPFC_SLI4_MBX_NEMBED);
16314
16315 if (alloclen < reqlen) {
16316 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16317 "2561 Allocated DMA memory size (%d) is "
16318 "less than the requested DMA memory "
16319 "size (%d)\n", alloclen, reqlen);
16320 lpfc_sli4_mbox_cmd_free(phba, mbox);
16321 return -ENOMEM;
16322 }
James Smart6d368e52011-05-24 11:44:12 -040016323
James Smart4f774512009-05-22 14:52:35 -040016324 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016325 viraddr = mbox->sge_array->addr[0];
16326
16327 /* Set up the SGL pages in the non-embedded DMA pages */
16328 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16329 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16330
16331 pg_pairs = 0;
16332 list_for_each_entry(psb, sblist, list) {
16333 /* Set up the sge entry */
16334 sgl_pg_pairs->sgl_pg0_addr_lo =
16335 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16336 sgl_pg_pairs->sgl_pg0_addr_hi =
16337 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16338 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16339 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16340 else
16341 pdma_phys_bpl1 = 0;
16342 sgl_pg_pairs->sgl_pg1_addr_lo =
16343 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16344 sgl_pg_pairs->sgl_pg1_addr_hi =
16345 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16346 /* Keep the first xritag on the list */
16347 if (pg_pairs == 0)
16348 xritag_start = psb->cur_iocbq.sli4_xritag;
16349 sgl_pg_pairs++;
16350 pg_pairs++;
16351 }
16352 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16353 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16354 /* Perform endian conversion if necessary */
16355 sgl->word0 = cpu_to_le32(sgl->word0);
16356
16357 if (!phba->sli4_hba.intr_enable)
16358 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16359 else {
James Smarta183a152011-10-10 21:32:43 -040016360 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016361 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16362 }
16363 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16364 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16365 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16366 if (rc != MBX_TIMEOUT)
16367 lpfc_sli4_mbox_cmd_free(phba, mbox);
16368 if (shdr_status || shdr_add_status || rc) {
16369 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16370 "2564 POST_SGL_BLOCK mailbox command failed "
16371 "status x%x add_status x%x mbx status x%x\n",
16372 shdr_status, shdr_add_status, rc);
16373 rc = -ENXIO;
16374 }
16375 return rc;
16376}
16377
16378/**
16379 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16380 * @phba: pointer to lpfc_hba struct that the frame was received on
16381 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16382 *
16383 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16384 * valid type of frame that the LPFC driver will handle. This function will
16385 * return a zero if the frame is a valid frame or a non zero value when the
16386 * frame does not pass the check.
16387 **/
16388static int
16389lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16390{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016391 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016392 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016393 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016394
James Smartae9e28f2017-05-15 15:20:51 -070016395#define FC_RCTL_MDS_DIAGS 0xF4
16396
James Smart4f774512009-05-22 14:52:35 -040016397 switch (fc_hdr->fh_r_ctl) {
16398 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16399 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16400 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16401 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16402 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16403 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16404 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16405 case FC_RCTL_DD_CMD_STATUS: /* command status */
16406 case FC_RCTL_ELS_REQ: /* extended link services request */
16407 case FC_RCTL_ELS_REP: /* extended link services reply */
16408 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16409 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16410 case FC_RCTL_BA_NOP: /* basic link service NOP */
16411 case FC_RCTL_BA_ABTS: /* basic link service abort */
16412 case FC_RCTL_BA_RMC: /* remove connection */
16413 case FC_RCTL_BA_ACC: /* basic accept */
16414 case FC_RCTL_BA_RJT: /* basic reject */
16415 case FC_RCTL_BA_PRMT:
16416 case FC_RCTL_ACK_1: /* acknowledge_1 */
16417 case FC_RCTL_ACK_0: /* acknowledge_0 */
16418 case FC_RCTL_P_RJT: /* port reject */
16419 case FC_RCTL_F_RJT: /* fabric reject */
16420 case FC_RCTL_P_BSY: /* port busy */
16421 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16422 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16423 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016424 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016425 case FC_RCTL_END: /* end */
16426 break;
16427 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16428 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16429 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16430 return lpfc_fc_frame_check(phba, fc_hdr);
16431 default:
16432 goto drop;
16433 }
James Smartae9e28f2017-05-15 15:20:51 -070016434
16435#define FC_TYPE_VENDOR_UNIQUE 0xFF
16436
James Smart4f774512009-05-22 14:52:35 -040016437 switch (fc_hdr->fh_type) {
16438 case FC_TYPE_BLS:
16439 case FC_TYPE_ELS:
16440 case FC_TYPE_FCP:
16441 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016442 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016443 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016444 break;
16445 case FC_TYPE_IP:
16446 case FC_TYPE_ILS:
16447 default:
16448 goto drop;
16449 }
James Smart546fc852011-03-11 16:06:29 -050016450
James Smart4f774512009-05-22 14:52:35 -040016451 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016452 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016453 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016454 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16455 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16456 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16457 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16458 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016459 return 0;
16460drop:
16461 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016462 "2539 Dropped frame rctl:x%x type:x%x\n",
16463 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016464 return 1;
16465}
16466
16467/**
16468 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16469 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16470 *
16471 * This function processes the FC header to retrieve the VFI from the VF
16472 * header, if one exists. This function will return the VFI if one exists
16473 * or 0 if no VSAN Header exists.
16474 **/
16475static uint32_t
16476lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16477{
16478 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16479
16480 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16481 return 0;
16482 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16483}
16484
16485/**
16486 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16487 * @phba: Pointer to the HBA structure to search for the vport on
16488 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16489 * @fcfi: The FC Fabric ID that the frame came from
16490 *
16491 * This function searches the @phba for a vport that matches the content of the
16492 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16493 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16494 * returns the matching vport pointer or NULL if unable to match frame to a
16495 * vport.
16496 **/
16497static struct lpfc_vport *
16498lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016499 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016500{
16501 struct lpfc_vport **vports;
16502 struct lpfc_vport *vport = NULL;
16503 int i;
James Smart939723a2012-05-09 21:19:03 -040016504
James Smartbf086112011-08-21 21:48:13 -040016505 if (did == Fabric_DID)
16506 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016507 if ((phba->pport->fc_flag & FC_PT2PT) &&
16508 !(phba->link_state == LPFC_HBA_READY))
16509 return phba->pport;
16510
James Smart4f774512009-05-22 14:52:35 -040016511 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016512 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016513 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16514 if (phba->fcf.fcfi == fcfi &&
16515 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16516 vports[i]->fc_myDID == did) {
16517 vport = vports[i];
16518 break;
16519 }
16520 }
James Smart895427b2017-02-12 13:52:30 -080016521 }
James Smart4f774512009-05-22 14:52:35 -040016522 lpfc_destroy_vport_work_array(phba, vports);
16523 return vport;
16524}
16525
16526/**
James Smart45ed1192009-10-02 15:17:02 -040016527 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16528 * @vport: The vport to work on.
16529 *
16530 * This function updates the receive sequence time stamp for this vport. The
16531 * receive sequence time stamp indicates the time that the last frame of the
16532 * the sequence that has been idle for the longest amount of time was received.
16533 * the driver uses this time stamp to indicate if any received sequences have
16534 * timed out.
16535 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016536static void
James Smart45ed1192009-10-02 15:17:02 -040016537lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16538{
16539 struct lpfc_dmabuf *h_buf;
16540 struct hbq_dmabuf *dmabuf = NULL;
16541
16542 /* get the oldest sequence on the rcv list */
16543 h_buf = list_get_first(&vport->rcv_buffer_list,
16544 struct lpfc_dmabuf, list);
16545 if (!h_buf)
16546 return;
16547 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16548 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16549}
16550
16551/**
16552 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16553 * @vport: The vport that the received sequences were sent to.
16554 *
16555 * This function cleans up all outstanding received sequences. This is called
16556 * by the driver when a link event or user action invalidates all the received
16557 * sequences.
16558 **/
16559void
16560lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16561{
16562 struct lpfc_dmabuf *h_buf, *hnext;
16563 struct lpfc_dmabuf *d_buf, *dnext;
16564 struct hbq_dmabuf *dmabuf = NULL;
16565
16566 /* start with the oldest sequence on the rcv list */
16567 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16568 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16569 list_del_init(&dmabuf->hbuf.list);
16570 list_for_each_entry_safe(d_buf, dnext,
16571 &dmabuf->dbuf.list, list) {
16572 list_del_init(&d_buf->list);
16573 lpfc_in_buf_free(vport->phba, d_buf);
16574 }
16575 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16576 }
16577}
16578
16579/**
16580 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16581 * @vport: The vport that the received sequences were sent to.
16582 *
16583 * This function determines whether any received sequences have timed out by
16584 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16585 * indicates that there is at least one timed out sequence this routine will
16586 * go through the received sequences one at a time from most inactive to most
16587 * active to determine which ones need to be cleaned up. Once it has determined
16588 * that a sequence needs to be cleaned up it will simply free up the resources
16589 * without sending an abort.
16590 **/
16591void
16592lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16593{
16594 struct lpfc_dmabuf *h_buf, *hnext;
16595 struct lpfc_dmabuf *d_buf, *dnext;
16596 struct hbq_dmabuf *dmabuf = NULL;
16597 unsigned long timeout;
16598 int abort_count = 0;
16599
16600 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16601 vport->rcv_buffer_time_stamp);
16602 if (list_empty(&vport->rcv_buffer_list) ||
16603 time_before(jiffies, timeout))
16604 return;
16605 /* start with the oldest sequence on the rcv list */
16606 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16607 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16608 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16609 dmabuf->time_stamp);
16610 if (time_before(jiffies, timeout))
16611 break;
16612 abort_count++;
16613 list_del_init(&dmabuf->hbuf.list);
16614 list_for_each_entry_safe(d_buf, dnext,
16615 &dmabuf->dbuf.list, list) {
16616 list_del_init(&d_buf->list);
16617 lpfc_in_buf_free(vport->phba, d_buf);
16618 }
16619 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16620 }
16621 if (abort_count)
16622 lpfc_update_rcv_time_stamp(vport);
16623}
16624
16625/**
James Smart4f774512009-05-22 14:52:35 -040016626 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16627 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16628 *
16629 * This function searches through the existing incomplete sequences that have
16630 * been sent to this @vport. If the frame matches one of the incomplete
16631 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16632 * make up that sequence. If no sequence is found that matches this frame then
16633 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16634 * This function returns a pointer to the first dmabuf in the sequence list that
16635 * the frame was linked to.
16636 **/
16637static struct hbq_dmabuf *
16638lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16639{
16640 struct fc_frame_header *new_hdr;
16641 struct fc_frame_header *temp_hdr;
16642 struct lpfc_dmabuf *d_buf;
16643 struct lpfc_dmabuf *h_buf;
16644 struct hbq_dmabuf *seq_dmabuf = NULL;
16645 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016646 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016647
James Smart4d9ab992009-10-02 15:16:39 -040016648 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016649 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016650 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016651
James Smart4f774512009-05-22 14:52:35 -040016652 /* Use the hdr_buf to find the sequence that this frame belongs to */
16653 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16654 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16655 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16656 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16657 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16658 continue;
16659 /* found a pending sequence that matches this frame */
16660 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16661 break;
16662 }
16663 if (!seq_dmabuf) {
16664 /*
16665 * This indicates first frame received for this sequence.
16666 * Queue the buffer on the vport's rcv_buffer_list.
16667 */
16668 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016669 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016670 return dmabuf;
16671 }
16672 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016673 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16674 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016675 list_del_init(&seq_dmabuf->hbuf.list);
16676 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16677 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016678 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016679 return dmabuf;
16680 }
James Smart45ed1192009-10-02 15:17:02 -040016681 /* move this sequence to the tail to indicate a young sequence */
16682 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16683 seq_dmabuf->time_stamp = jiffies;
16684 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016685 if (list_empty(&seq_dmabuf->dbuf.list)) {
16686 temp_hdr = dmabuf->hbuf.virt;
16687 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16688 return seq_dmabuf;
16689 }
James Smart4f774512009-05-22 14:52:35 -040016690 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016691 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16692 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016693 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16694 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16695 /*
16696 * If the frame's sequence count is greater than the frame on
16697 * the list then insert the frame right after this frame
16698 */
James Smarteeead812009-12-21 17:01:23 -050016699 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16700 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016701 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016702 found = 1;
16703 break;
James Smart4f774512009-05-22 14:52:35 -040016704 }
James Smart4360ca92015-12-16 18:12:04 -050016705
16706 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16707 break;
16708 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016709 }
James Smart4360ca92015-12-16 18:12:04 -050016710
16711 if (found)
16712 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016713 return NULL;
16714}
16715
16716/**
James Smart6669f9b2009-10-02 15:16:45 -040016717 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16718 * @vport: pointer to a vitural port
16719 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16720 *
16721 * This function tries to abort from the partially assembed sequence, described
16722 * by the information from basic abbort @dmabuf. It checks to see whether such
16723 * partially assembled sequence held by the driver. If so, it shall free up all
16724 * the frames from the partially assembled sequence.
16725 *
16726 * Return
16727 * true -- if there is matching partially assembled sequence present and all
16728 * the frames freed with the sequence;
16729 * false -- if there is no matching partially assembled sequence present so
16730 * nothing got aborted in the lower layer driver
16731 **/
16732static bool
16733lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16734 struct hbq_dmabuf *dmabuf)
16735{
16736 struct fc_frame_header *new_hdr;
16737 struct fc_frame_header *temp_hdr;
16738 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16739 struct hbq_dmabuf *seq_dmabuf = NULL;
16740
16741 /* Use the hdr_buf to find the sequence that matches this frame */
16742 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16743 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16744 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16745 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16746 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16747 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16748 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16749 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16750 continue;
16751 /* found a pending sequence that matches this frame */
16752 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16753 break;
16754 }
16755
16756 /* Free up all the frames from the partially assembled sequence */
16757 if (seq_dmabuf) {
16758 list_for_each_entry_safe(d_buf, n_buf,
16759 &seq_dmabuf->dbuf.list, list) {
16760 list_del_init(&d_buf->list);
16761 lpfc_in_buf_free(vport->phba, d_buf);
16762 }
16763 return true;
16764 }
16765 return false;
16766}
16767
16768/**
James Smart6dd9e312013-01-03 15:43:37 -050016769 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16770 * @vport: pointer to a vitural port
16771 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16772 *
16773 * This function tries to abort from the assembed sequence from upper level
16774 * protocol, described by the information from basic abbort @dmabuf. It
16775 * checks to see whether such pending context exists at upper level protocol.
16776 * If so, it shall clean up the pending context.
16777 *
16778 * Return
16779 * true -- if there is matching pending context of the sequence cleaned
16780 * at ulp;
16781 * false -- if there is no matching pending context of the sequence present
16782 * at ulp.
16783 **/
16784static bool
16785lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16786{
16787 struct lpfc_hba *phba = vport->phba;
16788 int handled;
16789
16790 /* Accepting abort at ulp with SLI4 only */
16791 if (phba->sli_rev < LPFC_SLI_REV4)
16792 return false;
16793
16794 /* Register all caring upper level protocols to attend abort */
16795 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16796 if (handled)
16797 return true;
16798
16799 return false;
16800}
16801
16802/**
James Smart546fc852011-03-11 16:06:29 -050016803 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016804 * @phba: Pointer to HBA context object.
16805 * @cmd_iocbq: pointer to the command iocbq structure.
16806 * @rsp_iocbq: pointer to the response iocbq structure.
16807 *
James Smart546fc852011-03-11 16:06:29 -050016808 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016809 * event. It properly releases the memory allocated to the sequence abort
16810 * accept iocb.
16811 **/
16812static void
James Smart546fc852011-03-11 16:06:29 -050016813lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016814 struct lpfc_iocbq *cmd_iocbq,
16815 struct lpfc_iocbq *rsp_iocbq)
16816{
James Smart6dd9e312013-01-03 15:43:37 -050016817 struct lpfc_nodelist *ndlp;
16818
16819 if (cmd_iocbq) {
16820 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16821 lpfc_nlp_put(ndlp);
16822 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016823 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016824 }
James Smart6b5151f2012-01-18 16:24:06 -050016825
16826 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16827 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16828 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16829 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16830 rsp_iocbq->iocb.ulpStatus,
16831 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016832}
16833
16834/**
James Smart6d368e52011-05-24 11:44:12 -040016835 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16836 * @phba: Pointer to HBA context object.
16837 * @xri: xri id in transaction.
16838 *
16839 * This function validates the xri maps to the known range of XRIs allocated an
16840 * used by the driver.
16841 **/
James Smart7851fe22011-07-22 18:36:52 -040016842uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016843lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16844 uint16_t xri)
16845{
James Smarta2fc4aef2014-09-03 12:57:55 -040016846 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016847
16848 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16849 if (xri == phba->sli4_hba.xri_ids[i])
16850 return i;
16851 }
16852 return NO_XRI;
16853}
16854
James Smart6d368e52011-05-24 11:44:12 -040016855/**
James Smart546fc852011-03-11 16:06:29 -050016856 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016857 * @phba: Pointer to HBA context object.
16858 * @fc_hdr: pointer to a FC frame header.
16859 *
James Smart546fc852011-03-11 16:06:29 -050016860 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016861 * event after aborting the sequence handling.
16862 **/
James Smart86c67372017-04-21 16:05:04 -070016863void
James Smart6dd9e312013-01-03 15:43:37 -050016864lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16865 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016866{
James Smart6dd9e312013-01-03 15:43:37 -050016867 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040016868 struct lpfc_iocbq *ctiocb = NULL;
16869 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040016870 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050016871 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040016872 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050016873 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040016874
16875 if (!lpfc_is_link_up(phba))
16876 return;
16877
16878 sid = sli4_sid_from_fc_hdr(fc_hdr);
16879 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050016880 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040016881
James Smart6dd9e312013-01-03 15:43:37 -050016882 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040016883 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070016884 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050016885 if (!ndlp) {
16886 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16887 "1268 Failed to allocate ndlp for "
16888 "oxid:x%x SID:x%x\n", oxid, sid);
16889 return;
16890 }
James Smart6dd9e312013-01-03 15:43:37 -050016891 /* Put ndlp onto pport node list */
16892 lpfc_enqueue_node(vport, ndlp);
16893 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16894 /* re-setup ndlp without removing from node list */
16895 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16896 if (!ndlp) {
16897 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16898 "3275 Failed to active ndlp found "
16899 "for oxid:x%x SID:x%x\n", oxid, sid);
16900 return;
16901 }
James Smart6669f9b2009-10-02 15:16:45 -040016902 }
16903
James Smart546fc852011-03-11 16:06:29 -050016904 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040016905 ctiocb = lpfc_sli_get_iocbq(phba);
16906 if (!ctiocb)
16907 return;
16908
James Smart5ffc2662009-11-18 15:39:44 -050016909 /* Extract the F_CTL field from FC_HDR */
16910 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16911
James Smart6669f9b2009-10-02 15:16:45 -040016912 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040016913 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050016914 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040016915 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16916 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16917 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16918
16919 /* Fill in the rest of iocb fields */
16920 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16921 icmd->ulpBdeCount = 0;
16922 icmd->ulpLe = 1;
16923 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040016924 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050016925 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016926
James Smart6669f9b2009-10-02 15:16:45 -040016927 ctiocb->iocb_cmpl = NULL;
16928 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050016929 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040016930 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050016931 ctiocb->sli4_xritag = NO_XRI;
16932
James Smartee0f4fe2012-05-09 21:19:14 -040016933 if (fctl & FC_FC_EX_CTX)
16934 /* Exchange responder sent the abort so we
16935 * own the oxid.
16936 */
16937 xri = oxid;
16938 else
16939 xri = rxid;
16940 lxri = lpfc_sli4_xri_inrange(phba, xri);
16941 if (lxri != NO_XRI)
16942 lpfc_set_rrq_active(phba, ndlp, lxri,
16943 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050016944 /* For BA_ABTS from exchange responder, if the logical xri with
16945 * the oxid maps to the FCP XRI range, the port no longer has
16946 * that exchange context, send a BLS_RJT. Override the IOCB for
16947 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050016948 */
James Smart6dd9e312013-01-03 15:43:37 -050016949 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080016950 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050016951 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16952 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16953 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16954 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16955 }
16956
16957 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16958 * the driver no longer has that exchange, send a BLS_RJT. Override
16959 * the IOCB for a BA_RJT.
16960 */
16961 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050016962 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16963 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16964 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16965 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16966 }
James Smart6669f9b2009-10-02 15:16:45 -040016967
James Smart5ffc2662009-11-18 15:39:44 -050016968 if (fctl & FC_FC_EX_CTX) {
16969 /* ABTS sent by responder to CT exchange, construction
16970 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16971 * field and RX_ID from ABTS for RX_ID field.
16972 */
James Smart546fc852011-03-11 16:06:29 -050016973 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050016974 } else {
16975 /* ABTS sent by initiator to CT exchange, construction
16976 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050016977 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050016978 */
James Smart546fc852011-03-11 16:06:29 -050016979 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050016980 }
James Smartf09c3ac2012-03-01 22:33:29 -050016981 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050016982 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050016983
James Smart546fc852011-03-11 16:06:29 -050016984 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050016985 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16986 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16987 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050016988
16989 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16990 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050016991 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16992 "2925 Failed to issue CT ABTS RSP x%x on "
16993 "xri x%x, Data x%x\n",
16994 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16995 phba->link_state);
16996 lpfc_nlp_put(ndlp);
16997 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050016998 lpfc_sli_release_iocbq(phba, ctiocb);
16999 }
James Smart6669f9b2009-10-02 15:16:45 -040017000}
17001
17002/**
17003 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
17004 * @vport: Pointer to the vport on which this sequence was received
17005 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17006 *
17007 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
17008 * receive sequence is only partially assembed by the driver, it shall abort
17009 * the partially assembled frames for the sequence. Otherwise, if the
17010 * unsolicited receive sequence has been completely assembled and passed to
17011 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
17012 * unsolicited sequence has been aborted. After that, it will issue a basic
17013 * accept to accept the abort.
17014 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017015static void
James Smart6669f9b2009-10-02 15:16:45 -040017016lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
17017 struct hbq_dmabuf *dmabuf)
17018{
17019 struct lpfc_hba *phba = vport->phba;
17020 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050017021 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050017022 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040017023
James Smart6669f9b2009-10-02 15:16:45 -040017024 /* Make a copy of fc_hdr before the dmabuf being released */
17025 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050017026 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040017027
James Smart5ffc2662009-11-18 15:39:44 -050017028 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050017029 /* ABTS by responder to exchange, no cleanup needed */
17030 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050017031 } else {
James Smart6dd9e312013-01-03 15:43:37 -050017032 /* ABTS by initiator to exchange, need to do cleanup */
17033 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
17034 if (aborted == false)
17035 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050017036 }
James Smart6dd9e312013-01-03 15:43:37 -050017037 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17038
James Smart86c67372017-04-21 16:05:04 -070017039 if (phba->nvmet_support) {
17040 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
17041 return;
17042 }
17043
James Smart6dd9e312013-01-03 15:43:37 -050017044 /* Respond with BA_ACC or BA_RJT accordingly */
17045 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040017046}
17047
17048/**
James Smart4f774512009-05-22 14:52:35 -040017049 * lpfc_seq_complete - Indicates if a sequence is complete
17050 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17051 *
17052 * This function checks the sequence, starting with the frame described by
17053 * @dmabuf, to see if all the frames associated with this sequence are present.
17054 * the frames associated with this sequence are linked to the @dmabuf using the
17055 * dbuf list. This function looks for two major things. 1) That the first frame
17056 * has a sequence count of zero. 2) There is a frame with last frame of sequence
17057 * set. 3) That there are no holes in the sequence count. The function will
17058 * return 1 when the sequence is complete, otherwise it will return 0.
17059 **/
17060static int
17061lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
17062{
17063 struct fc_frame_header *hdr;
17064 struct lpfc_dmabuf *d_buf;
17065 struct hbq_dmabuf *seq_dmabuf;
17066 uint32_t fctl;
17067 int seq_count = 0;
17068
17069 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17070 /* make sure first fame of sequence has a sequence count of zero */
17071 if (hdr->fh_seq_cnt != seq_count)
17072 return 0;
17073 fctl = (hdr->fh_f_ctl[0] << 16 |
17074 hdr->fh_f_ctl[1] << 8 |
17075 hdr->fh_f_ctl[2]);
17076 /* If last frame of sequence we can return success. */
17077 if (fctl & FC_FC_END_SEQ)
17078 return 1;
17079 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
17080 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17081 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17082 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050017083 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040017084 return 0;
17085 fctl = (hdr->fh_f_ctl[0] << 16 |
17086 hdr->fh_f_ctl[1] << 8 |
17087 hdr->fh_f_ctl[2]);
17088 /* If last frame of sequence we can return success. */
17089 if (fctl & FC_FC_END_SEQ)
17090 return 1;
17091 }
17092 return 0;
17093}
17094
17095/**
17096 * lpfc_prep_seq - Prep sequence for ULP processing
17097 * @vport: Pointer to the vport on which this sequence was received
17098 * @dmabuf: pointer to a dmabuf that describes the FC sequence
17099 *
17100 * This function takes a sequence, described by a list of frames, and creates
17101 * a list of iocbq structures to describe the sequence. This iocbq list will be
17102 * used to issue to the generic unsolicited sequence handler. This routine
17103 * returns a pointer to the first iocbq in the list. If the function is unable
17104 * to allocate an iocbq then it throw out the received frames that were not
17105 * able to be described and return a pointer to the first iocbq. If unable to
17106 * allocate any iocbqs (including the first) this function will return NULL.
17107 **/
17108static struct lpfc_iocbq *
17109lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
17110{
James Smart7851fe22011-07-22 18:36:52 -040017111 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040017112 struct lpfc_dmabuf *d_buf, *n_buf;
17113 struct lpfc_iocbq *first_iocbq, *iocbq;
17114 struct fc_frame_header *fc_hdr;
17115 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017116 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017117 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017118
17119 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17120 /* remove from receive buffer list */
17121 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017122 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017123 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017124 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017125 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017126 /* Get an iocbq struct to fill in. */
17127 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17128 if (first_iocbq) {
17129 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017130 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017131 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017132 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017133
17134 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17135 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17136 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17137 first_iocbq->iocb.un.rcvels.parmRo =
17138 sli4_did_from_fc_hdr(fc_hdr);
17139 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17140 } else
17141 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017142 first_iocbq->iocb.ulpContext = NO_XRI;
17143 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17144 be16_to_cpu(fc_hdr->fh_ox_id);
17145 /* iocbq is prepped for internal consumption. Physical vpi. */
17146 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17147 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017148 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017149 tot_len = bf_get(lpfc_rcqe_length,
17150 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17151
James Smart4f774512009-05-22 14:52:35 -040017152 first_iocbq->context2 = &seq_dmabuf->dbuf;
17153 first_iocbq->context3 = NULL;
17154 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017155 if (tot_len > LPFC_DATA_BUF_SIZE)
17156 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017157 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017158 else
17159 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17160
James Smart4f774512009-05-22 14:52:35 -040017161 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017162
James Smart7851fe22011-07-22 18:36:52 -040017163 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017164 }
17165 iocbq = first_iocbq;
17166 /*
17167 * Each IOCBq can have two Buffers assigned, so go through the list
17168 * of buffers for this sequence and save two buffers in each IOCBq
17169 */
17170 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17171 if (!iocbq) {
17172 lpfc_in_buf_free(vport->phba, d_buf);
17173 continue;
17174 }
17175 if (!iocbq->context3) {
17176 iocbq->context3 = d_buf;
17177 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017178 /* We need to get the size out of the right CQE */
17179 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17180 len = bf_get(lpfc_rcqe_length,
17181 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017182 pbde = (struct ulp_bde64 *)
17183 &iocbq->iocb.unsli3.sli3Words[4];
17184 if (len > LPFC_DATA_BUF_SIZE)
17185 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17186 else
17187 pbde->tus.f.bdeSize = len;
17188
James Smart7851fe22011-07-22 18:36:52 -040017189 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17190 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017191 } else {
17192 iocbq = lpfc_sli_get_iocbq(vport->phba);
17193 if (!iocbq) {
17194 if (first_iocbq) {
17195 first_iocbq->iocb.ulpStatus =
17196 IOSTAT_FCP_RSP_ERROR;
17197 first_iocbq->iocb.un.ulpWord[4] =
17198 IOERR_NO_RESOURCES;
17199 }
17200 lpfc_in_buf_free(vport->phba, d_buf);
17201 continue;
17202 }
James Smart7851fe22011-07-22 18:36:52 -040017203 /* We need to get the size out of the right CQE */
17204 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17205 len = bf_get(lpfc_rcqe_length,
17206 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017207 iocbq->context2 = d_buf;
17208 iocbq->context3 = NULL;
17209 iocbq->iocb.ulpBdeCount = 1;
17210 if (len > LPFC_DATA_BUF_SIZE)
17211 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17212 LPFC_DATA_BUF_SIZE;
17213 else
17214 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17215
James Smart7851fe22011-07-22 18:36:52 -040017216 tot_len += len;
17217 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17218
James Smart4f774512009-05-22 14:52:35 -040017219 iocbq->iocb.un.rcvels.remoteID = sid;
17220 list_add_tail(&iocbq->list, &first_iocbq->list);
17221 }
17222 }
17223 return first_iocbq;
17224}
17225
James Smart6669f9b2009-10-02 15:16:45 -040017226static void
17227lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17228 struct hbq_dmabuf *seq_dmabuf)
17229{
17230 struct fc_frame_header *fc_hdr;
17231 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17232 struct lpfc_hba *phba = vport->phba;
17233
17234 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17235 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17236 if (!iocbq) {
17237 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17238 "2707 Ring %d handler: Failed to allocate "
17239 "iocb Rctl x%x Type x%x received\n",
17240 LPFC_ELS_RING,
17241 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17242 return;
17243 }
17244 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017245 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017246 iocbq, fc_hdr->fh_r_ctl,
17247 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017248 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017249 "2540 Ring %d handler: unexpected Rctl "
17250 "x%x Type x%x received\n",
17251 LPFC_ELS_RING,
17252 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17253
17254 /* Free iocb created in lpfc_prep_seq */
17255 list_for_each_entry_safe(curr_iocb, next_iocb,
17256 &iocbq->list, list) {
17257 list_del_init(&curr_iocb->list);
17258 lpfc_sli_release_iocbq(phba, curr_iocb);
17259 }
17260 lpfc_sli_release_iocbq(phba, iocbq);
17261}
17262
James Smartae9e28f2017-05-15 15:20:51 -070017263static void
17264lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17265 struct lpfc_iocbq *rspiocb)
17266{
17267 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17268
17269 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017270 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017271 kfree(pcmd);
17272 lpfc_sli_release_iocbq(phba, cmdiocb);
17273}
17274
17275static void
17276lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17277 struct hbq_dmabuf *dmabuf)
17278{
17279 struct fc_frame_header *fc_hdr;
17280 struct lpfc_hba *phba = vport->phba;
17281 struct lpfc_iocbq *iocbq = NULL;
17282 union lpfc_wqe *wqe;
17283 struct lpfc_dmabuf *pcmd = NULL;
17284 uint32_t frame_len;
17285 int rc;
17286
17287 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17288 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17289
17290 /* Send the received frame back */
17291 iocbq = lpfc_sli_get_iocbq(phba);
17292 if (!iocbq)
17293 goto exit;
17294
17295 /* Allocate buffer for command payload */
17296 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17297 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017298 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017299 &pcmd->phys);
17300 if (!pcmd || !pcmd->virt)
17301 goto exit;
17302
17303 INIT_LIST_HEAD(&pcmd->list);
17304
17305 /* copyin the payload */
17306 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17307
17308 /* fill in BDE's for command */
17309 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17310 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17311 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17312 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17313
17314 iocbq->context2 = pcmd;
17315 iocbq->vport = vport;
17316 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17317 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17318
17319 /*
17320 * Setup rest of the iocb as though it were a WQE
17321 * Build the SEND_FRAME WQE
17322 */
17323 wqe = (union lpfc_wqe *)&iocbq->iocb;
17324
17325 wqe->send_frame.frame_len = frame_len;
17326 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17327 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17328 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17329 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17330 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17331 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17332
17333 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17334 iocbq->iocb.ulpLe = 1;
17335 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17336 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17337 if (rc == IOCB_ERROR)
17338 goto exit;
17339
17340 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17341 return;
17342
17343exit:
17344 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17345 "2023 Unable to process MDS loopback frame\n");
17346 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017347 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017348 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017349 if (iocbq)
17350 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017351 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17352}
17353
James Smart4f774512009-05-22 14:52:35 -040017354/**
17355 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17356 * @phba: Pointer to HBA context object.
17357 *
17358 * This function is called with no lock held. This function processes all
17359 * the received buffers and gives it to upper layers when a received buffer
17360 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017361 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017362 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17363 * appropriate receive function when the final frame in a sequence is received.
17364 **/
James Smart4d9ab992009-10-02 15:16:39 -040017365void
17366lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17367 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017368{
James Smart4d9ab992009-10-02 15:16:39 -040017369 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017370 struct fc_frame_header *fc_hdr;
17371 struct lpfc_vport *vport;
17372 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017373 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017374
James Smart4f774512009-05-22 14:52:35 -040017375 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017376 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017377
James Smart4d9ab992009-10-02 15:16:39 -040017378 /* check to see if this a valid type of frame */
17379 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17380 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17381 return;
17382 }
James Smart2ea259e2017-02-12 13:52:27 -080017383
James Smart7851fe22011-07-22 18:36:52 -040017384 if ((bf_get(lpfc_cqe_code,
17385 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17386 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17387 &dmabuf->cq_event.cqe.rcqe_cmpl);
17388 else
17389 fcfi = bf_get(lpfc_rcqe_fcf_id,
17390 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017391
James Smartae9e28f2017-05-15 15:20:51 -070017392 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17393 vport = phba->pport;
17394 /* Handle MDS Loopback frames */
17395 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17396 return;
17397 }
17398
James Smart895427b2017-02-12 13:52:30 -080017399 /* d_id this frame is directed to */
17400 did = sli4_did_from_fc_hdr(fc_hdr);
17401
17402 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017403 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017404 /* throw out the frame */
17405 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17406 return;
17407 }
James Smart939723a2012-05-09 21:19:03 -040017408
James Smart939723a2012-05-09 21:19:03 -040017409 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17410 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17411 (did != Fabric_DID)) {
17412 /*
17413 * Throw out the frame if we are not pt2pt.
17414 * The pt2pt protocol allows for discovery frames
17415 * to be received without a registered VPI.
17416 */
17417 if (!(vport->fc_flag & FC_PT2PT) ||
17418 (phba->link_state == LPFC_HBA_READY)) {
17419 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17420 return;
17421 }
17422 }
17423
James Smart6669f9b2009-10-02 15:16:45 -040017424 /* Handle the basic abort sequence (BA_ABTS) event */
17425 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17426 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17427 return;
17428 }
17429
James Smart4d9ab992009-10-02 15:16:39 -040017430 /* Link this frame */
17431 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17432 if (!seq_dmabuf) {
17433 /* unable to add frame to vport - throw it out */
17434 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17435 return;
17436 }
17437 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017438 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017439 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017440
James Smart6669f9b2009-10-02 15:16:45 -040017441 /* Send the complete sequence to the upper layer protocol */
17442 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017443}
James Smart6fb120a2009-05-22 14:52:59 -040017444
17445/**
17446 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17447 * @phba: pointer to lpfc hba data structure.
17448 *
17449 * This routine is invoked to post rpi header templates to the
17450 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017451 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17452 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017453 *
17454 * This routine does not require any locks. It's usage is expected
17455 * to be driver load or reset recovery when the driver is
17456 * sequential.
17457 *
17458 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017459 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017460 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017461 * When this error occurs, the driver is not guaranteed
17462 * to have any rpi regions posted to the device and
17463 * must either attempt to repost the regions or take a
17464 * fatal error.
17465 **/
17466int
17467lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17468{
17469 struct lpfc_rpi_hdr *rpi_page;
17470 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017471 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017472
James Smart6d368e52011-05-24 11:44:12 -040017473 /* SLI4 ports that support extents do not require RPI headers. */
17474 if (!phba->sli4_hba.rpi_hdrs_in_use)
17475 goto exit;
17476 if (phba->sli4_hba.extents_in_use)
17477 return -EIO;
17478
James Smart6fb120a2009-05-22 14:52:59 -040017479 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017480 /*
17481 * Assign the rpi headers a physical rpi only if the driver
17482 * has not initialized those resources. A port reset only
17483 * needs the headers posted.
17484 */
17485 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17486 LPFC_RPI_RSRC_RDY)
17487 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17488
James Smart6fb120a2009-05-22 14:52:59 -040017489 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17490 if (rc != MBX_SUCCESS) {
17491 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17492 "2008 Error %d posting all rpi "
17493 "headers\n", rc);
17494 rc = -EIO;
17495 break;
17496 }
17497 }
17498
James Smart6d368e52011-05-24 11:44:12 -040017499 exit:
17500 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17501 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017502 return rc;
17503}
17504
17505/**
17506 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17507 * @phba: pointer to lpfc hba data structure.
17508 * @rpi_page: pointer to the rpi memory region.
17509 *
17510 * This routine is invoked to post a single rpi header to the
17511 * HBA consistent with the SLI-4 interface spec. This memory region
17512 * maps up to 64 rpi context regions.
17513 *
17514 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017515 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017516 * -ENOMEM - No available memory
17517 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017518 **/
17519int
17520lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17521{
17522 LPFC_MBOXQ_t *mboxq;
17523 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17524 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017525 uint32_t shdr_status, shdr_add_status;
17526 union lpfc_sli4_cfg_shdr *shdr;
17527
James Smart6d368e52011-05-24 11:44:12 -040017528 /* SLI4 ports that support extents do not require RPI headers. */
17529 if (!phba->sli4_hba.rpi_hdrs_in_use)
17530 return rc;
17531 if (phba->sli4_hba.extents_in_use)
17532 return -EIO;
17533
James Smart6fb120a2009-05-22 14:52:59 -040017534 /* The port is notified of the header region via a mailbox command. */
17535 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17536 if (!mboxq) {
17537 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17538 "2001 Unable to allocate memory for issuing "
17539 "SLI_CONFIG_SPECIAL mailbox command\n");
17540 return -ENOMEM;
17541 }
17542
17543 /* Post all rpi memory regions to the port. */
17544 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017545 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17546 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17547 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017548 sizeof(struct lpfc_sli4_cfg_mhdr),
17549 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017550
17551
17552 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017553 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17554 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017555 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17556 hdr_tmpl, rpi_page->page_count);
17557
James Smart6fb120a2009-05-22 14:52:59 -040017558 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17559 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017560 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017561 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17562 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17563 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17564 if (rc != MBX_TIMEOUT)
17565 mempool_free(mboxq, phba->mbox_mem_pool);
17566 if (shdr_status || shdr_add_status || rc) {
17567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17568 "2514 POST_RPI_HDR mailbox failed with "
17569 "status x%x add_status x%x, mbx status x%x\n",
17570 shdr_status, shdr_add_status, rc);
17571 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017572 } else {
17573 /*
17574 * The next_rpi stores the next logical module-64 rpi value used
17575 * to post physical rpis in subsequent rpi postings.
17576 */
17577 spin_lock_irq(&phba->hbalock);
17578 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17579 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017580 }
17581 return rc;
17582}
17583
17584/**
17585 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17586 * @phba: pointer to lpfc hba data structure.
17587 *
17588 * This routine is invoked to post rpi header templates to the
17589 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017590 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17591 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017592 *
17593 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017594 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017595 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17596 **/
17597int
17598lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17599{
James Smart6d368e52011-05-24 11:44:12 -040017600 unsigned long rpi;
17601 uint16_t max_rpi, rpi_limit;
17602 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017603 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017604 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017605
James Smart6fb120a2009-05-22 14:52:59 -040017606 /*
James Smart6d368e52011-05-24 11:44:12 -040017607 * Fetch the next logical rpi. Because this index is logical,
17608 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017609 */
James Smart4902b382013-10-10 12:20:35 -040017610 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017611 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17612 rpi_limit = phba->sli4_hba.next_rpi;
17613
James Smart6d368e52011-05-24 11:44:12 -040017614 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17615 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017616 rpi = LPFC_RPI_ALLOC_ERROR;
17617 else {
17618 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17619 phba->sli4_hba.max_cfg_param.rpi_used++;
17620 phba->sli4_hba.rpi_count++;
17621 }
James Smartbe6bb942015-04-07 15:07:22 -040017622 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17623 "0001 rpi:%x max:%x lim:%x\n",
17624 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017625
17626 /*
17627 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017628 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017629 */
17630 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17631 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017632 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017633 return rpi;
17634 }
17635
17636 /*
James Smart6d368e52011-05-24 11:44:12 -040017637 * RPI header postings are not required for SLI4 ports capable of
17638 * extents.
17639 */
17640 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017641 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017642 return rpi;
17643 }
17644
17645 /*
James Smart6fb120a2009-05-22 14:52:59 -040017646 * If the driver is running low on rpi resources, allocate another
17647 * page now. Note that the next_rpi value is used because
17648 * it represents how many are actually in use whereas max_rpi notes
17649 * how many are supported max by the device.
17650 */
James Smart6d368e52011-05-24 11:44:12 -040017651 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017652 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017653 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17654 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17655 if (!rpi_hdr) {
17656 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17657 "2002 Error Could not grow rpi "
17658 "count\n");
17659 } else {
James Smart6d368e52011-05-24 11:44:12 -040017660 lrpi = rpi_hdr->start_rpi;
17661 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017662 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17663 }
17664 }
17665
17666 return rpi;
17667}
17668
17669/**
17670 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17671 * @phba: pointer to lpfc hba data structure.
17672 *
17673 * This routine is invoked to release an rpi to the pool of
17674 * available rpis maintained by the driver.
17675 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017676static void
James Smartd7c47992010-06-08 18:31:54 -040017677__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17678{
17679 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17680 phba->sli4_hba.rpi_count--;
17681 phba->sli4_hba.max_cfg_param.rpi_used--;
17682 }
17683}
17684
17685/**
17686 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17687 * @phba: pointer to lpfc hba data structure.
17688 *
17689 * This routine is invoked to release an rpi to the pool of
17690 * available rpis maintained by the driver.
17691 **/
17692void
James Smart6fb120a2009-05-22 14:52:59 -040017693lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17694{
17695 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017696 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017697 spin_unlock_irq(&phba->hbalock);
17698}
17699
17700/**
17701 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17702 * @phba: pointer to lpfc hba data structure.
17703 *
17704 * This routine is invoked to remove the memory region that
17705 * provided rpi via a bitmask.
17706 **/
17707void
17708lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17709{
17710 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017711 kfree(phba->sli4_hba.rpi_ids);
17712 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017713}
17714
17715/**
17716 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17717 * @phba: pointer to lpfc hba data structure.
17718 *
17719 * This routine is invoked to remove the memory region that
17720 * provided rpi via a bitmask.
17721 **/
17722int
James Smart6b5151f2012-01-18 16:24:06 -050017723lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17724 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017725{
17726 LPFC_MBOXQ_t *mboxq;
17727 struct lpfc_hba *phba = ndlp->phba;
17728 int rc;
17729
17730 /* The port is notified of the header region via a mailbox command. */
17731 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17732 if (!mboxq)
17733 return -ENOMEM;
17734
17735 /* Post all rpi memory regions to the port. */
17736 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017737 if (cmpl) {
17738 mboxq->mbox_cmpl = cmpl;
17739 mboxq->context1 = arg;
17740 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017741 } else
17742 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017743 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017744 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17745 if (rc == MBX_NOT_FINISHED) {
17746 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17747 "2010 Resume RPI Mailbox failed "
17748 "status %d, mbxStatus x%x\n", rc,
17749 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17750 mempool_free(mboxq, phba->mbox_mem_pool);
17751 return -EIO;
17752 }
17753 return 0;
17754}
17755
17756/**
17757 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017758 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017759 *
James Smart76a95d72010-11-20 23:11:48 -050017760 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017761 *
17762 * Returns:
17763 * 0 success
17764 * -Evalue otherwise
17765 **/
17766int
James Smart76a95d72010-11-20 23:11:48 -050017767lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017768{
17769 LPFC_MBOXQ_t *mboxq;
17770 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017771 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017772 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017773 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017774 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17775 if (!mboxq)
17776 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017777 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017778 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017779 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017780 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017781 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017782 "2022 INIT VPI Mailbox failed "
17783 "status %d, mbxStatus x%x\n", rc,
17784 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017785 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017786 }
James Smart6a9c52c2009-10-02 15:16:51 -040017787 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017788 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017789
17790 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017791}
17792
17793/**
17794 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17795 * @phba: pointer to lpfc hba data structure.
17796 * @mboxq: Pointer to mailbox object.
17797 *
17798 * This routine is invoked to manually add a single FCF record. The caller
17799 * must pass a completely initialized FCF_Record. This routine takes
17800 * care of the nonembedded mailbox operations.
17801 **/
17802static void
17803lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17804{
17805 void *virt_addr;
17806 union lpfc_sli4_cfg_shdr *shdr;
17807 uint32_t shdr_status, shdr_add_status;
17808
17809 virt_addr = mboxq->sge_array->addr[0];
17810 /* The IOCTL status is embedded in the mailbox subheader. */
17811 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17812 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17813 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17814
17815 if ((shdr_status || shdr_add_status) &&
17816 (shdr_status != STATUS_FCF_IN_USE))
17817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17818 "2558 ADD_FCF_RECORD mailbox failed with "
17819 "status x%x add_status x%x\n",
17820 shdr_status, shdr_add_status);
17821
17822 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17823}
17824
17825/**
17826 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17827 * @phba: pointer to lpfc hba data structure.
17828 * @fcf_record: pointer to the initialized fcf record to add.
17829 *
17830 * This routine is invoked to manually add a single FCF record. The caller
17831 * must pass a completely initialized FCF_Record. This routine takes
17832 * care of the nonembedded mailbox operations.
17833 **/
17834int
17835lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17836{
17837 int rc = 0;
17838 LPFC_MBOXQ_t *mboxq;
17839 uint8_t *bytep;
17840 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017841 struct lpfc_mbx_sge sge;
17842 uint32_t alloc_len, req_len;
17843 uint32_t fcfindex;
17844
17845 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17846 if (!mboxq) {
17847 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17848 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17849 return -ENOMEM;
17850 }
17851
17852 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17853 sizeof(uint32_t);
17854
17855 /* Allocate DMA memory and set up the non-embedded mailbox command */
17856 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17857 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17858 req_len, LPFC_SLI4_MBX_NEMBED);
17859 if (alloc_len < req_len) {
17860 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17861 "2523 Allocated DMA memory size (x%x) is "
17862 "less than the requested DMA memory "
17863 "size (x%x)\n", alloc_len, req_len);
17864 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17865 return -ENOMEM;
17866 }
17867
17868 /*
17869 * Get the first SGE entry from the non-embedded DMA memory. This
17870 * routine only uses a single SGE.
17871 */
17872 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040017873 virt_addr = mboxq->sge_array->addr[0];
17874 /*
17875 * Configure the FCF record for FCFI 0. This is the driver's
17876 * hardcoded default and gets used in nonFIP mode.
17877 */
17878 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17879 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17880 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17881
17882 /*
17883 * Copy the fcf_index and the FCF Record Data. The data starts after
17884 * the FCoE header plus word10. The data copy needs to be endian
17885 * correct.
17886 */
17887 bytep += sizeof(uint32_t);
17888 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17889 mboxq->vport = phba->pport;
17890 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17891 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17892 if (rc == MBX_NOT_FINISHED) {
17893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17894 "2515 ADD_FCF_RECORD mailbox failed with "
17895 "status 0x%x\n", rc);
17896 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17897 rc = -EIO;
17898 } else
17899 rc = 0;
17900
17901 return rc;
17902}
17903
17904/**
17905 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17906 * @phba: pointer to lpfc hba data structure.
17907 * @fcf_record: pointer to the fcf record to write the default data.
17908 * @fcf_index: FCF table entry index.
17909 *
17910 * This routine is invoked to build the driver's default FCF record. The
17911 * values used are hardcoded. This routine handles memory initialization.
17912 *
17913 **/
17914void
17915lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17916 struct fcf_record *fcf_record,
17917 uint16_t fcf_index)
17918{
17919 memset(fcf_record, 0, sizeof(struct fcf_record));
17920 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17921 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17922 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17923 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17924 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17925 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17926 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17927 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17928 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17929 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17930 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17931 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17932 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040017933 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040017934 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17935 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17936 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17937 /* Set the VLAN bit map */
17938 if (phba->valid_vlan) {
17939 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17940 = 1 << (phba->vlan_id % 8);
17941 }
17942}
17943
17944/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017945 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040017946 * @phba: pointer to lpfc hba data structure.
17947 * @fcf_index: FCF table entry offset.
17948 *
James Smart0c9ab6f2010-02-26 14:15:57 -050017949 * This routine is invoked to scan the entire FCF table by reading FCF
17950 * record and processing it one at a time starting from the @fcf_index
17951 * for initial FCF discovery or fast FCF failover rediscovery.
17952 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017953 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017954 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040017955 **/
17956int
James Smart0c9ab6f2010-02-26 14:15:57 -050017957lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040017958{
17959 int rc = 0, error;
17960 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040017961
James Smart32b97932009-07-19 10:01:21 -040017962 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050017963 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040017964 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17965 if (!mboxq) {
17966 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17967 "2000 Failed to allocate mbox for "
17968 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040017969 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050017970 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017971 }
James Smartecfd03c2010-02-12 14:41:27 -050017972 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050017973 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050017974 if (rc) {
17975 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050017976 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017977 }
James Smartecfd03c2010-02-12 14:41:27 -050017978 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040017979 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050017980 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040017981
17982 spin_lock_irq(&phba->hbalock);
17983 phba->hba_flag |= FCF_TS_INPROG;
17984 spin_unlock_irq(&phba->hbalock);
17985
James Smart6fb120a2009-05-22 14:52:59 -040017986 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050017987 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040017988 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050017989 else {
James Smart38b92ef2010-08-04 16:11:39 -040017990 /* Reset eligible FCF count for new scan */
17991 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040017992 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017993 error = 0;
James Smart32b97932009-07-19 10:01:21 -040017994 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017995fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040017996 if (error) {
17997 if (mboxq)
17998 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040017999 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040018000 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040018001 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040018002 spin_unlock_irq(&phba->hbalock);
18003 }
James Smart6fb120a2009-05-22 14:52:59 -040018004 return error;
18005}
James Smarta0c87cb2009-07-19 10:01:10 -040018006
18007/**
James Smarta93ff372010-10-22 11:06:08 -040018008 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050018009 * @phba: pointer to lpfc hba data structure.
18010 * @fcf_index: FCF table entry offset.
18011 *
18012 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040018013 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050018014 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018015 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018016 * otherwise.
18017 **/
18018int
18019lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18020{
18021 int rc = 0, error;
18022 LPFC_MBOXQ_t *mboxq;
18023
18024 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18025 if (!mboxq) {
18026 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18027 "2763 Failed to allocate mbox for "
18028 "READ_FCF cmd\n");
18029 error = -ENOMEM;
18030 goto fail_fcf_read;
18031 }
18032 /* Construct the read FCF record mailbox command */
18033 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18034 if (rc) {
18035 error = -EINVAL;
18036 goto fail_fcf_read;
18037 }
18038 /* Issue the mailbox command asynchronously */
18039 mboxq->vport = phba->pport;
18040 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
18041 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18042 if (rc == MBX_NOT_FINISHED)
18043 error = -EIO;
18044 else
18045 error = 0;
18046
18047fail_fcf_read:
18048 if (error && mboxq)
18049 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18050 return error;
18051}
18052
18053/**
18054 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
18055 * @phba: pointer to lpfc hba data structure.
18056 * @fcf_index: FCF table entry offset.
18057 *
18058 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040018059 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050018060 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030018061 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050018062 * otherwise.
18063 **/
18064int
18065lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
18066{
18067 int rc = 0, error;
18068 LPFC_MBOXQ_t *mboxq;
18069
18070 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18071 if (!mboxq) {
18072 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
18073 "2758 Failed to allocate mbox for "
18074 "READ_FCF cmd\n");
18075 error = -ENOMEM;
18076 goto fail_fcf_read;
18077 }
18078 /* Construct the read FCF record mailbox command */
18079 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
18080 if (rc) {
18081 error = -EINVAL;
18082 goto fail_fcf_read;
18083 }
18084 /* Issue the mailbox command asynchronously */
18085 mboxq->vport = phba->pport;
18086 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
18087 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
18088 if (rc == MBX_NOT_FINISHED)
18089 error = -EIO;
18090 else
18091 error = 0;
18092
18093fail_fcf_read:
18094 if (error && mboxq)
18095 lpfc_sli4_mbox_cmd_free(phba, mboxq);
18096 return error;
18097}
18098
18099/**
James Smartf5cb5302015-12-16 18:11:52 -050018100 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040018101 * phba pointer to the lpfc_hba struct for this port.
18102 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
18103 * routine when the rr_bmask is empty. The FCF indecies are put into the
18104 * rr_bmask based on their priority level. Starting from the highest priority
18105 * to the lowest. The most likely FCF candidate will be in the highest
18106 * priority group. When this routine is called it searches the fcf_pri list for
18107 * next lowest priority group and repopulates the rr_bmask with only those
18108 * fcf_indexes.
18109 * returns:
18110 * 1=success 0=failure
18111 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018112static int
James Smart7d791df2011-07-22 18:37:52 -040018113lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
18114{
18115 uint16_t next_fcf_pri;
18116 uint16_t last_index;
18117 struct lpfc_fcf_pri *fcf_pri;
18118 int rc;
18119 int ret = 0;
18120
18121 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18122 LPFC_SLI4_FCF_TBL_INDX_MAX);
18123 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18124 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018125
18126 /* Verify the priority list has 2 or more entries */
18127 spin_lock_irq(&phba->hbalock);
18128 if (list_empty(&phba->fcf.fcf_pri_list) ||
18129 list_is_singular(&phba->fcf.fcf_pri_list)) {
18130 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018131 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18132 "3061 Last IDX %d\n", last_index);
18133 return 0; /* Empty rr list */
18134 }
James Smart25626692013-03-01 16:36:54 -050018135 spin_unlock_irq(&phba->hbalock);
18136
James Smart7d791df2011-07-22 18:37:52 -040018137 next_fcf_pri = 0;
18138 /*
18139 * Clear the rr_bmask and set all of the bits that are at this
18140 * priority.
18141 */
18142 memset(phba->fcf.fcf_rr_bmask, 0,
18143 sizeof(*phba->fcf.fcf_rr_bmask));
18144 spin_lock_irq(&phba->hbalock);
18145 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18146 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18147 continue;
18148 /*
18149 * the 1st priority that has not FLOGI failed
18150 * will be the highest.
18151 */
18152 if (!next_fcf_pri)
18153 next_fcf_pri = fcf_pri->fcf_rec.priority;
18154 spin_unlock_irq(&phba->hbalock);
18155 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18156 rc = lpfc_sli4_fcf_rr_index_set(phba,
18157 fcf_pri->fcf_rec.fcf_index);
18158 if (rc)
18159 return 0;
18160 }
18161 spin_lock_irq(&phba->hbalock);
18162 }
18163 /*
18164 * if next_fcf_pri was not set above and the list is not empty then
18165 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018166 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018167 */
18168 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18169 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18170 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18171 /*
18172 * the 1st priority that has not FLOGI failed
18173 * will be the highest.
18174 */
18175 if (!next_fcf_pri)
18176 next_fcf_pri = fcf_pri->fcf_rec.priority;
18177 spin_unlock_irq(&phba->hbalock);
18178 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18179 rc = lpfc_sli4_fcf_rr_index_set(phba,
18180 fcf_pri->fcf_rec.fcf_index);
18181 if (rc)
18182 return 0;
18183 }
18184 spin_lock_irq(&phba->hbalock);
18185 }
18186 } else
18187 ret = 1;
18188 spin_unlock_irq(&phba->hbalock);
18189
18190 return ret;
18191}
18192/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018193 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18194 * @phba: pointer to lpfc hba data structure.
18195 *
18196 * This routine is to get the next eligible FCF record index in a round
18197 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018198 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018199 * shall be returned, otherwise, the next eligible FCF record's index
18200 * shall be returned.
18201 **/
18202uint16_t
18203lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18204{
18205 uint16_t next_fcf_index;
18206
James Smart421c6622013-01-03 15:44:16 -050018207initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018208 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018209 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18210
James Smart7d791df2011-07-22 18:37:52 -040018211next_priority:
James Smart421c6622013-01-03 15:44:16 -050018212 /* Determine the next fcf index to check */
18213 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018214 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18215 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018216 next_fcf_index);
18217
James Smart0c9ab6f2010-02-26 14:15:57 -050018218 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018219 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18220 /*
18221 * If we have wrapped then we need to clear the bits that
18222 * have been tested so that we can detect when we should
18223 * change the priority level.
18224 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018225 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18226 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018227 }
18228
James Smart0c9ab6f2010-02-26 14:15:57 -050018229
James Smart3804dc82010-07-14 15:31:37 -040018230 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018231 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18232 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18233 /*
18234 * If next fcf index is not found check if there are lower
18235 * Priority level fcf's in the fcf_priority list.
18236 * Set up the rr_bmask with all of the avaiable fcf bits
18237 * at that level and continue the selection process.
18238 */
18239 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018240 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018241 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18242 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018243 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18244 return LPFC_FCOE_FCF_NEXT_NONE;
18245 else {
18246 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18247 "3063 Only FCF available idx %d, flag %x\n",
18248 next_fcf_index,
18249 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18250 return next_fcf_index;
18251 }
James Smart3804dc82010-07-14 15:31:37 -040018252 }
18253
James Smart7d791df2011-07-22 18:37:52 -040018254 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18255 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018256 LPFC_FCF_FLOGI_FAILED) {
18257 if (list_is_singular(&phba->fcf.fcf_pri_list))
18258 return LPFC_FCOE_FCF_NEXT_NONE;
18259
James Smart7d791df2011-07-22 18:37:52 -040018260 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018261 }
James Smart7d791df2011-07-22 18:37:52 -040018262
James Smart3804dc82010-07-14 15:31:37 -040018263 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018264 "2845 Get next roundrobin failover FCF (x%x)\n",
18265 next_fcf_index);
18266
James Smart0c9ab6f2010-02-26 14:15:57 -050018267 return next_fcf_index;
18268}
18269
18270/**
18271 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18272 * @phba: pointer to lpfc hba data structure.
18273 *
18274 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018275 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018276 * does not go beyond the range of the driver allocated bmask dimension
18277 * before setting the bit.
18278 *
18279 * Returns 0 if the index bit successfully set, otherwise, it returns
18280 * -EINVAL.
18281 **/
18282int
18283lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18284{
18285 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18286 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018287 "2610 FCF (x%x) reached driver's book "
18288 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018289 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18290 return -EINVAL;
18291 }
18292 /* Set the eligible FCF record index bmask */
18293 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18294
James Smart3804dc82010-07-14 15:31:37 -040018295 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018296 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018297 "bmask\n", fcf_index);
18298
James Smart0c9ab6f2010-02-26 14:15:57 -050018299 return 0;
18300}
18301
18302/**
James Smart3804dc82010-07-14 15:31:37 -040018303 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018304 * @phba: pointer to lpfc hba data structure.
18305 *
18306 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018307 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018308 * does not go beyond the range of the driver allocated bmask dimension
18309 * before clearing the bit.
18310 **/
18311void
18312lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18313{
James Smart9a803a72013-09-06 12:17:56 -040018314 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018315 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18316 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018317 "2762 FCF (x%x) reached driver's book "
18318 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018319 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18320 return;
18321 }
18322 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018323 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018324 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18325 list) {
James Smart7d791df2011-07-22 18:37:52 -040018326 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18327 list_del_init(&fcf_pri->list);
18328 break;
18329 }
18330 }
18331 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018332 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018333
18334 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018335 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018336 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018337}
18338
18339/**
James Smartecfd03c2010-02-12 14:41:27 -050018340 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18341 * @phba: pointer to lpfc hba data structure.
18342 *
18343 * This routine is the completion routine for the rediscover FCF table mailbox
18344 * command. If the mailbox command returned failure, it will try to stop the
18345 * FCF rediscover wait timer.
18346 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018347static void
James Smartecfd03c2010-02-12 14:41:27 -050018348lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18349{
18350 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18351 uint32_t shdr_status, shdr_add_status;
18352
18353 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18354
18355 shdr_status = bf_get(lpfc_mbox_hdr_status,
18356 &redisc_fcf->header.cfg_shdr.response);
18357 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18358 &redisc_fcf->header.cfg_shdr.response);
18359 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018360 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018361 "2746 Requesting for FCF rediscovery failed "
18362 "status x%x add_status x%x\n",
18363 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018364 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018365 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018366 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018367 spin_unlock_irq(&phba->hbalock);
18368 /*
18369 * CVL event triggered FCF rediscover request failed,
18370 * last resort to re-try current registered FCF entry.
18371 */
18372 lpfc_retry_pport_discovery(phba);
18373 } else {
18374 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018375 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018376 spin_unlock_irq(&phba->hbalock);
18377 /*
18378 * DEAD FCF event triggered FCF rediscover request
18379 * failed, last resort to fail over as a link down
18380 * to FCF registration.
18381 */
18382 lpfc_sli4_fcf_dead_failthrough(phba);
18383 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018384 } else {
18385 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018386 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018387 /*
18388 * Start FCF rediscovery wait timer for pending FCF
18389 * before rescan FCF record table.
18390 */
18391 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018392 }
James Smartecfd03c2010-02-12 14:41:27 -050018393
18394 mempool_free(mbox, phba->mbox_mem_pool);
18395}
18396
18397/**
James Smart3804dc82010-07-14 15:31:37 -040018398 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018399 * @phba: pointer to lpfc hba data structure.
18400 *
18401 * This routine is invoked to request for rediscovery of the entire FCF table
18402 * by the port.
18403 **/
18404int
18405lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18406{
18407 LPFC_MBOXQ_t *mbox;
18408 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18409 int rc, length;
18410
James Smart0c9ab6f2010-02-26 14:15:57 -050018411 /* Cancel retry delay timers to all vports before FCF rediscover */
18412 lpfc_cancel_all_vport_retry_delay_timer(phba);
18413
James Smartecfd03c2010-02-12 14:41:27 -050018414 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18415 if (!mbox) {
18416 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18417 "2745 Failed to allocate mbox for "
18418 "requesting FCF rediscover.\n");
18419 return -ENOMEM;
18420 }
18421
18422 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18423 sizeof(struct lpfc_sli4_cfg_mhdr));
18424 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18425 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18426 length, LPFC_SLI4_MBX_EMBED);
18427
18428 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18429 /* Set count to 0 for invalidating the entire FCF database */
18430 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18431
18432 /* Issue the mailbox command asynchronously */
18433 mbox->vport = phba->pport;
18434 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18435 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18436
18437 if (rc == MBX_NOT_FINISHED) {
18438 mempool_free(mbox, phba->mbox_mem_pool);
18439 return -EIO;
18440 }
18441 return 0;
18442}
18443
18444/**
James Smartfc2b9892010-02-26 14:15:29 -050018445 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18446 * @phba: pointer to lpfc hba data structure.
18447 *
18448 * This function is the failover routine as a last resort to the FCF DEAD
18449 * event when driver failed to perform fast FCF failover.
18450 **/
18451void
18452lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18453{
18454 uint32_t link_state;
18455
18456 /*
18457 * Last resort as FCF DEAD event failover will treat this as
18458 * a link down, but save the link state because we don't want
18459 * it to be changed to Link Down unless it is already down.
18460 */
18461 link_state = phba->link_state;
18462 lpfc_linkdown(phba);
18463 phba->link_state = link_state;
18464
18465 /* Unregister FCF if no devices connected to it */
18466 lpfc_unregister_unused_fcf(phba);
18467}
18468
18469/**
James Smart026abb82011-12-13 13:20:45 -050018470 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018471 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018472 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018473 *
James Smart026abb82011-12-13 13:20:45 -050018474 * This function gets SLI3 port configure region 23 data through memory dump
18475 * mailbox command. When it successfully retrieves data, the size of the data
18476 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018477 **/
James Smart026abb82011-12-13 13:20:45 -050018478static uint32_t
18479lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018480{
18481 LPFC_MBOXQ_t *pmb = NULL;
18482 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018483 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018484 int rc;
18485
James Smart026abb82011-12-13 13:20:45 -050018486 if (!rgn23_data)
18487 return 0;
18488
James Smarta0c87cb2009-07-19 10:01:10 -040018489 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18490 if (!pmb) {
18491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018492 "2600 failed to allocate mailbox memory\n");
18493 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018494 }
18495 mb = &pmb->u.mb;
18496
James Smarta0c87cb2009-07-19 10:01:10 -040018497 do {
18498 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18499 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18500
18501 if (rc != MBX_SUCCESS) {
18502 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018503 "2601 failed to read config "
18504 "region 23, rc 0x%x Status 0x%x\n",
18505 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018506 mb->un.varDmp.word_cnt = 0;
18507 }
18508 /*
18509 * dump mem may return a zero when finished or we got a
18510 * mailbox error, either way we are done.
18511 */
18512 if (mb->un.varDmp.word_cnt == 0)
18513 break;
18514 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18515 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18516
18517 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018518 rgn23_data + offset,
18519 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018520 offset += mb->un.varDmp.word_cnt;
18521 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18522
James Smart026abb82011-12-13 13:20:45 -050018523 mempool_free(pmb, phba->mbox_mem_pool);
18524 return offset;
18525}
18526
18527/**
18528 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18529 * @phba: pointer to lpfc hba data structure.
18530 * @rgn23_data: pointer to configure region 23 data.
18531 *
18532 * This function gets SLI4 port configure region 23 data through memory dump
18533 * mailbox command. When it successfully retrieves data, the size of the data
18534 * will be returned, otherwise, 0 will be returned.
18535 **/
18536static uint32_t
18537lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18538{
18539 LPFC_MBOXQ_t *mboxq = NULL;
18540 struct lpfc_dmabuf *mp = NULL;
18541 struct lpfc_mqe *mqe;
18542 uint32_t data_length = 0;
18543 int rc;
18544
18545 if (!rgn23_data)
18546 return 0;
18547
18548 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18549 if (!mboxq) {
18550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18551 "3105 failed to allocate mailbox memory\n");
18552 return 0;
18553 }
18554
18555 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18556 goto out;
18557 mqe = &mboxq->u.mqe;
18558 mp = (struct lpfc_dmabuf *) mboxq->context1;
18559 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18560 if (rc)
18561 goto out;
18562 data_length = mqe->un.mb_words[5];
18563 if (data_length == 0)
18564 goto out;
18565 if (data_length > DMP_RGN23_SIZE) {
18566 data_length = 0;
18567 goto out;
18568 }
18569 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18570out:
18571 mempool_free(mboxq, phba->mbox_mem_pool);
18572 if (mp) {
18573 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18574 kfree(mp);
18575 }
18576 return data_length;
18577}
18578
18579/**
18580 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18581 * @phba: pointer to lpfc hba data structure.
18582 *
18583 * This function read region 23 and parse TLV for port status to
18584 * decide if the user disaled the port. If the TLV indicates the
18585 * port is disabled, the hba_flag is set accordingly.
18586 **/
18587void
18588lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18589{
18590 uint8_t *rgn23_data = NULL;
18591 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18592 uint32_t offset = 0;
18593
18594 /* Get adapter Region 23 data */
18595 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18596 if (!rgn23_data)
18597 goto out;
18598
18599 if (phba->sli_rev < LPFC_SLI_REV4)
18600 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18601 else {
18602 if_type = bf_get(lpfc_sli_intf_if_type,
18603 &phba->sli4_hba.sli_intf);
18604 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18605 goto out;
18606 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18607 }
James Smarta0c87cb2009-07-19 10:01:10 -040018608
18609 if (!data_size)
18610 goto out;
18611
18612 /* Check the region signature first */
18613 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18615 "2619 Config region 23 has bad signature\n");
18616 goto out;
18617 }
18618 offset += 4;
18619
18620 /* Check the data structure version */
18621 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18623 "2620 Config region 23 has bad version\n");
18624 goto out;
18625 }
18626 offset += 4;
18627
18628 /* Parse TLV entries in the region */
18629 while (offset < data_size) {
18630 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18631 break;
18632 /*
18633 * If the TLV is not driver specific TLV or driver id is
18634 * not linux driver id, skip the record.
18635 */
18636 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18637 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18638 (rgn23_data[offset + 3] != 0)) {
18639 offset += rgn23_data[offset + 1] * 4 + 4;
18640 continue;
18641 }
18642
18643 /* Driver found a driver specific TLV in the config region */
18644 sub_tlv_len = rgn23_data[offset + 1] * 4;
18645 offset += 4;
18646 tlv_offset = 0;
18647
18648 /*
18649 * Search for configured port state sub-TLV.
18650 */
18651 while ((offset < data_size) &&
18652 (tlv_offset < sub_tlv_len)) {
18653 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18654 offset += 4;
18655 tlv_offset += 4;
18656 break;
18657 }
18658 if (rgn23_data[offset] != PORT_STE_TYPE) {
18659 offset += rgn23_data[offset + 1] * 4 + 4;
18660 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18661 continue;
18662 }
18663
18664 /* This HBA contains PORT_STE configured */
18665 if (!rgn23_data[offset + 2])
18666 phba->hba_flag |= LINK_DISABLED;
18667
18668 goto out;
18669 }
18670 }
James Smart026abb82011-12-13 13:20:45 -050018671
James Smarta0c87cb2009-07-19 10:01:10 -040018672out:
James Smarta0c87cb2009-07-19 10:01:10 -040018673 kfree(rgn23_data);
18674 return;
18675}
James Smart695a8142010-01-26 23:08:03 -050018676
18677/**
James Smart52d52442011-05-24 11:42:45 -040018678 * lpfc_wr_object - write an object to the firmware
18679 * @phba: HBA structure that indicates port to create a queue on.
18680 * @dmabuf_list: list of dmabufs to write to the port.
18681 * @size: the total byte value of the objects to write to the port.
18682 * @offset: the current offset to be used to start the transfer.
18683 *
18684 * This routine will create a wr_object mailbox command to send to the port.
18685 * the mailbox command will be constructed using the dma buffers described in
18686 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18687 * BDEs that the imbedded mailbox can support. The @offset variable will be
18688 * used to indicate the starting offset of the transfer and will also return
18689 * the offset after the write object mailbox has completed. @size is used to
18690 * determine the end of the object and whether the eof bit should be set.
18691 *
18692 * Return 0 is successful and offset will contain the the new offset to use
18693 * for the next write.
18694 * Return negative value for error cases.
18695 **/
18696int
18697lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18698 uint32_t size, uint32_t *offset)
18699{
18700 struct lpfc_mbx_wr_object *wr_object;
18701 LPFC_MBOXQ_t *mbox;
18702 int rc = 0, i = 0;
18703 uint32_t shdr_status, shdr_add_status;
18704 uint32_t mbox_tmo;
18705 union lpfc_sli4_cfg_shdr *shdr;
18706 struct lpfc_dmabuf *dmabuf;
18707 uint32_t written = 0;
18708
18709 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18710 if (!mbox)
18711 return -ENOMEM;
18712
18713 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18714 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18715 sizeof(struct lpfc_mbx_wr_object) -
18716 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18717
18718 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18719 wr_object->u.request.write_offset = *offset;
18720 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18721 wr_object->u.request.object_name[0] =
18722 cpu_to_le32(wr_object->u.request.object_name[0]);
18723 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18724 list_for_each_entry(dmabuf, dmabuf_list, list) {
18725 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18726 break;
18727 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18728 wr_object->u.request.bde[i].addrHigh =
18729 putPaddrHigh(dmabuf->phys);
18730 if (written + SLI4_PAGE_SIZE >= size) {
18731 wr_object->u.request.bde[i].tus.f.bdeSize =
18732 (size - written);
18733 written += (size - written);
18734 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18735 } else {
18736 wr_object->u.request.bde[i].tus.f.bdeSize =
18737 SLI4_PAGE_SIZE;
18738 written += SLI4_PAGE_SIZE;
18739 }
18740 i++;
18741 }
18742 wr_object->u.request.bde_count = i;
18743 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18744 if (!phba->sli4_hba.intr_enable)
18745 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18746 else {
James Smarta183a152011-10-10 21:32:43 -040018747 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018748 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18749 }
18750 /* The IOCTL status is embedded in the mailbox subheader. */
18751 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18752 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18753 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18754 if (rc != MBX_TIMEOUT)
18755 mempool_free(mbox, phba->mbox_mem_pool);
18756 if (shdr_status || shdr_add_status || rc) {
18757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18758 "3025 Write Object mailbox failed with "
18759 "status x%x add_status x%x, mbx status x%x\n",
18760 shdr_status, shdr_add_status, rc);
18761 rc = -ENXIO;
18762 } else
18763 *offset += wr_object->u.response.actual_write_length;
18764 return rc;
18765}
18766
18767/**
James Smart695a8142010-01-26 23:08:03 -050018768 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18769 * @vport: pointer to vport data structure.
18770 *
18771 * This function iterate through the mailboxq and clean up all REG_LOGIN
18772 * and REG_VPI mailbox commands associated with the vport. This function
18773 * is called when driver want to restart discovery of the vport due to
18774 * a Clear Virtual Link event.
18775 **/
18776void
18777lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18778{
18779 struct lpfc_hba *phba = vport->phba;
18780 LPFC_MBOXQ_t *mb, *nextmb;
18781 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018782 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018783 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018784 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018785 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018786 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018787
James Smartd439d282010-09-29 11:18:45 -040018788 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018789 spin_lock_irq(&phba->hbalock);
18790 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18791 if (mb->vport != vport)
18792 continue;
18793
18794 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18795 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18796 continue;
18797
James Smartd439d282010-09-29 11:18:45 -040018798 list_del(&mb->list);
18799 list_add_tail(&mb->list, &mbox_cmd_list);
18800 }
18801 /* Clean up active mailbox command with the vport */
18802 mb = phba->sli.mbox_active;
18803 if (mb && (mb->vport == vport)) {
18804 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18805 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18806 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18807 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18808 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18809 /* Put reference count for delayed processing */
18810 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18811 /* Unregister the RPI when mailbox complete */
18812 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18813 }
18814 }
James Smart63e801c2010-11-20 23:14:19 -050018815 /* Cleanup any mailbox completions which are not yet processed */
18816 do {
18817 restart_loop = 0;
18818 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18819 /*
18820 * If this mailox is already processed or it is
18821 * for another vport ignore it.
18822 */
18823 if ((mb->vport != vport) ||
18824 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18825 continue;
18826
18827 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18828 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18829 continue;
18830
18831 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18832 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18833 ndlp = (struct lpfc_nodelist *)mb->context2;
18834 /* Unregister the RPI when mailbox complete */
18835 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18836 restart_loop = 1;
18837 spin_unlock_irq(&phba->hbalock);
18838 spin_lock(shost->host_lock);
18839 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18840 spin_unlock(shost->host_lock);
18841 spin_lock_irq(&phba->hbalock);
18842 break;
18843 }
18844 }
18845 } while (restart_loop);
18846
James Smartd439d282010-09-29 11:18:45 -040018847 spin_unlock_irq(&phba->hbalock);
18848
18849 /* Release the cleaned-up mailbox commands */
18850 while (!list_empty(&mbox_cmd_list)) {
18851 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018852 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18853 mp = (struct lpfc_dmabuf *) (mb->context1);
18854 if (mp) {
18855 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18856 kfree(mp);
18857 }
James Smart78730cf2010-04-06 15:06:30 -040018858 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018859 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018860 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018861 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018862 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018863 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018864 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018865 }
James Smart695a8142010-01-26 23:08:03 -050018866 }
James Smart695a8142010-01-26 23:08:03 -050018867 mempool_free(mb, phba->mbox_mem_pool);
18868 }
James Smartd439d282010-09-29 11:18:45 -040018869
18870 /* Release the ndlp with the cleaned-up active mailbox command */
18871 if (act_mbx_ndlp) {
18872 spin_lock(shost->host_lock);
18873 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18874 spin_unlock(shost->host_lock);
18875 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050018876 }
James Smart695a8142010-01-26 23:08:03 -050018877}
18878
James Smart2a9bf3d2010-06-07 15:24:45 -040018879/**
18880 * lpfc_drain_txq - Drain the txq
18881 * @phba: Pointer to HBA context object.
18882 *
18883 * This function attempt to submit IOCBs on the txq
18884 * to the adapter. For SLI4 adapters, the txq contains
18885 * ELS IOCBs that have been deferred because the there
18886 * are no SGLs. This congestion can occur with large
18887 * vport counts during node discovery.
18888 **/
18889
18890uint32_t
18891lpfc_drain_txq(struct lpfc_hba *phba)
18892{
18893 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080018894 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090018895 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040018896 unsigned long iflags = 0;
18897 char *fail_msg = NULL;
18898 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080018899 union lpfc_wqe128 wqe128;
18900 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040018901 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040018902
James Smart895427b2017-02-12 13:52:30 -080018903 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070018904 if (unlikely(!pring))
18905 return 0;
James Smart895427b2017-02-12 13:52:30 -080018906
James Smart398d81c2013-05-31 17:04:19 -040018907 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050018908 list_for_each_entry(piocbq, &pring->txq, list) {
18909 txq_cnt++;
18910 }
18911
18912 if (txq_cnt > pring->txq_max)
18913 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018914
James Smart398d81c2013-05-31 17:04:19 -040018915 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018916
James Smart0e9bb8d2013-03-01 16:35:12 -050018917 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040018918 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018919
James Smart19ca7602010-11-20 23:11:55 -050018920 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040018921 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040018922 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040018923 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18924 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050018925 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040018926 break;
18927 }
James Smart895427b2017-02-12 13:52:30 -080018928 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040018929 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050018930 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040018931 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018932 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040018933 }
James Smart0e9bb8d2013-03-01 16:35:12 -050018934 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040018935
18936 /* The xri and iocb resources secured,
18937 * attempt to issue request
18938 */
James Smart6d368e52011-05-24 11:44:12 -040018939 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040018940 piocbq->sli4_xritag = sglq->sli4_xritag;
18941 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18942 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080018943 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018944 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080018945 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018946 fail_msg = " - Wq is full";
18947 else
18948 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18949
18950 if (fail_msg) {
18951 /* Failed means we can't issue and need to cancel */
18952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18953 "2822 IOCB failed %s iotag 0x%x "
18954 "xri 0x%x\n",
18955 fail_msg,
18956 piocbq->iotag, piocbq->sli4_xritag);
18957 list_add_tail(&piocbq->list, &completions);
18958 }
James Smart398d81c2013-05-31 17:04:19 -040018959 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018960 }
18961
James Smart2a9bf3d2010-06-07 15:24:45 -040018962 /* Cancel all the IOCBs that cannot be issued */
18963 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18964 IOERR_SLI_ABORTED);
18965
James Smart0e9bb8d2013-03-01 16:35:12 -050018966 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018967}
James Smart895427b2017-02-12 13:52:30 -080018968
18969/**
18970 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18971 * @phba: Pointer to HBA context object.
18972 * @pwqe: Pointer to command WQE.
18973 * @sglq: Pointer to the scatter gather queue object.
18974 *
18975 * This routine converts the bpl or bde that is in the WQE
18976 * to a sgl list for the sli4 hardware. The physical address
18977 * of the bpl/bde is converted back to a virtual address.
18978 * If the WQE contains a BPL then the list of BDE's is
18979 * converted to sli4_sge's. If the WQE contains a single
18980 * BDE then it is converted to a single sli_sge.
18981 * The WQE is still in cpu endianness so the contents of
18982 * the bpl can be used without byte swapping.
18983 *
18984 * Returns valid XRI = Success, NO_XRI = Failure.
18985 */
18986static uint16_t
18987lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18988 struct lpfc_sglq *sglq)
18989{
18990 uint16_t xritag = NO_XRI;
18991 struct ulp_bde64 *bpl = NULL;
18992 struct ulp_bde64 bde;
18993 struct sli4_sge *sgl = NULL;
18994 struct lpfc_dmabuf *dmabuf;
18995 union lpfc_wqe *wqe;
18996 int numBdes = 0;
18997 int i = 0;
18998 uint32_t offset = 0; /* accumulated offset in the sg request list */
18999 int inbound = 0; /* number of sg reply entries inbound from firmware */
19000 uint32_t cmd;
19001
19002 if (!pwqeq || !sglq)
19003 return xritag;
19004
19005 sgl = (struct sli4_sge *)sglq->sgl;
19006 wqe = &pwqeq->wqe;
19007 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
19008
19009 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
19010 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
19011 return sglq->sli4_xritag;
19012 numBdes = pwqeq->rsvd2;
19013 if (numBdes) {
19014 /* The addrHigh and addrLow fields within the WQE
19015 * have not been byteswapped yet so there is no
19016 * need to swap them back.
19017 */
19018 if (pwqeq->context3)
19019 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
19020 else
19021 return xritag;
19022
19023 bpl = (struct ulp_bde64 *)dmabuf->virt;
19024 if (!bpl)
19025 return xritag;
19026
19027 for (i = 0; i < numBdes; i++) {
19028 /* Should already be byte swapped. */
19029 sgl->addr_hi = bpl->addrHigh;
19030 sgl->addr_lo = bpl->addrLow;
19031
19032 sgl->word2 = le32_to_cpu(sgl->word2);
19033 if ((i+1) == numBdes)
19034 bf_set(lpfc_sli4_sge_last, sgl, 1);
19035 else
19036 bf_set(lpfc_sli4_sge_last, sgl, 0);
19037 /* swap the size field back to the cpu so we
19038 * can assign it to the sgl.
19039 */
19040 bde.tus.w = le32_to_cpu(bpl->tus.w);
19041 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
19042 /* The offsets in the sgl need to be accumulated
19043 * separately for the request and reply lists.
19044 * The request is always first, the reply follows.
19045 */
19046 switch (cmd) {
19047 case CMD_GEN_REQUEST64_WQE:
19048 /* add up the reply sg entries */
19049 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
19050 inbound++;
19051 /* first inbound? reset the offset */
19052 if (inbound == 1)
19053 offset = 0;
19054 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19055 bf_set(lpfc_sli4_sge_type, sgl,
19056 LPFC_SGE_TYPE_DATA);
19057 offset += bde.tus.f.bdeSize;
19058 break;
19059 case CMD_FCP_TRSP64_WQE:
19060 bf_set(lpfc_sli4_sge_offset, sgl, 0);
19061 bf_set(lpfc_sli4_sge_type, sgl,
19062 LPFC_SGE_TYPE_DATA);
19063 break;
19064 case CMD_FCP_TSEND64_WQE:
19065 case CMD_FCP_TRECEIVE64_WQE:
19066 bf_set(lpfc_sli4_sge_type, sgl,
19067 bpl->tus.f.bdeFlags);
19068 if (i < 3)
19069 offset = 0;
19070 else
19071 offset += bde.tus.f.bdeSize;
19072 bf_set(lpfc_sli4_sge_offset, sgl, offset);
19073 break;
19074 }
19075 sgl->word2 = cpu_to_le32(sgl->word2);
19076 bpl++;
19077 sgl++;
19078 }
19079 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
19080 /* The addrHigh and addrLow fields of the BDE have not
19081 * been byteswapped yet so they need to be swapped
19082 * before putting them in the sgl.
19083 */
19084 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
19085 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
19086 sgl->word2 = le32_to_cpu(sgl->word2);
19087 bf_set(lpfc_sli4_sge_last, sgl, 1);
19088 sgl->word2 = cpu_to_le32(sgl->word2);
19089 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
19090 }
19091 return sglq->sli4_xritag;
19092}
19093
19094/**
19095 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
19096 * @phba: Pointer to HBA context object.
19097 * @ring_number: Base sli ring number
19098 * @pwqe: Pointer to command WQE.
19099 **/
19100int
19101lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
19102 struct lpfc_iocbq *pwqe)
19103{
19104 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080019105 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080019106 struct lpfc_queue *wq;
19107 struct lpfc_sglq *sglq;
19108 struct lpfc_sli_ring *pring;
19109 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070019110 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080019111
19112 /* NVME_LS and NVME_LS ABTS requests. */
19113 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
19114 pring = phba->sli4_hba.nvmels_wq->pring;
19115 spin_lock_irqsave(&pring->ring_lock, iflags);
19116 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19117 if (!sglq) {
19118 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19119 return WQE_BUSY;
19120 }
19121 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19122 pwqe->sli4_xritag = sglq->sli4_xritag;
19123 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19124 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19125 return WQE_ERROR;
19126 }
19127 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19128 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019129 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19130 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019131 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019132 return ret;
James Smart895427b2017-02-12 13:52:30 -080019133 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019134
James Smart895427b2017-02-12 13:52:30 -080019135 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19136 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19137 return 0;
19138 }
19139
19140 /* NVME_FCREQ and NVME_ABTS requests */
19141 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19142 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19143 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19144
19145 spin_lock_irqsave(&pring->ring_lock, iflags);
19146 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19147 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19148 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019149 ret = lpfc_sli4_wq_put(wq, wqe);
19150 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019151 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019152 return ret;
James Smart895427b2017-02-12 13:52:30 -080019153 }
19154 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19155 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19156 return 0;
19157 }
19158
James Smartf358dd02017-02-12 13:52:34 -080019159 /* NVMET requests */
19160 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19161 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19162 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19163
19164 spin_lock_irqsave(&pring->ring_lock, iflags);
19165 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019166 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019167 if (pwqe->sli4_xritag == NO_XRI) {
19168 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19169 pwqe->sli4_xritag = sglq->sli4_xritag;
19170 }
19171 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19172 pwqe->sli4_xritag);
19173 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19174 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19175 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019176 ret = lpfc_sli4_wq_put(wq, wqe);
19177 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019178 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019179 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019180 }
19181 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19182 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19183 return 0;
19184 }
James Smart895427b2017-02-12 13:52:30 -080019185 return WQE_ERROR;
19186}